From ee7c269301f84bd5c7c942900aaa2d96d17d886f Mon Sep 17 00:00:00 2001
From: David Li
Date: Sat, 4 Nov 2017 19:06:13 -0400
Subject: Parse binary operators with precedence
---
src/taiga.lalrpop | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
(limited to 'src/taiga.lalrpop')
diff --git a/src/taiga.lalrpop b/src/taiga.lalrpop
index 08c3cd3..0182a04 100644
--- a/src/taiga.lalrpop
+++ b/src/taiga.lalrpop
@@ -8,7 +8,32 @@ pub Program: ast::Program = {
};
Expression: Box> = {
+ "+" => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Add, Box::new(v1), Box::new(v2))
+ })),
+ "-" => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Sub, Box::new(v1), Box::new(v2))
+ })),
+ => <>,
+};
+
+ExpressionMul: Box> = {
+ "*" => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Mul, Box::new(v1), Box::new(v2))
+ })),
+ "/" => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Div, Box::new(v1), Box::new(v2))
+ })),
+ "//" => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::FloorDiv, Box::new(v1), Box::new(v2))
+ })),
+ => <>,
+};
+
+ExpressionBase: Box> = {
Num => Box::new(<>.map(|v| ast::Expression::Number(v))),
+ Spanned => Box::new(<>.map(|v| ast::Expression::Nil)),
+ "(" ")" => <>,
};
Num: WithLocation = > => e.map(|v| u64::from_str(v).unwrap());
--
cgit v1.2.3