summary refs log tree commit diff
path: root/18/src/expression.lalrpop
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2020-12-17 21:56:07 -0800
committerIrene Knapp <ireneista@gmail.com>2020-12-17 21:56:07 -0800
commit781e65e5a4444800982da71d97cb6b05d7dca17e (patch)
treec9919f20c0153409b0d344f33e5a4791177387da /18/src/expression.lalrpop
parent9acefe571def801d63282620cb31833a321bc551 (diff)
18
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,
+}
+