From 39a8bf6a54047a91458117e9c60fd3103634ef2b Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 27 Apr 2021 11:49:07 +0200 Subject: [PATCH] Redone original parser = implemented original features --- able-script-test/parse_test.able | 6 ++++-- src/error.rs | 9 +-------- src/parser/mod.rs | 13 ++++++++----- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/able-script-test/parse_test.able b/able-script-test/parse_test.able index bfab6c5..efe7b0e 100644 --- a/able-script-test/parse_test.able +++ b/able-script-test/parse_test.able @@ -1,2 +1,4 @@ -var a = 3; -var b = 4; \ No newline at end of file +functio test () { + var a = 3; + var b = 4; +} \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index a5188eb..0d725ac 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,11 +11,4 @@ pub enum ErrorKind { SyntaxError(String), EndOfTokenStream, InvalidIdentifier, -} - -impl Error { - pub fn panic(&self, span: &str) { - println!("{:?} occured at {:?}", self.kind, self.position); - println!(" {}", &span); - } -} +} \ No newline at end of file diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 826b25c..f4970c5 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -74,7 +74,7 @@ impl<'a> Parser<'a> { Some(Token::Semicolon) => None, Some(Token::Assignment) => { let value = self.lexer.next(); - let value = self.parse_expr(value)?; // TODO: Shouldn't be limited to boolean (pattern match?) + let value = self.parse_expr(value)?; self.require(Token::Semicolon)?; Some(Box::new(value)) } @@ -95,12 +95,15 @@ impl<'a> Parser<'a> { fn function_declaration(&mut self) -> Result { let iden = self.require_iden()?; self.require(Token::LeftParenthesis)?; - // TODO: Arguments self.require(Token::RightParenthesis)?; + self.require(Token::LeftBrace)?; - let expr = self.lexer.next(); - let expr = self.parse_expr(expr); - let body = vec![expr?]; + // Parse function body + let mut body = Vec::new(); + while let Some(token) = self.lexer.next() { + if token == Token::RightBrace { break } + body.push(self.parse_expr(Some(token))?); + } Ok(Expr::FunctionDeclaration { iden, body }) }