0xMiden / 0xMiden/air-script

Improve `Let` statements lowering in MIR

Aperta
#454 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
96
Fork
39
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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_

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Esamina il codice di MIR lowering nel compilatore, probabilmente in un modulo come `mir/lowering.rs` o `translate.rs`. Trova la funzione `translate_statement` e la gestione corrente delle istruzioni `Let`. L'obiettivo è rifattorizzare in modo che `translate_statement` restituisca un `Vec>` e che `translate_let` appiattisca le istruzioni del corpo. Controlla come le funzioni/valutatori inseriscono le istruzioni nei loro corpi dopo questo cambiamento. Esegui i test esistenti per assicurarti che la rifattorizzazione non rompa nulla.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
rust
Ambito
compilers
Tipo di issue
Refactoring
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.