From 72cd5407280c59f516467b0f60156252b5efbc52 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 3 May 2021 23:02:19 +0200 Subject: [PATCH] Tidy up --- src/parser/mod.rs | 5 ++++- src/parser/ops.rs | 16 ++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 7692d13..f6acf6d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -99,7 +99,7 @@ impl<'a> Parser<'a> { } _ => Err(Error { - kind: ErrorKind::SyntaxError("Unexpected identifier".to_owned()), + kind: ErrorKind::SyntaxError("Unexpected token".to_owned()), position: start..self.lexer.span().end, }), } @@ -153,6 +153,7 @@ impl<'a> Parser<'a> { /// `functio [iden] ([expr], [expr]) { ... } fn function_declaration(&mut self) -> ParseResult { let iden = self.require_iden()?; + self.require(Token::LeftParenthesis)?; let mut args = vec![]; loop { @@ -163,7 +164,9 @@ impl<'a> Parser<'a> { _ => return Err(self.unexpected_token(None)), } } + self.require(Token::LeftBrace)?; + // Parse function body let body = self.parse_body()?; diff --git a/src/parser/ops.rs b/src/parser/ops.rs index bfd0053..e64506f 100644 --- a/src/parser/ops.rs +++ b/src/parser/ops.rs @@ -20,6 +20,7 @@ macro_rules! gen_infix { impl<'a> Parser<'a> { pub(super) fn parse_ops(&mut self, token: Token) -> ParseResult { + // Statements match self.lexer.peek() { Some(Token::LeftParenthesis) => return self.fn_call(token), Some(Token::Assignment) => return self.parse_assignment(token), @@ -31,12 +32,15 @@ impl<'a> Parser<'a> { loop { let peek = self.lexer.peek().clone(); buf = match peek { + // Print statement Some(Token::Print) => { self.lexer.next(); self.require(Token::Semicolon)?; return Ok(Stmt::Print(buf).into()); } None => return Ok(buf.into()), + + // An expression _ => self.parse_operation(peek, buf)?, } } @@ -65,6 +69,8 @@ impl<'a> Parser<'a> { fn parse_assignment(&mut self, token: Token) -> ParseResult { self.lexer.next(); + + // Extract identifier let iden = if let Token::Identifier(i) = token { Iden(i) } else { @@ -76,6 +82,7 @@ impl<'a> Parser<'a> { let next = self.lexer.next(); let mut value = self.parse_expr(next)?; + loop { let peek = self.lexer.peek().clone(); value = match peek { @@ -89,6 +96,7 @@ impl<'a> Parser<'a> { Some(t) => self.parse_operation(Some(t), value)?, }; } + self.lexer.next(); Ok(Stmt::VarAssignment { iden, value }.into()) @@ -143,12 +151,8 @@ impl<'a> Parser<'a> { let next = self.lexer.next(); let mut buf = self.parse_expr(next)?; loop { - let next = self.lexer.peek().clone().ok_or(Error { - kind: ErrorKind::EndOfTokenStream, - position: self.lexer.span(), - })?; - - buf = match Some(next) { + let peek = self.lexer.peek().clone(); + buf = match peek { Some(Token::RightParenthesis) => { self.lexer.next(); return Ok(buf);