Skip to content

robkuz/sweet.js

 
 

Repository files navigation

sweet.js

Hygienic Macros for JavaScript!

  • Documentation at sweetjs.org.
  • Overview and motivation in this talk.
  • Example macros on the wiki.
  • Mailing list for discussion
  • IRC channel #sweet.js on irc.mozilla.org

Early stage at the moment. Lots of bugs so be warned!

Use

Using Node

Clone sweet.js and then install its dependencies:

$ npm install underscore optimist escodegen

To try it out make a file test_macros.sjs:

// functions can now be spelled def!
macro def {
  case $name:ident $params $body => {
    function $name $params $body
  }
}
def add (a, b) {
  return a + b;
}

console.log( add(3, 7) );

And compile it with sjs:

$ bin/sjs -o output.js test_macros.sjs
$ node output.js
10

Alternately you can require an sjs file from node. For example, in main.js add:

var sjs = require('sweet.js'),
    example = require('./example');

example.one;

Where ./example.sjs contains:

macro A {
    case ($a + $b) => {
        $a
    }
}

exports.one = A(1 + 2);

And just run main.js in node.

Using AMD in the browser

An AMD loader is provided at require-sweet.

define(['sweeten!a/javascript/dep-with-macros'], function(dep) {
  // dep is compiled to JS at this point.
});

Using ruby

To compile sweet.js source files from within Ruby, use the SweetJS gem:

gem install sweetjs

or in your Gemfile:

gem "sweetjs"

then call the SweetJS.compile (or SweetJS#compile) method to compile a sweet.js source file to plain JavaScript:

require "sweetjs"

SweetJS.compile(File.read("macros.js.sjs"))
# => Resulting JS source

# Alternatively:
sweet  = SweetJS.new
source = File.open("macros.js", "r:UTF-8").read
js     = sweet.compile(source)

Hacking

Install the dev dependencies:

$ npm install --dev

And run the tests

$ npm test

About

Sweeten your JavaScript.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published