don't repeat l/rhs in eval

This commit is contained in:
Raptorox 2026-05-21 17:40:53 +02:00
parent 8c507cb1da
commit a887bf4b3e
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8

View file

@ -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:?}")