Add comparison operators

+  make operator evaluation more concise
This commit is contained in:
greg 2017-01-02 22:14:01 -08:00
parent 84fbe73cf6
commit 1858d26638

View File

@ -257,40 +257,17 @@ impl Evaluator {
fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression { fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression {
use parser::Expression::*; use parser::Expression::*;
match &op[..] { match (&op[..], left, right) {
"+" => { ("+", Number(l), Number(r)) => Number(l + r),
match (left, right) { ("+", StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)),
(Number(l), Number(r)) => Number(l + r), ("-", Number(l), Number(r)) => Number(l - r),
(StringLiteral(s1), StringLiteral(s2)) => { ("*", Number(l), Number(r)) => Number(l * r),
StringLiteral(format!("{}{}", s1, s2)) ("/", Number(l), Number(r)) if r != 0.0 => Number(l / r),
} ("%", Number(l), Number(r)) => Number(l % r),
_ => 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 },
"-" => { (">=", Number(l), Number(r)) => if l >= r { Number(1.0) } else { 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,
}
}
_ => Null, _ => Null,
} }
} }