Mixin template evaluated to string can convert to string mixin expression implicitly
Alex_Dovhal
alex_dovhal at yahoo.com
Fri Jun 24 11:43:42 PDT 2011
"Timon Gehr" <timon.gehr at gmx.ch> wrote:
> Macros operate on ASTs, not strings. You won't get real macros, just
> something
> that feels somewhat like them. You still could implement the equivalent to
> C
> macros (not real macros either) in a library, but they would have all the
> problems
> of C macros, as string mixins do now. (You make this fact explicit by
> typing "mixin").
>
> For anything but the most trivial code transformations, you have to cheat
> the
> compiler into parsing your code into an AST (further complicated by the
> fact that
> you cannot overload all operators freely), use clever tricks to get the
> information you want and then you have to transform it back into a string
> so that
> it can then be mixed in (which of course requires reparsing the whole
> thing.) This
> is just painful and a waste of cycles.
> String mixins are useful, but they cannot replace macros.
>
> What would your library be capable of?
Yes. Library macros can build AST with CTFE :)
About Library interface, one sample:
template macroFunct(string T)
{
enum macroFunct = macroFunctImpl(T);
}
string macroFunctImpl(string arg)
{
auto ast = parseAST_1(arg);
// do something. e.g.
return ast[0];
}
/*AST, here string[]*/ string[] parseAST_1(string arg)
{
//do something. e.g.
return [arg];
}
int main()
{
assert(mixin(macroFunct!q{123})==123);
assert(! (mixin(macroFunct!q{321})==322));
return 0;
}
More information about the Digitalmars-d
mailing list