From 8129519935ce06804497c57a04c902aaddb69973 Mon Sep 17 00:00:00 2001 From: KPMGE Date: Wed, 24 Apr 2024 22:40:23 -0300 Subject: [PATCH] :recycle: update ReturnStatement to hold a boxed expression --- src/ast.rs | 2 +- src/evaluator.rs | 2 +- src/parser.rs | 2 +- tests/parser.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 3ef4eb6..875d60e 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -38,7 +38,7 @@ pub struct BlockStatement { #[derive(Debug, Eq, Clone, PartialEq)] pub enum Statement { - ReturnStatement(Expression), + ReturnStatement(Box), LetStatement { name: Box, value: Box }, } diff --git a/src/evaluator.rs b/src/evaluator.rs index 8d03695..b7a952a 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -153,7 +153,7 @@ impl Evaluator { fn eval_statement(&mut self, statement: Statement) -> Object { match statement { Statement::ReturnStatement(value) => { - let result_object = self.eval(AstNode::Expression(value)); + let result_object = self.eval(AstNode::Expression(*value)); Object::Return(Box::new(result_object)) } Statement::LetStatement { name, value } => { diff --git a/src/parser.rs b/src/parser.rs index d95c528..e98008b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -124,7 +124,7 @@ impl Parser { return None; } - let expression = self.parse_expression(Precedence::Lowest)?; + let expression = Box::new(self.parse_expression(Precedence::Lowest)?); Some(AstNode::Statement(Statement::ReturnStatement(expression))) } diff --git a/tests/parser.rs b/tests/parser.rs index ed4625b..95f971c 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -68,7 +68,7 @@ fn given_return_statements_with_single_integers_shold_parse_correctly() { Expression::Int(expected_int.to_string().parse().unwrap()); let expected_statement = - AstNode::Statement(Statement::ReturnStatement(expected_expression)); + AstNode::Statement(Statement::ReturnStatement(Box::new(expected_expression))); assert_eq!(*statement, expected_statement); })