fix(joinir): Phase 170-B Break condition delegation to condition_to_joinir

Remove hardcoded break condition (i >= 2) from loop_with_break_minimal.rs.
Now delegates to condition_to_joinir for dynamic break condition lowering.

Changes:
- Add extract_break_condition() to ast_feature_extractor.rs
- Pattern2 lowerer extracts break condition AST and passes to lowerer
- loop_with_break_minimal.rs uses BoolExprLowerer instead of hardcoded values
- Add TrimTest.main/0 to JoinIR routing whitelist

Box Theory compliance:
- Single responsibility: Pattern2 handles structure, condition_to_joinir handles lowering
- Zero hardcoding: All break conditions now dynamic

Verified:
- test_loop_return.hako (i >= 2) → RC: 2 
- test_trim_loop.hako (i >= 3) → RC: 3 

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-07 12:59:33 +09:00
parent a6a97b3781
commit 7470bebb0b
4 changed files with 64 additions and 25 deletions

View File

@ -117,10 +117,12 @@ use crate::mir::ValueId;
///
/// # Arguments
///
/// * `break_condition` - AST node for the break condition (e.g., `i >= 2`) - Phase 170-B
/// * `loop_var_name` - Name of the loop variable (for ExitMeta construction)
pub fn lower_loop_with_break_minimal(
_scope: LoopScopeShape,
condition: &ASTNode,
break_condition: &ASTNode,
env: &ConditionEnv,
loop_var_name: &str,
) -> Result<(JoinModule, JoinFragmentMeta), String> {
@ -162,8 +164,14 @@ pub fn lower_loop_with_break_minimal(
// After condition lowering, allocate remaining ValueIds
let exit_cond = alloc_value(); // Exit condition (negated loop condition)
let const_2 = alloc_value(); // Break limit (hardcoded for now)
let break_cond = alloc_value(); // Break condition (i >= 2)
// Phase 170-B: Lower break condition using condition_to_joinir (no hardcoding!)
let (break_cond_value, mut break_cond_instructions) = lower_condition_to_joinir(
break_condition,
&mut alloc_value,
env,
)?;
let const_1 = alloc_value(); // Increment constant
let i_next = alloc_value(); // i + 1
@ -224,31 +232,16 @@ pub fn lower_loop_with_break_minimal(
});
// ------------------------------------------------------------------
// Break Condition Check: i >= 2
// Phase 170-B: Break Condition Check (delegated to condition_to_joinir)
// ------------------------------------------------------------------
// Step 1: const 2
loop_step_func
.body
.push(JoinInst::Compute(MirLikeInst::Const {
dst: const_2,
value: ConstValue::Integer(2),
}));
// Step 2: break_cond = (i >= 2)
loop_step_func
.body
.push(JoinInst::Compute(MirLikeInst::Compare {
dst: break_cond,
op: CompareOp::Ge,
lhs: i_param,
rhs: const_2,
}));
// Insert all break condition evaluation instructions
loop_step_func.body.append(&mut break_cond_instructions);
// Jump(k_exit, [i], cond=break_cond) // Break exit path
loop_step_func.body.push(JoinInst::Jump {
cont: k_exit_id.as_cont(),
args: vec![i_param], // Pass current i as exit value
cond: Some(break_cond),
cond: Some(break_cond_value), // Phase 170-B: Use lowered condition
});
// ------------------------------------------------------------------
@ -302,9 +295,10 @@ pub fn lower_loop_with_break_minimal(
// Set entry point
join_module.entry = Some(main_id);
eprintln!("[joinir/pattern2] Generated JoinIR for Loop with Break Pattern (Phase 169)");
eprintln!("[joinir/pattern2] Generated JoinIR for Loop with Break Pattern (Phase 170-B)");
eprintln!("[joinir/pattern2] Functions: main, loop_step, k_exit");
eprintln!("[joinir/pattern2] Condition from AST (not hardcoded)");
eprintln!("[joinir/pattern2] Loop condition from AST (delegated to condition_to_joinir)");
eprintln!("[joinir/pattern2] Break condition from AST (delegated to condition_to_joinir)");
eprintln!("[joinir/pattern2] Exit PHI: k_exit receives i from both natural exit and break");
// Phase 172-3 → Phase 33-14: Build JoinFragmentMeta with expr_result