refactor(joinir): Phase 108 unify Pattern2 policy routing

This commit is contained in:
nyash-codex
2025-12-17 23:30:08 +09:00
parent 9fa2f5a8ad
commit 27e8e0f16a
8 changed files with 142 additions and 88 deletions

View File

@ -12,14 +12,9 @@ use crate::mir::join_ir::lowering::loop_update_analyzer::{UpdateExpr, UpdateRhs}
use crate::mir::join_ir::BinOpKind;
use super::PolicyDecision;
use super::post_loop_early_return_plan::PostLoopEarlyReturnPlan;
use std::collections::BTreeMap;
#[derive(Debug, Clone)]
pub(crate) struct PostLoopEarlyReturnPlan {
pub loop_counter_name: String,
pub bound_name: String,
}
#[derive(Debug, Clone)]
pub(crate) struct BalancedDepthScanPolicyResult {
pub break_condition_node: ASTNode,
@ -101,8 +96,13 @@ fn classify_balanced_depth_scan(
depth_next_name,
},
post_loop_early_return: PostLoopEarlyReturnPlan {
loop_counter_name,
bound_name,
cond: ASTNode::BinaryOp {
operator: BinaryOperator::Less,
left: Box::new(var(&loop_counter_name)),
right: Box::new(var(&bound_name)),
span: Span::unknown(),
},
ret_expr: var(&loop_counter_name),
},
})
}
@ -618,8 +618,28 @@ mod tests {
other => panic!("expected Use, got {:?}", other),
};
assert_eq!(result.post_loop_early_return.loop_counter_name, "i");
assert_eq!(result.post_loop_early_return.bound_name, "n");
assert!(
matches!(
&result.post_loop_early_return.cond,
ASTNode::BinaryOp {
operator: BinaryOperator::Less,
left,
right,
..
} if matches!(left.as_ref(), ASTNode::Variable { name, .. } if name == "i")
&& matches!(right.as_ref(), ASTNode::Variable { name, .. } if name == "n")
),
"post-loop cond must be `i < n`, got {:?}",
result.post_loop_early_return.cond
);
assert!(
matches!(
&result.post_loop_early_return.ret_expr,
ASTNode::Variable { name, .. } if name == "i"
),
"post-loop ret_expr must be `i`, got {:?}",
result.post_loop_early_return.ret_expr
);
assert!(result.allowed_body_locals_for_conditions.contains(&"ch".to_string()));
assert!(result.allowed_body_locals_for_conditions.contains(&"depth_next".to_string()));
assert!(result.carrier_updates_override.contains_key("i"));

View File

@ -35,3 +35,4 @@ pub(in crate::mir::builder) mod trim_policy;
pub(in crate::mir::builder) mod loop_true_read_digits_policy;
pub(in crate::mir::builder) mod balanced_depth_scan_policy;
pub(in crate::mir::builder) mod balanced_depth_scan_policy_box;
pub(in crate::mir::builder) mod post_loop_early_return_plan;

View File

@ -0,0 +1,15 @@
//! Post-loop early return plan (policy-level SSOT)
//!
//! Responsibility:
//! - Describe a post-loop guard that emulates an in-loop `return` without making
//! Pattern2 lowering itself return-in-loop aware.
//! - Keep the plan policy-agnostic so multiple Pattern2 families can reuse it.
use crate::ast::ASTNode;
#[derive(Debug, Clone)]
pub(crate) struct PostLoopEarlyReturnPlan {
pub cond: ASTNode,
pub ret_expr: ASTNode,
}