Browse Source

Environment will now accept ':' as temp labels and handle their values accordingly. As soon as a new var is defined, all temp labels are cleared.

master
Bryan Miller 5 years ago
parent
commit
8c174a787d
1 changed files with 33 additions and 8 deletions
  1. +33
    -8
      src/assembler/env.js

+ 33
- 8
src/assembler/env.js View File

var PCL = "__PC__"; var PCL = "__PC__";




function StoreVar(env, name, val){
let initSet = false;
if (!(name in Object.keys(env.__vars))){
if (env.__parent && env.__parent.hasVar(name))
return env.__parent.setVarVal(name, val);
initSet = true;
}
env.__vars[name] = val;
if (initSet)
env.emitVarVal(name);
if (env.__templabels.length > 0){
env.__templabels.forEach((l)=>{
delete env.__vars[l];
delete env.__listeners[l];
});
env.__templabels = [];
}
}

function StoreTempLabel(env, name, val){
env.__vars[name] = val;
env.__templabels.push(name);
env.emitVarVal(name);
}


class Environment{ class Environment{
constructor(parent){ constructor(parent){
this.__parent = (parent instanceof Environment) ? parent : null; this.__parent = (parent instanceof Environment) ? parent : null;
this.__vars = {}; this.__vars = {};
this.__listeners = {}; this.__listeners = {};

this.__templabels = [];
} }


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 (this.__parent && this.__parent.hasVar(name))
return this.__parent.setVarVal(name, val);
initSet = true;
if (name === ":"){
StoreTempLabel(this, ":" + this.__templabels.length, val);
} else {
StoreVar(this, name, val);
} }
this.__vars[name] = val;
if (initSet){
this.emitVarVal(name);
return this; return this;
} }



Loading…
Cancel
Save