-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjcaml.js
executable file
·61 lines (55 loc) · 1.49 KB
/
jcaml.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
#!/usr/bin/env node
/*
* A JCaml Compiler
*
* This is a command line application that compiles a JCamll program from
* a file. There are three options:
*
* ./jcaml.js -a <filename>
* writes out the AST and stops
*
* ./jcaml.js -i <filename>
* writes the decorated AST then stops
*
* ./jcaml.js <filename>
* compiles the JCamll program to JavaScript, writing the generated
* JavaScript code to standard output.
*
* ./jcaml.js -o <filename>
* optimizes the intermediate code before generating target JavaScript.
*
* Output of the AST and decorated AST uses the object inspection functionality
* built into Node.js.
*/
const argv = require("yargs")
.usage("$0 [-a] [-o] [-i] filename")
.boolean(["a", "o", "i"])
.describe("a", "show abstract syntax tree after parsing then stop")
.describe("o", "do optimizations")
.describe("i", "generate and show the decorated abstract syntax tree then stop")
.demand(1)
.argv;
const fs = require("fs");
const util = require("util");
const parse = require("./parser");
require("./generator/generator");
fs.readFile(argv._[0], "utf-8", (err, text) => {
if (err) {
console.error(err);
return;
}
let program = parse.parse(text);
if (argv.a) {
console.log(util.inspect(program, { depth: null }));
return;
}
// program.analyze();
if (argv.o) {
program = program.optimize();
}
if (argv.i) {
console.log(util.inspect(program, { depth: null }));
return;
}
program.gen();
});