forked from ibnsultan/swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
51 lines (43 loc) · 1.87 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
47
48
49
50
51
#!/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(" / / // // /");
console.log(" / / // // / swahili");
console.log(" /_________ / //________//________/ programming");
console.log(" // / // // Language");
console.log(" // / // // author: Abdulbasit Rubeiyya");
console.log("________//____/_____// // ");
console.log("Examples:");
console.log(" $ swap file.sw");
console.log(" $ swap -h");
console.log(" $ swap -v");
});
commander.version(packageJson.version, "-v, --version");
commander.arguments("[file]")
.option("")
.action((file, options) => {
if (path.extname(file) === constants.SWAP_EXT) {
setGlobalVars(options);
startSwapProcess(file);
} else {
throw new Error("Invalid file extension used, only swap file can be used");
}
});
commander.parse(process.argv);
function setGlobalVars (options) {
const lang = [ "english", "kiswahili", ];
global.defaultLang = lang.includes(options.lang) ? options.lang : "english";
}
function startSwapProcess (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();
}