From a780ef4ae2d21ed8e69d46821664aafef7d92ac3 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 16 Jan 2017 15:29:14 -0800 Subject: [PATCH] Split Expression::Atom into Identifier and Literal cases --- src/parser/mod.rs | 4 ++-- src/parser/nodes.rs | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index cee73fb..f3fd51a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -45,8 +45,8 @@ impl Parser { fn parse_expression(&mut self) -> Result { if let Some(next) = self.lexer.next() { match next.token { - Token::Boolean(value) => Ok(Expression::Atom(Box::new(value))), - Token::Character(value) => Ok(Expression::Atom(Box::new(value))), + Token::Boolean(value) => Ok(Expression::Literal(Box::new(value))), + Token::Character(value) => Ok(Expression::Literal(Box::new(value))), _ => Err(Error { lex: next, desc: "Invalid token".to_string() }) } } diff --git a/src/parser/nodes.rs b/src/parser/nodes.rs index 35f2e73..33b52ce 100644 --- a/src/parser/nodes.rs +++ b/src/parser/nodes.rs @@ -21,7 +21,8 @@ impl Program { pub enum Expression { EOF, - Atom(Box), + Identifier(String), + Literal(Box), List { left: Token, expr: Vec>, right: Token }, } @@ -29,7 +30,8 @@ impl fmt::Debug for Expression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Expression::EOF => write!(f, "EOF"), - Expression::Atom(ref value) => write!(f, "Atom{{ {:?} }}", value), + Expression::Identifier(ref id) => write!(f, "Id{{{:?}}}", id), + Expression::Literal(ref value) => write!(f, "Literal{{{:?}}}", value), Expression::List{ left: ref lt, ref expr, right: ref rt } => { write!(f, "{:?} {:?} {:?}", lt, expr, rt) }, @@ -41,7 +43,8 @@ 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::Identifier(ref id) => self.eq_identifier(other, id), + Expression::Literal(ref value) => self.eq_literal(other, value.deref()), Expression::List { ref left, ref expr, ref right } => { self.eq_list(other, left, expr, right) }, @@ -57,9 +60,16 @@ impl Expression { } } - fn eq_atom(&self, other: &Expression, value: &types::Value) -> bool { + fn eq_identifier(&self, other: &Expression, id: &str) -> bool { match *other { - Expression::Atom(ref ovalue) => value == ovalue.deref(), + Expression::Identifier(ref oid) => id == oid, + _ => false, + } + } + + fn eq_literal(&self, other: &Expression, value: &types::Value) -> bool { + match *other { + Expression::Literal(ref ovalue) => value == ovalue.deref(), _ => false, } }