| @@ -8,6 +8,7 @@ class Environment{ | |||
| constructor(parent){ | |||
| this.__parent = (parent instanceof Environment) ? parent : null; | |||
| this.__vars = {}; | |||
| this.__listeners = {}; | |||
| } | |||
| get PC(){return PC;} | |||
| @@ -48,13 +49,41 @@ class Environment{ | |||
| PC = Math.floor(val); | |||
| return this; | |||
| } | |||
| let initSet = false; | |||
| 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); | |||
| initSet = true; | |||
| } | |||
| this.__vars[name] = val; | |||
| if (initSet){ | |||
| this.emitVarVal(name); | |||
| 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); | |||
| } | |||
| } | |||
| } | |||