diff --git a/src/eval.rs b/src/eval.rs index feedf85..df5b4f3 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -20,16 +20,12 @@ impl Evaluator { } }, Expr::Binary { op, left, right } => { - let lhs = self.eval_expr(*left); - let rhs = self.eval_expr(*right); - match op { - BinaryOp::Add => lhs + rhs, - BinaryOp::Sub => lhs - rhs, - BinaryOp::Mul => lhs * rhs, - BinaryOp::Div => lhs / rhs, - BinaryOp::Mod => lhs % rhs, - BinaryOp::Exp => lhs.pow(rhs as u32) // probably shouldn't cast like that + BinaryOp::Add => self.eval_expr(*left) + self.eval_expr(*right), + BinaryOp::Sub => self.eval_expr(*left) - self.eval_expr(*right), + BinaryOp::Mul => self.eval_expr(*left) * self.eval_expr(*right), + BinaryOp::Div => self.eval_expr(*left) / self.eval_expr(*right), + BinaryOp::Mod => self.eval_expr(*left) % self.eval_expr(*right) } }, expr => panic!("can't eval expression: {expr:?}") diff --git a/src/expr.rs b/src/expr.rs index 06da22a..7a32386 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -4,8 +4,7 @@ pub enum BinaryOp { Sub, Mul, Div, - Mod, - Exp + Mod } #[derive(Debug, Clone)] @@ -15,15 +14,14 @@ pub enum UnaryOp { pub fn prefix_bp(op: &UnaryOp) -> u8 { match op { - UnaryOp::Neg => 7 + UnaryOp::Neg => 5 } } pub fn infix_bp(op: &BinaryOp) -> (u8, u8) { match op { BinaryOp::Add | BinaryOp::Sub => (1, 2), - BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => (3, 4), - BinaryOp::Exp => (5, 6) + BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => (3, 4) } } diff --git a/src/lexer.rs b/src/lexer.rs index 32d806a..a4045c3 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -59,7 +59,6 @@ impl Lexer { Some(b'*') => {self.advance(); Some(Token::Asterisk)}, Some(b'/') => {self.advance(); Some(Token::Slash)}, Some(b'%') => {self.advance(); Some(Token::Percent)}, - Some(b'^') => {self.advance(); Some(Token::Caret)}, Some(b'(') => {self.advance(); Some(Token::LParen)}, Some(b')') => {self.advance(); Some(Token::RParen)}, diff --git a/src/parser.rs b/src/parser.rs index 3afc6e0..eb8937a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -69,7 +69,6 @@ impl Parser { Some(Token::Asterisk) => BinaryOp::Mul, Some(Token::Slash) => BinaryOp::Div, Some(Token::Percent) => BinaryOp::Mod, - Some(Token::Caret) => BinaryOp::Exp, _ => break }; diff --git a/src/token.rs b/src/token.rs index c193fbe..e5759e8 100644 --- a/src/token.rs +++ b/src/token.rs @@ -9,7 +9,6 @@ pub enum Token { Asterisk, Slash, Percent, - Caret, // Parentheses LParen,