-
Notifications
You must be signed in to change notification settings - Fork 7
/
jack.js
executable file
·51 lines (46 loc) · 1.45 KB
/
jack.js
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
#!/usr/local/bin/node --harmony-collections
var fs = require('fs');
var pathJoin = require('path').join;
var runtime = require('./runtime');
fs.stat(pathJoin(__dirname, "parser.js"), function (err, pstat) {
if (err) {
// If the parser doesn't exist, generate it.
if (err.code === "ENOENT") return generate();
throw err;
}
fs.stat(pathJoin(__dirname, "grammar.json"), function (err, gstat) {
if (err) throw err;
// See if the grammer has been changed since the generated file.
if (gstat.mtime > pstat.mtime) return generate();
// Otherwise we can use the cached version.
ready();
});
});
function generate() {
console.log("Compiling fresh parser...");
var Parser = require("jison").Parser;
var parser = new Parser(require('./grammar'));
fs.writeFile(pathJoin(__dirname, "parser.js"), parser.generate(), function (err) {
if (err) throw err;
ready();
});
}
function ready() {
var filename = process.argv[2];
if (!filename) throw new Error("filename required");
// console.log("Parsing: " + filename);
var parser = require('./parser').parser;
runtime.attachParser(parser);
fs.readFile(filename, "utf8", function (err, code) {
if (err) throw err;
var tree = parser.parse(code);
if (process.env.DUMP) {
var inspect = require('util').inspect;
console.log(inspect(tree, false, 15, true));
}
else {
// console.log("Running program...")
runtime.eval(code);
}
});
}