From eaf86ea9082b10e19b123559d8668e6db879b6da Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 19 Jan 2017 19:44:43 -0800 Subject: [PATCH] Add support for +, - in num literals --- src/parser.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 5264cdf..9ca57c4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,8 +15,9 @@ use std::convert::From; // exprlist := Expression (Comma Expression)* | e // // expression := primary_expression (op primary_expression)* -// primary_expression := Number | String | identifier_expr | paren_expr | conditional_expr | +// primary_expression := number_expr | String | identifier_expr | paren_expr | conditional_expr | // while_expr | lambda_expr +// number_expr := (PLUS | MINUS ) number_expr | Number // identifier_expr := call_expression | Variable // call_expr := Identifier LParen exprlist RParen // while_expr := WHILE primary_expression LCurlyBrace (expression delimiter)* RCurlyBrace @@ -391,10 +392,8 @@ impl Parser { self.next(); Expression::Null } - Some(NumLiteral(n)) => { - self.next(); - Expression::Number(n) - } + Some(NumLiteral(_)) => try!(self.number_expression()), + Some(Operator(OpTok(ref a))) if **a == "+" || **a == "-" => try!(self.number_expression()), Some(StrLiteral(s)) => { self.next(); Expression::StringLiteral(s) @@ -413,6 +412,33 @@ impl Parser { }) } + fn number_expression(&mut self) -> ParseResult { + let mut multiplier = 1; + loop { + match self.peek() { + Some(NumLiteral(n)) => { + self.next(); + return Ok(Expression::Number(n * multiplier as f64)); + } + Some(Operator(OpTok(ref a))) if **a == "+" => { + self.next(); + } + Some(Operator(OpTok(ref a))) if **a == "-" => { + multiplier *= -1; + self.next(); + } + Some(e) => { + return ParseError::result_from_str( + &format!("Expected +, - or number, got {:?}", e)); + } + None => { + return ParseError::result_from_str( + &format!("Expected +, - or number, got EoI")); + } + } + } + } + fn lambda_expr(&mut self) -> ParseResult { use self::Expression::*; expect!(self, Keyword(Kw::Fn));