Skip to content

Commit

Permalink
Merge pull request #285 from null-a/deepcopy-ast-in-compile
Browse files Browse the repository at this point in the history
Deep copy AST in compile
  • Loading branch information
stuhlmueller committed Jan 8, 2016
2 parents e9d30f5 + 7020280 commit cb21d4b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ packages.forEach(function(pkg) {
pkg.headers.forEach(webppl.requireHeaderWrapper);
});

var extra = webppl.parsePackageCode(packages);

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

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

Expand Down
10 changes: 9 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ function applyCaching(asts) {
});
}

function copyAst(ast) {
var ret = _.isArray(ast) ? [] : {};
_.each(ast, function(val, key) {
ret[key] = _.isObject(val) ? copyAst(val) : val;
});
return ret;
}

function compile(code, options) {
var options = util.mergeDefaults(options, { verbose: false, generateCode: true });

Expand All @@ -144,7 +152,7 @@ function compile(code, options) {

function _compile() {
var programAst = parse(code, extra.macros);
var asts = extra.asts.concat(programAst);
var asts = extra.asts.map(copyAst).concat(programAst);
var doCaching = _.any(asts, caching.transformRequired);

if (options.verbose && doCaching) {
Expand Down
8 changes: 8 additions & 0 deletions tests/browser/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ QUnit.test('run', function(test) {
});
});

QUnit.test('run twice', function(test) {
_.times(2, function() {
webppl.run('Enumerate(flip)', function(s, erp) {
test.ok(_.isEqual([false, true], erp.support().sort()));
});
});
});

QUnit.test('compile', function(test) {
test.ok(_.isString(webppl.compile('1 + 1')));
});
Expand Down

0 comments on commit cb21d4b

Please sign in to comment.