From c9d3f7629cfdacededf2a92252e6c9de8a8b75ae Mon Sep 17 00:00:00 2001
From: David Li
Date: Sat, 4 Nov 2017 19:18:49 -0400
Subject: Parse unary operators
---
src/ast.rs | 7 +++++++
src/taiga.lalrpop | 18 +++++++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/src/ast.rs b/src/ast.rs
index 2814ef9..97ed91d 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -34,6 +34,12 @@ impl WithLocation {
#[derive(Debug)]
pub struct Program(pub Box>);
+#[derive(Debug)]
+pub enum UnaryOp {
+ Pos,
+ Neg,
+}
+
#[derive(Debug)]
pub enum BinOp {
Add,
@@ -45,6 +51,7 @@ pub enum BinOp {
#[derive(Debug)]
pub enum Expression {
+ UnaryOp(UnaryOp, Box>),
BinOp(BinOp, Box>, Box>),
Number(u64),
Nil,
diff --git a/src/taiga.lalrpop b/src/taiga.lalrpop
index 0182a04..6bf750d 100644
--- a/src/taiga.lalrpop
+++ b/src/taiga.lalrpop
@@ -18,15 +18,27 @@ Expression: Box> = {
};
ExpressionMul: Box> = {
- "*" => Box::new(e1.join_map(*e2, |v1, v2| {
+ "*" => 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| {
+ "/" => 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| {
+ "//" => Box::new(e1.join_map(*e2, |v1, v2| {
ast::Expression::BinOp(ast::BinOp::FloorDiv, Box::new(v1), Box::new(v2))
})),
+ => <>,
+};
+
+ExpressionSign: Box> = {
+ "+" => Box::new(WithLocation::new(
+ ast::Expression::UnaryOp(ast::UnaryOp::Pos, e),
+ l, r
+ )),
+ "-" => Box::new(WithLocation::new(
+ ast::Expression::UnaryOp(ast::UnaryOp::Neg, e),
+ l, r
+ )),
=> <>,
};
--
cgit v1.2.3