From 31da25a66e128fe2b784c51e3b69dcaa109ecf38 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 13 Jan 2016 23:29:18 -0800 Subject: [PATCH] Expression parsing work --- src/parser.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 69c2e2e..80d27f8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -192,10 +192,19 @@ impl Parser { fn expression(&mut self) -> ParseResult { use tokenizer::Token::*; - let expr: Expression = match self.next() { - Some(Identifier(s)) => Expression::StringLiteral(s), - _ => panic!("bad expression parse"), + let mut lhr: Expression = try!(self.primary_expression()); + Ok(lhr) + } + + fn primary_expression(&mut self) -> ParseResult { + use tokenizer::Token::*; + let expr = match self.peek() { + Some(NumLiteral(n)) => { self.next(); Expression::Number(n) }, + Some(StrLiteral(s)) => { self.next(); Expression::StringLiteral(s) }, + Some(Identifier(var)) => { self.next(); Expression::Variable(var) }, + _ => unimplemented!() }; + Ok(expr) } } @@ -204,4 +213,3 @@ pub fn parse(tokens: &[Token], _parsed_tree: &[ASTNode]) -> ParseResult { let mut parser = Parser::initialize(tokens); parser.program() } -