|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ALL Environments share the same program counter! |
|
|
|
|
|
|
|
|
// ALL Environments share the same program counter and label! |
|
|
var PC = 0; |
|
|
var PC = 0; |
|
|
|
|
|
var PCL = "__PC__"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Environment{ |
|
|
class Environment{ |
|
|
|
|
|
|
|
|
if (val < 0) |
|
|
if (val < 0) |
|
|
throw new RangeError("Program Counter value is out of bounds."); |
|
|
throw new RangeError("Program Counter value is out of bounds."); |
|
|
PC = val; |
|
|
PC = val; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get PCLabel(){return PCL;} |
|
|
|
|
|
set PCLabel(pcl){ |
|
|
|
|
|
if (typeof(pcl) !== 'string' || pcl === "") |
|
|
|
|
|
throw new TypeError("Expected a non-zero-length string."); |
|
|
|
|
|
PCL = pcl; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
hasVar(name){ |
|
|
hasVar(name){ |
|
|
if (name in Object.keys(this.__vars)) |
|
|
|
|
|
|
|
|
if (name === PCL || name in Object.keys(this.__vars)) |
|
|
return true; |
|
|
return true; |
|
|
if (this.__parent !== null) |
|
|
if (this.__parent !== null) |
|
|
return this.__parent.hasVar(name); |
|
|
return this.__parent.hasVar(name); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getVarValue(name){ |
|
|
getVarValue(name){ |
|
|
|
|
|
if (name === PCL){return PC;} |
|
|
if (name in Object.keys(this.__vars)) |
|
|
if (name in Object.keys(this.__vars)) |
|
|
return this.__vars[name]; |
|
|
return this.__vars[name]; |
|
|
if (this.__parent !== null) |
|
|
if (this.__parent !== null) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
setVarValue(name, val){ |
|
|
setVarValue(name, val){ |
|
|
|
|
|
if (name === PCL){ |
|
|
|
|
|
if (typeof(val) !== 'number' || val < 0) |
|
|
|
|
|
throw new Error("Special Variable '" + name + "' expected to be a possitive integer."); |
|
|
|
|
|
PC = Math.floor(val); |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
if (!(name in Object.keys(this.__vars))){ |
|
|
if (!(name in Object.keys(this.__vars))){ |
|
|
if (this.__parent.hasVar(name)) |
|
|
if (this.__parent.hasVar(name)) |
|
|
return this.__parent.setVarVal(name, val); |
|
|
return this.__parent.setVarVal(name, val); |