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

@@ -4,11 +4,39 @@ var PC = 0;
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{
constructor(parent){
this.__parent = (parent instanceof Environment) ? parent : null;
this.__vars = {};
this.__listeners = {};

this.__templabels = [];
}

get PC(){return PC;}
@@ -49,15 +77,12 @@ class Environment{
PC = Math.floor(val);
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;
}


Loading…
Cancel
Save