MIR(builder_modularized/control_flow): switch loop lowering to loop_api::build_simple_loop via LoopBuilderApi facade; keeps semantics and decouples from legacy loop builder.

This commit is contained in:
Moe Charm
2025-08-25 20:20:46 +09:00
parent 48499f593f
commit f2a8e31842

View File

@ -95,37 +95,20 @@ impl MirBuilder {
/// Build a loop statement: loop(condition) { body } /// Build a loop statement: loop(condition) { body }
/// ///
/// Note: This modularized builder uses a simplified lowering that avoids /// Uses the shared LoopBuilder facade to avoid tight coupling.
/// coupling to mir::loop_builder (which expects mir::builder::MirBuilder).
/// Proper SSA phi construction should be reintroduced via an adapter/facade.
pub(super) fn build_loop_statement(&mut self, condition: ASTNode, body: Vec<ASTNode>) -> Result<ValueId, String> { pub(super) fn build_loop_statement(&mut self, condition: ASTNode, body: Vec<ASTNode>) -> Result<ValueId, String> {
// Blocks: preheader (current), header, body, after // Evaluate condition first (boolean-ish value)
let header_bb = self.block_gen.next();
let body_bb = self.block_gen.next();
let after_bb = self.block_gen.next();
// Jump to header from current block
self.emit_instruction(MirInstruction::Jump { target: header_bb })?;
// Header: evaluate condition and branch
self.start_new_block(header_bb)?;
let cond_val = self.build_expression(condition)?; let cond_val = self.build_expression(condition)?;
self.emit_instruction(MirInstruction::Branch { condition: cond_val, then_bb: body_bb, else_bb: after_bb })?;
// Body: build statements, then jump back to header // Use loop_api helper with a closure that builds the loop body
self.start_new_block(body_bb)?; let mut body_builder = |lb: &mut Self| -> Result<(), String> {
for stmt in body { for stmt in &body {
let _ = self.build_expression(stmt)?; let _ = lb.build_expression(stmt.clone())?;
}
if !self.is_current_block_terminated() {
self.emit_instruction(MirInstruction::Jump { target: header_bb })?;
} }
Ok(())
};
// After: return void value crate::mir::loop_api::build_simple_loop(self, cond_val, &mut body_builder)
self.start_new_block(after_bb)?;
let void_id = self.value_gen.next();
self.emit_instruction(MirInstruction::Const { dst: void_id, value: ConstValue::Void })?;
Ok(void_id)
} }
/// Build a try/catch statement /// Build a try/catch statement