From 003640b43862e66256dd8e21711246d909431d98 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 May 2023 13:08:56 -0500 Subject: update grammar --- src/parser/grammar.jison.bak | 113 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/parser/grammar.jison.bak (limited to 'src/parser/grammar.jison.bak') diff --git a/src/parser/grammar.jison.bak b/src/parser/grammar.jison.bak new file mode 100644 index 0000000..6ac55df --- /dev/null +++ b/src/parser/grammar.jison.bak @@ -0,0 +1,113 @@ +%lex + +%x comment + +%% + +";" { this.pushState('comment'); return "COMMENT"; } +"\n" { this.popState(); return "NEWLINE"; } +. { /* ignore anything else inside a comment */ } + +"\n" { return "NEWLINE"; } +\s+ { /* ignore whitespace */ } + +"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 + +%% + + +program + : lines { return $lines; } + ; + + + +lines + : lines line { $lines.push($line); $$ = $lines; } + | line { $$ = [ $line ]; } + ; + +line + : op NEWLINE { $$ = $op; } + | op EOF { $$ = $op; } + ; + + +op + : opcode address address + { $$ = { opcode: $opcode, a: $address1, b: $address2 }; } + ; + + +opcode + : MOV { $$ = $1; } + | ADD { $$ = $1; } + | SUB { $$ = $1; } + | CMP { $$ = $1; } + | SLT { $$ = $1; } + | 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 }; } + ; + + +address_mode + : "#" { $$ = 'immediate'; } + | "@" { $$ = 'indirect'; } + | "<" { $$ = 'predecrement'; } + | "$" { $$ = 'direct'; } + ; + +e + : NUMBER { $$ = Math.floor(Number(yytext)); } + | LABEL { + $$ = yy.getLabel(yytext); + if ($$ === null) { + YYABORT; + } + } + | e '+' e { $$ = Math.floor($e1 + $e2); } + | e '-' e { $$ = Math.floor($e1 - $e2); } + | e '*' e { $$ = Math.floor($e1 * $e2); } + | e '/' e { $$ = Math.floor($e1 / $e2); } + | '(' e ')' { $$ = Math.floor($e); } + ; -- cgit v1.2.1