Giter Site home page Giter Site logo

sweet.js's Introduction

sweet.js

Hygienic Macros for JavaScript!

Use

Using Node

Install with npm:

$ npm install -g sweet.js

To try it out make a file test_macros.js:

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

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

And compile it with sjs:

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

If you want to collect your macro definitions into a separate file you can use the -m flag:

$ sjs -m macros.js -o output.js main.js 

Where the macros defined in macros.js that you want to be available in main.js are exported with the keyword export:

// macros.js

macro m { /* ... */ }
export m;

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.
});

Hacking

Install the dev dependencies:

$ npm install

Build and run the tests:

$ grunt

Sweet.js is self hosted so the files in src/ are compiled with the files in lib/ and then copied into build/lib/. Then the tests in test/ are compiled with build/lib/. Once you are happy with your changes run grunt dist to compile sweet.js into lib/ which can then be checked into git.

Slightly confusing but the process goes like this:

<hack hack hack>
$ grunt
<tests fail!>
<hack hack hack>
$ grunt
<tests pass!>
$ grunt dist
$ git add lib/
$ git commit -m "sweet!"

sweet.js's People

Contributors

disnet avatar natefaubion avatar jlongster avatar puffnfresh avatar carlqlange avatar int3 avatar magnetised avatar iammerrick avatar parndt avatar tuchida avatar

Watchers

Alps Tsui avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.