diff --git a/src/mir/builder/control_flow/mod.rs b/src/mir/builder/control_flow/mod.rs index 35a5f992..adf6ff65 100644 --- a/src/mir/builder/control_flow/mod.rs +++ b/src/mir/builder/control_flow/mod.rs @@ -6,6 +6,7 @@ //! - Phase 3: JoinIR routing (joinir/routing.rs) ✅ //! - Phase 4: Merge implementation (joinir/merge/) ✅ //! - Phase 5: Exception handling (exception/) ✅ +//! - Phase 6: Utility functions (utils.rs) ✅ use super::ValueId; use crate::ast::ASTNode; @@ -19,6 +20,9 @@ pub(in crate::mir::builder) mod joinir; // Phase 5: Exception handling pub(in crate::mir::builder) mod exception; +// Phase 6: Utility functions +pub(in crate::mir::builder) mod utils; + impl super::MirBuilder { /// Control-flow: block pub(super) fn cf_block(&mut self, statements: Vec) -> Result { @@ -138,35 +142,9 @@ impl super::MirBuilder { /// For `i < 3`, extracts `i`. /// For `arr.length() > 0`, extracts `arr`. /// - /// This is a minimal implementation that handles simple comparison patterns. + /// Delegates to utils::extract_loop_variable_from_condition for implementation. fn extract_loop_variable_from_condition(&self, condition: &ASTNode) -> Result { - use crate::ast::BinaryOperator; - - match condition { - ASTNode::BinaryOp { - operator, left, .. - } if matches!( - operator, - BinaryOperator::Less - | BinaryOperator::Greater - | BinaryOperator::LessEqual - | BinaryOperator::GreaterEqual - ) => - { - // Binary comparison: extract variable from left side - match &**left { - ASTNode::Variable { name, .. } => Ok(name.clone()), - _ => Err(format!( - "[cf_loop/pattern1] Cannot extract loop variable from condition: {:?}", - condition - )), - } - } - _ => Err(format!( - "[cf_loop/pattern1] Unsupported loop condition pattern: {:?}", - condition - )), - } + utils::extract_loop_variable_from_condition(condition) } /// Control-flow: throw diff --git a/src/mir/builder/control_flow/utils.rs b/src/mir/builder/control_flow/utils.rs new file mode 100644 index 00000000..4954a75b --- /dev/null +++ b/src/mir/builder/control_flow/utils.rs @@ -0,0 +1,53 @@ +//! Control flow utility functions. +//! +//! This module provides helper functions for control flow analysis and manipulation. + +use crate::ast::{ASTNode, BinaryOperator}; + +/// Extract loop variable name from condition. +/// +/// # Examples +/// +/// - For `i < 3`, extracts `i` +/// - For `arr.length() > 0`, extracts `arr` +/// +/// # Implementation +/// +/// This is a minimal implementation that handles simple comparison patterns. +/// It looks for binary comparison operators (< > <= >=) and extracts the +/// variable name from the left side of the comparison. +/// +/// # Errors +/// +/// Returns an error if: +/// - The condition is not a binary comparison +/// - The left side of the comparison is not a simple variable +pub(in crate::mir::builder) fn extract_loop_variable_from_condition( + condition: &ASTNode, +) -> Result { + match condition { + ASTNode::BinaryOp { + operator, left, .. + } if matches!( + operator, + BinaryOperator::Less + | BinaryOperator::Greater + | BinaryOperator::LessEqual + | BinaryOperator::GreaterEqual + ) => + { + // Binary comparison: extract variable from left side + match &**left { + ASTNode::Variable { name, .. } => Ok(name.clone()), + _ => Err(format!( + "[cf_loop/pattern1] Cannot extract loop variable from condition: {:?}", + condition + )), + } + } + _ => Err(format!( + "[cf_loop/pattern1] Unsupported loop condition pattern: {:?}", + condition + )), + } +}