Skip to content

Commit

Permalink
♻️ update BlockStatement to be boxed
Browse files Browse the repository at this point in the history
  • Loading branch information
KPMGE committed Apr 25, 2024
1 parent b28a974 commit 0a1feeb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub enum Expression {
},
IfExpression {
condition: Box<AstNode>,
consequence: BlockStatement,
consequence: Box<BlockStatement>,
alternative: Option<BlockStatement>,
},
FunctionExpression {
parameters: Vec<Token>,
body: BlockStatement,
body: Box<BlockStatement>,
},
CallExpression {
function: Box<Expression>,
Expand Down
6 changes: 3 additions & 3 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Object {
Null,
Function {
parameters: Vec<Token>,
body: BlockStatement,
body: Box<BlockStatement>,
scope: HashMap<String, Object>,
},
}
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Evaluator {
parameters,
body,
mut scope,
} => self.eval_function_call(parameters, arguments, body, &mut scope),
} => self.eval_function_call(parameters, arguments, *body, &mut scope),
obj => panic!("Wrong object, expected Object::Function, got: {:?}", obj),
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Object {
Object::Null => "null".to_string(),
Object::Array(elems) => {
let elements_str = elems
.into_iter()
.iter()
.map(|e| e.inspect())
.collect::<Vec<String>>()
.join(", ");
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Parser {
}
self.advance_tokens();

let consequence = self.parse_block_statement()?;
let consequence = Box::new(self.parse_block_statement()?);

self.advance_tokens();

Expand Down Expand Up @@ -317,7 +317,7 @@ impl Parser {
}

let parameters = self.parse_function_parameters()?;
let body = self.parse_block_statement()?;
let body = Box::new(self.parse_block_statement()?);

Some(Expression::FunctionExpression { parameters, body })
}
Expand Down
12 changes: 6 additions & 6 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ fn given_an_if_expression_it_should_parse_correctly() {
left: Box::new(Expression::Identifier("x".to_string())),
right: Box::new(Expression::Identifier("y".to_string())),
})),
consequence: BlockStatement {
consequence: Box::new(BlockStatement {
statements: vec![AstNode::Expression(Expression::Identifier("x".to_string()))],
},
}),
alternative: None,
};

Expand Down Expand Up @@ -325,9 +325,9 @@ fn given_an_if_else_expression_it_should_parse_correctly() {
left: Box::new(Expression::Identifier("x".to_string())),
right: Box::new(Expression::Identifier("y".to_string())),
})),
consequence: BlockStatement {
consequence: Box::new(BlockStatement {
statements: vec![AstNode::Expression(Expression::Identifier("x".to_string()))],
},
}),
alternative: Some(BlockStatement {
statements: vec![AstNode::Expression(Expression::Identifier("y".to_string()))],
}),
Expand Down Expand Up @@ -359,13 +359,13 @@ fn given_a_function_expression_it_should_parse_correctly() {
Token::Identifier("a".to_string()),
Token::Identifier("b".to_string()),
],
body: BlockStatement {
body: Box::new(BlockStatement {
statements: vec![AstNode::Expression(Expression::Infix {
operator: Token::Plus,
left: Box::new(Expression::Identifier("a".to_string())),
right: Box::new(Expression::Identifier("b".to_string())),
})],
},
}),
};

let lexer = Lexer::new(code.to_string());
Expand Down

0 comments on commit 0a1feeb

Please sign in to comment.