diff options
Diffstat (limited to 'godel/grammar.peg')
-rw-r--r-- | godel/grammar.peg | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/godel/grammar.peg b/godel/grammar.peg index 1096b00..73ba08e 100644 --- a/godel/grammar.peg +++ b/godel/grammar.peg @@ -1,8 +1,8 @@ -Program = instructions: Line* { - return { instructions: instructions }; +Program = lines: (Line / (_/[\n]))* { + return { instructions: lines.filter((line) => typeof line !== "string" || line.trim() != "") }; } -Line = instruction: (LabeledInstruction / Instruction) _ { +Line = _? instruction: (LabeledInstruction / Instruction) _? [\n]? { return instruction; } @@ -16,9 +16,14 @@ Label = "[" _? label:LABEL_V _? "]" { Instruction = conditional: Conditional { return { conditional }; } / assignment: Assignment { return { assignment }; } + / goto: Goto { return { goto }; } -Conditional = "IF" _ variable: VAR _ "!=" _ "0" _ "GOTO" _ label: Label { - return { variable, label }; +Goto = GOTO _ label: LABEL_V { + return { label }; +} + +Conditional = "IF" _ variable: VAR _? "!=" _? "0" _ goto: Goto { + return { variable, goto }; } Assignment = variable: VAR _ "<-" _ expr: Expression { @@ -30,19 +35,25 @@ Assignment = variable: VAR _ "<-" _ expr: Expression { Expression = left: VAR _ opr: OPERATION _ "1" { return { left, opr }; +} / left: VAR { + return { left }; } VAR = symbol:"Y" { return symbol } / symbol:("X" / "Z") ind:Integer+ { return symbol + ind; } +GOTO = "GOTO" + OPERATION = "+" / "-" -LABEL_V = symbol:[A-E] ind:Integer+ { +LABEL_V = symbol:END_LABEL { return symbol } / symbol:[A-E] ind:Integer+ { return symbol + ind; } +END_LABEL = "E" + Integer "integer" - = _ [0-9]+ { return parseInt(text(), 10); } + = [0-9]+ { return parseInt(text(), 10); } -_ "whitespace" = [ \t\n\r]* { } +_ "whitespace" = [ \t]+ { } |