feat(joinir): Phase 247-EX - DigitPos dual-value architecture
Extends DigitPos promotion to generate TWO carriers for Pattern A/B support: - Boolean carrier (is_digit_pos) for break conditions - Integer carrier (digit_value) for NumberAccumulation ## Implementation 1. **DigitPosPromoter** (loop_body_digitpos_promoter.rs) - Generates dual carriers: is_<var> (bool) + <base>_value (int) - Smart naming: "digit_pos" → "digit" (removes "_pos" suffix) 2. **UpdateEnv** (update_env.rs) - Context-aware promoted variable resolution - Priority: <base>_value (int) → is_<var> (bool) → standard - Pass promoted_loopbodylocals from CarrierInfo 3. **Integration** (loop_with_break_minimal.rs) - UpdateEnv constructor updated to pass promoted list ## Test Results - **Before**: 925 tests PASS - **After**: 931 tests PASS (+6 new tests, 0 failures) ## New Tests - test_promoted_variable_resolution_digit_pos - Full dual-value - test_promoted_variable_resolution_fallback_to_bool - Fallback - test_promoted_variable_not_a_carrier - Error handling ## Impact | Pattern | Before | After | |---------|--------|-------| | _parse_number | ✅ Works (bool only) | ✅ Works (bool used, int unused) | | _atoi | ❌ Failed (missing int) | ✅ READY (int carrier available!) | 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -1,9 +1,18 @@
|
||||
//! Phase 184: Update Expression Environment
|
||||
//! Phase 247-EX: Extended with promoted variable resolution for dual-value carriers
|
||||
//!
|
||||
//! This module provides a unified variable resolution layer for carrier update expressions.
|
||||
//! It combines ConditionEnv (condition variables) and LoopBodyLocalEnv (body-local variables)
|
||||
//! with clear priority order.
|
||||
//!
|
||||
//! ## Phase 247-EX: Promoted Variable Resolution
|
||||
//!
|
||||
//! For promoted LoopBodyLocal variables (e.g., `digit_pos` → `is_digit_pos` + `digit_value`):
|
||||
//! - When resolving `digit_pos` in update expressions (e.g., `result = result * 10 + digit_pos`)
|
||||
//! - Try `<var>_value` first (e.g., `digit_value`)
|
||||
//! - Then fall back to `is_<var>` (boolean carrier, less common in updates)
|
||||
//! - Finally fall back to standard resolution
|
||||
//!
|
||||
//! ## Design Philosophy
|
||||
//!
|
||||
//! **Single Responsibility**: This module ONLY handles variable resolution priority logic.
|
||||
@ -49,7 +58,7 @@ use crate::mir::ValueId;
|
||||
/// ```ignore
|
||||
/// let condition_env = /* ... i, sum ... */;
|
||||
/// let body_local_env = /* ... temp ... */;
|
||||
/// let update_env = UpdateEnv::new(&condition_env, &body_local_env);
|
||||
/// let update_env = UpdateEnv::new(&condition_env, &body_local_env, &[]);
|
||||
///
|
||||
/// // Resolve "sum" → ConditionEnv (priority 1)
|
||||
/// assert_eq!(update_env.resolve("sum"), Some(ValueId(X)));
|
||||
@ -60,6 +69,17 @@ use crate::mir::ValueId;
|
||||
/// // Resolve "unknown" → None
|
||||
/// assert_eq!(update_env.resolve("unknown"), None);
|
||||
/// ```
|
||||
///
|
||||
/// # Phase 247-EX Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// // digit_pos promoted → is_digit_pos (bool) + digit_value (i64)
|
||||
/// let promoted = vec!["digit_pos".to_string()];
|
||||
/// let update_env = UpdateEnv::new(&condition_env, &body_local_env, &promoted);
|
||||
///
|
||||
/// // Resolve "digit_pos" in NumberAccumulation → digit_value (integer carrier)
|
||||
/// assert_eq!(update_env.resolve("digit_pos"), Some(ValueId(X))); // digit_value
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
pub struct UpdateEnv<'a> {
|
||||
/// Condition variable environment (priority 1)
|
||||
@ -67,6 +87,10 @@ pub struct UpdateEnv<'a> {
|
||||
|
||||
/// Body-local variable environment (priority 2)
|
||||
body_local_env: &'a LoopBodyLocalEnv,
|
||||
|
||||
/// Phase 247-EX: List of promoted LoopBodyLocal variable names
|
||||
/// For these variables, resolve to <var>_value carrier instead of is_<var>
|
||||
promoted_loopbodylocals: &'a [String],
|
||||
}
|
||||
|
||||
impl<'a> UpdateEnv<'a> {
|
||||
@ -76,22 +100,28 @@ impl<'a> UpdateEnv<'a> {
|
||||
///
|
||||
/// * `condition_env` - Condition variable environment (highest priority)
|
||||
/// * `body_local_env` - Body-local variable environment (fallback)
|
||||
/// * `promoted_loopbodylocals` - Phase 247-EX: List of promoted variable names
|
||||
pub fn new(
|
||||
condition_env: &'a ConditionEnv,
|
||||
body_local_env: &'a LoopBodyLocalEnv,
|
||||
promoted_loopbodylocals: &'a [String],
|
||||
) -> Self {
|
||||
Self {
|
||||
condition_env,
|
||||
body_local_env,
|
||||
promoted_loopbodylocals,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a variable name to JoinIR ValueId
|
||||
///
|
||||
/// Resolution order:
|
||||
/// 1. Try condition_env.get(name)
|
||||
/// 2. If not found, try body_local_env.get(name)
|
||||
/// 3. If still not found, return None
|
||||
/// Resolution order (Phase 247-EX extended):
|
||||
/// 1. If name is in promoted_loopbodylocals:
|
||||
/// a. Try condition_env.get("<name>_value") // Integer carrier for accumulation
|
||||
/// b. If not found, try condition_env.get("is_<name>") // Boolean carrier (rare in updates)
|
||||
/// 2. Try condition_env.get(name)
|
||||
/// 3. If not found, try body_local_env.get(name)
|
||||
/// 4. If still not found, return None
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
@ -101,7 +131,52 @@ impl<'a> UpdateEnv<'a> {
|
||||
///
|
||||
/// * `Some(ValueId)` - Variable found in one of the environments
|
||||
/// * `None` - Variable not found in either environment
|
||||
///
|
||||
/// # Phase 247-EX Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// // digit_pos promoted → is_digit_pos + digit_value
|
||||
/// // When resolving "digit_pos" in update expr:
|
||||
/// env.resolve("digit_pos") → env.get("digit_value") → Some(ValueId(X))
|
||||
/// ```
|
||||
pub fn resolve(&self, name: &str) -> Option<ValueId> {
|
||||
// Phase 247-EX: Check if this is a promoted variable
|
||||
if self.promoted_loopbodylocals.iter().any(|v| v == name) {
|
||||
// Phase 247-EX: Naming convention - "digit_pos" → "digit_value" (not "digit_pos_value")
|
||||
// Extract base name: "digit_pos" → "digit", "pos" → "pos"
|
||||
let base_name = if name.ends_with("_pos") {
|
||||
&name[..name.len() - 4] // Remove "_pos" suffix
|
||||
} else {
|
||||
name
|
||||
};
|
||||
|
||||
// Priority 1a: Try <base>_value (integer carrier for NumberAccumulation)
|
||||
let int_carrier_name = format!("{}_value", base_name);
|
||||
if let Some(value_id) = self.condition_env.get(&int_carrier_name) {
|
||||
eprintln!(
|
||||
"[update_env/phase247ex] Resolved promoted '{}' → '{}' (integer carrier): {:?}",
|
||||
name, int_carrier_name, value_id
|
||||
);
|
||||
return Some(value_id);
|
||||
}
|
||||
|
||||
// Priority 1b: Try is_<name> (boolean carrier, less common in updates)
|
||||
let bool_carrier_name = format!("is_{}", name);
|
||||
if let Some(value_id) = self.condition_env.get(&bool_carrier_name) {
|
||||
eprintln!(
|
||||
"[update_env/phase247ex] Resolved promoted '{}' → '{}' (boolean carrier): {:?}",
|
||||
name, bool_carrier_name, value_id
|
||||
);
|
||||
return Some(value_id);
|
||||
}
|
||||
|
||||
eprintln!(
|
||||
"[update_env/phase247ex] WARNING: Promoted variable '{}' not found as carrier ({} or {})",
|
||||
name, int_carrier_name, bool_carrier_name
|
||||
);
|
||||
}
|
||||
|
||||
// Standard resolution (Phase 184)
|
||||
self.condition_env
|
||||
.get(name)
|
||||
.or_else(|| self.body_local_env.get(name))
|
||||
@ -149,7 +224,8 @@ mod tests {
|
||||
// Condition variables should be found first
|
||||
let cond_env = test_condition_env();
|
||||
let body_env = LoopBodyLocalEnv::new(); // Empty
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![]; // Phase 247-EX: No promoted variables
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
assert_eq!(update_env.resolve("i"), Some(ValueId(10)));
|
||||
assert_eq!(update_env.resolve("sum"), Some(ValueId(20)));
|
||||
@ -161,7 +237,8 @@ mod tests {
|
||||
// Body-local variables should be found when not in condition env
|
||||
let cond_env = ConditionEnv::new(); // Empty
|
||||
let body_env = test_body_local_env();
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
assert_eq!(update_env.resolve("temp"), Some(ValueId(50)));
|
||||
assert_eq!(update_env.resolve("digit"), Some(ValueId(60)));
|
||||
@ -176,7 +253,8 @@ mod tests {
|
||||
let mut body_env = LoopBodyLocalEnv::new();
|
||||
body_env.insert("x".to_string(), ValueId(200)); // Body-local: x=200
|
||||
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
// Should resolve to condition env value (100), not body-local (200)
|
||||
assert_eq!(update_env.resolve("x"), Some(ValueId(100)));
|
||||
@ -187,7 +265,8 @@ mod tests {
|
||||
// Variable not in either environment → None
|
||||
let cond_env = test_condition_env();
|
||||
let body_env = test_body_local_env();
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
assert_eq!(update_env.resolve("unknown"), None);
|
||||
assert_eq!(update_env.resolve("nonexistent"), None);
|
||||
@ -198,7 +277,8 @@ mod tests {
|
||||
// Mixed lookup: some in condition, some in body-local
|
||||
let cond_env = test_condition_env();
|
||||
let body_env = test_body_local_env();
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
// Condition variables
|
||||
assert_eq!(update_env.resolve("i"), Some(ValueId(10)));
|
||||
@ -216,7 +296,8 @@ mod tests {
|
||||
fn test_contains() {
|
||||
let cond_env = test_condition_env();
|
||||
let body_env = test_body_local_env();
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
assert!(update_env.contains("i"));
|
||||
assert!(update_env.contains("temp"));
|
||||
@ -228,7 +309,8 @@ mod tests {
|
||||
// Both environments empty
|
||||
let cond_env = ConditionEnv::new();
|
||||
let body_env = LoopBodyLocalEnv::new();
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
assert_eq!(update_env.resolve("anything"), None);
|
||||
assert!(!update_env.contains("anything"));
|
||||
@ -239,10 +321,60 @@ mod tests {
|
||||
// Test diagnostic accessor methods
|
||||
let cond_env = test_condition_env();
|
||||
let body_env = test_body_local_env();
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env);
|
||||
let promoted: Vec<String> = vec![];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
// Should return references to underlying environments
|
||||
assert_eq!(update_env.condition_env().len(), 3);
|
||||
assert_eq!(update_env.body_local_env().len(), 2);
|
||||
}
|
||||
|
||||
// Phase 247-EX: Test promoted variable resolution (dual-value carriers)
|
||||
#[test]
|
||||
fn test_promoted_variable_resolution_digit_pos() {
|
||||
// Test case: digit_pos promoted → is_digit_pos (bool) + digit_value (i64)
|
||||
// Naming: "digit_pos" → "is_digit_pos" + "digit_value" (base_name="_pos" removed)
|
||||
let mut cond_env = ConditionEnv::new();
|
||||
|
||||
// Register both carriers in ConditionEnv
|
||||
cond_env.insert("is_digit_pos".to_string(), ValueId(100)); // Boolean carrier
|
||||
cond_env.insert("digit_value".to_string(), ValueId(200)); // Integer carrier (digit_pos → digit)
|
||||
|
||||
let body_env = LoopBodyLocalEnv::new();
|
||||
let promoted: Vec<String> = vec!["digit_pos".to_string()];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
// When resolving "digit_pos" in update expr → should get digit_value (integer carrier)
|
||||
assert_eq!(update_env.resolve("digit_pos"), Some(ValueId(200)));
|
||||
|
||||
// Direct carrier access still works
|
||||
assert_eq!(update_env.resolve("is_digit_pos"), Some(ValueId(100)));
|
||||
assert_eq!(update_env.resolve("digit_value"), Some(ValueId(200)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_promoted_variable_resolution_fallback_to_bool() {
|
||||
// Test case: Only boolean carrier exists (integer carrier missing)
|
||||
let mut cond_env = ConditionEnv::new();
|
||||
cond_env.insert("is_pos".to_string(), ValueId(150)); // Only boolean carrier
|
||||
|
||||
let body_env = LoopBodyLocalEnv::new();
|
||||
let promoted: Vec<String> = vec!["pos".to_string()];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
// Should fall back to is_pos (boolean carrier)
|
||||
assert_eq!(update_env.resolve("pos"), Some(ValueId(150)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_promoted_variable_not_a_carrier() {
|
||||
// Test case: Variable in promoted list but no carrier exists
|
||||
let cond_env = ConditionEnv::new(); // Empty
|
||||
let body_env = LoopBodyLocalEnv::new();
|
||||
let promoted: Vec<String> = vec!["missing_var".to_string()];
|
||||
let update_env = UpdateEnv::new(&cond_env, &body_env, &promoted);
|
||||
|
||||
// Should return None (with warning logged)
|
||||
assert_eq!(update_env.resolve("missing_var"), None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user