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:
@ -201,6 +201,45 @@ fn has_break_node(node: &ASTNode) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 170-B: Extract break condition from loop body
|
||||
///
|
||||
/// Searches for the first `if <condition> { break }` pattern in the loop body.
|
||||
/// This is used to delegate break condition lowering to `condition_to_joinir`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `body` - Loop body statements to search
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// `Some(&ASTNode)` - The condition AST node from `if <condition> { break }`
|
||||
/// `None` - No break statement found or break is not in a simple if statement
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```nyash
|
||||
/// loop(i < 3) {
|
||||
/// if i >= 2 { break } // <- Returns the "i >= 2" condition
|
||||
/// i = i + 1
|
||||
/// }
|
||||
/// ```
|
||||
pub fn extract_break_condition(body: &[ASTNode]) -> Option<&ASTNode> {
|
||||
for stmt in body {
|
||||
if let ASTNode::If {
|
||||
condition,
|
||||
then_body,
|
||||
..
|
||||
} = stmt
|
||||
{
|
||||
// Check if the then_body contains a break statement
|
||||
if then_body.iter().any(|node| matches!(node, ASTNode::Break { .. })) {
|
||||
return Some(condition.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@ -141,9 +141,14 @@ impl MirBuilder {
|
||||
variable_definitions: BTreeMap::new(),
|
||||
};
|
||||
|
||||
// Phase 169 / Phase 171-fix / Phase 172-3: Call Pattern 2 lowerer with ConditionEnv
|
||||
// Phase 170-B: Extract break condition from loop body
|
||||
use super::ast_feature_extractor;
|
||||
let break_condition = ast_feature_extractor::extract_break_condition(_body)
|
||||
.ok_or_else(|| "[cf_loop/pattern2] Failed to extract break condition from loop body".to_string())?;
|
||||
|
||||
// Phase 169 / Phase 171-fix / Phase 172-3 / Phase 170-B: Call Pattern 2 lowerer with break_condition
|
||||
// Phase 33-14: Now returns (JoinModule, JoinFragmentMeta) for expr_result + carrier separation
|
||||
let (join_module, fragment_meta) = match lower_loop_with_break_minimal(scope, condition, &env, &loop_var_name) {
|
||||
let (join_module, fragment_meta) = match lower_loop_with_break_minimal(scope, condition, break_condition, &env, &loop_var_name) {
|
||||
Ok((module, meta)) => (module, meta),
|
||||
Err(e) => {
|
||||
// Phase 195: Use unified trace
|
||||
|
||||
@ -76,6 +76,7 @@ impl MirBuilder {
|
||||
"TrimTest.trim/1" => true,
|
||||
"Main.trim/1" => true, // Phase 171-fix: Main box variant
|
||||
"Main.trim_string_simple/1" => true, // Phase 33-13: Simple trim variant
|
||||
"TrimTest.main/0" => true, // Phase 170: TrimTest.main for loop pattern test
|
||||
_ => false,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user