From b3d7ee65065df5d89be020b9fbadb8a74791e2d7 Mon Sep 17 00:00:00 2001 From: KPMGE Date: Wed, 24 Apr 2024 22:29:38 -0300 Subject: [PATCH] :recycle: update ast representation --- src/ast.rs | 2 +- src/evaluator.rs | 4 ++-- src/parser.rs | 4 ++-- tests/parser.rs | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 2ec0e28..9beac82 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -17,7 +17,7 @@ pub enum Expression { right: Box, }, IfExpression { - condition: Box, + condition: Box, consequence: Box, alternative: Option, }, diff --git a/src/evaluator.rs b/src/evaluator.rs index 47a18c3..9e0c289 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -68,7 +68,7 @@ impl Evaluator { alternative, .. } => { - let condition = self.eval(*condition); + let condition = self.eval(AstNode::Expression(*condition)); if condition.is_truthy() { return self.eval_block_statement(consequence.statements); @@ -261,7 +261,7 @@ impl Object { Object::Null => "null".to_string(), Object::Array(elems) => { let elements_str = elems - .iter() + .into_iter() .map(|e| e.inspect()) .collect::>() .join(", "); diff --git a/src/parser.rs b/src/parser.rs index c121092..a74fbe4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -280,7 +280,7 @@ impl Parser { } self.advance_tokens(); - let condition = self.parse_expression(Precedence::Lowest)?; + let condition = Box::new(self.parse_expression(Precedence::Lowest)?); if !self.expect_next_token(Token::RightParentesis) { self.report_expected_token_error(Token::LeftParentesis, self.next_token.clone()); @@ -301,7 +301,7 @@ impl Parser { }; Some(Expression::IfExpression { - condition: Box::new(AstNode::Expression(condition)), + condition, consequence, alternative, }) diff --git a/tests/parser.rs b/tests/parser.rs index 6418656..3d9a75a 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -285,11 +285,11 @@ fn given_an_if_expression_it_should_parse_correctly() { let mut parser = Parser::new(lexer); let expected_expression = Expression::IfExpression { - condition: Box::new(AstNode::Expression(Expression::Infix { + condition: Box::new(Expression::Infix { operator: Token::LessThan, left: Box::new(Expression::Identifier("x".to_string())), right: Box::new(Expression::Identifier("y".to_string())), - })), + }), consequence: Box::new(BlockStatement { statements: vec![AstNode::Expression(Expression::Identifier("x".to_string()))], }), @@ -320,11 +320,11 @@ fn given_an_if_else_expression_it_should_parse_correctly() { let mut parser = Parser::new(lexer); let expected_expression = Expression::IfExpression { - condition: Box::new(AstNode::Expression(Expression::Infix { + condition: Box::new(Expression::Infix { operator: Token::LessThan, left: Box::new(Expression::Identifier("x".to_string())), right: Box::new(Expression::Identifier("y".to_string())), - })), + }), consequence: Box::new(BlockStatement { statements: vec![AstNode::Expression(Expression::Identifier("x".to_string()))], }),