From 5dd1cd79ff3565b212868f7ba3a33fe5437f3019 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 15 Sep 2017 03:49:47 -0700 Subject: [PATCH] Parsing call expressions --- src/schala_lang/parsing.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index f0a1b24..7047d74 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -548,8 +548,11 @@ impl Parser { let identifier = self.identifier()?; match self.peek() { LParen => { - let call = self.call_expr()?; - unimplemented!() + let call_params = self.call_expr()?; + Ok(Expression::Call { + name: identifier, + params: call_params, + }) }, LSquareBracket => { let indexers = self.index_expr()?; @@ -562,8 +565,21 @@ impl Parser { } } - fn call_expr(&mut self) -> ParseResult { - unimplemented!() + fn call_expr(&mut self) -> ParseResult> { + let mut exprs = Vec::new(); + expect!(self, LParen, "Expected '('"); + loop { + if let RParen = self.peek() { + break; + } + exprs.push(self.expression()?); + match self.peek() { + Comma => { self.next(); }, + _ => break, + } + } + expect!(self, RParen, "Expected ')'"); + Ok(exprs) } fn index_expr(&mut self) -> ParseResult> { @@ -745,6 +761,11 @@ mod parse_tests { #[test] fn parsing_functions() { parse_test!("fn oi()", AST(vec![Declaration(FuncDecl { name: rc!(oi), params: vec![] })])); + parse_test!("oi()", AST(vec![Expression(Call { name: rc!(oi), params: vec![] })])); + parse_test!("oi(a, 2 + 2)", AST(vec![Expression(Call + { name: rc!(oi), + params: vec![var!("a"), binexp!(op!("+"), IntLiteral(2), IntLiteral(2))] + })])); } #[test]