don't repeat l/rhs in eval
This commit is contained in:
parent
8c507cb1da
commit
a887bf4b3e
1 changed files with 9 additions and 6 deletions
15
src/eval.rs
15
src/eval.rs
|
|
@ -20,13 +20,16 @@ impl Evaluator {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Expr::Binary { op, left, right } => {
|
Expr::Binary { op, left, right } => {
|
||||||
|
let lhs = self.eval_expr(*left);
|
||||||
|
let rhs = self.eval_expr(*right);
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
BinaryOp::Add => self.eval_expr(*left) + self.eval_expr(*right),
|
BinaryOp::Add => lhs + rhs,
|
||||||
BinaryOp::Sub => self.eval_expr(*left) - self.eval_expr(*right),
|
BinaryOp::Sub => lhs - rhs,
|
||||||
BinaryOp::Mul => self.eval_expr(*left) * self.eval_expr(*right),
|
BinaryOp::Mul => lhs * rhs,
|
||||||
BinaryOp::Div => self.eval_expr(*left) / self.eval_expr(*right),
|
BinaryOp::Div => lhs / rhs,
|
||||||
BinaryOp::Mod => self.eval_expr(*left) % self.eval_expr(*right),
|
BinaryOp::Mod => lhs % rhs,
|
||||||
BinaryOp::Exp => self.eval_expr(*left).pow(self.eval_expr(*right) as u32) // probably shouldn't cast like that
|
BinaryOp::Exp => lhs.pow(rhs as u32) // probably shouldn't cast like that
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
expr => panic!("can't eval expression: {expr:?}")
|
expr => panic!("can't eval expression: {expr:?}")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue