-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.js
84 lines (64 loc) · 1.93 KB
/
Interpreter.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
//@ts-check
const {Runtime, Variable} = require("./Runtime");
const ModuleLoader = require("./ModuleLoader");
const fs = require("fs");
const LOG = false;
class Interpreter {
constructor(options) {
const {source} = options;
this.source = source.replace(/\t/g, " ");
}
/**
* @return {Variable | FailureResult}
* @memberof Interpreter
*/
interpret() {
const ast = ModuleLoader.generateAST(this.source);
if(ast instanceof FailureResult) return ast;
//Look for built-in modules
const builtInModules = fs.readdirSync("./builtins")
.filter(filename => filename.endsWith(".spj"));
const runtime = new Runtime();
//Load all the built-in modules
for(const filename of builtInModules) {
LOG && console.log(`Loading built-in module '${filename}'...`);
const module = ModuleLoader.loadModule(`./builtins/${filename}`, runtime);
if(module instanceof FailureResult) return module;
for(const [name, value] of module) {
runtime._global.variables.set(name, value);
}
LOG && console.log(`Loaded built-in module '${filename}'!`);
}
const result = runtime.evaluate(ast.node);
if(result instanceof FailureResult) {
result.__source = this.source;
}
//LOG && console.dir(ast, {depth: null});
//console.log(ast.toString());
return result;
}
}
module.exports = {Interpreter};
const {FailureResult} = require("./Result");
(async function() {
if(require.main === module) {
const file = process.argv[2];
if(!file) {
console.error("No file specified!");
process.exit(1);
}
fs.promises.readFile(file, "utf8").then(source => {
const interpreter = new Interpreter({source});
const result = interpreter.interpret();
if(result instanceof FailureResult) {
console.error(result.toString());
process.exit(1);
}
//console.log(result.toString());
process.exit(0);
}).catch(err => {
console.error(`Error reading file '${file}': ${err.message}`);
process.exit(1);
});
}
})();