feat(joinir): Phase 191 body-local init integration into Pattern2
- Integrated LoopBodyLocalInitLowerer into Pattern2 lowering - Fixed ValueId double-allocation issue (delegate to InitLowerer) - Added body_ast parameter to lower_loop_with_break_minimal() - Fixed json_program_loop.rs test for body-local scope - New test: phase191_body_local_atoi.hako (expected: 123) Supported init expressions: - Integer literals, variable references, binary operations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -177,10 +177,100 @@ fn test_number_accumulation_with_body_local() {
|
||||
|
||||
## 成功基準
|
||||
|
||||
- [ ] 代表ループ(body-local 版 `_atoi`)が JoinIR only で期待値を返す
|
||||
- [ ] 既存テスト(phase190_*.hako)が退行しない
|
||||
- [ ] UpdateEnv が body-local 変数を正しく解決できる
|
||||
- [ ] ドキュメントが更新されている
|
||||
- [x] 代表ループ(body-local 版 `_atoi`)が JoinIR only で期待値を返す
|
||||
- [x] 既存テスト(phase190_*.hako)が退行しない
|
||||
- [x] UpdateEnv が body-local 変数を正しく解決できる
|
||||
- [x] ドキュメントが更新されている
|
||||
|
||||
---
|
||||
|
||||
## 実装完了レポート (2025-12-09)
|
||||
|
||||
### 実装概要
|
||||
|
||||
Phase 191 が**完全成功**しました! `LoopBodyLocalInitLowerer` を Pattern2 に統合し、body-local 変数の初期化式を JoinIR に正しく降下できるようになりました。
|
||||
|
||||
### 主な変更
|
||||
|
||||
1. **`loop_with_break_minimal.rs`**:
|
||||
- 関数シグネチャに `body_ast: &[ASTNode]` を追加
|
||||
- `body_local_env: Option<&mut LoopBodyLocalEnv>` に変更(mutable)
|
||||
- ループ body の先頭で `LoopBodyLocalInitLowerer` を呼び出し、初期化式を JoinIR に emit
|
||||
- Carrier update の前に body-local init を配置(正しい依存順序)
|
||||
|
||||
2. **`pattern2_with_break.rs`**:
|
||||
- `collect_body_local_variables` 呼び出しを削除(ValueId の二重割り当てを回避)
|
||||
- 空の `LoopBodyLocalEnv` を作成し、`LoopBodyLocalInitLowerer` に委譲
|
||||
- `lower_loop_with_break_minimal` に `_body` AST を渡すよう修正
|
||||
|
||||
3. **テストファイル**:
|
||||
- `apps/tests/phase191_body_local_atoi.hako` 新規作成
|
||||
- `local digit = i + 1` パターンで body-local 変数を使用
|
||||
- `result = result * 10 + digit` で NumberAccumulation パターン検証
|
||||
|
||||
4. **テスト修正**:
|
||||
- `tests/json_program_loop.rs` の `program_loop_body_local_exit` を修正
|
||||
- スコープ外の body-local 変数参照を削除(正しい動作に修正)
|
||||
|
||||
### 実行結果
|
||||
|
||||
```bash
|
||||
$ ./target/release/hakorune apps/tests/phase191_body_local_atoi.hako
|
||||
123 # ✅ 期待値通り
|
||||
|
||||
$ ./target/release/hakorune apps/tests/phase190_atoi_impl.hako
|
||||
12 # ✅ 退行なし
|
||||
|
||||
$ ./target/release/hakorune apps/tests/phase190_parse_number_impl.hako
|
||||
123 # ✅ 退行なし
|
||||
|
||||
$ cargo test --release json_program_loop
|
||||
test json_loop_simple_verifies ... ok
|
||||
test json_loop_body_local_exit_verifies ... ok
|
||||
test json_loop_with_continue_verifies ... ok
|
||||
# ✅ 全テストパス
|
||||
```
|
||||
|
||||
### 技術的発見
|
||||
|
||||
1. **ValueId 二重割り当て問題**:
|
||||
- 旧実装: `collect_body_local_variables` が ValueId を事前割り当て → `LoopBodyLocalInitLowerer` がスキップ
|
||||
- 解決: 空の `LoopBodyLocalEnv` を渡し、`LoopBodyLocalInitLowerer` に完全委譲
|
||||
|
||||
2. **JoinIR ValueId 空間**:
|
||||
- JoinIR は独立した ValueId 空間を持つ(0 から開始)
|
||||
- Host ValueId へのマッピングは merge 段階で実施
|
||||
- `body_local_start_offset` は Host 空間の概念であり、JoinIR には不要
|
||||
|
||||
3. **UpdateEnv の優先順位**:
|
||||
- ConditionEnv(ループパラメータ・条件変数)が最優先
|
||||
- LoopBodyLocalEnv(body-local 変数)がフォールバック
|
||||
- この順序により、シャドウイングが正しく動作
|
||||
|
||||
### 制限事項
|
||||
|
||||
現在の実装では以下の初期化式のみサポート:
|
||||
- 整数リテラル: `local x = 42`
|
||||
- 変数参照: `local y = i`
|
||||
- 二項演算: `local z = i + 1`, `local w = pos - start`
|
||||
|
||||
未サポート(Fail-Fast):
|
||||
- メソッド呼び出し: `local s = str.substring(...)`
|
||||
- 文字列操作: `local t = s + "abc"`
|
||||
- 複雑な式: ネストした呼び出し、非算術演算
|
||||
|
||||
### 次のステップ
|
||||
|
||||
Phase 191 で以下が達成されました:
|
||||
- [x] Pattern 2 への body-local init lowering 統合
|
||||
- [x] UpdateEnv による body-local 変数解決
|
||||
- [x] NumberAccumulation パターンでの動作検証
|
||||
- [x] 既存テストの退行なし
|
||||
|
||||
今後の拡張:
|
||||
- Pattern 4 (continue) への同様の統合(必要に応じて)
|
||||
- 文字列初期化式のサポート(Phase 188 の延長)
|
||||
- メソッド呼び出し初期化式のサポート(Phase 192+)
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user