-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
60 lines (46 loc) · 1.32 KB
/
cli.ts
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
import Interpreter from "./src/interpreter";
import Debug from "./src/util/debug"
import process from "node:process";
class GolosinaCommandLineInterface {
private debug: Debug;
constructor() {
this.debug = new Debug();
};
private parseFlags(): string[] {
const flags = process.argv;
return flags.splice(2, 2);
};
public execute(): void {
const flags = this.parseFlags();
const state = {
dumpTokens: false,
dumpAST: false
};
const paths: string[] = [];
for (const flag of flags) {
if (flag === "--dump-ast") {
state.dumpAST = true;
} else if (flag === "--dump-tokens") {
state.dumpTokens = true;
} else if (!flag.startsWith("--")) {
// suppose its a path
paths.push(flag);
} else {
throw new Error(`Unexpected arg or flag at "${flag}"`);
};
};
for (const path of paths) {
Interpreter.parser.setSource(path, true);
if (state.dumpTokens) {
this.debug.logTokens(Interpreter.parser.getTokens);
};
const tree = Interpreter.parser.execute();
if (state.dumpAST) {
this.debug.logAST(tree);
};
Interpreter.walker.setSource = tree;
Interpreter.walker.execute();
};
};
};
export default GolosinaCommandLineInterface;