-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.js
52 lines (47 loc) · 1.44 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
let variables = {};
let operations = {
"+": (a, b) => {
return a + b;
}
}
let functions = {
log: (args) => {
console.log(args);
}
};
function parseNode(node) {
switch (node.type) {
case "identifier":
let value = variables.hasOwnProperty(node.name) ? variables[node.name] : node.value;
return value;
break;
case "variableDeclaration":
variables[node.name] = node.value;
break;
case "functionCall":
let args = [];
for (const _node of node.arguments) {
args.push(parseNode(_node));
}
if (functions.hasOwnProperty(node.callee)) {
functions[node.callee].apply(null, args);
} else throw new Error(`Function \"${node.name}\" is not defined.`);
break;
case "number":
return node.value;
break;
case "operation":
if (!operations[node.name]) throw new Error(`Operator \"${node.name}\" is not defined.`);
if (node.left) return operations[node.name](parseNode(node.left), parseNode(node.right));
return operations[node.name](parseNode(node.right));
break;
default:
throw new Error("Invalid node type");
}
}
function parseAST(ast) {
for (const node of ast.program.body) {
parseNode(node);
}
}
module.exports = {parseAST, parseNode};