Add .idea to gitignore and prep return value types Add middleend module

for AST return value tracking

The changes introduce support for tracking return values and labels
across AST nodes. The key changes include:

- Adding ReturnValue type to BaseValue - Adding return_type field to
LoxFunction - Adding label field to Block statements - Creating
middleend crawler module for AST traversal Add return value types to AST

This accurately captures both changes: adding `.idea/` to gitignore and
adding return value types to the AST structure. The changes include
adding return value fields to statement nodes and adjusting the
interpreter logic accordingly.
This commit is contained in:
Giulio Agostini
2025-10-10 10:18:02 +02:00
parent c1ecd7d1f7
commit 07cedc27bf
10 changed files with 331 additions and 64 deletions
+15 -10
View File
@@ -182,16 +182,17 @@ impl Interpreter {
fn interpret_stmt_inner(&mut self, stmt: Stmt) -> LoxResult<BaseValue> {
match stmt {
Stmt::Expression { expression } => self.evaluate(*expression),
Stmt::Print { expression } => {
Stmt::Expression { expression, .. } => self.evaluate(*expression),
Stmt::Print { expression, .. } => {
let value = self.evaluate(*expression)?;
println!("{}", value);
Ok(BaseValue::Nil)
}
Stmt::Block { statements } => self.evaluate_block(*statements),
Stmt::Stmt { expression } => self.evaluate(*expression.clone()),
Stmt::Return { expression } => self.evaluate(*expression),
Stmt::VarDeclaration { name, initializer } => {
Stmt::Block { statements, .. } => self.evaluate_block(*statements),
Stmt::Return { expression, .. } => self.evaluate(*expression),
Stmt::VarDeclaration {
name, initializer, ..
} => {
let value = match initializer {
Some(expr_node) => match &expr_node.node {
Expr::Literal { value } => {
@@ -208,7 +209,7 @@ impl Interpreter {
self.enviorment.declare(name.clone(), value)
}
Stmt::VarAssigment { name, value } => {
Stmt::VarAssigment { name, value, .. } => {
let result = self.evaluate(*value)?;
self.enviorment.set(name.clone(), result)
}
@@ -217,8 +218,11 @@ impl Interpreter {
then_branch,
elif_branch,
else_branch,
..
} => self.evaluate_if(condition, then_branch, elif_branch, else_branch),
Stmt::While { condition, body } => {
Stmt::While {
condition, body, ..
} => {
while self.evaluate(*condition.clone())?.is_truthy() {
self.evaluate(*body.clone())?;
}
@@ -229,6 +233,7 @@ impl Interpreter {
condition,
increment,
body,
..
} => {
self.enviorment.push_new_scope();
let source_slice = variable.source_slice.clone();
@@ -291,8 +296,8 @@ impl Interpreter {
self.enviorment.push_new_scope();
let (elements, final_expr) = match statements.split_last() {
Some((stmt, body)) => match &stmt.node {
Stmt::Expression { expression } => (body, Some(expression)),
Stmt::Return { expression } => (body, Some(expression)),
Stmt::Expression { expression, .. } => (body, Some(expression)),
Stmt::Return { expression, .. } => (body, Some(expression)),
_ => (statements.as_slice(), None),
},