From 1858d26638b77afc4edc0a33df65416c52ae70a6 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 2 Jan 2017 22:14:01 -0800 Subject: [PATCH] Add comparison operators + make operator evaluation more concise --- src/eval.rs | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 638f298..911c65a 100644 --- a/src/eval.rs +++ b/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, } }