## Phase 92全体の成果 **Phase 92 P0-P2**: ConditionalStep JoinIR生成とbody-local変数サポート - ConditionalStep(条件付きキャリア更新)のJoinIR生成実装 - Body-local変数(ch等)の条件式での参照サポート - 変数解決優先度: ConditionEnv → LoopBodyLocalEnv **Phase 92 P3**: BodyLocalPolicyBox + 安全ガード - BodyLocalPolicyDecision実装(Accept/Reject判定) - BodyLocalSlot + DualValueRewriter(JoinIR/MIR二重書き込み) - Fail-Fast契約(Cannot promote LoopBodyLocal検出) **Phase 92 P4**: E2E固定+回帰最小化 (本コミット) - Unit test 3本追加(body-local変数解決検証) - Integration smoke追加(phase92_pattern2_baseline.sh、2ケースPASS) - P4-E2E-PLAN.md、P4-COMPLETION.md作成 ## 主要な実装 ### ConditionalStep(条件付きキャリア更新) - `conditional_step_emitter.rs`: JoinIR Select命令生成 - `loop_with_break_minimal.rs`: ConditionalStep検出と統合 - `loop_with_continue_minimal.rs`: Pattern4対応 ### Body-local変数サポート - `condition_lowerer.rs`: body-local変数解決機能 - `lower_condition_to_joinir`: body_local_env パラメータ追加 - 変数解決優先度実装(ConditionEnv優先) - Unit test 3本追加: 変数解決/優先度/エラー - `header_break_lowering.rs`: break条件でbody-local変数参照 - 7ファイルで後方互換ラッパー(lower_condition_to_joinir_no_body_locals) ### Body-local Policy & Safety - `body_local_policy.rs`: BodyLocalPolicyDecision(Accept/Reject) - `body_local_slot.rs`: JoinIR/MIR二重書き込み - `dual_value_rewriter.rs`: ValueId書き換えヘルパー ## テスト体制 ### Unit Tests (+3) - `test_body_local_variable_resolution`: body-local変数解決 - `test_variable_resolution_priority`: 変数解決優先度(ConditionEnv優先) - `test_undefined_variable_error`: 未定義変数エラー - 全7テストPASS(cargo test --release condition_lowerer::tests) ### Integration Smoke (+1) - `phase92_pattern2_baseline.sh`: - Case A: loop_min_while.hako (Pattern2 baseline) - Case B: phase92_conditional_step_minimal.hako (条件付きインクリメント) - 両ケースPASS、integration profileで発見可能 ### 退行確認 - ✅ 既存Pattern2Breakテスト正常(退行なし) - ✅ Phase 135 smoke正常(MIR検証PASS) ## アーキテクチャ設計 ### 変数解決メカニズム ```rust // Priority 1: ConditionEnv (loop params, captured) if let Some(value_id) = env.get(name) { return Ok(value_id); } // Priority 2: LoopBodyLocalEnv (body-local like `ch`) if let Some(body_env) = body_local_env { if let Some(value_id) = body_env.get(name) { return Ok(value_id); } } ``` ### Fail-Fast契約 - Delta equality check (conditional_step_emitter.rs) - Variable resolution error messages (ConditionEnv) - Body-local promotion rejection (BodyLocalPolicyDecision::Reject) ## ドキュメント - `P4-E2E-PLAN.md`: 3レベルテスト戦略(Level 1-2完了、Level 3延期) - `P4-COMPLETION.md`: Phase 92完了報告 - `README.md`: Phase 92全体のまとめ ## 将来の拡張(Phase 92スコープ外) - Body-local promotionシステム拡張 - P5bパターン認識の汎化(flagベース条件サポート) - 完全なP5b E2Eテスト(body-local promotion実装後) 🎯 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
155 lines
5.2 KiB
Rust
155 lines
5.2 KiB
Rust
//! Phase 169: JoinIR Condition Lowering Orchestrator
|
|
//!
|
|
//! This module provides the high-level API for lowering AST conditions to JoinIR.
|
|
//! It re-exports functionality from specialized modules:
|
|
//!
|
|
//! - `condition_env`: Variable name → ValueId mapping
|
|
//! - `condition_lowerer`: AST → JoinIR lowering logic
|
|
//! - `condition_var_extractor`: Variable extraction from AST
|
|
//!
|
|
//! ## Design Philosophy
|
|
//!
|
|
//! **Orchestration Layer**: This module provides a unified API by composing
|
|
//! functionality from specialized modules. Each module has a single responsibility:
|
|
//!
|
|
//! - `condition_env.rs`: Environment management (80 lines)
|
|
//! - `condition_lowerer.rs`: Core lowering logic (330 lines)
|
|
//! - `condition_var_extractor.rs`: Variable extraction (90 lines)
|
|
//! - `condition_to_joinir.rs` (this file): API orchestration (100 lines)
|
|
//!
|
|
//! **Total: 600 lines → 500 lines (17% reduction)**
|
|
//!
|
|
//! ## Separation of Concerns
|
|
//!
|
|
//! - BoolExprLowerer: AST → MIR (for regular control flow)
|
|
//! - condition_to_joinir: AST → JoinIR (for loop lowerers)
|
|
//!
|
|
//! This dual approach maintains clean boundaries:
|
|
//! - Loop lowerers work in JoinIR space (pure functional transformation)
|
|
//! - Regular control flow uses MIR space (stateful builder)
|
|
|
|
// Re-export public API from specialized modules
|
|
pub use super::condition_env::{ConditionBinding, ConditionEnv};
|
|
pub use super::condition_lowerer::{
|
|
lower_condition_to_joinir, lower_condition_to_joinir_no_body_locals, lower_value_expression,
|
|
};
|
|
pub use super::condition_var_extractor::extract_condition_variables;
|
|
|
|
// Re-export JoinIR types for convenience
|
|
pub use crate::mir::join_ir::JoinInst;
|
|
pub use crate::mir::ValueId;
|
|
|
|
/// Module documentation test
|
|
///
|
|
/// This test verifies that the public API is accessible and works as expected.
|
|
#[cfg(test)]
|
|
mod api_tests {
|
|
use super::*;
|
|
use crate::ast::{ASTNode, BinaryOperator, LiteralValue, Span};
|
|
|
|
#[test]
|
|
fn test_api_condition_env() {
|
|
let mut env = ConditionEnv::new();
|
|
env.insert("i".to_string(), ValueId(0));
|
|
assert_eq!(env.get("i"), Some(ValueId(0)));
|
|
}
|
|
|
|
#[test]
|
|
fn test_api_condition_binding() {
|
|
let binding = ConditionBinding::new("start".to_string(), ValueId(33), ValueId(1));
|
|
assert_eq!(binding.name, "start");
|
|
assert_eq!(binding.host_value, ValueId(33));
|
|
assert_eq!(binding.join_value, ValueId(1));
|
|
}
|
|
|
|
#[test]
|
|
fn test_api_lower_condition() {
|
|
let mut env = ConditionEnv::new();
|
|
env.insert("i".to_string(), ValueId(0));
|
|
|
|
let mut value_counter = 1u32;
|
|
let mut alloc_value = || {
|
|
let id = ValueId(value_counter);
|
|
value_counter += 1;
|
|
id
|
|
};
|
|
|
|
// AST: i < 10
|
|
let ast = ASTNode::BinaryOp {
|
|
operator: BinaryOperator::Less,
|
|
left: Box::new(ASTNode::Variable {
|
|
name: "i".to_string(),
|
|
span: Span::unknown(),
|
|
}),
|
|
right: Box::new(ASTNode::Literal {
|
|
value: LiteralValue::Integer(10),
|
|
span: Span::unknown(),
|
|
}),
|
|
span: Span::unknown(),
|
|
};
|
|
|
|
let result = lower_condition_to_joinir_no_body_locals(&ast, &mut alloc_value, &env);
|
|
assert!(result.is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn test_api_extract_variables() {
|
|
// AST: start < end
|
|
let ast = ASTNode::BinaryOp {
|
|
operator: BinaryOperator::Less,
|
|
left: Box::new(ASTNode::Variable {
|
|
name: "start".to_string(),
|
|
span: Span::unknown(),
|
|
}),
|
|
right: Box::new(ASTNode::Variable {
|
|
name: "end".to_string(),
|
|
span: Span::unknown(),
|
|
}),
|
|
span: Span::unknown(),
|
|
};
|
|
|
|
let vars = extract_condition_variables(&ast, &[]);
|
|
assert_eq!(vars, vec!["end", "start"]); // Sorted
|
|
}
|
|
|
|
#[test]
|
|
fn test_api_integration() {
|
|
// Full integration: extract vars, create env, lower condition
|
|
let ast = ASTNode::BinaryOp {
|
|
operator: BinaryOperator::Less,
|
|
left: Box::new(ASTNode::Variable {
|
|
name: "i".to_string(),
|
|
span: Span::unknown(),
|
|
}),
|
|
right: Box::new(ASTNode::Variable {
|
|
name: "end".to_string(),
|
|
span: Span::unknown(),
|
|
}),
|
|
span: Span::unknown(),
|
|
};
|
|
|
|
// Step 1: Extract variables (excluding loop param 'i')
|
|
let condition_vars = extract_condition_variables(&ast, &["i".to_string()]);
|
|
assert_eq!(condition_vars, vec!["end"]);
|
|
|
|
// Step 2: Create environment
|
|
let mut env = ConditionEnv::new();
|
|
env.insert("i".to_string(), ValueId(0)); // Loop parameter
|
|
env.insert("end".to_string(), ValueId(1)); // Condition-only variable
|
|
|
|
// Step 3: Lower condition
|
|
let mut value_counter = 2u32;
|
|
let mut alloc_value = || {
|
|
let id = ValueId(value_counter);
|
|
value_counter += 1;
|
|
id
|
|
};
|
|
|
|
let result = lower_condition_to_joinir_no_body_locals(&ast, &mut alloc_value, &env);
|
|
assert!(result.is_ok());
|
|
|
|
let (_cond_value, instructions) = result.unwrap();
|
|
assert_eq!(instructions.len(), 1); // Single Compare instruction
|
|
}
|
|
}
|