www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Compile-time AST manipulation API

reply oldrev <oldrev gmail.com> writes:
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
Nov 25 2007
parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
Why would anyone need this in a general-purpose, OO language, is beyond 
me...
Nov 25 2007
parent reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Dejan Lekic wrote:
 Why would anyone need this in a general-purpose, OO language, is beyond 
 me...

A few examples of what would be possible to add to any program you have the source of, by just including an appropriate module: - Stack backtraces - Runtime statistics & profiling - Array access range checking - Integral overflow checks - New numeric types that behave like primitive types without any need for syntax overloading - Anything regards, frank
Nov 25 2007
parent reply =?ISO-8859-1?Q?Ma=ebl?= <someone somewhere.com> writes:
Plus, if it is implemented as a pre-processor (CamlP4-like) you could make
powerful domain-specific languages and directly embbed them in D programs
Nov 25 2007
parent 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Maël wrote:
 Plus, if it is implemented as a pre-processor (CamlP4-like) you could
 make powerful domain-specific languages and directly embbed them in D
 programs

You wouldn't even need CTFE for it, if you'd just add a compiler switch to output the AST in some text format (say, in Annotated Term Format), and a possibility to feed those files back to the compiler, instead of D source. Hacking the compiler would become a breeze. =) regards, frank
Nov 25 2007