Skip to content

Commit

Permalink
Merge pull request #241 from null-a/macros2
Browse files Browse the repository at this point in the history
Add sweet.js macros.
  • Loading branch information
stuhlmueller committed Oct 13, 2015
2 parents 90a07fc + 0ef887e commit 602807d
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 113 deletions.
2 changes: 1 addition & 1 deletion compiled/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $(MINIFIED): $(BROWSERIFIED)
uglifyjs $< -b ascii_only=true,beautify=false > $@

$(BROWSERIFIED): $(SRCFILES)
browserify -t brfs $(MAINFILE) > $@
browserify -g brfs $(MAINFILE) > $@

test: $(BROWSERIFIED)
node -e "require('open')(process.argv[1], process.env.BROWSER)" $(TESTS)
Expand Down
2 changes: 1 addition & 1 deletion docs/analysis/documentation.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HOW TO USE IT

The top level of the control-flow analysis is the 'analyze' function in the 'analysis/main' module. This function takes a named, CPS'd AST (which 'prepare' of the 'main' module produces given a literal program) and produces a relation over abstract program states along with some information about the relation itself. All this is encoded as a JS object naming this information. These parts are
The top level of the control-flow analysis is the 'analyze' function in the 'analysis/main' module. This function takes a named, CPS'd AST (which 'prepare' of the 'analysis/main' module produces given a literal program) and produces a relation over abstract program states along with some information about the relation itself. All this is encoded as a JS object naming this information. These parts are

- 'seen': the set of abstract states over which the relation is defined
- 'calls': a map from an abstract entry point to a set of abstract call points
Expand Down
2 changes: 1 addition & 1 deletion docs/development/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ using the ``BROWSER`` environment variable. For example::
Packages can also be used in the browser. For example, to include the
``webppl-viz`` package use::

browserify -t [./src/bundle.js --require webppl-viz] -t brfs src/browser.js > compiled/webppl.js
browserify -t [./src/bundle.js --require webppl-viz] -g brfs src/browser.js > compiled/webppl.js

Multiple ``--require`` arguments can be used to include multiple
packages.
Expand Down
34 changes: 32 additions & 2 deletions docs/packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ WebPPL Packages
===============

WebPPL packages are regular Node.js packages optionally extended to
include WebPPL code and headers.
include WebPPL code, macros and headers.

To make a package available in your program use the ``--require``
argument::
Expand All @@ -21,7 +21,7 @@ Packages can be loaded from other locations by passing a path::

webppl myFile.wppl --require ../myPackage

Packages can extend WebPPL in three ways:
Packages can extend WebPPL in several ways:

WebPPL code
-----------
Expand All @@ -36,6 +36,33 @@ You can automatically prepend WebPPL files to your code by added a
}
}

Macros
------

`sweet.js`_ modules can be included in a package as follows:

1. Add a file containing the macros to the package::

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

Note that macros must be exported explicitly using the ``export``
keyword. See the `sweet.js module documentation`_ for further details.

2. Add a ``macros`` entry to ``package.json``::

{
"name": "my-package",
"webppl": {
"macros": ["macros.sjs"]
}
}

These macros will be visible to the WebPPL program which is been run
or compiled, and to any WebPPL code within the same package. They will
not be visible to WebPPL code in other packages.

Javascript functions and libraries
----------------------------------

Expand Down Expand Up @@ -118,3 +145,6 @@ available in WebPPL:
};

foo();

.. _sweet.js: http://sweetjs.org
.. _sweet.js module documentation: http://sweetjs.org/doc/main/sweet.html#using-modules
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"git-rev-2": "^0.1.0",
"immutable": "^3.7.1",
"minimist": "^1.1.1",
"priorityqueuejs": "~1.0.0",
"numeric": "~1.2.6",
"priorityqueuejs": "~1.0.0",
"sweet.js": "probmods/sweet.js#browserify",
"underscore": "^1.8.3"
},
"devDependencies": {
"brfs": "^1.4.0",
"brfs": "0.0.8",
"closure-linter-wrapper": "^0.2.11",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.1",
Expand Down
17 changes: 16 additions & 1 deletion src/analysis/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var parse = require('./parser-combinator');
var analyzeRefs = require('./analyze-refs').analyzeRefs;
var isHeapRef = require('./analyze-refs').isHeapRef;

var thunkify = require('../syntax').thunkify;
var naming = require('../transforms/naming').naming;
var cps = require('../transforms/cps').cps;
var optimize = require('../transforms/optimize').optimize;

var compile = require('../main').compile;

var isHeapVar = null;

Expand Down Expand Up @@ -478,6 +484,15 @@ function analyzeMain(program) {
}
}

function prepare(code, verbose) {
return compile(code, {
transforms: [thunkify, naming, cps, optimize],
verbose: verbose,
generateCode: false
});
}

module.exports = {
analyze: analyzeMain
analyze: analyzeMain,
prepare: prepare
};
4 changes: 2 additions & 2 deletions src/analysis/visualize-run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var prepare = require('../main').prepare;
var analyze = require('../main').analyze;
var prepare = require('./main').prepare;
var analyze = require('./main').analyze;
var vizualize = require('./visualize').vizualize;

process.stdin.setEncoding('utf8');
Expand Down
9 changes: 4 additions & 5 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var fs = require('fs');
var esprima = require('esprima');
var escodegen = require('escodegen');

var webppl = require('./main');
var optimize = require('./transforms/optimize').optimize;
var naming = require('./transforms/naming').naming;
Expand All @@ -23,14 +22,14 @@ packages.forEach(function(pkg) {
pkg.headers.forEach(webppl.requireHeaderWrapper);
});

var wpplExtra = packages.map(function(pkg) { return pkg.wppl.join(';'); }).join(';');

function run(code, k, verbose) {
return webppl.run(wpplExtra + code, k, verbose);
var extra = webppl.parsePackageCode(packages, verbose);
return webppl.run(code, k, { extra: extra, verbose: verbose });
}

function compile(code, verbose) {
return webppl.compile(wpplExtra + code, verbose);
var extra = webppl.parsePackageCode(packages, verbose);
return webppl.compile(code, { extra: extra, verbose: verbose });
}

function webpplCPS(code) {
Expand Down
2 changes: 2 additions & 0 deletions src/headerMacros.sjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
operator (|>) 0 left { $val, $f } => #{ $f($val) }
export (|>)

0 comments on commit 602807d

Please sign in to comment.