-
Notifications
You must be signed in to change notification settings - Fork 8
/
greenlight.js
53 lines (44 loc) · 2.07 KB
/
greenlight.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
let colors = require(__dirname + "/lib/colors");
console.log("//" + colors.green + " ⬤ GreenLight v0.0.1" + colors.reset + "//")
let fs = require("fs");
let lexer = require(__dirname + "/lib/lexer");
let transpiler = require(__dirname + "/lib/transpiler");
let code = "";
if(process.argv[2] == null || !( process.argv[2] === "e" || process.argv[2] === "c" )) {
console.log(colors.red + "[ERR] Please provide either e or c as second argument!" + colors.reset);
process.exit(1);
}
if(process.argv[3] != null) {
for(let i = 3; process.argv[i] != null; i++) {
if(!fs.existsSync(process.argv[i])) {
console.log(colors.red + "[ERR] " + process.argv[i] + " doesn't exist!" + colors.reset);
process.exit(1);
}
code += new String(fs.readFileSync(process.argv[i])).replace(/(\r\n|\n|\r)/gm, "");
}
} else {
console.log(colors.red + "[ERR] No input file supplied. Pass source files as arguments. Exiting..." + colors.reset);
process.exit(1);
}
if(process.argv[2] !== "c") console.log(colors.blue + "[*] Executing lexer..." + colors.reset);
let lexerResults = lexer.execute(code)
if(process.argv[2] !== "c") console.log(colors.green + "[OK] Lexer finished successfully!" + colors.reset);
// Uncomment to see the AST constructed by the transpiler, maybe the bad regex-based parser messed up if it isn't recognized
// console.log(lexerResults);
if(process.argv[2] !== "c") console.log(colors.blue + "[*] Transpiling..." + colors.reset);
lexerResults.forEach(token => {
transpiler.transpile(token);
})
if(process.argv[2] !== "c") console.log(colors.green + "[OK] Transpilation complete!" + colors.reset);
if(process.argv[2] !== "c") console.log(colors.blue + "[*] Running syntax checker & linter..." + colors.reset);
if(transpiler.runChecks()) {
if(process.argv[2] !== "c") console.log(colors.green + "[OK] Syntax checker finished, no issues found!" + colors.reset);
} else {
console.log(colors.yellow + "[WARN] Syntax checker found at least one issue in your code!" + colors.reset);
return -1;
}
if(process.argv[2] === "c") {
console.log(transpiler.getCode());
} else {
eval(transpiler.getCode());
}