Skip to content

Commit

Permalink
♻️ update ReturnStatement to hold a boxed expression
Browse files Browse the repository at this point in the history
  • Loading branch information
KPMGE committed Apr 25, 2024
1 parent 6d47397 commit 8129519
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct BlockStatement {

#[derive(Debug, Eq, Clone, PartialEq)]
pub enum Statement {
ReturnStatement(Expression),
ReturnStatement(Box<Expression>),
LetStatement { name: Box<Expression>, value: Box<Expression> },
}

Expand Down
2 changes: 1 addition & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down

0 comments on commit 8129519

Please sign in to comment.