diff --git a/src/mir/builder_modularized/control_flow.rs b/src/mir/builder_modularized/control_flow.rs index abd0f89b..7809757a 100644 --- a/src/mir/builder_modularized/control_flow.rs +++ b/src/mir/builder_modularized/control_flow.rs @@ -94,10 +94,38 @@ impl MirBuilder { } /// Build a loop statement: loop(condition) { body } + /// + /// Note: This modularized builder uses a simplified lowering that avoids + /// 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) -> Result { - // Use the specialized LoopBuilder for proper SSA loop construction - let mut loop_builder = super::loop_builder::LoopBuilder::new(self); - loop_builder.build_loop(condition, body) + // Blocks: preheader (current), header, body, after + 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)?; + self.emit_instruction(MirInstruction::Branch { condition: cond_val, then_bb: body_bb, else_bb: after_bb })?; + + // Body: build statements, then jump back to header + self.start_new_block(body_bb)?; + for stmt in body { + let _ = self.build_expression(stmt)?; + } + if !self.is_current_block_terminated() { + self.emit_instruction(MirInstruction::Jump { target: header_bb })?; + } + + // After: return void value + 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 @@ -191,4 +219,4 @@ impl MirBuilder { } false } -} \ No newline at end of file +}