Kaynağa Gözat

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

master
Bryan Miller 5 yıl önce
ebeveyn
işleme
f1528e3a8d
1 değiştirilmiş dosya ile 18 ekleme ve 3 silme
  1. +18
    -3
      src/compiler/env.js

+ 18
- 3
src/compiler/env.js Dosyayı Görüntüle

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

Yükleniyor…
İptal
Kaydet