summaryrefslogtreecommitdiff
path: root/godel/grammar.peg
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-11-16 14:56:56 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-11-16 14:56:56 -0700
commitec2b924fdac0b609c2bda4e857113674965732af (patch)
tree1997edfae1227bbee0654a6ca05c2c2585e89c20 /godel/grammar.peg
parent30f9f2bc185b88669030f7b1a433d79c39c9f1bf (diff)
downloadsimponic.xyz-ec2b924fdac0b609c2bda4e857113674965732af.tar.gz
simponic.xyz-ec2b924fdac0b609c2bda4e857113674965732af.zip
godel init foo
Diffstat (limited to 'godel/grammar.peg')
-rw-r--r--godel/grammar.peg48
1 files changed, 48 insertions, 0 deletions
diff --git a/godel/grammar.peg b/godel/grammar.peg
new file mode 100644
index 0000000..1096b00
--- /dev/null
+++ b/godel/grammar.peg
@@ -0,0 +1,48 @@
+Program = instructions: Line* {
+ return { instructions: instructions };
+}
+
+Line = instruction: (LabeledInstruction / Instruction) _ {
+ return instruction;
+}
+
+LabeledInstruction = label:Label _ instruction:Instruction {
+ return { label, instruction };
+}
+
+Label = "[" _? label:LABEL_V _? "]" {
+ return label;
+}
+
+Instruction = conditional: Conditional { return { conditional }; }
+ / assignment: Assignment { return { assignment }; }
+
+Conditional = "IF" _ variable: VAR _ "!=" _ "0" _ "GOTO" _ label: Label {
+ return { variable, label };
+}
+
+Assignment = variable: VAR _ "<-" _ expr: Expression {
+ if (expr.left != variable) {
+ error("left hand variable must match right hand");
+ }
+ return { variable, expr };
+}
+
+Expression = left: VAR _ opr: OPERATION _ "1" {
+ return { left, opr };
+}
+
+VAR = symbol:"Y" { return symbol } / symbol:("X" / "Z") ind:Integer+ {
+ return symbol + ind;
+}
+
+OPERATION = "+" / "-"
+
+LABEL_V = symbol:[A-E] ind:Integer+ {
+ return symbol + ind;
+}
+
+Integer "integer"
+ = _ [0-9]+ { return parseInt(text(), 10); }
+
+_ "whitespace" = [ \t\n\r]* { }