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.

sweetjs

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

into

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. 

 

sweetjs

 

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

Sweet.js

 

Related Articles

TypeScript - Microsoft's Replacement For JavaScript

Ready to Go - Go Reaches Version 1

JavaScript On the Rise

Dart + Chromium = Dartium

Ceylon Achieves Milestone 1

Eclipse Launches a JDK language

 

espbook

 

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.

 

Banner


.NET 9 Released
18/11/2024

.NET 9 has been released with a number of performance improvements and new features designed to help developers use AI.



The Feds Want Us To Move On From C/C++
13/11/2024

The clamour for safe programming languages seems to be growing and becoming official. We have known for a while that C and C++ are dangerous languages so why has it become such an issue now and is it  [ ... ]


More News

Last Updated ( Tuesday, 16 October 2012 )