refactor(joinir): Unify Pattern 1 with PatternPipelineContext

- Reduced preprocessing from 61 lines to ~30 lines (51% reduction)
- Total file size: 139 → 118 lines (15% reduction)
- Uses build_pattern_context() for unified initialization
- All tests passing (loop_min_while.hako verified)
This commit is contained in:
nyash-codex
2025-12-08 19:34:31 +09:00
parent 3b6b2027a1
commit 48ef94e9ba

View File

@ -27,62 +27,41 @@ pub fn lower(
}
impl MirBuilder {
/// Phase 188-Impl-1-F: Pattern 1 (Simple While Loop) minimal lowerer
/// Phase 179-B: Pattern 1 (Simple While Loop) minimal lowerer
///
/// This bypasses the LoopFrontendBinding JSON path and directly calls
/// the Pattern 1 minimal lowerer for apps/tests/loop_min_while.hako
/// **Refactored**: Now uses PatternPipelineContext for unified preprocessing
///
/// # Phase 188-Impl-2: Host Variable Integration
///
/// Extracts the loop variable from the host function (e.g., `i` from `i < 3`)
/// and creates a JoinInlineBoundary for mapping between host and JoinIR ValueIds.
///
/// # Pipeline
/// 1. Extract loop variable name from condition
/// 2. Look up ValueId in host variable_map
/// 3. Call simple_while_minimal::lower_simple_while_minimal() → JoinModule
/// 4. convert_join_module_to_mir_with_meta() → MirModule
/// 5. Merge MIR blocks into current_function
/// # Pipeline (Phase 179-B)
/// 1. Build preprocessing context → PatternPipelineContext
/// 2. Call JoinIR lowerer → JoinModule
/// 3. Create boundary from context → JoinInlineBoundary
/// 4. Merge MIR blocks → JoinIRConversionPipeline
pub(in crate::mir::builder) fn cf_loop_pattern1_minimal(
&mut self,
condition: &ASTNode,
_body: &[ASTNode],
body: &[ASTNode],
_func_name: &str,
debug: bool,
) -> Result<Option<ValueId>, String> {
use crate::mir::join_ir::lowering::simple_while_minimal::lower_simple_while_minimal;
use crate::mir::BasicBlockId;
use std::collections::BTreeSet;
// Phase 195: Use unified trace
trace::trace().debug("pattern1", "Calling Pattern 1 minimal lowerer");
// Phase 33-22: Use CommonPatternInitializer for loop variable extraction
use super::common_init::CommonPatternInitializer;
let (loop_var_name, loop_var_id, _carrier_info) =
CommonPatternInitializer::initialize_pattern(
// Phase 179-B: Use PatternPipelineContext for unified preprocessing
use super::pattern_pipeline::{build_pattern_context, PatternVariant};
let ctx = build_pattern_context(
self,
condition,
&self.variable_map,
None, // No carrier exclusions for Pattern 1
body,
PatternVariant::Pattern1,
)?;
// Phase 195: Use unified trace
trace::trace().varmap("pattern1_start", &self.variable_map);
// Phase 171-172: Use LoopScopeShapeBuilder for unified initialization (Issue 4)
// Pattern 1 lowerer ignores the scope anyway, so this is just a placeholder
use super::loop_scope_shape_builder::LoopScopeShapeBuilder;
let scope = LoopScopeShapeBuilder::empty_body_locals(
BasicBlockId(0),
BasicBlockId(0),
BasicBlockId(0),
BasicBlockId(0),
BTreeSet::new(),
);
// Call Pattern 1 lowerer
let join_module = match lower_simple_while_minimal(scope) {
// Call Pattern 1 lowerer with preprocessed scope
let join_module = match lower_simple_while_minimal(ctx.loop_scope) {
Some(module) => module,
None => {
// Phase 195: Use unified trace
@ -91,16 +70,16 @@ impl MirBuilder {
}
};
// Phase 33-22: Create boundary for JoinIR conversion
// Phase 179-B: Create boundary from context
// Phase 201: Use JoinInlineBoundaryBuilder for clean construction
// Canonical Builder pattern - see docs/development/current/main/joinir-boundary-builder-pattern.md
use crate::mir::join_ir::lowering::JoinInlineBoundaryBuilder;
let boundary = JoinInlineBoundaryBuilder::new()
.with_inputs(
vec![ValueId(0)], // JoinIR's main() parameter (loop variable)
vec![loop_var_id], // Host's loop variable
vec![ctx.loop_var_id], // Host's loop variable
)
.with_loop_var_name(Some(loop_var_name.clone())) // Phase 33-16: Enable header PHI generation for SSA correctness
.with_loop_var_name(Some(ctx.loop_var_name.clone())) // Phase 33-16: Enable header PHI generation for SSA correctness
.build();
// Phase 33-22: Use JoinIRConversionPipeline for unified conversion flow