From c3b655bad99c1c4110d2af18b5b5845b4b2a65df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 6 Feb 2021 12:27:34 +0100 Subject: use fancy macro in parse branches --- src/compiler.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index bd23cd8..914f720 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -371,16 +371,10 @@ impl Compiler { } fn grouping_or_tuple(&mut self, block: &mut Block) { - let block_length = block.ops.len(); - let token_length = self.curr; - if self.try_tuple(block).is_err() { - block.ops.truncate(block_length); - self.curr = token_length; - self.grouping(block); - } + parse_branch!(self, block, [self.tuple(block), self.grouping(block)]); } - fn try_tuple(&mut self, block: &mut Block) -> Result<(), ()> { + fn tuple(&mut self, block: &mut Block) { expect!(self, Token::LeftParen, "Expected '(' at start of tuple"); let mut num_args = 0; @@ -398,18 +392,21 @@ impl Compiler { match self.peek() { Token::Comma => { self.eat(); }, Token::RightParen => {}, - _ => { return Err(()); }, + _ => { + error!(self, "Expected ',' or ')' in tuple"); + return; + }, } } } } if num_args == 1 { - return Err(()); + error!(self, "A tuple must contain more than 1 element."); + return; } expect!(self, Token::RightParen, "Expected ')' after tuple."); add_op(self, block, Op::Tuple(num_args)); - Ok(()) } fn grouping(&mut self, block: &mut Block) { -- cgit v1.2.1