|Written by Ian Elliot|
|Tuesday, 16 October 2012|
A macro language is essentially a text processing language and the big problem is that they vary a lot in power. For example, the basic C macro facility is more-or-less automated copy and paste. Simple macro language have almost succeeded in bringing macro languages into disrepute, but sweet.js is a bit more sophisticated.
For example, if you want to write def in place of the usual function keyword you can define a macro:
and following this a statement like:
is transformed into
Notice that while this example looks like a simple substitution of "function" for "def" the macro defines what happens to whatever follows the "def" in potentially complex ways.
Using macros is a matter of pattern matching, but when macros include conditionals and recursion like sweet.js does then things become very powerful and in principle you could write a compiler using nothing but the macro language.
You could also use sweet.js to implement Domain Specific Languages, DSLs.
The project has been going for a month or so and sweet.js is fairly usable with some omissions and bugs. You can also use it with Ruby. The code is open source and you can download it from Github.
or email your comment to: email@example.com
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
|Last Updated ( Tuesday, 16 October 2012 )|