-
Notifications
You must be signed in to change notification settings - Fork 70
/
cli.js
executable file
·46 lines (38 loc) · 1.48 KB
/
cli.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
#!/usr/bin/env node
const packageJson = require("./package.json");
const path = require("path");
const constants = require("./src/constants.js");
const commander = require("commander");
commander.on("--help", function () {
console.log("");
console.log("Examples:");
console.log(" $ yorl test.yl");
console.log(" $ yorl test.yl -l yoruba");
console.log(" $ yorl -h");
console.log(" $ yorl -v");
});
commander.version(packageJson.version, "-v, --version");
commander.arguments("[file]")
.option("-l, --lang [lang]", "Select language to use")
.action((file, options) => {
if (path.extname(file) === constants.YL_EXT) {
setGlobalVars(options);
startYorlangProcess(file);
} else {
throw new Error("Invalid Yorlang file. Expected a .yl file");
}
});
commander.parse(process.argv);
function setGlobalVars (options) {
const lang = [ "english", "yoruba", ];
global.defaultLang = lang.includes(options.lang) ? options.lang : "english";
}
function startYorlangProcess (file) {
const InputStream = require("./src/inputstream.js");
const Lexer = require("./src/lexer.js");
const Parser = require("./src/parsers/parser.js");
const Environment = require("./src/environment.js");
const MainInterpreter = require("./src/interpreters/maininterpreter.js");
const parser = new Parser(new Lexer(new InputStream(file)));
new MainInterpreter(new Environment(), parser).interpreteProgram();
}