Add support for +, - in num literals
This commit is contained in:
parent
eb6354e55a
commit
eaf86ea908
@ -15,8 +15,9 @@ use std::convert::From;
|
|||||||
// exprlist := Expression (Comma Expression)* | e
|
// exprlist := Expression (Comma Expression)* | e
|
||||||
//
|
//
|
||||||
// expression := primary_expression (op primary_expression)*
|
// 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
|
// while_expr | lambda_expr
|
||||||
|
// number_expr := (PLUS | MINUS ) number_expr | Number
|
||||||
// identifier_expr := call_expression | Variable
|
// identifier_expr := call_expression | Variable
|
||||||
// call_expr := Identifier LParen exprlist RParen
|
// call_expr := Identifier LParen exprlist RParen
|
||||||
// while_expr := WHILE primary_expression LCurlyBrace (expression delimiter)* RCurlyBrace
|
// while_expr := WHILE primary_expression LCurlyBrace (expression delimiter)* RCurlyBrace
|
||||||
@ -391,10 +392,8 @@ impl Parser {
|
|||||||
self.next();
|
self.next();
|
||||||
Expression::Null
|
Expression::Null
|
||||||
}
|
}
|
||||||
Some(NumLiteral(n)) => {
|
Some(NumLiteral(_)) => try!(self.number_expression()),
|
||||||
self.next();
|
Some(Operator(OpTok(ref a))) if **a == "+" || **a == "-" => try!(self.number_expression()),
|
||||||
Expression::Number(n)
|
|
||||||
}
|
|
||||||
Some(StrLiteral(s)) => {
|
Some(StrLiteral(s)) => {
|
||||||
self.next();
|
self.next();
|
||||||
Expression::StringLiteral(s)
|
Expression::StringLiteral(s)
|
||||||
@ -413,6 +412,33 @@ impl Parser {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn number_expression(&mut self) -> ParseResult<Expression> {
|
||||||
|
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<Expression> {
|
fn lambda_expr(&mut self) -> ParseResult<Expression> {
|
||||||
use self::Expression::*;
|
use self::Expression::*;
|
||||||
expect!(self, Keyword(Kw::Fn));
|
expect!(self, Keyword(Kw::Fn));
|
||||||
|
Loading…
Reference in New Issue
Block a user