-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
99 lines (86 loc) · 2.16 KB
/
main.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
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
'use strict';
var fs = require('fs'),
path = require('path'),
argv = require('minimist')(process.argv.slice(2)),
Scanner = require('./scanner.js'),
Syntax = require('./syntax.js'),
SymbolTable = require('./symbol-table.js'),
filename = (argv._[0]) ? path.join(process.cwd(), argv._[0]) : null,
_callbacks = [];
if (!filename || argv.h || argv.help) {
var helpText = path.join(__dirname, 'help.txt');
console.log(fs.readFileSync(helpText).toString());
return;
}
GLOBAL.DEBUG = !!(argv.d || argv.debug);
/* jshint latedef: false */
function _then(cb) {
if (!cb) {
return {
then: _then
};
}
if (typeof cb === 'string') {
_callbacks.push(runPass.bind(null, cb));
} else {
_callbacks.push(cb);
}
return {
then: _then
};
}
function runPass(passName) {
Scanner.init(filename, function() {
try {
SymbolTable().startPass();
Syntax.pass(passName);
} catch(e) {
var token = Scanner.currentToken;
e.message = 'Pass: ' + passName + '\nLine ' + token.lineNumber + ': ' + e.message;
if (GLOBAL.DEBUG) {
throw e;
} else {
console.log(e.message);
process.exit(1);
}
}
if (_callbacks.length > 0) {
_callbacks.shift()();
}
});
return {
then: _then
};
}
/* jshint latedef: true */
runPass('syntax')
.then('type inference')
.then('semantics')
.then(function() {
var target = argv.t || argv.target || 'uvu',
ICode = require('./icode.js'),
TCode = require('./target/' + target + '.js'),
outputICode = argv.i || argv.icode,
outputTCode = argv.o || argv.output,
icode = '',
targetCode;
if (outputICode) {
icode = ICode.createCode();
if (typeof outputICode === 'string') {
fs.writeFileSync(path.join(process.cwd(), outputICode), icode);
} else {
console.log(icode);
}
}
if (outputTCode) {
targetCode = TCode.compile(ICode.quads);
if (typeof outputTCode === 'string') {
fs.writeFileSync(path.join(process.cwd(), outputTCode), targetCode);
} else {
console.log(targetCode);
}
}
if (argv.s || argv.symbol) {
SymbolTable().print();
}
});