From ceaa275af86b4790478fc7470219589be3ea6226 Mon Sep 17 00:00:00 2001 From: KPMGE Date: Fri, 22 Mar 2024 13:30:18 -0300 Subject: [PATCH] :recycle: remove unneded fields from the ast nodes --- src/ast.rs | 9 +-------- src/evaluator.rs | 4 ++-- src/parser.rs | 21 +++------------------ tests/parser.rs | 11 ++--------- 4 files changed, 8 insertions(+), 37 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 3fff295..0fe7fa1 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -15,18 +15,15 @@ pub enum Expression { right: Box, }, IfExpression { - token: Token, // Token::If condition: Box, consequence: BlockStatement, // Statement::BlockStatement alternative: Option, // Statement::BlockStatement }, FunctionExpression { - token: Token, // Token::Fn parameters: Vec, // Vec body: BlockStatement, }, CallExpression { - token: Token, // Token::LeftParentesis function: Box, // Expression::FunctionExpression or Expression::Identifier arguments: Vec, }, @@ -40,15 +37,11 @@ pub struct BlockStatement { #[derive(Debug, Eq, Clone, PartialEq)] pub enum Statement { + ReturnStatement(Expression), LetStatement { - token: Token, // Token::Let name: Expression, // Expression::Identifer value: Expression, }, - ReturnStatement { - token: Token, // Token::Return - value: Expression, - }, } #[derive(Debug, PartialEq, PartialOrd)] diff --git a/src/evaluator.rs b/src/evaluator.rs index 69cf7dc..8c75a5c 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -132,11 +132,11 @@ impl Evaluator { fn eval_statement(&mut self, statement: Statement) -> Object { match statement { - Statement::ReturnStatement { value, .. } => { + Statement::ReturnStatement(value) => { let result_object = self.eval(AstNode::Expression(value)); Object::Return(Box::new(result_object)) } - Statement::LetStatement { name, value, .. } => { + Statement::LetStatement { name, value } => { let let_name = match name { Expression::Identifier(identifier_name) => identifier_name, _ => panic!(), diff --git a/src/parser.rs b/src/parser.rs index ae58b35..c5964d9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -89,11 +89,7 @@ impl Parser { return None; } - Some(AstNode::Statement(Statement::LetStatement { - token: Token::Let, - name, - value, - })) + Some(AstNode::Statement(Statement::LetStatement { name, value })) } fn parse_expression_statement(&mut self) -> Option { @@ -119,10 +115,7 @@ impl Parser { let expression = self.parse_expression(Precedence::Lowest)?; - Some(AstNode::Statement(Statement::ReturnStatement { - token: Token::Return, - value: expression, - })) + Some(AstNode::Statement(Statement::ReturnStatement(expression))) } fn parse_expression(&mut self, precedence: Precedence) -> Option { @@ -257,7 +250,6 @@ impl Parser { }; Some(Expression::IfExpression { - token: Token::If, condition: Box::new(AstNode::Expression(condition)), consequence, alternative, @@ -275,11 +267,7 @@ impl Parser { let parameters = self.parse_function_parameters()?; let body = self.parse_block_statement()?; - Some(Expression::FunctionExpression { - token: Token::Function, - parameters, - body, - }) + Some(Expression::FunctionExpression { parameters, body }) } fn parse_function_parameters(&mut self) -> Option> { @@ -345,13 +333,10 @@ impl Parser { }) } fn parse_call_expression(&mut self, function: Expression) -> Option { - let token = self.current_token.clone(); - self.advance_tokens(); let arguments = self.parse_call_expression_arguments()?; Some(Expression::CallExpression { - token, function: Box::new(function), arguments, }) diff --git a/tests/parser.rs b/tests/parser.rs index 1cd6d66..2f5df9b 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -31,7 +31,6 @@ 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 { - token: Token::Let, name: Expression::Identifier(expected_identifier.to_string()), value: Expression::Int(expected_int.to_string().parse().unwrap()), }); @@ -68,10 +67,8 @@ fn given_return_statements_with_single_integers_shold_parse_correctly() { let expected_expression = Expression::Int(expected_int.to_string().parse().unwrap()); - let expected_statement = AstNode::Statement(Statement::ReturnStatement { - token: Token::Return, - value: expected_expression, - }); + let expected_statement = + AstNode::Statement(Statement::ReturnStatement(expected_expression)); assert_eq!(*statement, expected_statement); }) @@ -288,7 +285,6 @@ fn given_an_if_expression_it_should_parse_correctly() { let mut parser = Parser::new(lexer); let expected_expression = Expression::IfExpression { - token: Token::If, condition: Box::new(AstNode::Expression(Expression::Infix { operator: Token::LessThan, left: Box::new(Expression::Identifier("x".to_string())), @@ -325,7 +321,6 @@ fn given_an_if_else_expression_it_should_parse_correctly() { let mut parser = Parser::new(lexer); let expected_expression = Expression::IfExpression { - token: Token::If, condition: Box::new(AstNode::Expression(Expression::Infix { operator: Token::LessThan, left: Box::new(Expression::Identifier("x".to_string())), @@ -363,7 +358,6 @@ fn given_an_if_else_expression_it_should_parse_correctly() { fn given_a_function_expression_it_should_parse_correctly() { let code = "fn(a, b) { a + b };"; let expected_expression = Expression::FunctionExpression { - token: Token::Function, parameters: vec![ Token::Identifier("a".to_string()), Token::Identifier("b".to_string()), @@ -404,7 +398,6 @@ fn given_a_call_expression_it_should_parse_correctly() { let code = "add(2 * 3, 1 + 4);"; let expected_expression = Expression::CallExpression { - token: Token::LeftParentesis, function: Box::new(Expression::Identifier("add".to_string())), arguments: vec![ Expression::Infix {