From 4253c1c20013ab16564aa3ec34585dd1a358d182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 30 Jan 2021 22:34:19 +0100 Subject: add in tuples --- src/compiler.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index 6fc8e78..0ddae1a 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -315,6 +315,8 @@ impl Compiler { | Token::NotEqual => self.binary(block), + Token::LeftBracket => self.index(block), + _ => { return false; }, } return true; @@ -384,6 +386,15 @@ impl Compiler { expect!(self, Token::RightParen, "Expected ')' around expression."); } + fn index(&mut self, block: &mut Block) { + expect!(self, Token::LeftBracket, "Expected ']' around index."); + + self.expression(block); + block.add(Op::Index, self.line()); + + expect!(self, Token::RightBracket, "Expected '[' around index."); + } + fn unary(&mut self, block: &mut Block) { let op = match self.eat() { Token::Minus => Op::Neg, @@ -797,6 +808,22 @@ impl Compiler { let f = Type::Function(params, Box::new(return_type)); Ok(f) } + Token::LeftParen => { + self.eat(); + let mut element = Vec::new(); + loop { + element.push(self.parse_type()?); + if self.peek() == Token::RightParen { + self.eat(); + return Ok(Type::Tuple(element)); + } + if !expect!(self, + Token::Comma, + "Expect comma efter element in tuple.") { + return Err(()); + } + } + } Token::Identifier(x) => { self.eat(); match x.as_str() { -- cgit v1.2.1