-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivot.js
60 lines (57 loc) · 1.61 KB
/
pivot.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
#!/usr/bin/env node
const { argv } = require('yargs')
.usage('$0 [-a] [-o] [-i] [-g] filename')
.boolean(['a', 'o', 'i', 'g'])
.describe('a', 'show abstract syntax tree after parsing then stop')
.describe('o', 'generate optimized JavaScript code')
.describe('i', 'generate and show the intermediate code then stop')
.describe('g', 'generate non-optimized JavaScript code')
.demand(1);
const fs = require('fs');
const util = require('util');
const { exec } = require('child_process');
const parse = require('./ast/parser');
const view = require('./semantics/viewer');
const analyze = require('./semantics/analyzer');
const generate = require('./generator/javascript-generator');
const optimizer = require('./semantics/optimizer');
// require(`./backend/${argv.target}generator`);
fs.readFile(argv._[0], 'utf-8', (error, text) => {
if (error) {
console.error(error);
return;
}
let program = parse(text);
if (argv.a) {
console.log(util.inspect(program, { depth: null }));
return;
}
if (argv.i) {
exec('nodemon graph/server.j');
view(program);
exec('open http://localhost:5000');
console.log('ctrl-c to kill');
process.on('SIGINT', function() {
exec('kill -9 $(lsof -t -i:5000)');
console.log('\nbye');
});
return;
}
if (argv.o) {
try {
analyze(program);
program = optimizer(program);
console.log(generate(program));
} catch (error) {
console.error(error.message);
}
}
if (argv.g) {
try {
analyze(program);
console.log(generate(program));
} catch (error) {
console.error(error.message);
}
}
});