summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2017-11-04 19:33:45 -0400
committerDavid Li <li.davidm96@gmail.com>2017-11-04 19:33:45 -0400
commit9891204b026c7660676f456128c94b01ae7f39cf (patch)
tree7f7aaddcabb8c653145f309aeb77a3a392c576d0
parent6cbe4d98095020451f5b427fecbc3f374812dec8 (diff)
Parse comparison operators
-rw-r--r--src/ast.rs6
-rw-r--r--src/taiga.lalrpop30
2 files changed, 34 insertions, 2 deletions
diff --git a/src/ast.rs b/src/ast.rs
index 97ed91d..ab9493e 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -47,6 +47,12 @@ pub enum BinOp {
Mul,
Div,
FloorDiv,
+ Eq,
+ Neq,
+ Gt,
+ Lt,
+ Ge,
+ Le,
}
#[derive(Debug)]
diff --git a/src/taiga.lalrpop b/src/taiga.lalrpop
index 6bf750d..6aedf42 100644
--- a/src/taiga.lalrpop
+++ b/src/taiga.lalrpop
@@ -8,10 +8,36 @@ pub Program: ast::Program = {
};
Expression: Box<ast::WithLocation<ast::Expression>> = {
- <e1:Expression> "+" <e2:ExpressionMul> => Box::new(e1.join_map(*e2, |v1, v2| {
+ <e1:Expression> "=" <e2:ExpressionCmp> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Eq, Box::new(v1), Box::new(v2))
+ })),
+ <e1:Expression> "!=" <e2:ExpressionCmp> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Neq, Box::new(v1), Box::new(v2))
+ })),
+ <ExpressionCmp> => <>,
+};
+
+ExpressionCmp: Box<ast::WithLocation<ast::Expression>> = {
+ <e1:ExpressionCmp> ">" <e2:ExpressionAdd> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Gt, Box::new(v1), Box::new(v2))
+ })),
+ <e1:ExpressionCmp> "<" <e2:ExpressionAdd> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Lt, Box::new(v1), Box::new(v2))
+ })),
+ <e1:ExpressionCmp> ">=" <e2:ExpressionAdd> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Ge, Box::new(v1), Box::new(v2))
+ })),
+ <e1:ExpressionCmp> "<=" <e2:ExpressionAdd> => Box::new(e1.join_map(*e2, |v1, v2| {
+ ast::Expression::BinOp(ast::BinOp::Le, Box::new(v1), Box::new(v2))
+ })),
+ <ExpressionAdd> => <>,
+};
+
+ExpressionAdd: Box<ast::WithLocation<ast::Expression>> = {
+ <e1:ExpressionAdd> "+" <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| {
+ <e1:ExpressionAdd> "-" <e2:ExpressionMul> => Box::new(e1.join_map(*e2, |v1, v2| {
ast::Expression::BinOp(ast::BinOp::Sub, Box::new(v1), Box::new(v2))
})),
<ExpressionMul> => <>,