Commit Graph

3 Commits

Author SHA1 Message Date
9f2c791f2a fix(json_parser): Eliminate _parse_number() infinite loop with flat control flow
Root cause: Nested-if-in-loop pattern triggered MIR compiler bug
- `if parsing_done == 1 { break }` inside loop caused infinite execution
- Flag variable approach created 2-level nesting that MIR couldn't handle

Fix: Refactored to flat control flow
- Removed `parsing_done` flag variable
- Inverted condition: check exit first (`digit_pos < 0 → break`)
- Direct break without nesting
- Moved `digits` constant outside loop (optimization)

Result:
- Lines: 28 → 15 (-46% complexity)
- Nesting: 2 levels → 1 level
- MIR-safe: No nested-if-in-loop pattern

This unblocks Phase 161-2 full MIR JSON parsing (rep1_if_simple.mir.json 8.5KB)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 20:00:27 +09:00
608693af7f fix(json_parser): Fix infinite loop by working around MIR nested-if bug
🐛 バグ修正: JsonParserBox 無限ループ解消

**問題**:
- `vm step budget exceeded` エラーが発生
- `_parse_number()` で無限ループ

**根本原因**:
- MIR lowering の既知のバグ: ループ内のネスト if-else 文が無限ジャンプチェーンを生成
- bb11 → bb6 → bb4 の無条件ジャンプループ
- PHI 更新が実行されず、ループ変数が更新されない

**修正内容**:
1. `while` → `loop()` 構文に統一(10箇所)
2. ネスト if-else をフラット化(workaround):
   - `_parse_number()`: `ch >= "0" && ch <= "9"` → `digits.indexOf(ch) >= 0`
   - `_parse_string()`: ネスト if を分離
   - `_unescape_string()`: `ch == "\\" && i + 1 < s.length()` をフラット化

**テスト**:
- json_parser.hako: RC 0 
- ネスト if-else バグを回避して正常動作

**注記**:
- 根本修正(MIR lowering bug fix)は別タスク
- workaround で Phase 173 ブロック解除

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 17:42:57 +09:00
cec34d2485 feat(hako_shared): Phase 172 JsonParserBox expansion - Program JSON v0 support
🌐 JSON processing SSOT infrastructure established!

🔧 Implementation:
- parse_program() method added to JsonParserBox (lines 448-463)
- ProgramJSONBox wrapper type for type-safe Program JSON access
- Methods: get_version(), get_kind(), get_defs(), get_meta(), get_usings()
- Validates required fields (version, kind) for Program JSON v0

📊 Reuse candidate survey results:
- compiler.hako (543 lines): Emits Program JSON (not consumer)
- json_loader.hako (51 lines): Utility functions only
- json_v0_reader.hako (142 lines): MIR JSON parser (not Program JSON)
- Finding: No significant Program JSON v0 consumers in current codebase

 Verification:
- JsonParserBox.parse_program/1: Compiled successfully
- ProgramJSONBox methods: All compiled (birth, get_version, get_kind, etc.)
- json_parser.hako: Runs without errors (RC: 0)

🎯 Phase 172 Achievement:
- Established JSON processing standardization foundation
- Infrastructure ready for future selfhost depth-2 integration
- Boxed modularization pattern fully applied (SSOT + gradual extension)

📝 Documentation:
- NEW: phase172_implementation_results.md (comprehensive analysis)
- MOD: CURRENT_TASK.md (Phase 172 completion status)

🏗️ Next Phase (173+):
- to_json() reverse conversion
- Schema validation
- Full selfhost depth-2 JSON unification

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 16:35:34 +09:00