From bbcd2143e76be5feb565af38620472a93fda5df6 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Tue, 2 Jul 2024 22:46:36 -0300 Subject: [PATCH] refact: no need for ReturnExpression --- src/interpreter.rs | 4 ---- src/parser.rs | 18 +++++++++++------- src/semantic.rs | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index 6e60881..20d980b 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -102,10 +102,6 @@ impl Interpreter { let value = self.evaluate_expr(expr)?; return Ok(value); } - Stmt::ReturnExpression(expr) => { - let value = self.evaluate_expr(expr)?; - return Ok(value); - } } Ok(Value::Void) } diff --git a/src/parser.rs b/src/parser.rs index b53a8a4..478276c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -73,7 +73,6 @@ pub enum Stmt { Function(String, Vec<(String, String)>, String, Vec, Span), If(Expr, Vec, Vec, Span), Match(Expr, Vec<(Expr, Vec)>, Span), - ReturnExpression(Expr), Expression(Expr), Struct(String, Vec<(String, String)>, Span), Enum(String, Vec<(String, String)>, Span), @@ -542,7 +541,7 @@ impl Parser { self.current_token().span, )); } - return Ok(Stmt::ReturnExpression(expr)); + return Ok(Stmt::Expression(expr)); } Err(Error::new_parse( @@ -593,6 +592,11 @@ impl Parser { let name = name.clone(); self.advance(); // Consume identifier + // println!( + // "lookahead: {:?}", + // self.lookahead_for_struct_initialization() + // ); + match self.current_token().kind { TokenKind::Dot => { self.advance(); // Consume '.' @@ -1048,7 +1052,7 @@ mod tests { ("y".to_string(), "int".to_string()), ], "int".to_string(), - vec![Stmt::ReturnExpression(Expr::BinaryOp( + vec![Stmt::Expression(Expr::BinaryOp( Box::new(Expr::Identifier("x".to_string(), Span::new(32, 33))), Operator::Plus, Box::new(Expr::Identifier("y".to_string(), Span::new(36, 37))), @@ -1085,7 +1089,7 @@ mod tests { ("age".to_string(), "int".to_string()), ], "Person".to_string(), - vec![Stmt::ReturnExpression(Expr::StructInit( + vec![Stmt::Expression(Expr::StructInit( "Person".to_string(), vec![ ( @@ -1148,8 +1152,8 @@ mod tests { Box::new(Expr::Identifier("y".to_string(), Span::new(21, 22))), Span::new(16, 20), ), - vec![Stmt::ReturnExpression(Expr::Int(1, Span::new(41, 42)))], - vec![Stmt::ReturnExpression(Expr::Int(0, Span::new(80, 81)))], + vec![Stmt::Expression(Expr::Int(1, Span::new(41, 42)))], + vec![Stmt::Expression(Expr::Int(0, Span::new(80, 81)))], Span::new(13, 95), ) ); @@ -1321,7 +1325,7 @@ mod tests { Ok(ast) => { assert_eq!( ast[0], - Stmt::ReturnExpression(Expr::EnumVariant { + Stmt::Expression(Expr::EnumVariant { name: "Name".to_string(), variant: "Existing".to_string(), value: Some(Box::new(Expr::Identifier( diff --git a/src/semantic.rs b/src/semantic.rs index 9a13ee9..0c19ca9 100644 --- a/src/semantic.rs +++ b/src/semantic.rs @@ -161,7 +161,7 @@ impl SemanticAnalyzer { } Ok(()) } - Stmt::ReturnExpression(expr) | Stmt::Expression(expr) => { + Stmt::Expression(expr) => { self.analyze_expr(expr)?; Ok(()) }