-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthetic-runtime.js
73 lines (61 loc) · 2.08 KB
/
synthetic-runtime.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { parse, RenderInputStream, RenderTokenStream } = require("../parser");
const { Transpiler, CpsTransformer } = require("../compiler");
const { Execute, STACK_GUARD } = require("../interpreter/context.js");
const CpsOptimizer = require("../optimizer/CPS-optimizer.js");
if (typeof process != "undefined") (() => {
const sys = require("util");
const print = function(k) {
console.log([].slice.call(arguments, 1).join(" "));
k(false);
};
function readStdin(callback) {
let text = "";
process.stdin.setEncoding("utf8");
process.stdin.on("readable", () => {
const chunk = process.stdin.read();
if (chunk) {
text += chunk;
}
});
process.stdin.on("end", () => callback(text));
};
readStdin((code) => {
const ast = parse(RenderTokenStream(RenderInputStream(code)));
const cps = CpsTransformer(ast, (x) =>
({
type: "CALL",
func: { type: "VARIABLE", value: "ε_TOPLEVEL" },
args: [ x ]
})
);
// console.log(cps);
const opt = CpsOptimizer(cps);
let jsc = Transpiler(opt);
console.log(jsc)
jsc = "var ε_TMP;\n\n" + jsc;
if (opt.env) {
const vars = Object.keys(opt.env.vars);
if (vars.length > 0) {
jsc = "var " + vars.map((name) =>
Transpiler({
type: "VARIABLE",
value: name
})
).join(", ") + ";\n\n" + jsc;
}
}
jsc = '"use strict";\n\n' + jsc;
// console.log(sys.inspect(jsc, { depth: null }));
const func = new Function("ε_TOPLEVEL, STACK_GUARD, print, Execute", jsc);
console.time("Runtime");
Execute(func, [
function(result) {
console.timeEnd("Runtime");
console.log("Final: ", result);
},
STACK_GUARD,
print,
Execute
]);
});
})();