diff --git a/src/eval.rs b/src/eval.rs index 3d486e8..feedf85 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -20,13 +20,16 @@ impl Evaluator { } }, Expr::Binary { op, left, right } => { + let lhs = self.eval_expr(*left); + let rhs = self.eval_expr(*right); + match op { - 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), - BinaryOp::Exp => self.eval_expr(*left).pow(self.eval_expr(*right) as u32) // probably shouldn't cast like that + 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 } }, expr => panic!("can't eval expression: {expr:?}")