Files
hakorune/src/mir/builder/control_flow/joinir/patterns/router.rs

194 lines
6.9 KiB
Rust
Raw Normal View History

feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
//! Pattern Router - Table-driven dispatch for loop patterns
//!
//! Phase 194: Replace if/else chain with table-driven routing
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
//! Phase 193: Modularized feature extraction using ast_feature_extractor module
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
//!
//! # Architecture
//!
//! - Each pattern registers a detect function and a lower function
//! - Patterns are tried in priority order (lower = tried first)
//! - First matching pattern wins
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
//! - Feature extraction delegated to ast_feature_extractor module
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
//!
//! # Adding New Patterns
//!
//! 1. Create a new module in `patterns/` (e.g., `pattern4_your_name.rs`)
//! 2. Implement `pub fn can_lower(ctx: &LoopPatternContext) -> bool`
//! 3. Implement `pub fn lower(builder: &mut MirBuilder, ctx: &LoopPatternContext) -> Result<Option<ValueId>, String>`
//! 4. Add entry to `LOOP_PATTERNS` table below
//!
//! That's it! No need to modify routing logic.
use crate::ast::ASTNode;
use crate::mir::builder::MirBuilder;
use crate::mir::ValueId;
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
use crate::mir::loop_pattern_detection::{LoopFeatures, LoopPatternKind};
/// Phase 193: Import AST Feature Extractor Box
/// (declared in mod.rs as pub module, import from parent)
use super::ast_feature_extractor as ast_features;
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
/// Context passed to pattern detect/lower functions
pub struct LoopPatternContext<'a> {
/// Loop condition AST node
pub condition: &'a ASTNode,
/// Loop body statements
pub body: &'a [ASTNode],
/// Current function name (for routing)
pub func_name: &'a str,
/// Debug logging enabled
pub debug: bool,
/// Has continue statement(s) in body? (Phase 194+)
pub has_continue: bool,
/// Has break statement(s) in body? (Phase 194+)
pub has_break: bool,
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// Phase 192: Loop features extracted from AST
pub features: LoopFeatures,
/// Phase 192: Pattern classification based on features
pub pattern_kind: LoopPatternKind,
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
}
impl<'a> LoopPatternContext<'a> {
/// Create new context from routing parameters
///
/// Phase 194+: Automatically detects continue/break statements in body
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// Phase 192: Extract features and classify pattern from AST
/// Phase 193: Feature extraction delegated to ast_feature_extractor module
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
pub fn new(
condition: &'a ASTNode,
body: &'a [ASTNode],
func_name: &'a str,
debug: bool,
) -> Self {
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
// Phase 193: Use AST Feature Extractor Box for break/continue detection
let has_continue = ast_features::detect_continue_in_body(body);
let has_break = ast_features::detect_break_in_body(body);
// Phase 193: Extract features using modularized extractor
let features = ast_features::extract_features(body, has_continue, has_break);
// Phase 192: Classify pattern based on features
let pattern_kind = crate::mir::loop_pattern_detection::classify(&features);
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
Self {
condition,
body,
func_name,
debug,
has_continue,
has_break,
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
features,
pattern_kind,
}
}
}
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// Phase 193: Feature extraction moved to ast_feature_extractor module
/// See: src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
/// Entry in the loop pattern router table.
/// Each pattern registers a detect function and a lower function.
pub struct LoopPatternEntry {
/// Human-readable pattern name for debugging
pub name: &'static str,
/// Priority (lower = tried first). Pattern1=10, Pattern2=20, Pattern3=30
pub priority: u8,
/// Detection function: returns true if this pattern matches
pub detect: fn(&MirBuilder, &LoopPatternContext) -> bool,
/// Lowering function: performs the actual JoinIR generation
pub lower: fn(&mut MirBuilder, &LoopPatternContext) -> Result<Option<ValueId>, String>,
}
/// Static table of all registered loop patterns.
/// Patterns are tried in priority order (lowest first).
///
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// # Current Patterns (Phase 192: Structure-based detection)
///
/// All patterns now use structure-based detection via LoopFeatures and classify():
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
///
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// - Pattern 4 (priority 5): Loop with Continue (loop_continue_pattern4.hako)
/// - Detection: pattern_kind == Pattern4Continue
/// - Structure: has_continue && !has_break
///
/// - Pattern 3 (priority 30): Loop with If-Else PHI (loop_if_phi.hako)
/// - Detection: pattern_kind == Pattern3IfPhi
/// - Structure: has_if_else_phi && !has_break && !has_continue
///
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
/// - Pattern 1 (priority 10): Simple While Loop (loop_min_while.hako)
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// - Detection: pattern_kind == Pattern1SimpleWhile
/// - Structure: !has_break && !has_continue && !has_if_else_phi
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
///
/// - Pattern 2 (priority 20): Loop with Conditional Break (joinir_min_loop.hako)
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// - Detection: pattern_kind == Pattern2Break
/// - Structure: has_break && !has_continue
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
///
refactor(joinir): Phase 193-1 - AST Feature Extractor Box modularization **Phase 193-1**: Create independent AST Feature Extractor Box module ## Summary Extracted feature detection logic from router.rs into a new, reusable ast_feature_extractor.rs module. This improves: - **Modularity**: Feature extraction is now a pure, side-effect-free module - **Reusability**: Can be used for Pattern 5-6 detection and analysis tools - **Testability**: Pure functions can be unit tested independently - **Maintainability**: Clear separation of concerns (router does dispatch, extractor does analysis) ## Changes ### New Files - **src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs** (+180 lines) - `detect_continue_in_body()`: Detect continue statements - `detect_break_in_body()`: Detect break statements - `extract_features()`: Full feature extraction pipeline - `detect_if_else_phi_in_body()`: Pattern detection for if-else PHI - `count_carriers_in_body()`: Heuristic carrier counting - Unit tests for basic functionality ### Modified Files - **src/mir/builder/control_flow/joinir/patterns/router.rs** - Removed 75 lines of feature detection code - Now delegates to `ast_features::` module - Phase 193 documentation in comments - Cleaner separation of concerns - **src/mir/builder/control_flow/joinir/patterns/mod.rs** - Added module declaration for ast_feature_extractor - Updated documentation with Phase 193 info ## Architecture ``` router.rs (10 lines) └─→ ast_feature_extractor.rs (180 lines) - Pure functions for AST analysis - No side effects - High reusability - Testable in isolation ``` ## Testing ✅ Build succeeds: `cargo build --release` compiles cleanly ✅ Binary compatibility: Existing .hako files execute correctly ✅ No logic changes: Feature detection identical to previous implementation ## Metrics - Lines moved from router to new module: 75 - New module total: 180 lines (including tests and documentation) - Router.rs reduced by ~40% in feature detection code - New module rated ⭐⭐⭐⭐⭐ for reusability and independence ## Next Steps - Phase 193-2: CarrierInfo Builder Enhancement - Phase 193-3: Pattern Classification Improvement - Phase 194: Further pattern detection optimizations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-06 03:30:03 +09:00
/// Note: func_name is now only used for debug logging, not pattern detection
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
pub static LOOP_PATTERNS: &[LoopPatternEntry] = &[
LoopPatternEntry {
name: "Pattern4_WithContinue",
priority: 5, // Highest priority - continue is most specific
detect: super::pattern4_with_continue::can_lower,
lower: super::pattern4_with_continue::lower,
},
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
LoopPatternEntry {
name: "Pattern3_WithIfPhi",
priority: 30, // NOTE: Pattern 3 must be checked BEFORE Pattern 1 (both use "main")
detect: super::pattern3_with_if_phi::can_lower,
lower: super::pattern3_with_if_phi::lower,
},
LoopPatternEntry {
name: "Pattern1_Minimal",
priority: 10,
detect: super::pattern1_minimal::can_lower,
lower: super::pattern1_minimal::lower,
},
LoopPatternEntry {
name: "Pattern2_WithBreak",
priority: 20,
detect: super::pattern2_with_break::can_lower,
lower: super::pattern2_with_break::lower,
},
];
/// Try all registered patterns in priority order.
///
/// Returns Ok(Some(value_id)) if a pattern matched and lowered successfully.
/// Returns Ok(None) if no pattern matched.
/// Returns Err if a pattern matched but lowering failed.
pub fn route_loop_pattern(
builder: &mut MirBuilder,
ctx: &LoopPatternContext,
) -> Result<Option<ValueId>, String> {
feat(joinir): Phase 195 - Unified JoinLoopTrace for all JoinIR debug output Created centralized tracing module for JoinIR loop lowering operations, consolidating scattered eprintln! calls into a single SSOT interface. # Implementation 1. **Created trace.rs module** (~233 lines) - JoinLoopTrace struct with env var controls - Unified API: pattern(), varmap(), joinir_stats(), phi(), merge(), etc. - Global singleton via trace() function - Supports 5 env vars: NYASH_TRACE_VARMAP, NYASH_JOINIR_DEBUG, NYASH_OPTION_C_DEBUG, NYASH_JOINIR_MAINLINE_DEBUG, NYASH_LOOPFORM_DEBUG 2. **Updated debug.rs** - Delegates trace_varmap() to JoinLoopTrace 3. **Updated routing.rs** - All eprintln! replaced with trace calls (10 instances) 4. **Updated pattern routers** - All 3 patterns now use unified trace - pattern1_minimal.rs: 6 replacements - pattern2_with_break.rs: 6 replacements - pattern3_with_if_phi.rs: 6 replacements - router.rs: 2 replacements 5. **Updated merge/block_allocator.rs** - 6 eprintln! → trace calls # Benefits - **Single Source of Truth**: All trace control through environment variables - **Consistent Format**: Unified [trace:*] prefix for easy filtering - **Zero Overhead**: No output when env vars unset - **Easy Extension**: Add new trace points via existing API - **Better Debug Experience**: Structured output with clear categories # Testing ✅ cargo build --release - Success ✅ NYASH_TRACE_VARMAP=1 - Shows varmap traces only ✅ NYASH_JOINIR_DEBUG=1 - Shows joinir + blocks + routing traces ✅ No env vars - No debug output ✅ apps/tests/loop_min_while.hako - All tests pass # Related - Phase 191-194 groundwork (modular merge structure) - NYASH_TRACE_VARMAP added today for variable_map debugging - Consolidates ~80 scattered eprintln! calls across JoinIR 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:23:51 +09:00
use super::super::trace;
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
// Patterns are already sorted by priority in the table
// (Pattern 3 with priority 30 comes first, then Pattern 1 with priority 10, etc.)
// This ensures Pattern 3 is checked before Pattern 1, avoiding incorrect routing.
for entry in LOOP_PATTERNS {
if (entry.detect)(builder, ctx) {
feat(joinir): Phase 195 - Unified JoinLoopTrace for all JoinIR debug output Created centralized tracing module for JoinIR loop lowering operations, consolidating scattered eprintln! calls into a single SSOT interface. # Implementation 1. **Created trace.rs module** (~233 lines) - JoinLoopTrace struct with env var controls - Unified API: pattern(), varmap(), joinir_stats(), phi(), merge(), etc. - Global singleton via trace() function - Supports 5 env vars: NYASH_TRACE_VARMAP, NYASH_JOINIR_DEBUG, NYASH_OPTION_C_DEBUG, NYASH_JOINIR_MAINLINE_DEBUG, NYASH_LOOPFORM_DEBUG 2. **Updated debug.rs** - Delegates trace_varmap() to JoinLoopTrace 3. **Updated routing.rs** - All eprintln! replaced with trace calls (10 instances) 4. **Updated pattern routers** - All 3 patterns now use unified trace - pattern1_minimal.rs: 6 replacements - pattern2_with_break.rs: 6 replacements - pattern3_with_if_phi.rs: 6 replacements - router.rs: 2 replacements 5. **Updated merge/block_allocator.rs** - 6 eprintln! → trace calls # Benefits - **Single Source of Truth**: All trace control through environment variables - **Consistent Format**: Unified [trace:*] prefix for easy filtering - **Zero Overhead**: No output when env vars unset - **Easy Extension**: Add new trace points via existing API - **Better Debug Experience**: Structured output with clear categories # Testing ✅ cargo build --release - Success ✅ NYASH_TRACE_VARMAP=1 - Shows varmap traces only ✅ NYASH_JOINIR_DEBUG=1 - Shows joinir + blocks + routing traces ✅ No env vars - No debug output ✅ apps/tests/loop_min_while.hako - All tests pass # Related - Phase 191-194 groundwork (modular merge structure) - NYASH_TRACE_VARMAP added today for variable_map debugging - Consolidates ~80 scattered eprintln! calls across JoinIR 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:23:51 +09:00
// Phase 195: Use unified trace for pattern matching
trace::trace().pattern("route", entry.name, true);
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
return (entry.lower)(builder, ctx);
}
}
// No pattern matched - return None (caller will handle error)
// Phase 187-2: Legacy LoopBuilder removed, all loops must use JoinIR
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
if ctx.debug {
trace::trace().debug("route", &format!("No pattern matched for function '{}'", ctx.func_name));
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
}
Ok(None)
}