Files
hakorune/src/mir/loop_pattern_detection/tests.rs
tomoaki a2c5fd90fe feat(joinir): Phase 188.3 - Pattern6 (NestedLoopMinimal) 選択ロジック実装
## Phase 188.3 進捗: Phase 2 完了 (6/13 tasks)

### 実装完了 

**Phase 1: Fixture作成**
- apps/tests/phase1883_nested_minimal.hako 追加
  - Add/Compare のみ(乗算なし)
  - 期待 exit code: 9 (3×3 nested loops)
- 既存 lowering で fallback 動作確認

**Phase 2: 選択ロジック (SSOT)**
- LoopPatternContext に step_tree_max_loop_depth フィールド追加
- choose_pattern_kind() に Pattern6 選択ロジック実装:
  1. Cheap check (has_inner_loop)
  2. StepTree 構築 (max_loop_depth 取得)
  3. AST validation (is_pattern6_lowerable)
- pattern6_nested_minimal.rs モジュール作成 (stub)
- LOOP_PATTERNS に Pattern6 entry 追加
- **検証**: Pattern6 が正しく選択される 

### 設計原則 (確認済み)

1. **Fail-Fast**: Pattern6 選択後は Ok(None) で逃げない
2. **outer 変数 write-back 検出 → validation false** (Phase 188.4+)
3. **最小実装**: inner local だけ、Pattern1 モデル二重化
4. **cfg! 依存なし**: production で動作

### 検証結果

```
[choose_pattern_kind] has_inner_loop=true
[choose_pattern_kind] max_loop_depth=2
[choose_pattern_kind] is_pattern6_lowerable=true
 Pattern6 SELECTED!
```

Stub からの期待エラー:
```
[ERROR]  [Pattern6] Nested loop lowering not yet implemented
```

### 次: Phase 3 (Lowering 実装 - 推定4時間)

残りタスク:
- Phase 3-1: AST 抽出ヘルパー
- Phase 3-2: Validation ヘルパー
- Phase 3-3: Continuation 生成 (outer_step, inner_step, k_inner_exit)
- Phase 3-4: fixture が exit=9 を返すことを検証

### 変更ファイル

**新規**:
- apps/tests/phase1883_nested_minimal.hako
- src/mir/builder/control_flow/joinir/patterns/pattern6_nested_minimal.rs
- docs/development/current/main/phases/phase-188.{1,2,3}/README.md

**変更**:
- src/mir/builder/control_flow/joinir/routing.rs (Pattern6 選択)
- src/mir/builder/control_flow/joinir/patterns/router.rs (Context 拡張)
- src/mir/builder/control_flow/joinir/patterns/mod.rs (module 宣言)

🎯 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-27 05:45:12 +09:00

265 lines
7.4 KiB
Rust

