summaryrefslogtreecommitdiff
path: root/src/taiga.lalrpop
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2017-11-04 19:06:13 -0400
committerDavid Li <li.davidm96@gmail.com>2017-11-04 19:06:13 -0400
commitee7c269301f84bd5c7c942900aaa2d96d17d886f (patch)
tree6ecdae2104751db610526f9564c97177a5244db1 /src/taiga.lalrpop
parent35bf004cc923c1bd61a75fa0ff9b336644f0c8b0 (diff)
Parse binary operators with precedence
Diffstat (limited to 'src/taiga.lalrpop')
-rw-r--r--src/taiga.lalrpop25
1 files changed, 25 insertions, 0 deletions
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<ast::WithLocation<ast::Expression>> = {
+ <e1:Expression> "+" <e2:ExpressionMul> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Add, Box::new(v1), Box::new(v2))
+ })),
+ <e1:Expression> "-" <e2:ExpressionMul> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Sub, Box::new(v1), Box::new(v2))
+ })),
+ <ExpressionMul> => <>,
+};
+
+ExpressionMul: Box<ast::WithLocation<ast::Expression>> = {
+ <e1:ExpressionMul> "*" <e2:ExpressionBase> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Mul, Box::new(v1), Box::new(v2))
+ })),
+ <e1:ExpressionMul> "/" <e2:ExpressionBase> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Div, Box::new(v1), Box::new(v2))
+ })),
+ <e1:ExpressionMul> "//" <e2:ExpressionBase> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::FloorDiv, Box::new(v1), Box::new(v2))
+ })),
+ <ExpressionBase> => <>,
+};
+
+ExpressionBase: Box<ast::WithLocation<ast::Expression>> = {
Num => Box::new(<>.map(|v| ast::Expression::Number(v))),
+ Spanned<r"nil"> => Box::new(<>.map(|v| ast::Expression::Nil)),
+ "(" <Expression> ")" => <>,
};
Num: WithLocation<u64> = <e: Spanned<r"[0-9]+">> => e.map(|v| u64::from_str(v).unwrap());