Add comparison operators
+ make operator evaluation more concise
This commit is contained in:
parent
84fbe73cf6
commit
1858d26638
45
src/eval.rs
45
src/eval.rs
@ -257,40 +257,17 @@ impl Evaluator {
|
||||
|
||||
fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression {
|
||||
use parser::Expression::*;
|
||||
match &op[..] {
|
||||
"+" => {
|
||||
match (left, right) {
|
||||
(Number(l), Number(r)) => Number(l + r),
|
||||
(StringLiteral(s1), StringLiteral(s2)) => {
|
||||
StringLiteral(format!("{}{}", s1, s2))
|
||||
}
|
||||
_ => Null,
|
||||
}
|
||||
}
|
||||
"-" => {
|
||||
match (left, right) {
|
||||
(Number(l), Number(r)) => Number(l - r),
|
||||
_ => Null,
|
||||
}
|
||||
}
|
||||
"*" => {
|
||||
match (left, right) {
|
||||
(Number(l), Number(r)) => Number(l * r),
|
||||
_ => Null,
|
||||
}
|
||||
}
|
||||
"/" => {
|
||||
match (left, right) {
|
||||
(Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
||||
_ => Null,
|
||||
}
|
||||
}
|
||||
"%" => {
|
||||
match (left, right) {
|
||||
(Number(l), Number(r)) => Number(l % r),
|
||||
_ => Null,
|
||||
}
|
||||
}
|
||||
match (&op[..], left, right) {
|
||||
("+", Number(l), Number(r)) => Number(l + r),
|
||||
("+", StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)),
|
||||
("-", Number(l), Number(r)) => Number(l - r),
|
||||
("*", Number(l), Number(r)) => Number(l * r),
|
||||
("/", Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
||||
("%", Number(l), Number(r)) => Number(l % r),
|
||||
("<", Number(l), Number(r)) => if l < r { Number(1.0) } else { Null },
|
||||
("<=", Number(l), Number(r)) => if l <= r { Number(1.0) } else { Null },
|
||||
(">", Number(l), Number(r)) => if l > r { Number(1.0) } else { Null },
|
||||
(">=", Number(l), Number(r)) => if l >= r { Number(1.0) } else { Null },
|
||||
_ => Null,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user