瀏覽代碼

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

master
Bryan Miller 5 年之前
父節點
當前提交
f1528e3a8d
共有 1 個文件被更改,包括 18 次插入3 次删除
  1. +18
    -3
      src/compiler/env.js

+ 18
- 3
src/compiler/env.js 查看文件

@@ -1,6 +1,7 @@

// ALL Environments share the same program counter!
// ALL Environments share the same program counter and label!
var PC = 0;
var PCL = "__PC__";


class Environment{
@@ -14,10 +15,17 @@ class Environment{
if (val < 0)
throw new RangeError("Program Counter value is out of bounds.");
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){
if (name in Object.keys(this.__vars))
if (name === PCL || name in Object.keys(this.__vars))
return true;
if (this.__parent !== null)
return this.__parent.hasVar(name);
@@ -25,6 +33,7 @@ class Environment{
}

getVarValue(name){
if (name === PCL){return PC;}
if (name in Object.keys(this.__vars))
return this.__vars[name];
if (this.__parent !== null)
@@ -33,6 +42,12 @@ class Environment{
}

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 (this.__parent.hasVar(name))
return this.__parent.setVarVal(name, val);

Loading…
取消
儲存