From 2e3306b67b1d21a66d9910aabdba12e1e29ce6c9 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 14 Jan 2017 22:40:09 -0800 Subject: [PATCH] Compare Expressions --- src/parser/nodes.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/parser/nodes.rs b/src/parser/nodes.rs index e7dedcf..7d734ea 100644 --- a/src/parser/nodes.rs +++ b/src/parser/nodes.rs @@ -35,3 +35,43 @@ impl fmt::Debug for Expression { } } } + +impl PartialEq for Expression { + fn eq(&self, other: &Expression) -> bool { + match *self { + Expression::EOF => self.eq_eof(other), + Expression::Atom(ref value) => self.eq_atom(other, value.deref()), + Expression::List { ref left, ref expr, ref right } => { + self.eq_list(other, left, expr, right) + }, + } + } +} + +impl Expression { + fn eq_eof(&self, other: &Expression) -> bool { + match *other { + Expression::EOF => true, + _ => false, + } + } + + fn eq_atom(&self, other: &Expression, value: &types::Value) -> bool { + match *other { + Expression::Atom(ref ovalue) => value == ovalue.deref(), + _ => false, + } + } + + fn eq_list(&self, other: &Expression, left: &Token, expr: &Vec>, right: &Token) -> bool { + match *other { + Expression::List { left: ref olt, expr: ref oexpr, right: ref ort } => { + let left_eq = left == olt; + let right_eq = right == ort; + let expr_eq = expr == oexpr; + left_eq && expr_eq && right_eq + }, + _ => false, + } + } +}