| @@ -103,11 +103,34 @@ class Assembler{ | |||
| this.__varlabels = {}; | |||
| // Labels that hold jump/branch locations. | |||
| 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 | |||
| // 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){ | |||
| if (lbl in this.__varlabels) | |||
| throw new Error("Variable label '" + lbl + "' defined more than once."); | |||
| @@ -192,7 +215,7 @@ class Assembler{ | |||
| if (mode !== ""){ | |||
| if (tA.startsWith("+$")){ | |||
| v = parseInt(tA.substr(2)); | |||
| v = parseInt(tA.substr(2), 16); | |||
| } else { | |||
| let lbl = tA.substr(1); | |||
| if (lbl in this.__varlabels){ | |||
| @@ -233,11 +256,14 @@ class Assembler{ | |||
| throw new Error("Malformed op-code or value on program address " + toHexString(this.__PC)); | |||
| } | |||
| compile(src){ | |||
| var op = []; | |||
| src.split("\n").forEach((line)=>{ | |||
| line = line.trim(); | |||
| if (line === ""){return;} | |||
| if (line === ""){return this;} | |||
| if (line[0] !== ";"){ // Skip comment lines. | |||
| line = line.split(";")[0].trim(); // Take out any trailing comments. | |||
| var tokens = tokenize(line); | |||
| @@ -647,7 +673,20 @@ class Assembler{ | |||
| } | |||
| } | |||
| }); | |||
| 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 = []; | |||
| } | |||
| } | |||