https://github.com/probmods/webppl
Revision 1273a92e66f9d28ac2c6c465b966aa159e227f5e authored by Andreas Stuhlmüller on 20 January 2016, 19:04:30 UTC, committed by Andreas Stuhlmüller on 20 January 2016, 19:04:30 UTC
1 parent 60195d2
Tip revision: 1273a92e66f9d28ac2c6c465b966aa159e227f5e authored by Andreas Stuhlmüller on 20 January 2016, 19:04:30 UTC
0.5.0
0.5.0
Tip revision: 1273a92
webppl
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var webppl = require('./src/main');
var pkg = require('./src/pkg');
var util = require('./src/util');
var parseArgs = require('minimist');
var git = require('git-rev-2');
var _ = require('underscore');
function printWebPPLValue(x) {
if (isErp(x)) {
x.print();
} else {
console.log(x);
}
}
function run(code, packages, verbose) {
packages.forEach(function(pkg) {
if (pkg.js) { global[pkg.js.identifier] = require(pkg.js.path); }
pkg.headers.forEach(webppl.requireHeader);
});
webppl.run(
code,
function(s, x) {
console.log('\n* Program return value:\n');
printWebPPLValue(x);
},
{
extra: webppl.parsePackageCode(packages, verbose),
verbose: verbose
});
}
function compile(code, packages, verbose, outputFile) {
var compiledCode = 'var webppl = require("' + require.resolve('./src/main') + '");\n';
packages.forEach(function(pkg) {
if (pkg.js) { compiledCode += 'var ' + pkg.js.identifier + ' = require("' + pkg.js.path + '");\n'; }
pkg.headers.forEach(function(header) {
compiledCode += 'webppl.requireHeader("' + header + '");\n';
});
});
var compileOptions = { extra: webppl.parsePackageCode(packages, verbose), verbose: verbose };
compiledCode += (
webppl.runTrampoline.toString() + '\n' +
printWebPPLValue.toString() + '\n' +
'var topK = function(s, x){ \n' +
" console.log('\\n* Program return value:\\n'); \n" +
' printWebPPLValue(x); \n};\n\n');
compiledCode += 'var main = ' + webppl.compile(code, compileOptions) + '\n\n';
compiledCode += "main({}, topK, '');";
// Write Javascript code to file
fs.writeFile(
outputFile,
compiledCode,
function(err) {
if (err) {
console.log(err);
} else {
console.log('Wrote webppl code to', outputFile);
}
});
}
function main() {
var argv = parseArgs(process.argv.slice(2));
// Print version if requested
if (argv.version) {
git.branch(
__dirname,
function(err, branch) {
__dirname,
git.short(
function(err, shortName) {
console.log(branch, shortName, __dirname);
});
}
);
return;
}
// Return if no file argument given
if (argv._[0] === undefined) {
console.log('No program argument given! Run webppl as follows:');
console.log('webppl program.wppl [--compile] [--out compiled.js] ' +
'[--require path] [--random-seed int]');
return;
}
// Load webppl code
var programFile = argv._[0];
console.log('Processing', programFile);
var code = fs.readFileSync(programFile, 'utf8');
var processCode = argv.compile ? compile : run;
var outputFile = argv.out ? argv.out : 'tmp.js';
var packagePaths = [
path.join(path.dirname(programFile), 'node_modules'),
pkg.globalPkgDir()
];
var packages = util.asArray(argv.require).map(function(name_or_path) {
return pkg.load(pkg.read(name_or_path, packagePaths, argv.verbose));
});
var seed = argv['random-seed'];
if (seed !== undefined) {
util.assertValidRandomSeed(seed);
util.seedRNG(seed);
}
processCode(code, packages, argv.verbose, outputFile);
}
main();
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...