Files
hakorune/src/mir/builder/control_flow/joinir/patterns/mod.rs
tomoaki 8d0808b6d4 feat(joinir): Phase 282 P3 - Pattern1 ExtractionBased Migration
Migrated Pattern1 (Simple While Loop) from StructureBased to ExtractionBased
detection, creating a migration template for Pattern2-5.

**Changes**:
1. Created extractors/mod.rs - Module entry point with design principles
2. Created extractors/pattern1.rs - Pure extraction functions with 4-phase validation
3. Updated pattern1_minimal.rs - can_lower() + lower() use extraction
4. Updated mod.rs - Registered extractors module

**Pattern1Parts Design** (Lightweight):
- Stores only loop_var (AST reused from ctx, no heavy copies)
- Validation: 比較条件 + no break/continue/if-else + 単純step (i = i ± const)
- Return statements allowed (not loop control flow)

**4-Phase Validation**:
1. Validate condition structure (比較演算, left=variable)
2. Validate body (no break/continue/if-else-phi)
3. Validate step pattern (simple i = i ± const only)
4. Extract loop variable

**Safety Valve Strategy**:
- pattern_kind as O(1) early rejection guard
- Extraction as SSOT authoritative check
- Re-extraction in lower() (no caching from can_lower)

**Testing**:
- Unit tests: 3/3 PASS
- Build: 0 errors
- Regression: 45/46 PASS (zero regression)

**Migration Template**:
- Result<Option<Parts>, String> return type (Pattern8/9 model)
- Pure functions (no builder mutations)
- Fail-Fast error handling (Err for logic bugs, Ok(None) for non-matches)

🎉 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 06:09:57 +09:00

113 lines
6.6 KiB
Rust

