Improve `Let` statements lowering in MIR
- Dominant language
- Rust
- Stars
- 96
- Forks
- 39
- PR merge metrics
- No merged PRs in 30d
Description
Currently, the lowering of Let statements is not the cleanest, as translate_statement returns a single node but the statements can create multiple nodes in their bodies.
We should refactor with the following logic:
```rust
/// Translates a statement into a vector of operations. We return a vector as let statements contain all the statements in its scope.
fn translate_statement(&mut self, stmt: &'a ast::Statement) -> Result>, CompileError> {
match stmt {
ast::Statement::Let(let_stmt) => self.translate_let(let_stmt),
ast::Statement::Expr(expr) => Ok(vec![self.translate_expr(expr)?],
[...]
}
}
/// Translates a let statement, binding its value to its name for the scope of its body, and returning all the inner statements of its body.
fn translate_let(&mut self, let_stmt: &'a ast::Let) -> Result>, CompileError> {
[...]
// Translating all the statements of the let's body, and adding them to our returned vectors, flattened
let ret_value = let_stmt.body.iter()
.map(|stmt| self.translate_statement(stmt))
.collect::, _>>()?
.into_iter()
.flatten()
.collect();
Ok(ret_value)
}
```
Then, when we translate a statement, we handle all the inner statements like we do now. So if we are translating a fucntion / evaluator, we can directly insert all the statements into its body
_Originally posted by @Leo-Besancon in https://github.com/0xMiden/air-script/pull/449#discussion_r2313254144_
Contributor guide
Assessment
This issue has not been assessed yet.