forked from probmods/webppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webppl
executable file
·161 lines (137 loc) · 4.43 KB
/
webppl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/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 pkginfo = require('./src/pkginfo');
var git = require('./src/git');
var errors = require('./src/errors/errors');
var parseV8 = require('./src/errors/parsers').parseV8;
var showError = require('./src/errors/node').showError;
var paramsConfig = require('./src/params/config');
var parseArgs = require('minimist');
var args = require('./src/args');
var _ = require('lodash');
function topK(s, x) {
console.log(x);
};
function run(code, packages, verbose, debug, programFile) {
packages.forEach(function(pkg) {
if (pkg.js) { global[pkg.js.identifier] = require(pkg.js.path); }
pkg.headers.forEach(webppl.requireHeader);
});
webppl.run(code, topK, {
bundles: webppl.parsePackageCode(packages, verbose),
filename: programFile,
verbose: verbose,
debug: debug,
errorHandlers: [
function(error) {
if (error instanceof Error && error.wpplRuntimeError) {
try {
var stack = errors.recoverStack(error, parseV8);
showError(error, stack, programFile, debug);
process.exitCode = 1;
} catch (e) {
// If we fail to generate a readable error message re-throw
// the original error.
throw error;
}
} else {
throw error;
}
}
]
});
}
var lines = function(ar) {
return ar.join('\n')
}
function compile(code, packages, verbose, debug, programFile, outputFile) {
var compiledCode = 'var webppl = require("' + require.resolve('./src/main') + '");\n';
compiledCode += 'var args = require("'+require.resolve('./src/args')+'");\n';
compiledCode += 'args.makeGlobal(__filename, process.argv.slice(2));\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 = {
bundles: webppl.parsePackageCode(packages, verbose),
filename: programFile,
verbose: verbose,
debug: debug
};
var compiledBody = webppl.compile(code, compileOptions).code;
compiledCode += lines([
'var __runner__ = util.trampolineRunners.cli();',
topK.toString() + ';',
'var main = ' + compiledBody + '\n',
"webppl.runEvaled(main, __runner__, {}, {}, 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), {default: {debug: true}, '--': true});
// Print version if requested
if (argv.version) {
var dir = __dirname;
console.log(_.filter([git.branch(dir), pkginfo.version(), dir]).join(' '));
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] [--param-store name] ' +
'[--param-id id]');
return;
}
// Load webppl code
var programFile = argv._[0];
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));
});
// Random seed
var seed = argv['random-seed'];
if (seed !== undefined) {
util.assertValidRandomSeed(seed);
util.seedRNG(seed);
}
// Parameter store type
var paramStoreName = argv['param-store'];
if (paramStoreName) {
paramsConfig.setStore(paramStoreName);
}
// Parameter table id
var paramId = argv['param-id'];
if (paramId) {
paramsConfig.setId(paramId);
} else {
paramsConfig.setFreshId();
}
// Parse arguments for program.
args.makeGlobal(programFile, argv['--']);
processCode(code, packages, argv.verbose, argv.debug, programFile, outputFile);
}
main();