//! Pattern lowerers for different loop constructs
//!
//! Phase 2: Extracted from control_flow.rs
//! - Pattern 1: Simple While Loop (pattern1_minimal.rs)
//! - Pattern 2: Loop with Conditional Break (pattern2_with_break.rs)
//! - Pattern 3: Loop with If-Else PHI (pattern3_with_if_phi.rs)
//! - Pattern 4: Loop with Continue (pattern4_with_continue.rs) [Phase 194+]
//!
//! Phase 194: Table-driven router for pattern dispatch
//! - Router module provides table-driven pattern matching
//! - Each pattern exports can_lower() and lower() functions
//! - See router.rs for how to add new patterns
//!
//! Phase 193: AST Feature Extraction Modularization
//! - ast_feature_extractor.rs: Pure function module for analyzing loop AST
//! - High reusability for Pattern 5-6 and pattern analysis tools
//!
//! Phase 193-4 / Phase 222.5-C: Exit Binding Builder
//! - exit_binding.rs: Fully boxified exit binding generation (orchestrator)
//! - exit_binding_validator.rs: CarrierInfo and ExitMeta validation
//! - exit_binding_constructor.rs: Exit binding construction and ValueId allocation
//! - exit_binding_applicator.rs: Boundary application logic
//! - Eliminates hardcoded variable names and ValueId assumptions
//! - Supports both single and multi-carrier loop patterns
//!
//! Phase 33-22: Common Pattern Infrastructure
//! - common_init.rs: CommonPatternInitializer for unified initialization
//! - conversion_pipeline.rs: JoinIRConversionPipeline for unified conversion flow
//!
//! Phase 171-172: Refactoring Infrastructure
//! - loop_scope_shape_builder.rs: Unified LoopScopeShape initialization (Issue 4)
//! - condition_env_builder.rs: Unified ConditionEnv construction (Issue 5)
//!
//! Phase 33-23: Pattern-Specific Analyzers (Stage 2)
//! - pattern4_carrier_analyzer.rs: Pattern 4 carrier analysis and normalization (Issue 2)
//!
//! Stage 3 + Issue 1: Trim Pattern Extraction
//! - trim_pattern_validator.rs: Trim pattern validation and whitespace check generation
//! - trim_pattern_lowerer.rs: Trim-specific JoinIR lowering
//!
//! Phase 179-B: Generic Pattern Framework
//! - pattern_pipeline.rs: Unified preprocessing pipeline for Patterns 1-4
//!
//! Phase 91 P5b: Escape Pattern Recognizer
//! - escape_pattern_recognizer.rs: P5b (escape sequence handling) pattern detection
//! - Extracted from ast_feature_extractor for improved modularity
//!
//! Phase 93/94: Pattern Policies
//! - policies/: Pattern recognition and routing decision (future expansion)
//! - Currently a placeholder directory for future policy box organization
//!
//! Phase 255 P2: Common Utilities
//! - common/: Shared helper functions (var() etc.) to eliminate code duplication
pub(in crate::mir::builder) mod common; // Phase 255 P2: Common AST helpers
pub(in crate::mir::builder) mod extractors; // Phase 282 P3: Common extraction interfaces
pub(in crate::mir::builder) mod ast_feature_extractor;
pub(in crate::mir::builder) mod policies; // Phase 93/94: Pattern routing policies (future expansion)
pub(in crate::mir::builder) mod body_local_policy; // Phase 92 P3: promotion vs slot routing
pub(in crate::mir::builder) mod escape_pattern_recognizer; // Phase 91 P5b
pub(in crate::mir::builder) mod common_init;
pub(in crate::mir::builder) mod loop_true_counter_extractor; // Phase 104: loop(true) counter extraction for Pattern2
pub(in crate::mir::builder) mod read_digits_break_condition_box; // Phase 104: break cond normalization for read_digits(loop(true))
pub(in crate::mir::builder) mod pattern2; // Phase 263 P0.2: Pattern2 module (api/ SSOT entry point)
pub(in crate::mir::builder) mod pattern2_break_condition_policy_router; // Phase 105: policy router box for Pattern2 break condition
pub(in crate::mir::builder) mod pattern2_policy_router; // Phase 108: unified Pattern2 policy router (balanced/read_digits/default)
pub(in crate::mir::builder) mod pattern2_inputs_facts_box; // Phase 105: Pattern2 input facts (analysis only)
pub(in crate::mir::builder) mod pattern2_lowering_orchestrator; // Phase 105: Pattern2 orchestration (wiring/emission)
pub(in crate::mir::builder) mod pattern2_steps; // Phase 106: Pattern2 step boxes (pipeline SSOT)
pub(in crate::mir::builder) mod condition_env_builder;
pub(in crate::mir::builder) mod conversion_pipeline;
pub(in crate::mir::builder) mod exit_binding;
pub(in crate::mir::builder) mod exit_binding_applicator; // Phase 222.5-C
pub(in crate::mir::builder) mod exit_binding_constructor; // Phase 222.5-C
pub(in crate::mir::builder) mod exit_binding_validator; // Phase 222.5-C
pub(in crate::mir::builder) mod loop_scope_shape_builder;
pub(in crate::mir::builder) mod pattern1_minimal;
pub(in crate::mir::builder) mod pattern2_with_break;
pub(in crate::mir::builder) mod pattern3_with_if_phi;
pub(in crate::mir::builder) mod pattern4_carrier_analyzer;
pub(in crate::mir::builder) mod pattern4_with_continue;
pub(in crate::mir::builder) mod pattern5_infinite_early_exit; // Phase 131-11
pub(in crate::mir::builder) mod pattern6_scan_with_init; // Phase 254 P0: index_of/find/contains pattern
pub(in crate::mir::builder) mod pattern7_split_scan; // Phase 256 P0: split/tokenization with variable step
pub(in crate::mir::builder) mod pattern8_scan_bool_predicate; // Phase 259 P0: boolean predicate scan (is_integer/is_valid)
pub(in crate::mir::builder) mod pattern9_accum_const_loop; // Phase 270 P1: accumulator const loop (橋渡しパターン)
pub(in crate::mir::builder) mod pattern_pipeline;
pub(in crate::mir::builder) mod router;
pub(in crate::mir::builder) mod trim_loop_lowering; // Phase 180: Dedicated Trim/P5 lowering module
pub(in crate::mir::builder) mod trim_pattern_lowerer;
pub(in crate::mir::builder) mod trim_pattern_validator;
// Re-export router for convenience
pub(in crate::mir::builder) use router::{route_loop_pattern, LoopPatternContext};
// Phase 140-P4-A: Re-export for loop_canonicalizer SSOT (crate-wide visibility)
pub(crate) use ast_feature_extractor::detect_skip_whitespace_pattern;
// Phase 104: Re-export read_digits(loop(true)) detection for loop_canonicalizer
pub(crate) use ast_feature_extractor::detect_read_digits_loop_true_pattern;
// Phase 142-P1: Re-export continue pattern detection for loop_canonicalizer
pub(crate) use ast_feature_extractor::detect_continue_pattern;
// Phase 143-P0: Re-export parse_number pattern detection for loop_canonicalizer
pub(crate) use ast_feature_extractor::detect_parse_number_pattern;
// Phase 143-P1: Re-export parse_string pattern detection for loop_canonicalizer
pub(crate) use ast_feature_extractor::detect_parse_string_pattern;
// Phase 91 P5b: Re-export escape skip pattern detection for loop_canonicalizer
pub(crate) use ast_feature_extractor::detect_escape_skip_pattern;