diff options
author | sanine <sanine.not@pm.me> | 2023-05-22 23:43:39 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-05-22 23:43:39 -0500 |
commit | c1709249728cb9ad7011b0d39d18ad87d4636f4b (patch) | |
tree | d4fae900af8cc4ee2956bbbe3c5c70c1664b193f /src | |
parent | d6e89d16d332954dde3fc4c5ee7549af7c8bb556 (diff) |
begin testing and make grammar more spec-compliant
Diffstat (limited to 'src')
-rw-r--r-- | src/parser/grammar.jison | 66 | ||||
-rw-r--r-- | src/parser/grammar.js | 65 | ||||
-rw-r--r-- | src/parser/parser.js | 32 | ||||
-rw-r--r-- | src/parser/parser.test.js | 109 | ||||
-rw-r--r-- | src/vm/core.js | 3 | ||||
-rw-r--r-- | src/vm/instruction.js | 111 | ||||
-rw-r--r-- | src/vm/instruction.test.js | 15 |
7 files changed, 226 insertions, 175 deletions
diff --git a/src/parser/grammar.jison b/src/parser/grammar.jison index edcca5c..4f4a433 100644 --- a/src/parser/grammar.jison +++ b/src/parser/grammar.jison @@ -110,46 +110,80 @@ row op - : opcode address "," address - { $$ = { opcode: $opcode, a: $address1, b: $address2 }; } - | opcode address - { + : op_immediate_a { $$ = $1; } + | op_immediate_b { $$ = $1; } + | DAT dat_addr "," dat_addr { $$ = { opcode: $1, a: $dat_addr1, b: $dat_addr2 }; } + | DAT dat_addr { $$ = { opcode: $1, a: $dat_addr1, b: { mode: 'immediate', value: 0 } }; } + ; + + +op_immediate_a + : opcode_immediate_a address "," non_immediate_addr + { $$ = { opcode: $1, a: $2, b: $4 }; } + ; + +op_immediate_b + : opcode_immediate_b non_immediate_addr "," address + { $$ = { opcode: $1, a: $2, b: $4 }; } + | opcode_immediate_b non_immediate_addr + { $$ = { - opcode: $opcode, - a: $address, + opcode: $1, + a: $2, b: { mode: 'direct', value: 0 }, }; } ; -opcode + +opcode_immediate_a : MOV { $$ = $1; } | ADD { $$ = $1; } | SUB { $$ = $1; } | CMP { $$ = $1; } | SLT { $$ = $1; } - | JMP { $$ = $1; } + ; +opcode_immediate_b + : JMP { $$ = $1; } | JMZ { $$ = $1; } | JMN { $$ = $1; } | DJN { $$ = $1; } | SPL { $$ = $1; } - | DAT { $$ = $1; } ; address - : address_mode e { $$ = { mode: $1, value: $2 }; } - | e { $$ = { mode: 'direct', value: $1 }; } + : immediate_addr { $$ = $1; } + | non_immediate_addr { $$ = $1; } ; -address_mode - : "#" { $$ = 'immediate'; } - | "@" { $$ = 'indirect'; } - | "<" { $$ = 'predecrement'; } - | "$" { $$ = 'direct'; } +non_immediate_addr + : direct_addr { $$ = $1; } + | indirect_addr { $$ = $1; } + | predecrement_addr { $$ = $1; } ; +dat_addr + : immediate_addr { $$ = $1; } + | predecrement_addr { $$ = $1; } + ; + +immediate_addr + : "#" e { $$ = { mode: 'immediate', value: $e }; } + ; +direct_addr + : "$" e { $$ = { mode: 'direct', value: $e }; } + | e { $$ = { mode: 'direct', value: $e }; } + ; +indirect_addr + : "@" e { $$ = { mode: 'indirect', value: $e }; } + ; +predecrement_addr + : "<" e { $$ = { mode: 'predecrement', value: $e }; } + ; + + e diff --git a/src/parser/grammar.js b/src/parser/grammar.js index 6d055c7..6fa53b7 100644 --- a/src/parser/grammar.js +++ b/src/parser/grammar.js @@ -72,12 +72,12 @@ } */ var grammar = (function(){ -var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[1,5],$V1=[1,10],$V2=[1,11],$V3=[1,12],$V4=[1,13],$V5=[1,14],$V6=[1,15],$V7=[1,16],$V8=[1,17],$V9=[1,18],$Va=[1,19],$Vb=[1,20],$Vc=[1,9],$Vd=[5,10,19,20,21,22,23,24,25,26,27,28,29,42],$Ve=[1,29],$Vf=[1,30],$Vg=[1,31],$Vh=[1,32],$Vi=[1,34],$Vj=[1,33],$Vk=[1,35],$Vl=[31,32,33,34,36,39,41,42],$Vm=[1,40],$Vn=[1,41],$Vo=[10,18],$Vp=[1,45],$Vq=[1,46],$Vr=[1,47],$Vs=[1,48],$Vt=[36,39,41,42],$Vu=[10,18,35,36,37,38,40],$Vv=[9,10],$Vw=[10,18,35,36,40]; +var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[1,5],$V1=[1,10],$V2=[1,14],$V3=[1,15],$V4=[1,16],$V5=[1,17],$V6=[1,18],$V7=[1,19],$V8=[1,20],$V9=[1,21],$Va=[1,22],$Vb=[1,23],$Vc=[1,11],$Vd=[5,10,18,25,26,27,28,29,30,31,32,33,34,50],$Ve=[1,32],$Vf=[1,33],$Vg=[1,40],$Vh=[1,42],$Vi=[1,44],$Vj=[1,43],$Vk=[1,45],$Vl=[39,40,41,42,44,47,49,50],$Vm=[40,41,42,44,47,49,50],$Vn=[1,51],$Vo=[1,52],$Vp=[10,20],$Vq=[1,59],$Vr=[1,60],$Vs=[1,61],$Vt=[1,62],$Vu=[10,20,43,44,45,46,48],$Vv=[9,10],$Vw=[10,20,43,44,48]; var parser = {trace: function trace () { }, yy: {}, -symbols_: {"error":2,"program":3,"lines":4,"END":5,"coda":6,"label":7,"newlines":8,"EOF":9,"NEWLINE":10,"line":11,"row":12,"op":13,"EQU":14,"e":15,"opcode":16,"address":17,",":18,"MOV":19,"ADD":20,"SUB":21,"CMP":22,"SLT":23,"JMP":24,"JMZ":25,"JMN":26,"DJN":27,"SPL":28,"DAT":29,"address_mode":30,"#":31,"@":32,"<":33,"$":34,"+":35,"-":36,"*":37,"/":38,"(":39,")":40,"NUMBER":41,"LABEL":42,"$accept":0,"$end":1}, -terminals_: {2:"error",5:"END",9:"EOF",10:"NEWLINE",14:"EQU",18:",",19:"MOV",20:"ADD",21:"SUB",22:"CMP",23:"SLT",24:"JMP",25:"JMZ",26:"JMN",27:"DJN",28:"SPL",29:"DAT",31:"#",32:"@",33:"<",34:"$",35:"+",36:"-",37:"*",38:"/",39:"(",40:")",41:"NUMBER",42:"LABEL"}, -productions_: [0,[3,3],[3,4],[6,2],[6,1],[8,2],[8,1],[4,2],[4,1],[11,2],[11,1],[12,1],[12,2],[12,3],[13,4],[13,2],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[17,2],[17,1],[30,1],[30,1],[30,1],[30,1],[15,3],[15,3],[15,3],[15,3],[15,3],[15,2],[15,1],[15,1],[7,1]], +symbols_: {"error":2,"program":3,"lines":4,"END":5,"coda":6,"label":7,"newlines":8,"EOF":9,"NEWLINE":10,"line":11,"row":12,"op":13,"EQU":14,"e":15,"op_immediate_a":16,"op_immediate_b":17,"DAT":18,"dat_addr":19,",":20,"opcode_immediate_a":21,"address":22,"non_immediate_addr":23,"opcode_immediate_b":24,"MOV":25,"ADD":26,"SUB":27,"CMP":28,"SLT":29,"JMP":30,"JMZ":31,"JMN":32,"DJN":33,"SPL":34,"immediate_addr":35,"direct_addr":36,"indirect_addr":37,"predecrement_addr":38,"#":39,"$":40,"@":41,"<":42,"+":43,"-":44,"*":45,"/":46,"(":47,")":48,"NUMBER":49,"LABEL":50,"$accept":0,"$end":1}, +terminals_: {2:"error",5:"END",9:"EOF",10:"NEWLINE",14:"EQU",18:"DAT",20:",",25:"MOV",26:"ADD",27:"SUB",28:"CMP",29:"SLT",30:"JMP",31:"JMZ",32:"JMN",33:"DJN",34:"SPL",39:"#",40:"$",41:"@",42:"<",43:"+",44:"-",45:"*",46:"/",47:"(",48:")",49:"NUMBER",50:"LABEL"}, +productions_: [0,[3,3],[3,4],[6,2],[6,1],[8,2],[8,1],[4,2],[4,1],[11,2],[11,1],[12,1],[12,2],[12,3],[13,1],[13,1],[13,4],[13,2],[16,4],[17,4],[17,2],[21,1],[21,1],[21,1],[21,1],[21,1],[24,1],[24,1],[24,1],[24,1],[24,1],[22,1],[22,1],[23,1],[23,1],[23,1],[19,1],[19,1],[35,2],[36,2],[36,1],[37,2],[38,2],[15,3],[15,3],[15,3],[15,3],[15,3],[15,2],[15,1],[15,1],[7,1]], performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) { /* this == yyval */ @@ -131,11 +131,17 @@ case 13: this.$ = null; break; -case 14: +case 14: case 15: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: + this.$ = $$[$0]; +break; +case 16: case 18: case 19: this.$ = { opcode: $$[$0-3], a: $$[$0-2], b: $$[$0] }; break; -case 15: - +case 17: + this.$ = { opcode: $$[$0-1], a: $$[$0], b: { mode: 'immediate', value: 0 } }; +break; +case 20: + this.$ = { opcode: $$[$0-1], a: $$[$0], @@ -143,60 +149,51 @@ case 15: }; break; -case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: case 26: - this.$ = $$[$0]; -break; -case 27: - this.$ = { mode: $$[$0-1], value: $$[$0] }; +case 38: + this.$ = { mode: 'immediate', value: $$[$0] }; break; -case 28: +case 39: case 40: this.$ = { mode: 'direct', value: $$[$0] }; break; -case 29: - this.$ = 'immediate'; -break; -case 30: - this.$ = 'indirect'; -break; -case 31: - this.$ = 'predecrement'; +case 41: + this.$ = { mode: 'indirect', value: $$[$0] }; break; -case 32: - this.$ = 'direct'; +case 42: + this.$ = { mode: 'predecrement', value: $$[$0] }; break; -case 33: +case 43: this.$ = Math.floor($$[$0-2] + $$[$0]); break; -case 34: +case 44: this.$ = Math.floor($$[$0-2] - $$[$0]); break; -case 35: +case 45: this.$ = Math.floor($$[$0-2] * $$[$0]); break; -case 36: +case 46: this.$ = Math.floor($$[$0-2] / $$[$0]); break; -case 37: +case 47: this.$ = Math.floor($$[$0-1]); break; -case 38: +case 48: this.$ = - $$[$0]; break; -case 39: +case 49: this.$ = Math.floor(Number(yytext)); break; -case 40: +case 50: this.$ = yy.getLabel($$[$0]); break; -case 41: +case 51: this.$ = yytext; break; } }, -table: [{3:1,4:2,7:7,10:$V0,11:3,12:4,13:6,16:8,19:$V1,20:$V2,21:$V3,22:$V4,23:$V5,24:$V6,25:$V7,26:$V8,27:$V9,28:$Va,29:$Vb,42:$Vc},{1:[3]},{5:[1,21],7:7,10:$V0,11:22,12:4,13:6,16:8,19:$V1,20:$V2,21:$V3,22:$V4,23:$V5,24:$V6,25:$V7,26:$V8,27:$V9,28:$Va,29:$Vb,42:$Vc},o($Vd,[2,8]),{10:[1,23]},o($Vd,[2,10]),{10:[2,11]},{13:24,14:[1,25],16:8,19:$V1,20:$V2,21:$V3,22:$V4,23:$V5,24:$V6,25:$V7,26:$V8,27:$V9,28:$Va,29:$Vb},{7:36,15:28,17:26,30:27,31:$Ve,32:$Vf,33:$Vg,34:$Vh,36:$Vi,39:$Vj,41:$Vk,42:$Vc},o([9,10,14,18,19,20,21,22,23,24,25,26,27,28,29,35,36,37,38,40],[2,41]),o($Vl,[2,16]),o($Vl,[2,17]),o($Vl,[2,18]),o($Vl,[2,19]),o($Vl,[2,20]),o($Vl,[2,21]),o($Vl,[2,22]),o($Vl,[2,23]),o($Vl,[2,24]),o($Vl,[2,25]),o($Vl,[2,26]),{6:37,7:38,8:39,9:$Vm,10:$Vn,42:$Vc},o($Vd,[2,7]),o($Vd,[2,9]),{10:[2,12]},{7:36,15:42,36:$Vi,39:$Vj,41:$Vk,42:$Vc},{10:[2,15],18:[1,43]},{7:36,15:44,36:$Vi,39:$Vj,41:$Vk,42:$Vc},o($Vo,[2,28],{35:$Vp,36:$Vq,37:$Vr,38:$Vs}),o($Vt,[2,29]),o($Vt,[2,30]),o($Vt,[2,31]),o($Vt,[2,32]),{7:36,15:49,36:$Vi,39:$Vj,41:$Vk,42:$Vc},{7:36,15:50,36:$Vi,39:$Vj,41:$Vk,42:$Vc},o($Vu,[2,39]),o($Vu,[2,40]),{1:[2,1]},{6:51,8:39,9:$Vm,10:$Vn},{9:[1,52],10:[1,53]},{1:[2,4]},o($Vv,[2,6]),{10:[2,13],35:$Vp,36:$Vq,37:$Vr,38:$Vs},{7:36,15:28,17:54,30:27,31:$Ve,32:$Vf,33:$Vg,34:$Vh,36:$Vi,39:$Vj,41:$Vk,42:$Vc},o($Vo,[2,27],{35:$Vp,36:$Vq,37:$Vr,38:$Vs}),{7:36,15:55,36:$Vi,39:$Vj,41:$Vk,42:$Vc},{7:36,15:56,36:$Vi,39:$Vj,41:$Vk,42:$Vc},{7:36,15:57,36:$Vi,39:$Vj,41:$Vk,42:$Vc},{7:36,15:58,36:$Vi,39:$Vj,41:$Vk,42:$Vc},{35:$Vp,36:$Vq,37:$Vr,38:$Vs,40:[1,59]},o($Vu,[2,38]),{1:[2,2]},{1:[2,3]},o($Vv,[2,5]),{10:[2,14]},o($Vw,[2,33],{37:$Vr,38:$Vs}),o($Vw,[2,34],{37:$Vr,38:$Vs}),o($Vu,[2,35]),o($Vu,[2,36]),o($Vu,[2,37])], -defaultActions: {6:[2,11],24:[2,12],37:[2,1],40:[2,4],51:[2,2],52:[2,3],54:[2,14]}, +table: [{3:1,4:2,7:7,10:$V0,11:3,12:4,13:6,16:8,17:9,18:$V1,21:12,24:13,25:$V2,26:$V3,27:$V4,28:$V5,29:$V6,30:$V7,31:$V8,32:$V9,33:$Va,34:$Vb,50:$Vc},{1:[3]},{5:[1,24],7:7,10:$V0,11:25,12:4,13:6,16:8,17:9,18:$V1,21:12,24:13,25:$V2,26:$V3,27:$V4,28:$V5,29:$V6,30:$V7,31:$V8,32:$V9,33:$Va,34:$Vb,50:$Vc},o($Vd,[2,8]),{10:[1,26]},o($Vd,[2,10]),{10:[2,11]},{13:27,14:[1,28],16:8,17:9,18:$V1,21:12,24:13,25:$V2,26:$V3,27:$V4,28:$V5,29:$V6,30:$V7,31:$V8,32:$V9,33:$Va,34:$Vb},{10:[2,14]},{10:[2,15]},{19:29,35:30,38:31,39:$Ve,42:$Vf},o([9,10,14,18,20,25,26,27,28,29,30,31,32,33,34,43,44,45,46,48],[2,51]),{7:46,15:41,22:34,23:36,35:35,36:37,37:38,38:39,39:$Ve,40:$Vg,41:$Vh,42:$Vf,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:41,23:47,36:37,37:38,38:39,40:$Vg,41:$Vh,42:$Vf,44:$Vi,47:$Vj,49:$Vk,50:$Vc},o($Vl,[2,21]),o($Vl,[2,22]),o($Vl,[2,23]),o($Vl,[2,24]),o($Vl,[2,25]),o($Vm,[2,26]),o($Vm,[2,27]),o($Vm,[2,28]),o($Vm,[2,29]),o($Vm,[2,30]),{6:48,7:49,8:50,9:$Vn,10:$Vo,50:$Vc},o($Vd,[2,7]),o($Vd,[2,9]),{10:[2,12]},{7:46,15:53,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{10:[2,17],20:[1,54]},o($Vp,[2,36]),o($Vp,[2,37]),{7:46,15:55,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:56,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{20:[1,57]},o($Vp,[2,31]),o($Vp,[2,32]),o($Vp,[2,33]),o($Vp,[2,34]),o($Vp,[2,35]),{7:46,15:58,44:$Vi,47:$Vj,49:$Vk,50:$Vc},o($Vp,[2,40],{43:$Vq,44:$Vr,45:$Vs,46:$Vt}),{7:46,15:63,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:64,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:65,44:$Vi,47:$Vj,49:$Vk,50:$Vc},o($Vu,[2,49]),o($Vu,[2,50]),{10:[2,20],20:[1,66]},{1:[2,1]},{6:67,8:50,9:$Vn,10:$Vo},{9:[1,68],10:[1,69]},{1:[2,4]},o($Vv,[2,6]),{10:[2,13],43:$Vq,44:$Vr,45:$Vs,46:$Vt},{19:70,35:30,38:31,39:$Ve,42:$Vf},o($Vp,[2,38],{43:$Vq,44:$Vr,45:$Vs,46:$Vt}),o($Vp,[2,42],{43:$Vq,44:$Vr,45:$Vs,46:$Vt}),{7:46,15:41,23:71,36:37,37:38,38:39,40:$Vg,41:$Vh,42:$Vf,44:$Vi,47:$Vj,49:$Vk,50:$Vc},o($Vp,[2,39],{43:$Vq,44:$Vr,45:$Vs,46:$Vt}),{7:46,15:72,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:73,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:74,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{7:46,15:75,44:$Vi,47:$Vj,49:$Vk,50:$Vc},o($Vp,[2,41],{43:$Vq,44:$Vr,45:$Vs,46:$Vt}),{43:$Vq,44:$Vr,45:$Vs,46:$Vt,48:[1,76]},o($Vu,[2,48]),{7:46,15:41,22:77,23:36,35:35,36:37,37:38,38:39,39:$Ve,40:$Vg,41:$Vh,42:$Vf,44:$Vi,47:$Vj,49:$Vk,50:$Vc},{1:[2,2]},{1:[2,3]},o($Vv,[2,5]),{10:[2,16]},{10:[2,18]},o($Vw,[2,43],{45:$Vs,46:$Vt}),o($Vw,[2,44],{45:$Vs,46:$Vt}),o($Vu,[2,45]),o($Vu,[2,46]),o($Vu,[2,47]),{10:[2,19]}], +defaultActions: {6:[2,11],8:[2,14],9:[2,15],27:[2,12],48:[2,1],51:[2,4],67:[2,2],68:[2,3],70:[2,16],71:[2,18],77:[2,19]}, parseError: function parseError (str, hash) { if (hash.recoverable) { this.trace(str); diff --git a/src/parser/parser.js b/src/parser/parser.js index 8672832..3434fee 100644 --- a/src/parser/parser.js +++ b/src/parser/parser.js @@ -33,19 +33,19 @@ parser.yy = { }; -//if (process.argv[1] === 'parser.js' && process.argv.length >= 3) { - fs.readFile(process.argv[2], 'utf8', (err, data) => { - if (err) throw err; - let result = parser.parse(data.toUpperCase()); - if (typeof(result.start) === "string") { - result.start = parser.yy.line[result.start]; - } - for (let pc=0; pc<result.program.length; pc += 1) { - parser.yy.tidyAddress(pc, result.program[pc].a); - parser.yy.tidyAddress(pc, result.program[pc].b); - } - console.log(result.start); - console.log(result.program); - console.log(parser.yy); - }); -//} +exports.assemble = function(sourceCode) { + let result = parser.parse(sourceCode.toUpperCase()); + + // set start point + if (typeof(result.start) === "string") { + result.start = parser.yy.line[result.start]; + } + + // convert line labels to actual numbers + for (let pc=0; pc<result.program.length; pc += 1) { + parser.yy.tidyAddress(pc, result.program[pc].a); + parser.yy.tidyAddress(pc, result.program[pc].b); + } + + return result; +} diff --git a/src/parser/parser.test.js b/src/parser/parser.test.js new file mode 100644 index 0000000..b496cdc --- /dev/null +++ b/src/parser/parser.test.js @@ -0,0 +1,109 @@ +'use strict'; + +const { assemble } = require('./parser.js'); + + +test('assemble the imp', () => { + expect(assemble(` + mov 0, 1 + end + `)).toEqual({ + start: 0, + program: [ + { + opcode: 'MOV', + a: { mode: 'direct', value: 0 }, + b: { mode: 'direct', value: 1 }, + }, + ], + }); +}); + + +test('assemble dwarf', () => { + expect(assemble(` + bomb dat #0 + dwarf add #4, bomb + mov bomb, @bomb + jmp dwarf + end dwarf + `)).toEqual({ + start: 1, + program: [ + { opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: 0 }, + }, + { opcode: 'ADD', + a: { mode: 'immediate', value: 4 }, + b: { mode: 'direct', value: -1 }, + }, + { opcode: 'MOV', + a: { mode: 'direct', value: -2 }, + b: { mode: 'indirect', value: -2 }, + }, + { opcode: 'JMP', + a: { mode: 'direct', value: -2 }, + b: { mode: 'direct', value: 0 }, + }, + ], + }); +}); + + +test('assemble armadillo', () => { + expect(assemble(` + ; armadillo + ; author: stefan strack + + bomb spl 0 + loop add #3039, ptr + ptr mov bomb, 81 + jmp loop + mov 1, <-1 + end bomb + `)).toEqual({ + start: 0, + program: [ + { opcode: 'SPL', + a: { mode: 'direct', value: 0 }, + b: { mode: 'direct', value: 0 }, + }, + { opcode: 'ADD', + a: { mode: 'immediate', value: 3039 }, + b: { mode: 'direct', value: 1 }, + }, + { opcode: 'MOV', + a: { mode: 'direct', value: -2 }, + b: { mode: 'direct', value: 81 }, + }, + { opcode: 'JMP', + a: { mode: 'direct', value: -2 }, + b: { mode: 'direct', value: 0 }, + }, + { opcode: 'MOV', + a: { mode: 'direct', value: 1 }, + b: { mode: 'predecrement', value: -1 }, + }, + ], + }); +}); + + +test('illegal instructions are not assembled', () => { + expect(() => assemble("dat 0, #0\nend")).toThrow(); + expect(() => assemble("dat @0, #0\nend")).toThrow(); + + expect(() => assemble("mov 0, #0\nend")).toThrow(); + expect(() => assemble("add 0, #0\nend")).toThrow(); + expect(() => assemble("sub 0, #0\nend")).toThrow(); + expect(() => assemble("cmp 0, #0\nend")).toThrow(); + expect(() => assemble("slt 0, #0\nend")).toThrow(); + + expect(() => assemble("jmp #0, 0\nend")).toThrow(); + expect(() => assemble("jmz #0, 0\nend")).toThrow(); + expect(() => assemble("jmn #0, 0\nend")).toThrow(); + expect(() => assemble("jmn #0, 0\nend")).toThrow(); + expect(() => assemble("djn #0, 0\nend")).toThrow(); + expect(() => assemble("spl #0, 0\nend")).toThrow(); +}); diff --git a/src/vm/core.js b/src/vm/core.js index 292ceb0..399cfc6 100644 --- a/src/vm/core.js +++ b/src/vm/core.js @@ -49,3 +49,6 @@ class Core { return this.data[this.getLocation(pc, address)]; } } + + +exports.Core = Core; diff --git a/src/vm/instruction.js b/src/vm/instruction.js index 0fe922d..ac4cd83 100644 --- a/src/vm/instruction.js +++ b/src/vm/instruction.js @@ -3,114 +3,7 @@ const { AddrMode } = require('./enum.js'); -function DoDat(core, pc, ins) { - // do nothing +exports.DAT = function(core, pc, ins) { + // do nothing and die return []; } - - -function DoMov(core, pc, ins) { - if (ins.a.mode === AddrMode.Immediate) { - let mem = core.getValue(pc, ins.b); - mem.b.value = ins.a.value; - } else { - const src = core.getValue(pc, ins.a); - const dstLoc = core.getLocation(pc, ins.b); - - // hacky deep copy - core.data[dstLoc] = JSON.parse(JSON.stringify(src)); - } - return [pc + 1]; -} - - -function DoAdd(core, pc, ins) { - if (ins.a.mode === AddrMode.Immediate) { - let dst = core.getValue(pc, ins.b); - dst.b += ins.a.value; - } else { - let src = core.getValue(pc, ins.a); - let dst = core.getValue(pc, ins.b); - dst.a.value += src.a.value; - dst.b.value += src.b.value; - } - return [pc + 1]; -} - - -function DoSub(core, pc, ins) { - if (ins.a.mode === AddrMode.Immediate) { - let dst = core.getValue(pc, ins.b); - dst.b -= ins.a.value; - } else { - let src = core.getValue(pc, ins.a); - let dst = core.getValue(pc, ins.b); - dst.a.value -= src.a.value; - dst.b.value -= src.b.value; - } - return [pc + 1]; -} - - -function DoJmp(core, pc, ins) { - return [core.getLocation(pc, ins.a)]; -} - - -function DoJmz(core, pc, ins) { - let test - if (ins.b.mode === AddrMode.Immediate) { - test = ins; - } else { - test = core.getValue(pc, ins.b); - } - - if (test.b.value === 0) { - return [core.getLocation(pc, ins.a)]; - } else { - return [pc + 1]; - } -} - - -function DoJmn(core, pc, ins) { - let test - if (ins.b.mode === AddrMode.Immediate) { - test = ins; - } else { - test = core.getValue(pc, ins.b); - } - - if (test.b.value !== 0) { - return [core.getLocation(pc, ins.a)]; - } else { - return [pc + 1]; - } -} - - -function DoCmp(core, pc, ins) { - if (ins.a.mode === AddrMode.Immediate) { - const test = core.getValue(pc, ins.b); - if (ins.a.value === test.b.value) { - return [pc + 2]; - } else { - return [pc + 1]; - } - } else { - const a = core.getValue(pc, ins.a); - const b = core.getValue(pc, ins.b); - - if ( - a.opcode === b.opcode && - a.a.value === b.a.value && - a.b.value === b.b.value) - ) { - return [pc + 2]; - } else { - return [pc + 1]; - } - } -} - - diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js new file mode 100644 index 0000000..5dc7eea --- /dev/null +++ b/src/vm/instruction.test.js @@ -0,0 +1,15 @@ +'use strict'; + +const { Core } = require('./core.js'); +const { DAT } = require('./instruction.js'); + + +const CORESIZE = 8000; + + +test('DAT does nothing and kills the program', () => { + const core = new Core(CORESIZE); + const pc = 0; + const ins = core.data[pc]; + expect(DAT(core, pc, ins)).toEqual([]); +}); |