Skip to content

Commit

Permalink
♻️ update LetStatement to hold boxed expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
KPMGE committed Apr 25, 2024
1 parent b3d7ee6 commit 6d47397
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct BlockStatement {
#[derive(Debug, Eq, Clone, PartialEq)]
pub enum Statement {
ReturnStatement(Expression),
LetStatement { name: Expression, value: Expression },
LetStatement { name: Box<Expression>, value: Box<Expression> },
}

#[derive(Debug, Eq, Clone, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ impl Evaluator {
Object::Return(Box::new(result_object))
}
Statement::LetStatement { name, value } => {
let let_name = match name {
let let_name = match *name {
Expression::Identifier(identifier_name) => identifier_name,
_ => panic!(),
};

let result_object = self.eval(AstNode::Expression(value));
let result_object = self.eval(AstNode::Expression(*value));
self.context.insert(let_name.clone(), result_object.clone());
result_object
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Parser {
return None;
}

let name = self.parse_identifier()?;
let name = Box::new(self.parse_identifier()?);

self.advance_tokens();

Expand All @@ -91,7 +91,7 @@ impl Parser {

self.advance_tokens();

let value = self.parse_expression(Precedence::Lowest)?;
let value = Box::new(self.parse_expression(Precedence::Lowest)?);

self.advance_tokens();

Expand Down
4 changes: 2 additions & 2 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ fn given_let_statements_with_single_integers_shold_parse_correctly() {
let expected_int = expected_ints.get(idx).unwrap();

let expected_statement = AstNode::Statement(Statement::LetStatement {
name: Expression::Identifier(expected_identifier.to_string()),
value: Expression::Int(expected_int.to_string().parse().unwrap()),
name: Box::new(Expression::Identifier(expected_identifier.to_string())),
value: Box::new(Expression::Int(expected_int.to_string().parse().unwrap())),
});

assert_eq!(*statement, expected_statement);
Expand Down

0 comments on commit 6d47397

Please sign in to comment.