summary refs log tree commit diff
path: root/18/src/expression.lalrpop
diff options
context:
space:
mode:
Diffstat (limited to '18/src/expression.lalrpop')
-rw-r--r--18/src/expression.lalrpop54
1 files changed, 54 insertions, 0 deletions
diff --git a/18/src/expression.lalrpop b/18/src/expression.lalrpop
new file mode 100644
index 0000000..222a7e6
--- /dev/null
+++ b/18/src/expression.lalrpop
@@ -0,0 +1,54 @@
+grammar;
+
+pub Expression: i64 = {
+  <Term>,
+  <left:Expression> PLUS <right:Term> => {
+    left + right
+  },
+  <left:Expression> STAR <right:Term> => {
+    left * right
+  },
+};
+
+Term: i64 = {
+  NUMBER => {
+    <>.parse::<i64>().unwrap()
+  },
+  LEFT_PARENTHESIS <Expression> RIGHT_PARENTHESIS,
+};
+
+pub ExpressionPro: i64 = {
+  <Expression2Pro>,
+  <left:ExpressionPro> STAR <right:Expression2Pro> => {
+    left * right
+  },
+};
+
+Expression2Pro: i64 = {
+  <TermPro>,
+  <left:Expression2Pro> PLUS <right:TermPro> => {
+    left + right
+  },
+};
+
+TermPro: i64 = {
+  NUMBER => {
+    <>.parse::<i64>().unwrap()
+  },
+  LEFT_PARENTHESIS <ExpressionPro> RIGHT_PARENTHESIS,
+};
+
+match {
+  r"\p{Zs}+" => { },
+
+  r"[0-9]+" => NUMBER,
+
+  "+" => PLUS,
+
+  "*" => STAR,
+
+  "(" => LEFT_PARENTHESIS,
+
+  ")" => RIGHT_PARENTHESIS,
+}
+