| constructor(parent){ | constructor(parent){ | ||||
| this.__parent = (parent instanceof Environment) ? parent : null; | this.__parent = (parent instanceof Environment) ? parent : null; | ||||
| this.__vars = {}; | this.__vars = {}; | ||||
| this.__listeners = {}; | |||||
| } | } | ||||
| get PC(){return PC;} | get PC(){return PC;} | ||||
| PC = Math.floor(val); | PC = Math.floor(val); | ||||
| return this; | return this; | ||||
| } | } | ||||
| let initSet = false; | |||||
| if (!(name in Object.keys(this.__vars))){ | if (!(name in Object.keys(this.__vars))){ | ||||
| if (this.__parent.hasVar(name)) | |||||
| if (this.__parent && this.__parent.hasVar(name)) | |||||
| return this.__parent.setVarVal(name, val); | return this.__parent.setVarVal(name, val); | ||||
| initSet = true; | |||||
| } | } | ||||
| this.__vars[name] = val; | this.__vars[name] = val; | ||||
| if (initSet){ | |||||
| this.emitVarVal(name); | |||||
| return this; | return this; | ||||
| } | } | ||||
| emitVarVal(name){ | |||||
| if (!(name in Object.keys(this.__vars))) | |||||
| throw new Error("Failed to emit. Variable or Label '" + name + "' undefined."); | |||||
| if (name in this.__listeners){ | |||||
| let val = this.getVarValue(name); | |||||
| this.__listeners[name].forEach((l)=>{ | |||||
| l.call(l, val); | |||||
| }); | |||||
| delete this.__listeners[name]; | |||||
| } | |||||
| } | |||||
| onVarDef(name, func){ | |||||
| if (typeof(func) !== 'function') | |||||
| throw new TypeError("Expected a function."); | |||||
| if (this.hasVar(name)){ | |||||
| func.call(func, this.getVarVal(name)); | |||||
| } else { | |||||
| if (!(name in this.__listeners)) | |||||
| this.__listeners[name] = []; | |||||
| this.__listeners[name].push(func); | |||||
| } | |||||
| } | |||||
| } | } | ||||