diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-22 14:26:10 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-22 14:26:10 -0500 |
commit | f2a515a940ea5e66c9fbc9669e6293ae32b7041a (patch) | |
tree | 478d369195e90380027fef4137b9caac833bb219 /src | |
parent | ff3fdee3841743e7fe101c3fc7020a4b043bc91b (diff) |
update label style and add implicit second operand
Diffstat (limited to 'src')
-rw-r--r-- | src/parser/dwarf.red | 5 | ||||
-rw-r--r-- | src/parser/grammar.jison | 87 | ||||
-rw-r--r-- | src/parser/grammar.js | 129 | ||||
-rw-r--r-- | src/parser/parser.js | 36 |
4 files changed, 156 insertions, 101 deletions
diff --git a/src/parser/dwarf.red b/src/parser/dwarf.red new file mode 100644 index 0000000..b239246 --- /dev/null +++ b/src/parser/dwarf.red @@ -0,0 +1,5 @@ +bomb dat #0 +dwarf add #4, bomb + mov bomb, @bomb + jmp dwarf +end dwarf diff --git a/src/parser/grammar.jison b/src/parser/grammar.jison index 80edfc5..2f5b30a 100644 --- a/src/parser/grammar.jison +++ b/src/parser/grammar.jison @@ -4,44 +4,43 @@ %% -<<EOF>> { console.log("EOF"); return "EOF"; } -<comment>"\n" { this.popState(); console.log("NEWLINE"); return "NEWLINE"; } +<<EOF>> { return "EOF"; } +<comment>"\n" { this.popState(); return "NEWLINE"; } <comment>. { /* ignore anything else inside a comment */ } ";" { this.pushState('comment'); } -[\n] { console.log("NEWLINE"); return "NEWLINE"; } +[\n] { return "NEWLINE"; } \s { /* ignore whitespace */ } -"MOV" { console.log("MOV"); return "MOV"; } -"ADD" { console.log("ADD"); return "ADD"; } -"SUB" { console.log("SUB"); return "SUB"; } -"CMP" { console.log("CMP"); return "CMP"; } -"SLT" { console.log("SLT"); return "SLT"; } -"JMP" { console.log("JMP"); return "JMP"; } -"JMZ" { console.log("JMZ"); return "JMZ"; } -"JMN" { console.log("JMN"); return "JMN"; } -"DJN" { console.log("DJN"); return "DJN"; } -"SPL" { console.log("SPL"); return "SPL"; } -"DAT" { console.log("DAT"); return "DAT"; } - -"EQU" { console.log("EQU"); return "EQU"; } -"END" { console.log("END"); return "END"; } -":" { console.log(":"); return ":"; } -"," { console.log(","); return ","; } - -"#" { console.log("#"); return "#"; } -"@" { console.log("@"); return "@"; } -"<" { console.log("<"); return "<"; } -"$" { console.log("$"); return "$"; } - -"(" { console.log("("); return "("; } -")" { console.log(")"); return ")"; } -"+" { console.log("+"); return "+"; } -"-" { console.log("-"); return "-"; } -"*" { console.log("*"); return "*"; } -"/" { console.log("/"); return "/"; } -[0-9]+ { console.log("NUMBER"); return "NUMBER"; } -[A-Z][A-Z0-9_]+ { console.log("LABEL"); return "LABEL"; } +"MOV" { return "MOV"; } +"ADD" { return "ADD"; } +"SUB" { return "SUB"; } +"CMP" { return "CMP"; } +"SLT" { return "SLT"; } +"JMP" { return "JMP"; } +"JMZ" { return "JMZ"; } +"JMN" { return "JMN"; } +"DJN" { return "DJN"; } +"SPL" { return "SPL"; } +"DAT" { return "DAT"; } + +"EQU" { return "EQU"; } +"END" { return "END"; } +"," { return ","; } + +"#" { return "#"; } +"@" { return "@"; } +"<" { return "<"; } +"$" { return "$"; } + +"(" { return "("; } +")" { return ")"; } +"+" { return "+"; } +"-" { return "-"; } +"*" { return "*"; } +"/" { return "/"; } +[0-9]+ { return "NUMBER"; } +[A-Z][A-Z0-9_]+ { return "LABEL"; } /lex @@ -52,8 +51,8 @@ %% program - : lines END coda { yy.start = 0; console.log($lines); return $lines; } - | lines END label coda { yy.start = yy.getLabel($label); console.log($lines); return $lines; } + : lines END coda { yy.start = 0; return $lines; } + | lines END label coda { yy.start = yy.getLabel($label); return $lines; } ; coda @@ -93,18 +92,18 @@ line row : op { - yy.pc += 1; + yy.step(); $$ = $op; } - | label ":" op + | label op { - yy.setLabel($label, yy.pc); - yy.pc += 1; + yy.setLine($label); + yy.step(); $$ = $op; } | label EQU e { - yy.setLabel($label, $e); + yy.setEqu($label, $e); $$ = null; } ; @@ -113,6 +112,14 @@ row op : opcode address "," address { $$ = { opcode: $opcode, a: $address1, b: $address2 }; } + | opcode address + { + $$ = { + opcode: $opcode, + a: $address, + b: { mode: 'direct', value: 0 }, + }; + } ; diff --git a/src/parser/grammar.js b/src/parser/grammar.js index 7dea851..21f6b89 100644 --- a/src/parser/grammar.js +++ b/src/parser/grammar.js @@ -72,22 +72,22 @@ } */ 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,20,21,22,23,24,25,26,27,28,29,30,43],$Ve=[1,29],$Vf=[1,30],$Vg=[1,31],$Vh=[1,32],$Vi=[1,34],$Vj=[1,33],$Vk=[1,35],$Vl=[32,33,34,35,37,40,42,43],$Vm=[1,40],$Vn=[1,41],$Vo=[10,19],$Vp=[1,46],$Vq=[1,47],$Vr=[1,48],$Vs=[1,49],$Vt=[37,40,42,43],$Vu=[10,19,36,37,38,39,41],$Vv=[9,10],$Vw=[10,19,36,37,41]; +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 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,":":14,"EQU":15,"e":16,"opcode":17,"address":18,",":19,"MOV":20,"ADD":21,"SUB":22,"CMP":23,"SLT":24,"JMP":25,"JMZ":26,"JMN":27,"DJN":28,"SPL":29,"DAT":30,"address_mode":31,"#":32,"@":33,"<":34,"$":35,"+":36,"-":37,"*":38,"/":39,"(":40,")":41,"NUMBER":42,"LABEL":43,"$accept":0,"$end":1}, -terminals_: {2:"error",5:"END",9:"EOF",10:"NEWLINE",14:":",15:"EQU",19:",",20:"MOV",21:"ADD",22:"SUB",23:"CMP",24:"SLT",25:"JMP",26:"JMZ",27:"JMN",28:"DJN",29:"SPL",30:"DAT",32:"#",33:"@",34:"<",35:"$",36:"+",37:"-",38:"*",39:"/",40:"(",41:")",42:"NUMBER",43:"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,3],[12,3],[13,4],[17,1],[17,1],[17,1],[17,1],[17,1],[17,1],[17,1],[17,1],[17,1],[17,1],[17,1],[18,2],[18,1],[31,1],[31,1],[31,1],[31,1],[16,3],[16,3],[16,3],[16,3],[16,3],[16,2],[16,1],[16,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,"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]], performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) { /* this == yyval */ var $0 = $$.length - 1; switch (yystate) { case 1: - yy.start = 0; console.log($$[$0-2]); return $$[$0-2]; + yy.start = 0; return $$[$0-2]; break; case 2: - yy.start = yy.getLabel($$[$0-1]); console.log($$[$0-3]); return $$[$0-3]; + yy.start = yy.getLabel($$[$0-1]); return $$[$0-3]; break; case 7: @@ -114,80 +114,89 @@ case 10: break; case 11: - yy.pc += 1; + yy.step(); this.$ = $$[$0]; break; case 12: - yy.setLabel($$[$0-2], yy.pc); - yy.pc += 1; + yy.setLine($$[$0-1]); + yy.step(); this.$ = $$[$0]; break; case 13: - yy.setLabel($$[$0-2], $$[$0]); + yy.setEqu($$[$0-2], $$[$0]); this.$ = null; break; case 14: this.$ = { opcode: $$[$0-3], a: $$[$0-2], b: $$[$0] }; break; -case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: +case 15: + + this.$ = { + opcode: $$[$0-1], + a: $$[$0], + b: { mode: 'direct', value: 0 }, + }; + +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 26: +case 27: this.$ = { mode: $$[$0-1], value: $$[$0] }; break; -case 27: +case 28: this.$ = { mode: 'direct', value: $$[$0] }; break; -case 28: +case 29: this.$ = 'immediate'; break; -case 29: +case 30: this.$ = 'indirect'; break; -case 30: +case 31: this.$ = 'predecrement'; break; -case 31: +case 32: this.$ = 'direct'; break; -case 32: +case 33: this.$ = Math.floor($$[$0-2] + $$[$0]); break; -case 33: +case 34: this.$ = Math.floor($$[$0-2] - $$[$0]); break; -case 34: +case 35: this.$ = Math.floor($$[$0-2] * $$[$0]); break; -case 35: +case 36: this.$ = Math.floor($$[$0-2] / $$[$0]); break; -case 36: +case 37: this.$ = Math.floor($$[$0-1]); break; -case 37: +case 38: this.$ = - $$[$0]; break; -case 38: +case 39: this.$ = Math.floor(Number(yytext)); break; -case 39: +case 40: this.$ = yy.getLabel($$[$0]); break; -case 40: +case 41: this.$ = yytext; break; } }, -table: [{3:1,4:2,7:7,10:$V0,11:3,12:4,13:6,17:8,20:$V1,21:$V2,22:$V3,23:$V4,24:$V5,25:$V6,26:$V7,27:$V8,28:$V9,29:$Va,30:$Vb,43:$Vc},{1:[3]},{5:[1,21],7:7,10:$V0,11:22,12:4,13:6,17:8,20:$V1,21:$V2,22:$V3,23:$V4,24:$V5,25:$V6,26:$V7,27:$V8,28:$V9,29:$Va,30:$Vb,43:$Vc},o($Vd,[2,8]),{10:[1,23]},o($Vd,[2,10]),{10:[2,11]},{14:[1,24],15:[1,25]},{7:36,16:28,18:26,31:27,32:$Ve,33:$Vf,34:$Vg,35:$Vh,37:$Vi,40:$Vj,42:$Vk,43:$Vc},o([9,10,14,15,19,36,37,38,39,41],[2,40]),o($Vl,[2,15]),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]),{6:37,7:38,8:39,9:$Vm,10:$Vn,43:$Vc},o($Vd,[2,7]),o($Vd,[2,9]),{13:42,17:8,20:$V1,21:$V2,22:$V3,23:$V4,24:$V5,25:$V6,26:$V7,27:$V8,28:$V9,29:$Va,30:$Vb},{7:36,16:43,37:$Vi,40:$Vj,42:$Vk,43:$Vc},{19:[1,44]},{7:36,16:45,37:$Vi,40:$Vj,42:$Vk,43:$Vc},o($Vo,[2,27],{36:$Vp,37:$Vq,38:$Vr,39:$Vs}),o($Vt,[2,28]),o($Vt,[2,29]),o($Vt,[2,30]),o($Vt,[2,31]),{7:36,16:50,37:$Vi,40:$Vj,42:$Vk,43:$Vc},{7:36,16:51,37:$Vi,40:$Vj,42:$Vk,43:$Vc},o($Vu,[2,38]),o($Vu,[2,39]),{1:[2,1]},{6:52,8:39,9:$Vm,10:$Vn},{9:[1,53],10:[1,54]},{1:[2,4]},o($Vv,[2,6]),{10:[2,12]},{10:[2,13],36:$Vp,37:$Vq,38:$Vr,39:$Vs},{7:36,16:28,18:55,31:27,32:$Ve,33:$Vf,34:$Vg,35:$Vh,37:$Vi,40:$Vj,42:$Vk,43:$Vc},o($Vo,[2,26],{36:$Vp,37:$Vq,38:$Vr,39:$Vs}),{7:36,16:56,37:$Vi,40:$Vj,42:$Vk,43:$Vc},{7:36,16:57,37:$Vi,40:$Vj,42:$Vk,43:$Vc},{7:36,16:58,37:$Vi,40:$Vj,42:$Vk,43:$Vc},{7:36,16:59,37:$Vi,40:$Vj,42:$Vk,43:$Vc},{36:$Vp,37:$Vq,38:$Vr,39:$Vs,41:[1,60]},o($Vu,[2,37]),{1:[2,2]},{1:[2,3]},o($Vv,[2,5]),{10:[2,14]},o($Vw,[2,32],{38:$Vr,39:$Vs}),o($Vw,[2,33],{38:$Vr,39:$Vs}),o($Vu,[2,34]),o($Vu,[2,35]),o($Vu,[2,36])], -defaultActions: {6:[2,11],37:[2,1],40:[2,4],42:[2,12],52:[2,2],53:[2,3],55:[2,14]}, +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]}, parseError: function parseError (str, hash) { if (hash.recoverable) { this.trace(str); @@ -662,76 +671,74 @@ options: {}, performAction: function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) { var YYSTATE=YY_START; switch($avoiding_name_collisions) { -case 0: console.log("EOF"); return "EOF"; +case 0: return "EOF"; break; -case 1: this.popState(); console.log("NEWLINE"); return "NEWLINE"; +case 1: this.popState(); return "NEWLINE"; break; case 2: /* ignore anything else inside a comment */ break; case 3: this.pushState('comment'); break; -case 4: console.log("NEWLINE"); return "NEWLINE"; +case 4: return "NEWLINE"; break; case 5: /* ignore whitespace */ break; -case 6: console.log("MOV"); return "MOV"; -break; -case 7: console.log("ADD"); return "ADD"; +case 6: return "MOV"; break; -case 8: console.log("SUB"); return "SUB"; +case 7: return "ADD"; break; -case 9: console.log("CMP"); return "CMP"; +case 8: return "SUB"; break; -case 10: console.log("SLT"); return "SLT"; +case 9: return "CMP"; break; -case 11: console.log("JMP"); return "JMP"; +case 10: return "SLT"; break; -case 12: console.log("JMZ"); return "JMZ"; +case 11: return "JMP"; break; -case 13: console.log("JMN"); return "JMN"; +case 12: return "JMZ"; break; -case 14: console.log("DJN"); return "DJN"; +case 13: return "JMN"; break; -case 15: console.log("SPL"); return "SPL"; +case 14: return "DJN"; break; -case 16: console.log("DAT"); return "DAT"; +case 15: return "SPL"; break; -case 17: console.log("EQU"); return "EQU"; +case 16: return "DAT"; break; -case 18: console.log("END"); return "END"; +case 17: return "EQU"; break; -case 19: console.log(":"); return ":"; +case 18: return "END"; break; -case 20: console.log(","); return ","; +case 19: return ","; break; -case 21: console.log("#"); return "#"; +case 20: return "#"; break; -case 22: console.log("@"); return "@"; +case 21: return "@"; break; -case 23: console.log("<"); return "<"; +case 22: return "<"; break; -case 24: console.log("$"); return "$"; +case 23: return "$"; break; -case 25: console.log("("); return "("; +case 24: return "("; break; -case 26: console.log(")"); return ")"; +case 25: return ")"; break; -case 27: console.log("+"); return "+"; +case 26: return "+"; break; -case 28: console.log("-"); return "-"; +case 27: return "-"; break; -case 29: console.log("*"); return "*"; +case 28: return "*"; break; -case 30: console.log("/"); return "/"; +case 29: return "/"; break; -case 31: console.log("NUMBER"); return "NUMBER"; +case 30: return "NUMBER"; break; -case 32: console.log("LABEL"); return "LABEL"; +case 31: return "LABEL"; break; } }, -rules: [/^(?:$)/,/^(?:\\n)/,/^(?:.)/,/^(?:;)/,/^(?:[\n])/,/^(?:\s)/,/^(?:MOV\b)/,/^(?:ADD\b)/,/^(?:SUB\b)/,/^(?:CMP\b)/,/^(?:SLT\b)/,/^(?:JMP\b)/,/^(?:JMZ\b)/,/^(?:JMN\b)/,/^(?:DJN\b)/,/^(?:SPL\b)/,/^(?:DAT\b)/,/^(?:EQU\b)/,/^(?:END\b)/,/^(?::)/,/^(?:,)/,/^(?:#)/,/^(?:@)/,/^(?:<)/,/^(?:\$)/,/^(?:\()/,/^(?:\))/,/^(?:\+)/,/^(?:-)/,/^(?:\*)/,/^(?:\/)/,/^(?:[0-9]+)/,/^(?:[A-Z][A-Z0-9_]+)/], -conditions: {"comment":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],"inclusive":true},"INITIAL":{"rules":[0,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],"inclusive":true}} +rules: [/^(?:$)/,/^(?:\\n)/,/^(?:.)/,/^(?:;)/,/^(?:[\n])/,/^(?:\s)/,/^(?:MOV\b)/,/^(?:ADD\b)/,/^(?:SUB\b)/,/^(?:CMP\b)/,/^(?:SLT\b)/,/^(?:JMP\b)/,/^(?:JMZ\b)/,/^(?:JMN\b)/,/^(?:DJN\b)/,/^(?:SPL\b)/,/^(?:DAT\b)/,/^(?:EQU\b)/,/^(?:END\b)/,/^(?:,)/,/^(?:#)/,/^(?:@)/,/^(?:<)/,/^(?:\$)/,/^(?:\()/,/^(?:\))/,/^(?:\+)/,/^(?:-)/,/^(?:\*)/,/^(?:\/)/,/^(?:[0-9]+)/,/^(?:[A-Z][A-Z0-9_]+)/], +conditions: {"comment":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],"inclusive":true},"INITIAL":{"rules":[0,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],"inclusive":true}} }); return lexer; })(); diff --git a/src/parser/parser.js b/src/parser/parser.js new file mode 100644 index 0000000..ef84be3 --- /dev/null +++ b/src/parser/parser.js @@ -0,0 +1,36 @@ +const process = require('node:process'); +const fs = require('node:fs'); +const parser = require('./grammar.js').parser; + +parser.yy = { + pc: 0, + + step: function() { this.pc += 1; }, + + equ: {}, + line: {}, + + setEqu: function(l, v) { + this.equ[l] = v; + }, + setLine: function(l) { + this.line[l] = this.pc; + }, + + getLabel: function(l) { + if (this.equ[l] !== undefined) { + return this.equ[l]; + } + + return this.line[l] - this.pc; + }, +}; + +//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()); + console.log(result); + console.log(parser.yy); + }); +//} |