Is it possible to create non sub-blocked expressions with macro?

138 views
Skip to first unread message

dlots

unread,
Jan 7, 2014, 7:54:15 PM1/7/14
to haxe...@googlegroups.com
I would like to do the following:

    macro
    public static function de(debug:ExprOf<String>,e:Expr):Expr {
        var debug_s:String=ExprTools.toString(debug);
        debug_s=debug_s.substring(1,debug_s.length-1);
        if(Context.defined(debug_s)) {
            var es:String=ExprTools.toString(e);
            var r:String="{"+es+"}"; //objective is to remove {} for instances of single line expressions and prune them in the instance of multiline passed expressions
            return Context.parse(r,Context.currentPos());
        }
        return Context.parse("{}",Context.currentPos());
    }

The above should function similar to the conditional compilation directive. However, it does not because in the instance of newly declared variables in the expressions, their scope is limited to the generated expression subblock.

Consider this:

T.de('render_performance_debug',{
var start:Float=Utils.time_precise();
});
render_function();
T.de('render_performance_debug',{
Utils.measure_trace("render",start); //start does not exist
});

There is value in using this type of conditional as opposed to conditional compilation because:
1. It can potentially facilitate enabling a debug variable at runtime.
2. It facilitates the full power of macros and general purpose computing constructs as opposed to the limited functionality provided by conditional compilation.

d...@proletariat.com

unread,
Jan 7, 2014, 8:40:07 PM1/7/14
to haxe...@googlegroups.com
I'd suggest using build macros instead of macro functions for this.

dlots

unread,
Jan 7, 2014, 9:11:11 PM1/7/14
to haxe...@googlegroups.com
There is no way I'm going to use build macros for this.

On Tuesday, January 7, 2014 7:54:15 PM UTC-5, dlots wrote:

Jason O'Neil

unread,
Jan 7, 2014, 9:49:15 PM1/7/14
to haxe...@googlegroups.com
In your example, you're only trying to insert one expression, so that's not too hard.  Any time you have a block expression it is an ExprDef.EBlock, so you can get the inside parameters out:

switch ( myExpr.expr ) {
    case EBlock( exprs ): return exprs[0]; // return first in block;
    case expr: return expr; // return single expression as is
}

However if you're hoping to insert more than one expression, and it not be a block, I'm not sure what the best approach is there...


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.

dlots

unread,
Jan 8, 2014, 1:36:04 AM1/8/14
to haxe...@googlegroups.com, he...@jasono.co
To clarify, this is of course with regards to inserting multiple expressions but more simply it's about:

How does one declare variables with a macro that are accessible outside the block. It would appear that it is necessary to wrap expressions passed to Context.parse in a block. Maybe this can be accomplished with raw macros.

Simon Krajewski

unread,
Jan 8, 2014, 4:09:36 AM1/8/14
to haxe...@googlegroups.com
On dev you can make your macro return something like
macro @:mergeBlock {
var a = 1;
var b = "foo";
}

Simon
Reply all
Reply to author
Forward
0 new messages