JavaScript Your Way - Sweet.js Macros |
Written by Ian Elliot | |||
Tuesday, 16 October 2012 | |||
Macros have a long history in computing and sweet.js may just give you JavaScript any way that you would like to program it. Sweet.js is a Mozilla project to add a macro facility to JavaScript. While it is true that Mozilla has many projects that never really make much impact, this one is worth knowing about, if only to put languages and the problems they create into perspective. 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. It is referred to as a "hygienic" macro language because it doesn't pollute the language it manipulates with unnecessary resources. Put simply it doesn't create or use variables in the language it works with. However, sweet.js isn't just a hygienic language it is also a powerful template language which can transform macro code into equivalent JavaScript. You can think of it as a sort of customizable compiler - but this might be over stating the case. 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. Of course in this case the intention of sweet.js is to allow you to extend JavaScript in ways that suit your particular style of programming. If you would be happier with JavaScript with a Basic-style for loop then you can write a macro to convert
into
You could also use sweet.js to implement Domain Specific Languages, DSLs. Sweet.js is written in JavaScript and can work as a Node.js package. It acts as a compiler which takes in a macro file consisting of macro definitions and code and outputs pure JavaScript. You could say it was another compiler that treats JavaScript as its assembly language but in this case the output should be easy for a human to understand because fo the close and meaningful relationship between the macro and the output.
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. More Information
Related ArticlesTypeScript - Microsoft's Replacement For JavaScript Ready to Go - Go Reaches Version 1 Eclipse Launches a JDK language
Comments
or email your comment to: comments@i-programmer.info
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 ) |