Skip to content

Commit

Permalink
refact: no need for ReturnExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
fcoury committed Jul 3, 2024
1 parent 5c0f6a0 commit bbcd214
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 0 additions & 4 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 11 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ pub enum Stmt {
Function(String, Vec<(String, String)>, String, Vec<Stmt>, Span),
If(Expr, Vec<Stmt>, Vec<Stmt>, Span),
Match(Expr, Vec<(Expr, Vec<Stmt>)>, Span),
ReturnExpression(Expr),
Expression(Expr),
Struct(String, Vec<(String, String)>, Span),
Enum(String, Vec<(String, String)>, Span),
Expand Down Expand Up @@ -542,7 +541,7 @@ impl Parser {
self.current_token().span,
));
}
return Ok(Stmt::ReturnExpression(expr));
return Ok(Stmt::Expression(expr));
}

Err(Error::new_parse(
Expand Down Expand Up @@ -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 '.'
Expand Down Expand Up @@ -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))),
Expand Down Expand Up @@ -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![
(
Expand Down Expand Up @@ -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),
)
);
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl SemanticAnalyzer {
}
Ok(())
}
Stmt::ReturnExpression(expr) | Stmt::Expression(expr) => {
Stmt::Expression(expr) => {
self.analyze_expr(expr)?;
Ok(())
}
Expand Down

0 comments on commit bbcd214

Please sign in to comment.