From d0a71de566b03f1539a93f4414b45159503b2cb6 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Fri, 22 Nov 2024 22:32:50 -0700 Subject: [PATCH] return expressions working --- src/parser/mod.rs | 18 ++++++++++++++++-- src/parser/tree_node.rs | 28 ++++++++-------------------- tests/file.stlg | 19 ++++++------------- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9906fe4..f3e5445 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,7 +1,7 @@ mod tree_node; use crate::tokenizer::{ - token::{Keyword, Symbol, Token, TokenType}, + token::{self, Keyword, Symbol, Token, TokenType}, Tokenizer, TokenizerBuffer, TokenizerError, }; use std::{ @@ -518,11 +518,25 @@ impl Parser { }); } - while !self_matches_peek!(self, TokenType::Symbol(Symbol::RBrace)) { + while !self_matches_peek!( + self, + TokenType::Symbol(Symbol::RBrace) | TokenType::Keyword(Keyword::Return) + ) { let expression = self.parse()?.ok_or(ParseError::UnexpectedEOF)?; expressions.push(expression); } + // print the current token for debugging + let current_token = token_from_option!(self.get_next()?); + + if token_matches!(current_token, TokenType::Keyword(Keyword::Return)) { + self.assign_next()?; + let expression = self.expression()?.ok_or(ParseError::UnexpectedEOF)?; + let return_expr = Expression::ReturnExpression(Box::new(expression)); + expressions.push(return_expr); + self.assign_next()?; + } + self.assign_next()?; Ok(BlockExpression(expressions)) diff --git a/src/parser/tree_node.rs b/src/parser/tree_node.rs index 45ebfda..a2c44bb 100644 --- a/src/parser/tree_node.rs +++ b/src/parser/tree_node.rs @@ -138,32 +138,20 @@ impl std::fmt::Display for InvocationExpression { } } -#[derive(Debug, PartialEq, Eq)] -pub struct PropertyAccessorExpression { - pub object: Box, - pub property: String, -} - -impl std::fmt::Display for PropertyAccessorExpression { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}.{}", self.object, self.property) - } -} - #[derive(Debug, PartialEq, Eq)] pub enum Expression { - Literal(Literal), - Variable(String), - Negation(Box), - BinaryExpression(BinaryExpression), - LogicalExpression(LogicalExpression), AssignmentExpression(AssignmentExpression), + BinaryExpression(BinaryExpression), + BlockExpression(BlockExpression), DeclarationExpression(String, Box), FunctionExpression(FunctionExpression), - BlockExpression(BlockExpression), InvocationExpression(InvocationExpression), + Literal(Literal), + LogicalExpression(LogicalExpression), + Negation(Box), PriorityExpression(Box), - PropertyAccessorExpression(PropertyAccessorExpression) + ReturnExpression(Box), + Variable(String), } impl std::fmt::Display for Expression { @@ -180,7 +168,7 @@ impl std::fmt::Display for Expression { Expression::InvocationExpression(e) => write!(f, "{}", e), Expression::Variable(id) => write!(f, "{}", id), Expression::PriorityExpression(e) => write!(f, "({})", e), - Expression::PropertyAccessorExpression(e) => write!(f, "{}", e), + Expression::ReturnExpression(e) => write!(f, "(return {})", e), } } } diff --git a/tests/file.stlg b/tests/file.stlg index 208005d..f5ad149 100644 --- a/tests/file.stlg +++ b/tests/file.stlg @@ -1,14 +1,7 @@ -fn myPowerItem(arg) { - -} +fn doStuff() { + let i = { + return 234.5; + }; -fn doThings(x, y, z) { - let item = myPowerItem((12.45 + 5 * 123) ** 2); - - let item2 = 5 + 2 - 5 + 123.24323 / 234 ** 21 - (15 / 2); -} - - -fn main() { - doThings(1, 2, 3); -} + return i; +} \ No newline at end of file