Skip to content

Commit

Permalink
♻️ remove unneded fields from the ast nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
KPMGE committed Mar 22, 2024
1 parent 951f64f commit ceaa275
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 37 deletions.
9 changes: 1 addition & 8 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ pub enum Expression {
right: Box<Expression>,
},
IfExpression {
token: Token, // Token::If
condition: Box<AstNode>,
consequence: BlockStatement, // Statement::BlockStatement
alternative: Option<BlockStatement>, // Statement::BlockStatement
},
FunctionExpression {
token: Token, // Token::Fn
parameters: Vec<Token>, // Vec<Token::Identifier>
body: BlockStatement,
},
CallExpression {
token: Token, // Token::LeftParentesis
function: Box<Expression>, // Expression::FunctionExpression or Expression::Identifier
arguments: Vec<Expression>,
},
Expand All @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand Down
21 changes: 3 additions & 18 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AstNode> {
Expand All @@ -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<Expression> {
Expand Down Expand Up @@ -257,7 +250,6 @@ impl Parser {
};

Some(Expression::IfExpression {
token: Token::If,
condition: Box::new(AstNode::Expression(condition)),
consequence,
alternative,
Expand All @@ -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<Vec<Token>> {
Expand Down Expand Up @@ -345,13 +333,10 @@ impl Parser {
})
}
fn parse_call_expression(&mut self, function: Expression) -> Option<Expression> {
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,
})
Expand Down
11 changes: 2 additions & 9 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
});
Expand Down Expand Up @@ -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);
})
Expand Down Expand Up @@ -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())),
Expand Down Expand Up @@ -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())),
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ceaa275

Please sign in to comment.