use super::*;
use crate::ast::{ASTNode, BinaryOperator, LiteralValue, Span};
use crate::mir::loop_pattern_detection::LoopFeatures;
fn span() -> Span {
Span::unknown()
}
fn var(name: &str) -> ASTNode {
ASTNode::Variable {
name: name.to_string(),
span: span(),
}
}
fn lit_i(n: i64) -> ASTNode {
ASTNode::Literal {
value: LiteralValue::Integer(n),
span: span(),
}
}
fn bin(op: BinaryOperator, left: ASTNode, right: ASTNode) -> ASTNode {
ASTNode::BinaryOp {
operator: op,
left: Box::new(left),
right: Box::new(right),
span: span(),
}
}
fn assignment(target: ASTNode, value: ASTNode) -> ASTNode {
ASTNode::Assignment {
target: Box::new(target),
value: Box::new(value),
span: span(),
}
}
fn has_continue(node: &ASTNode) -> bool {
match node {
ASTNode::Continue { .. } => true,
ASTNode::If {
then_body,
else_body,
..
} => {
then_body.iter().any(has_continue)
|| else_body
.as_ref()
.map_or(false, |b| b.iter().any(has_continue))
}
ASTNode::Loop { body, .. } => body.iter().any(has_continue),
_ => false,
}
}
fn has_break(node: &ASTNode) -> bool {
match node {
ASTNode::Break { .. } => true,
ASTNode::If {
then_body,
else_body,
..
} => {
then_body.iter().any(has_break)
|| else_body
.as_ref()
.map_or(false, |b| b.iter().any(has_break))
}
ASTNode::Loop { body, .. } => body.iter().any(has_break),
_ => false,
}
}
fn has_if(body: &[ASTNode]) -> bool {
body.iter().any(|n| matches!(n, ASTNode::If { .. }))
}
fn carrier_count(body: &[ASTNode]) -> usize {
fn count(nodes: &[ASTNode]) -> usize {
let mut c = 0;
for n in nodes {
match n {
ASTNode::Assignment { .. } => c += 1,
ASTNode::If {
then_body,
else_body,
..
} => {
c += count(then_body);
if let Some(else_body) = else_body {
c += count(else_body);
}
}
_ => {}
}
}
c
}
if count(body) > 0 {
1
} else {
0
}
}
fn classify_body(body: &[ASTNode]) -> LoopPatternKind {
let has_continue_flag = body.iter().any(has_continue);
let has_break_flag = body.iter().any(has_break);
let features = LoopFeatures {
has_break: has_break_flag,
has_continue: has_continue_flag,
has_if: has_if(body),
has_if_else_phi: false,
carrier_count: carrier_count(body),
break_count: if has_break_flag { 1 } else { 0 },
continue_count: if has_continue_flag { 1 } else { 0 },
is_infinite_loop: false, // テストでは通常ループを想定
update_summary: None,
..Default::default() // Phase 188.1: Use default for new fields
};
classify(&features)
}
#[test]
fn pattern1_simple_while_is_detected() {
// loop(i < len) { i = i + 1 }
let body = vec![assignment(
var("i"),
bin(BinaryOperator::Add, var("i"), lit_i(1)),
)];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern1SimpleWhile);
}
#[test]
fn pattern2_break_loop_is_detected() {
// loop(i < len) { if i > 0 { break } i = i + 1 }
let cond = bin(BinaryOperator::Greater, var("i"), lit_i(0));
let body = vec![
ASTNode::If {
condition: Box::new(cond),
then_body: vec![ASTNode::Break { span: span() }],
else_body: None,
span: span(),
},
assignment(var("i"), bin(BinaryOperator::Add, var("i"), lit_i(1))),
];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern2Break);
}
#[test]
fn parse_number_like_loop_is_classified_as_pattern2() {
// loop(p < len) {
// if digit_pos < 0 { break }
// p = p + 1
// }
let break_cond = bin(BinaryOperator::Less, var("digit_pos"), lit_i(0));
let body = vec![
ASTNode::If {
condition: Box::new(break_cond),
then_body: vec![ASTNode::Break { span: span() }],
else_body: None,
span: span(),
},
assignment(var("p"), bin(BinaryOperator::Add, var("p"), lit_i(1))),
];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern2Break);
}
#[test]
fn pattern3_if_sum_shape_is_detected() {
// loop(i < len) { if i % 2 == 1 { sum = sum + 1 } i = i + 1 }
let cond = bin(
BinaryOperator::Equal,
bin(BinaryOperator::Modulo, var("i"), lit_i(2)),
lit_i(1),
);
let body = vec![
ASTNode::If {
condition: Box::new(cond),
then_body: vec![assignment(
var("sum"),
bin(BinaryOperator::Add, var("sum"), lit_i(1)),
)],
else_body: None,
span: span(),
},
assignment(var("i"), bin(BinaryOperator::Add, var("i"), lit_i(1))),
];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern3IfPhi);
}
#[test]
fn pattern4_continue_loop_is_detected() {
// loop(i < len) { if (i % 2 == 0) { continue } sum = sum + i; i = i + 1 }
let cond = bin(
BinaryOperator::Equal,
bin(BinaryOperator::Modulo, var("i"), lit_i(2)),
lit_i(0),
);
let body = vec![
ASTNode::If {
condition: Box::new(cond),
then_body: vec![ASTNode::Continue { span: span() }],
else_body: Some(vec![assignment(
var("sum"),
bin(BinaryOperator::Add, var("sum"), var("i")),
)]),
span: span(),
},
assignment(var("i"), bin(BinaryOperator::Add, var("i"), lit_i(1))),
];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern4Continue);
}
#[test]
fn test_atoi_loop_classified_as_pattern2() {
// Phase 246-EX Step 1: _atoi loop pattern classification
// loop(i < len) {
// local ch = s.substring(i, i+1)
// local digit_pos = digits.indexOf(ch)
// if digit_pos < 0 { break }
// result = result * 10 + digit_pos
// i = i + 1
// }
// Simplified: loop with break + two carrier updates
let break_cond = bin(BinaryOperator::Less, var("digit_pos"), lit_i(0));
// result = result * 10 + digit_pos (NumberAccumulation pattern)
let mul_expr = bin(BinaryOperator::Multiply, var("result"), lit_i(10));
let result_update = assignment(
var("result"),
bin(BinaryOperator::Add, mul_expr, var("digit_pos")),
);
// i = i + 1
let i_update = assignment(var("i"), bin(BinaryOperator::Add, var("i"), lit_i(1)));
let body = vec![
ASTNode::If {
condition: Box::new(break_cond),
then_body: vec![ASTNode::Break { span: span() }],
else_body: None,
span: span(),
},
result_update,
i_update,
];
let kind = classify_body(&body);
assert_eq!(
kind,
LoopPatternKind::Pattern2Break,
"_atoi loop should be classified as Pattern2 (Break) due to if-break structure"
);
}