-
Notifications
You must be signed in to change notification settings - Fork 1
/
lox.js
108 lines (90 loc) · 2.58 KB
/
lox.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const Parser = require('./Parser');
const readline = require('readline');
const Scanner = require('./Scanner');
const Resolver = require('./Resolver');
const TokenType = require('./TokenType');
const AstPrinter = require('./AstPrinter');
const Interpreter = require('./Interpreter');
const args = process.argv.slice(2);
const interpreter = new Interpreter();
let hadError = false;
let hadRuntimeError = false;
function runFile(filePath) {
const data = fs.readFileSync(path.join(process.cwd(), filePath), 'utf-8');
run(data);
if (hadError) process.exit(65);
if (hadRuntimeError) process.exit(70);
}
async function runPrompt() {
const question = (prompt) => {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(prompt, (line) => {
rl.close();
resolve(line);
});
});
};
for (;;) {
const line = await question('> ');
if (line === null) break;
runRepl(line);
hadError = false;
}
}
function runRepl(source) {
const scanner = new Scanner(source);
const tokens = scanner.scanTokens();
const parser = new Parser(tokens);
if (hadError) return;
const declaration = parser.declaration();
interpreter.repl(declaration);
}
function run(source) {
const scanner = new Scanner(source);
const tokens = scanner.scanTokens();
const parser = new Parser(tokens);
const statements = parser.parse();
if (hadError) return;
const resolver = new Resolver(interpreter);
resolver.resolve(statements);
if (hadError) return;
interpreter.interpret(statements);
}
function report(line, where, message) {
console.error(`[line ${line}] Error ${where}: ${message}`);
hadError = true;
}
function error(token, message) {
if (token === 0) return;
if (token.type === TokenType.EOF) {
report(token.line, 'at end', message);
} else {
report(token.line, `at \'${token.lexeme}\'`, message);
}
}
exports.error = error;
function runtimeError(error) {
console.log(error);
console.log(`${error.message}\n[line ${error.token.line}]`);
hadRuntimeError = true;
}
exports.runtimeError = runtimeError;
if (!module.parent) {
(async function () {
if (args.length > 1) {
console.log('Usage: nlox [script]');
process.exit(64);
} else if (args.length === 1) {
runFile(args[0]);
} else {
await runPrompt();
}
}());
}