-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
71 lines (54 loc) · 1.89 KB
/
main.js
File metadata and controls
71 lines (54 loc) · 1.89 KB
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
const interpreter = new Interpreter();
// Main methods.
function runCode(debug = false) {
const source = getSource();
// Add a running tag to show that the code is being processed.
output.innerHTML = "Running Code...<br><br>";
setTimeout(() => {
hadError = false;
hadRuntimeError = false;
const scanner = new Scanner(source);
const tokens = scanner.scanTokens();
if (hadError)
return;
// Print the tokens.
if (debug) {
tokens.forEach(token => {
println(token.toString());
});
println();
}
const parser = new Parser(tokens);
const statements = parser.parse();
if (hadError)
return;
// The we can display the AST classes.
if (debug) {
statements.forEach(statement => {
print(statement.constructor.name);
println("Stmt"); // NOTE: Every statement is an instance of Stmt.
// println(Object.getPrototypeOf(statement.constructor).name);
});
println();
}
const resolver = new Resolver(interpreter);
resolver.resolve(statements);
if (hadError)
return;
interpreter.interpret(statements);
output.innerHTML += "<br>Running Complete.";
console.log(output.innerHTML);
}, 0);
}
// Helper methods.
function getSource() {
return editor.value;
}
function copyCode() {
const source = getSource();
navigator.clipboard.writeText(source);
window.alert("Code was copied to clipboard. Thanks for trying the best programming language in the universe.");
}
// Focus on main textarea.
document.addEventListener("keydown", () => editor.focus());
editor.addEventListener("click", () => editor.focus());