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.

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.

Banner


Learn How To Use Firefox To Debug With Power Surge
15/07/2015

Power Surge is a game that Mozilla has put together to let you discover how good the performance tools in the latest Firefox developer edition are. It is a nice idea.



New Portal For IBM Open Source Projects
27/07/2015

IBM has just launched developerWorksOpen to enable developers to collaborate using its open sourced technologies. It is poised to provide new tools, in particular with regards to mobile.


More News

Last Updated ( Tuesday, 16 October 2012 )
 
 

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