From 685b579fddcca3be5d53595248dcbf0c97cb457b Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 13 Sep 2017 03:46:16 -0700 Subject: [PATCH] paren exprs --- src/schala_lang/parsing.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index ddabb2d..e030849 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -472,7 +472,17 @@ impl Parser { } fn primary(&mut self) -> ParseResult { - self.literal() + match self.peek() { + LParen => self.paren_expr(), + _ => self.literal(), + } + } + + fn paren_expr(&mut self) -> ParseResult { + expect!(self, LParen, "Expected '('"); + let expr = self.expression()?; + expect!(self, RParen, "Expected ')'"); + Ok(expr) } fn identifier(&mut self) -> ParseResult> { @@ -580,7 +590,7 @@ mod parse_tests { } #[test] - fn test_parsing() { + fn parsing_number_literals_and_binexps() { parse_test!("8.1", AST(vec![Expression(FloatLiteral(8.1))])); parse_test!("0b010", AST(vec![Expression(IntLiteral(2))])); parse_test!("3; 4; 4.3", AST( @@ -599,6 +609,19 @@ mod parse_tests { parse_test!("1 && 2", AST(vec![Expression(binexp!(op!("&&"), IntLiteral(1), IntLiteral(2)))])); + parse_test!("1 + 2 * 3 + 4", AST(vec![Expression( + binexp!(op!("+"), + binexp!(op!("+"), IntLiteral(1), + binexp!(op!("*"), IntLiteral(2), IntLiteral(3)) + ), + IntLiteral(4) + ) + )])); + parse_test!("(1 + 2) * 3", AST(vec! + [ + Expression(binexp!(op!("*"), binexp!(op!("+"), IntLiteral(1), IntLiteral(2)), IntLiteral(3))) + ])); } + }