swh:1:snp:3af1a03cd091600016317ad7197cfc7dfd1319c7
Tip revision: 55c92fce0a7164222bf37753bf8accb5e14be482 authored by Andreas Stuhlmüller on 02 December 2015, 17:14:42 UTC
0.4.0
0.4.0
Tip revision: 55c92fc
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 += (
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();