refactor: Extract utilities from control_flow.rs (Phase 6)
- Created utils.rs with extract_loop_variable_from_condition helper - Extracted ~30 lines of utility logic - control_flow/mod.rs now delegates to utils module - All builds pass, no behavior changes
This commit is contained in:
@ -6,6 +6,7 @@
|
|||||||
//! - Phase 3: JoinIR routing (joinir/routing.rs) ✅
|
//! - Phase 3: JoinIR routing (joinir/routing.rs) ✅
|
||||||
//! - Phase 4: Merge implementation (joinir/merge/) ✅
|
//! - Phase 4: Merge implementation (joinir/merge/) ✅
|
||||||
//! - Phase 5: Exception handling (exception/) ✅
|
//! - Phase 5: Exception handling (exception/) ✅
|
||||||
|
//! - Phase 6: Utility functions (utils.rs) ✅
|
||||||
|
|
||||||
use super::ValueId;
|
use super::ValueId;
|
||||||
use crate::ast::ASTNode;
|
use crate::ast::ASTNode;
|
||||||
@ -19,6 +20,9 @@ pub(in crate::mir::builder) mod joinir;
|
|||||||
// Phase 5: Exception handling
|
// Phase 5: Exception handling
|
||||||
pub(in crate::mir::builder) mod exception;
|
pub(in crate::mir::builder) mod exception;
|
||||||
|
|
||||||
|
// Phase 6: Utility functions
|
||||||
|
pub(in crate::mir::builder) mod utils;
|
||||||
|
|
||||||
impl super::MirBuilder {
|
impl super::MirBuilder {
|
||||||
/// Control-flow: block
|
/// Control-flow: block
|
||||||
pub(super) fn cf_block(&mut self, statements: Vec<ASTNode>) -> Result<ValueId, String> {
|
pub(super) fn cf_block(&mut self, statements: Vec<ASTNode>) -> Result<ValueId, String> {
|
||||||
@ -138,35 +142,9 @@ impl super::MirBuilder {
|
|||||||
/// For `i < 3`, extracts `i`.
|
/// For `i < 3`, extracts `i`.
|
||||||
/// For `arr.length() > 0`, extracts `arr`.
|
/// 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<String, String> {
|
fn extract_loop_variable_from_condition(&self, condition: &ASTNode) -> Result<String, String> {
|
||||||
use crate::ast::BinaryOperator;
|
utils::extract_loop_variable_from_condition(condition)
|
||||||
|
|
||||||
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
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Control-flow: throw
|
/// Control-flow: throw
|
||||||
|
|||||||
53
src/mir/builder/control_flow/utils.rs
Normal file
53
src/mir/builder/control_flow/utils.rs
Normal file
@ -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<String, String> {
|
||||||
|
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
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user