| this.__varlabels = {}; | this.__varlabels = {}; | ||||
| // Labels that hold jump/branch locations. | // Labels that hold jump/branch locations. | ||||
| this.__jmplabels = {}; | this.__jmplabels = {}; | ||||
| // Inital value for program counter. Used by the reset() method. | |||||
| this.__initPC = (typeof(initpc) === 'number' && initpc >= 0) ? initpc : 0; | |||||
| // Program counter to track where in the code/memory a | // Program counter to track where in the code/memory a | ||||
| // operation is located. | // operation is located. | ||||
| this.__PC = (initpc >= 0) ? initpc : 0; | |||||
| this.__PC = this.__initPC; | |||||
| this.__result = []; | |||||
| } | } | ||||
| get PC(){return this.__PC;} | |||||
| get variables(){ | |||||
| var v = {}; | |||||
| Object.keys(this.__varlabels).forEach((lbl)=>{ | |||||
| if (this.__varlabels.hasOwnProperty(lbl)) | |||||
| v[lbl] = this.__varlabels[lbl]; | |||||
| }); | |||||
| return v; | |||||
| } | |||||
| get jumplabels(){ | |||||
| var j = {}; | |||||
| Object.keys(this.__jmplabels).forEach((lbl)=>{ | |||||
| if (this.__jmplabels.hasOwnProperty(lbl)) | |||||
| j[lbl] = this.__jmplabels[lbl]; | |||||
| }); | |||||
| return j; | |||||
| } | |||||
| __StoreVarLabel(lbl, val){ | __StoreVarLabel(lbl, val){ | ||||
| if (lbl in this.__varlabels) | if (lbl in this.__varlabels) | ||||
| throw new Error("Variable label '" + lbl + "' defined more than once."); | throw new Error("Variable label '" + lbl + "' defined more than once."); | ||||
| if (mode !== ""){ | if (mode !== ""){ | ||||
| if (tA.startsWith("+$")){ | if (tA.startsWith("+$")){ | ||||
| v = parseInt(tA.substr(2)); | |||||
| v = parseInt(tA.substr(2), 16); | |||||
| } else { | } else { | ||||
| let lbl = tA.substr(1); | let lbl = tA.substr(1); | ||||
| if (lbl in this.__varlabels){ | if (lbl in this.__varlabels){ | ||||
| throw new Error("Malformed op-code or value on program address " + toHexString(this.__PC)); | throw new Error("Malformed op-code or value on program address " + toHexString(this.__PC)); | ||||
| } | } | ||||
| compile(src){ | compile(src){ | ||||
| var op = []; | var op = []; | ||||
| src.split("\n").forEach((line)=>{ | src.split("\n").forEach((line)=>{ | ||||
| line = line.trim(); | line = line.trim(); | ||||
| if (line === ""){return;} | |||||
| if (line === ""){return this;} | |||||
| if (line[0] !== ";"){ // Skip comment lines. | if (line[0] !== ";"){ // Skip comment lines. | ||||
| line = line.split(";")[0].trim(); // Take out any trailing comments. | line = line.split(";")[0].trim(); // Take out any trailing comments. | ||||
| var tokens = tokenize(line); | var tokens = tokenize(line); | ||||
| } | } | ||||
| } | } | ||||
| }); | }); | ||||
| return new Uint8Array(op); | |||||
| this.__result = this.__result.concat(op); | |||||
| return this; | |||||
| //return new Uint8Array(op); | |||||
| } | |||||
| result(){ | |||||
| return new Uint8Array(this.__result); | |||||
| } | |||||
| reset(){ | |||||
| this.__PC = this.__initPC; | |||||
| this.__varlabels = {}; | |||||
| this.__jmplabels = {}; | |||||
| this.__result = []; | |||||
| } | } | ||||
| } | } | ||||