| // ALL Environments share the same program counter! | |||||
| var PC = 0; | |||||
| class Environment{ | |||||
| constructor(parent){ | |||||
| this.__parent = (parent instanceof Environment) ? parent : null; | |||||
| this.__vars = {}; | |||||
| } | |||||
| get PC(){return PC;} | |||||
| set PC(val){ | |||||
| if (val < 0) | |||||
| throw new RangeError("Program Counter value is out of bounds."); | |||||
| PC = val; | |||||
| } | |||||
| hasVar(name){ | |||||
| if (name in Object.keys(this.__vars)) | |||||
| return true; | |||||
| if (this.__parent !== null) | |||||
| return this.__parent.hasVar(name); | |||||
| return false; | |||||
| } | |||||
| getVarValue(name){ | |||||
| if (name in Object.keys(this.__vars)) | |||||
| return this.__vars[name]; | |||||
| if (this.__parent !== null) | |||||
| return this.__parent.getVarValue(name); | |||||
| throw new Error("No var '" + name + "' defined."); | |||||
| } | |||||
| setVarValue(name, val){ | |||||
| if (!(name in Object.keys(this.__vars))){ | |||||
| if (this.__parent.hasVar(name)) | |||||
| return this.__parent.setVarVal(name, val); | |||||
| } | |||||
| this.__vars[name] = val; | |||||
| return this; | |||||
| } | |||||
| } | |||||
| module.exports = Environment; |