@parser-generator/grammar-interpreter
Version:
A Parser Generator that supports LL,SLR,LR1,LALR
41 lines • 1.19 kB
JavaScript
import { Single } from "../lexer";
/*
E-> E+E | E-E | E*E | E/E | (E) |digit
*/
export class Expr {
constructor(eles) {
this.children = eles;
}
eval() {
let first = this.children[0];
if (this.children.length == 3) {
//(E)
if (first instanceof Single) {
return this.children[1].eval();
}
//E op E
else {
let left = this.children[0];
let op = this.children[1].value();
let right = this.children[2];
switch (op) {
case "+":
return left.eval() + right.eval();
case "-":
return left.eval() - right.eval();
case "*":
return left.eval() * right.eval();
case "/":
return left.eval() / right.eval();
default:
throw new Error(`Unknown token :${op}`);
}
}
}
//NUM
else {
return first.value();
}
}
}
//# sourceMappingURL=ast.js.map