JavaScript Your Way - Sweet.js Macros
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:

macro def {
 case $name:ident $params $body => {
  function $name $params $body


and following this a statement like:

 def sweet(a) {
 console.log("Macros are sweet!");

is transformed into 


function sweet(a) {
 console.log("Macros are sweet!");

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

For i=1 To 10


for(var i=1;i<=10;i++)  

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.


Mozilla Wants Your Voice

Mozilla has launched Project Common Voice to crowdsource speech recognition. Once a massive amount of audio data has been captured, it will be made available for others to use in their own application [ ... ]

Learning To Walk

Deep Mind has been applying its deep reinforcement learning to the problem of walking - but not just any walking, this is difficult terrain. The neural network gets it wrong some of the time, but it i [ ... ]

More News

Last Updated ( Tuesday, 16 October 2012 )

RSS feed of news items only
I Programmer News
Copyright © 2017 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.