Add == operator

This commit is contained in:
greg 2017-01-02 23:34:59 -08:00
parent 27d4c2ccbd
commit edf342e65a

View File

@ -255,6 +255,8 @@ 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::*;
let truthy = Number(1.0);
let falsy = Null;
match (&op[..], left, right) { match (&op[..], left, right) {
("+", Number(l), Number(r)) => Number(l + r), ("+", Number(l), Number(r)) => Number(l + r),
("+", StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)), ("+", StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)),
@ -262,11 +264,15 @@ impl Evaluator {
("*", 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)) if r != 0.0 => Number(l / r),
("%", Number(l), Number(r)) => 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 { truthy } else { falsy },
("<=", Number(l), Number(r)) => if l <= r { Number(1.0) } else { Null }, ("<=", Number(l), Number(r)) => if l <= r { truthy } else { falsy },
(">", Number(l), Number(r)) => if l > r { Number(1.0) } else { Null }, (">", Number(l), Number(r)) => if l > r { truthy } else { falsy },
(">=", Number(l), Number(r)) => if l >= r { Number(1.0) } else { Null }, (">=", Number(l), Number(r)) => if l >= r { truthy } else { falsy },
_ => Null, ("==", Number(l), Number(r)) => if l == r { truthy } else { falsy },
("==", Null, Null) => truthy,
("==", StringLiteral(s1), StringLiteral(s2)) => if s1 == s2 { truthy } else { falsy },
("==", _, _) => falsy,
_ => falsy,
} }
} }