From 11569fdec123e9c7aeaf38d8a122842c4c758fdf Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 14 Jan 2017 19:41:51 -0800 Subject: parse out exponentiation --- modelica-parser-lalrpop/helper_macros.txt | 24 ++++++++++++++++++++++++ modelica-parser-lalrpop/src/ast.rs | 3 +++ modelica-parser-lalrpop/src/parser.lalrpop | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 modelica-parser-lalrpop/helper_macros.txt diff --git a/modelica-parser-lalrpop/helper_macros.txt b/modelica-parser-lalrpop/helper_macros.txt new file mode 100644 index 0000000..957586e --- /dev/null +++ b/modelica-parser-lalrpop/helper_macros.txt @@ -0,0 +1,24 @@ + +// === Helper Macros == + +Comma: Vec = { + ",")*> => match e { + None=> v, + Some(e) => { + let mut v = v; + v.push(e); + v + } + } +}; + +Dotted: Vec = { + ".")*> => match e { + None=> v, + Some(e) => { + let mut v = v; + v.push(e); + v + } + } +}; diff --git a/modelica-parser-lalrpop/src/ast.rs b/modelica-parser-lalrpop/src/ast.rs index f1e7d93..317944b 100644 --- a/modelica-parser-lalrpop/src/ast.rs +++ b/modelica-parser-lalrpop/src/ast.rs @@ -103,6 +103,7 @@ pub enum MathUnaryFunc { pub enum BinOperator { Multiply, Divide, + Exponentiate, Add, Subtract, } @@ -313,6 +314,8 @@ impl Debug for BinOperator { match *self { Multiply => write!(fmt, "*"), Divide => write!(fmt, "/"), + // TODO: this doesn't really work here + Exponentiate => write!(fmt, "^"), Add => write!(fmt, "+"), Subtract => write!(fmt, "-"), } diff --git a/modelica-parser-lalrpop/src/parser.lalrpop b/modelica-parser-lalrpop/src/parser.lalrpop index cb59a82..2d2469c 100644 --- a/modelica-parser-lalrpop/src/parser.lalrpop +++ b/modelica-parser-lalrpop/src/parser.lalrpop @@ -161,7 +161,8 @@ factor: Expr = { "/" => Expr::BinExpr(BinOperator::Divide, Box::new(lhs), Box::new(rhs)), "^" => - Expr::BinExpr(BinOperator::Divide, Box::new(lhs), Box::new(rhs)), + Expr::BinExpr(BinOperator::Exponentiate, Box::new(lhs), Box::new(rhs)), + // TODO: this could be a "negate" unary op? "-" => Expr::BinExpr(BinOperator::Multiply, Box::new(Expr::Integer(-1)), Box::new(t)), term, -- cgit v1.2.3