diff options
| author | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-02-09 00:30:30 +0100 |
|---|---|---|
| committer | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-02-09 17:47:34 +0100 |
| commit | aeab306921dfe9af4b444a77bd46d1700e43dd88 (patch) | |
| tree | 88cd04a7ea49342fe76b586007cafd807921abc1 | |
| parent | c574e4f707b1a2e638407bb60bf51355371874d3 (diff) | |
| download | sylt-aeab306921dfe9af4b444a77bd46d1700e43dd88.tar.gz | |
fancy call syntax
| -rw-r--r-- | src/compiler.rs | 75 | ||||
| -rw-r--r-- | src/lib.rs | 8 |
2 files changed, 65 insertions, 18 deletions
diff --git a/src/compiler.rs b/src/compiler.rs index 1d41c32..58e5f23 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -579,29 +579,68 @@ impl Compiler { } fn call(&mut self, block: &mut Block) { - expect!(self, Token::LeftParen, "Expected '(' at start of function call."); - let mut arity = 0; - loop { - match self.peek() { - Token::EOF => { - error!(self, "Unexpected EOF in function call."); - break; - } - Token::RightParen => { - self.eat(); - break; + match self.peek() { + Token::LeftParen => { + self.eat(); + loop { + match self.peek() { + Token::EOF => { + error!(self, "Unexpected EOF in function call."); + break; + } + Token::RightParen => { + self.eat(); + break; + } + _ => { + self.expression(block); + arity += 1; + if !matches!(self.peek(), Token::RightParen) { + expect!(self, Token::Comma, "Expected ',' after argument."); + } + } + } + if self.panic { + break; + } } - _ => { - self.expression(block); - arity += 1; - if !matches!(self.peek(), Token::RightParen) { - expect!(self, Token::Comma, "Expected ',' after argument."); + }, + + Token::Not => { + // I suspect this will be wierd with the boolean NOT + // operator... + self.eat(); + loop { + match self.peek() { + Token::EOF => { + error!(self, "Unexpected EOF in function call."); + break; + } + Token::Newline => { + break; + } + _ => { + if !parse_branch!(self, block, self.expression(block)) { + break; + } + arity += 1; + if matches!(self.peek(), Token::Comma) { + self.eat(); + } + } } + if self.panic { + break; + } + } + if !self.panic { + println!("LINE {} -- ", self.line()); } } - if self.panic { - break; + + _ => { + error!(self, "Not a valid function call, expected a '!' or a '('."); } } @@ -1046,6 +1046,14 @@ a.a <=> 0" ); test_multiple!( + fancy_call, + not: "f := fn {}\n f!\n", + one_arg: "f := fn a:int { a <=> 1 }\n f! 1\n", + two_arg: "f := fn a:int, b:int { b <=> 3 }\n f! 1, 1 + 2\n", + three_arg: "f := fn a:int, b:int, c:int { c <=> 13 }\n f! 1, 1 + 2, 1 + 4 * 3\n", + ); + + test_multiple!( newline_regression, simple: "a := 1 // blargh \na += 1 // blargh \n a <=> 2 // HARGH", expressions: "1 + 1 // blargh \n 2 // blargh \n // HARGH \n", |
