-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lox.ts
82 lines (65 loc) · 2.05 KB
/
Lox.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Interpreter } from "./Interpreter.ts";
import Parser from "./Parser.ts";
import { Resolver } from "./Resolver.ts";
import RuntimeError from "./RuntimeError.ts";
import Scanner from "./Scanner.ts";
import Token from "./Token.ts";
import TokenType from "./TokenType.ts";
export default class Lox {
private static readonly interpreter = new Interpreter();
static hadError = false;
static hadRuntimeError = false;
private static executeCode(code: string) {
const scanner = new Scanner(code);
const tokens = scanner.generateTokens();
const parser = new Parser(tokens);
const statements = parser.parse();
if (Lox.hadError) return;
const resolver = new Resolver(this.interpreter);
resolver.resolve(statements);
// Stop if there was a resolution error
if (this.hadError) return;
Lox.interpreter.interpret(statements);
}
private static runFile(filePath: string): void {
const code = Deno.readTextFileSync(filePath);
Lox.executeCode(code);
if (Lox.hadError) Deno.exit(65);
if (Lox.hadRuntimeError) Deno.exit(70);
}
private static runPrompt(): void {
while (true) {
const line = prompt(">");
if (line == null) {
break;
}
Lox.executeCode(line);
Lox.hadError = false;
}
}
static main(args: string[]) {
if (args.length > 1) {
console.log("Usage: tslox [script]");
} else if (args.length === 1) {
Lox.runFile(args[0]);
} else {
Lox.runPrompt();
}
}
static reportError(line: number, where: string, message: string): void {
console.log(`[line ${line}] Error ${where}: ${message}`);
Lox.hadError = true;
}
static error(token: Token, message: string): void {
if (token.type === TokenType.EOF) {
Lox.reportError(token.line, "at end", message);
} else {
Lox.reportError(token.line, `at '${token.lexeme}'`, message);
}
}
static runtimeError(error: RuntimeError): void {
console.error(`${error.message}
[line ${error.token.line}]`);
Lox.hadRuntimeError = true;
}
}