Abstract syntax tree manipulation

Belzurix 8412n4.250m8012 at gmail.com
Sun Apr 21 03:36:15 PDT 2013


On Sunday, 21 April 2013 at 07:57:51 UTC, Suminda Dharmasena 
wrote:
> Hi,
>
> Since macro is reserved perhaps D can introduce AST 
> manipulating macros.
>
> Suminda

Hi!

As far as I can remember, macros have been deferred to D3 because 
it has mixins and templates, so one can easily manipulate code in 
D. However, it's been long time since there was serious 
discussion about the implementation details. I'm happy to see 
that the topic emerged again just before DConf - maybe the 
discussion will continue there.
Several non-lispy languages introduced macros long ago so the 
developers may learn from others mistakes and they will avoid the 
pitfalls. These languages include Groovy,
Scala, Nimrod and Rust.

The Rust implementation is much like text substituation when 
used, so one can easily learn it in an hour. Its syntax is a 
little distinct from the base language, and there has been a 
problem with lexing it according to a past issue: 
https://github.com/mozilla/rust/issues/2755
It's still unfinished, there are some limitations. ( although 
it's been always hard to debug them ) Have a look at: 
http://static.rust-lang.org/doc/0.6/tutorial-macros.html
and also see the language manual ( at http://rust-lang.org )

Beyond AST macros, Nimrod ( http://nimrod-code.org ) has text 
substituation mechanisms called templates - much like C macros or 
templates in C++ and D. It also has term rewriting macros. In my 
opinion, Nimrod is a language where a lot of features can produce 
dirty code ( although some of them are very well done and they 
are extremely powerful tools ), and it's the developer's choice 
to use the features for the right purposes ( especially true with 
term rewriting ).
So obfuscation is just as easy in that language as in C++ or 
Perls before version 6 despite the fact that its sysntax is 
derived from Python.

I can't say much about Scala and Groovy, but their syntax for 
defining macros doesn't use as cryptic symbols as Nimrod. For 
example I couldn't find out for first time what is nnk the nnk 
prefix in the nnkInfix symbol.

Zsombor


More information about the Digitalmars-d mailing list