diff options
Diffstat (limited to 'src/taiga.lalrpop')
-rw-r--r-- | src/taiga.lalrpop | 25 |
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()); |