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.


Doubt Over Craig Wright's Claim To Be Bitcoin Inventor - Updated

In a blog post Craig Wright claims to be Satoshi Nakamoto and apparently the evidence is so overwhelming that news agencies are running headlines like "Inventor of Bitcoin identified". Not so fast.

U [ ... ]

Schmidt and Schwartz Defend Google's Use Of Java APIs

At the start of the second Oracle v Google trial, Alphabet CEO Eric Schmidt told the court that in creating Android Google used only what it thought was freely available, including the  [ ... ]

More News

Last Updated ( Tuesday, 16 October 2012 )

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