Browse Source

Environment class can now have a 'special' variable for the Program Counter defined (using the PCLabel property).

master
Bryan Miller 5 years ago
parent
commit
f1528e3a8d
1 changed files with 18 additions and 3 deletions
  1. +18
    -3
      src/compiler/env.js

+ 18
- 3
src/compiler/env.js View File



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

Loading…
Cancel
Save