From f2a515a940ea5e66c9fbc9669e6293ae32b7041a Mon Sep 17 00:00:00 2001 From: sanine-a Date: Mon, 22 May 2023 14:26:10 -0500 Subject: update label style and add implicit second operand --- src/parser/grammar.jison | 87 ++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 40 deletions(-) (limited to 'src/parser/grammar.jison') 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 @@ %% -<> { console.log("EOF"); return "EOF"; } -"\n" { this.popState(); console.log("NEWLINE"); return "NEWLINE"; } +<> { return "EOF"; } +"\n" { this.popState(); return "NEWLINE"; } . { /* 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 }, + }; + } ; -- cgit v1.2.1