浏览代码

Environment can now emit on initial variable/label value assignment.

master
Bryan Miller 5 年前
父节点
当前提交
ba2545fe64
共有 1 个文件被更改,包括 30 次插入1 次删除
  1. +30
    -1
      src/assembler/env.js

+ 30
- 1
src/assembler/env.js 查看文件

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);
}
}
} }





正在加载...
取消
保存