Compile-time AST manipulation API

oldrev oldrev at gmail.com
Sun Nov 25 05:04:49 PST 2007


0ffh Wrote:

> Ma�l wrote:
> > Take a look at OCaml / CamlP4, this is exactly what you are looking for, and this is done in a truely nice and (extremely) fast (as always when it comes to OCaml ...) way.
> > In my opinion, CamlP4 is one of the biggest advantage of OCaml, because it truly permits to implement full DSL, or to couple a DSL to OCaml code in a transparent way, so for instance you can define a logic DSL, and have things like
> > [...]
> > and this certainly is useful in many ways 
> 
> Thanks for the tip. I took a look at O'Caml earlier, but to be frank,
> functional languages turn me off... =)
> 
> I have been imperating since I was a small boy, and I'm reasonably good
> at it. But when I look at, say Haskell, Clean, Caml, the syntax alone is
> enough to turn me off. I find those even more repulsive than Lisp.
> 
> That's not to say that I do not appreciate how powerful those languages
> are. But it's perfectly possible to have metaprogramming in an imperative
> languages with C-style syntax, you don't need to go functional for that.
> 
> regards, frank

Anonymous AST macro is the ultimate way to solve it:

macro(args, "=>", expr)
{
    mixin("delegate(" ~ args.stringof ~ ")" ~ "{ return " ~ expr.stringof ~ "; };");
}

void foo(int delegate(int, int)){ 
...
}

foo(int x, int y => x + y);

Hope it's helpful :)

Regards,
Wei



More information about the Digitalmars-d mailing list