Eval a String and transform it to macro expression

78 views
Skip to first unread message

Francis Bourre

unread,
May 4, 2016, 4:24:07 AM5/4/16
to Haxe
Hi everyone,

this is the code that I would like to produce inside my final expression:

var o = a.b.c.d;

But I can't figure out a way without generating each step inside a merged block with a loop:

var s0 = a;
var s1 = s0.b;
var s2 = s1.c;
var o = s2.d;


I'm searching for the optimized way to write it directly in one expression:
var o = a.b.c.d;

It's important to mention that I can't predict String source value in advance.

Thank you so much in advance guys for your lights. :)


Juraj Kirchheim

unread,
May 4, 2016, 4:34:45 AM5/4/16
to haxe...@googlegroups.com
Not really sure what your input looks like, but $p might be what you're after: http://haxe.org/manual/macro-reification-expression.html

Example:

var string = "a.b.c.d";
var expr = macro var o = $p{string.pop(".")};

For arbitrary expressions, you may want to try this: http://api.haxe.org/haxe/macro/Context.html#parse

Best,
Juraj

--
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/d/optout.

Francis Bourre

unread,
May 4, 2016, 4:58:47 AM5/4/16
to Haxe
Thank you so much, it works like a charm.

var expr = macro var o = $p{string.split(".")};
Reply all
Reply to author
Forward
0 new messages