Files
hakorune/local_tests/phase161/test_rep1_inline.hako

165 lines
4.9 KiB
Plaintext
Raw Normal View History

feat(phase161): Add MirAnalyzerBox implementation (Phase 161-2 基本実装) Task先生による Phase 161-2 実装成果: **tools/hako_shared/mir_analyzer.hako** (375行) - MirAnalyzerBox: MIR JSON v1 パーサー&アナライザー - Core Methods: - birth(mirJsonText): JSON パース&キャッシュ - validateSchema(): MIR v1 構造検証 - summarize_function(funcIndex): メタデータ抽出 - count_phis(funcIndex): PHI 命令検出 - count_loops(funcIndex): CFG backward edge によるループ検出 **テストインフラ** - test_mir_analyzer.hako: テストハーネスフレームワーク - test_rep1_inline.hako: インラインテスト (rep1_if_simple) - rep1_if_simple.mir.json: MIR JSON テストデータ (8.5KB) - rep2_loop_simple.mir.json: ループパターンテストデータ (9.6KB) **箱理論適用** - 箱化: MirAnalyzerBox = MIR 分析専任(単一責務) - 境界: JsonParserBox との完全分離 - Fail-Fast: 明示的エラー、フォールバック無し - 遅延SG: _functions キャッシュ、オンデマンド計算 **発見された課題** - JsonParserBox._parse_number() 無限ループ問題(次タスクで対処) - VM ステップ予算超過でフル MIR JSON テスト一時ブロック Status: Phase 161-2 80%完了(コア実装OK、テスト検証はJsonParser修正後) Next: _parse_number() 修正 → Phase 161-2 テスト完了 → Phase 161-3 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 19:55:55 +09:00
// local_tests/phase161/test_rep1_inline.hako
// Inline test for rep1_if_simple MIR analysis
static box Main {
main(args) {
print("Phase 161-2: Testing MirAnalyzerBox on rep1_if_simple")
print("=======================================================")
// Minimal MIR JSON for testing (simplified from actual rep1)
local mir_json = '{"capabilities":["unified_call","phi","effects","callee_typing"],"functions":[{"name":"Main.main/0","params":[],"entry":7,"blocks":[{"id":7,"instructions":[{"op":"const","dst":2,"value":{"type":"i64","value":5}},{"op":"const","dst":5,"value":{"type":"i64","value":3}},{"op":"compare","operation":">","lhs":9,"rhs":10,"dst":8},{"op":"branch","cond":12,"then":8,"else":9}]},{"id":8,"instructions":[{"op":"const","dst":18,"value":{"type":"i64","value":10}},{"op":"jump","target":10}]},{"id":9,"instructions":[{"op":"const","dst":24,"value":{"type":"i64","value":20}},{"op":"jump","target":10}]},{"id":10,"instructions":[{"op":"phi","dst":30,"incoming":[[18,8],[24,9]]},{"op":"ret","value":32}]}]}]}'
// Parse and analyze
print("")
print("Step 1: Creating MirAnalyzerBox...")
local analyzer = new MirAnalyzerBox(mir_json)
if analyzer == null {
print("FAIL: Failed to create analyzer")
return 1
}
print("SUCCESS: MirAnalyzerBox created")
// Test 1: Validate schema
print("")
print("Step 2: Validating schema...")
local valid = analyzer.validateSchema()
if valid == 1 {
print("SUCCESS: Schema is valid")
} else {
print("FAIL: Schema validation failed")
return 1
}
// Test 2: Summarize function
print("")
print("Step 3: Summarizing function 0...")
local summary = analyzer.summarize_function(0)
if summary == null {
print("FAIL: summarize_function returned null")
return 1
}
local name = summary.get("name")
local blocks = summary.get("block_count")
local instructions = summary.get("instruction_count")
local has_phi = summary.get("has_phis")
local has_if = summary.get("has_ifs")
local has_loop = summary.get("has_loops")
print("Function name: " + name)
print("Blocks: " + blocks)
print("Instructions: " + instructions)
print("Has PHI: " + has_phi)
print("Has If: " + has_if)
print("Has Loop: " + has_loop)
// Verify expected values
local pass_count = 0
if name == "Main.main/0" {
print(" [OK] Function name correct")
pass_count = pass_count + 1
} else {
print(" [FAIL] Function name mismatch")
}
if blocks == 4 {
print(" [OK] Block count correct (4)")
pass_count = pass_count + 1
} else {
print(" [FAIL] Block count mismatch (expected 4, got " + blocks + ")")
}
if has_phi == 1 {
print(" [OK] PHI detected")
pass_count = pass_count + 1
} else {
print(" [FAIL] PHI not detected")
}
if has_if == 1 {
print(" [OK] If detected")
pass_count = pass_count + 1
} else {
print(" [FAIL] If not detected")
}
if has_loop == 0 {
print(" [OK] No loop (correct)")
pass_count = pass_count + 1
} else {
print(" [FAIL] Loop incorrectly detected")
}
// Test 3: Count PHI instructions
print("")
print("Step 4: Counting PHI instructions...")
local phi_list = analyzer.count_phis(0)
if phi_list == null {
print("FAIL: count_phis returned null")
return 1
}
local phi_count = phi_list.size()
print("PHI count: " + phi_count)
if phi_count == 1 {
print(" [OK] Correct PHI count (1)")
pass_count = pass_count + 1
local phi = phi_list.get(0)
feat(phase161): Add MirAnalyzerBox implementation (Phase 161-2 基本実装) Task先生による Phase 161-2 実装成果: **tools/hako_shared/mir_analyzer.hako** (375行) - MirAnalyzerBox: MIR JSON v1 パーサー&アナライザー - Core Methods: - birth(mirJsonText): JSON パース&キャッシュ - validateSchema(): MIR v1 構造検証 - summarize_function(funcIndex): メタデータ抽出 - count_phis(funcIndex): PHI 命令検出 - count_loops(funcIndex): CFG backward edge によるループ検出 **テストインフラ** - test_mir_analyzer.hako: テストハーネスフレームワーク - test_rep1_inline.hako: インラインテスト (rep1_if_simple) - rep1_if_simple.mir.json: MIR JSON テストデータ (8.5KB) - rep2_loop_simple.mir.json: ループパターンテストデータ (9.6KB) **箱理論適用** - 箱化: MirAnalyzerBox = MIR 分析専任(単一責務) - 境界: JsonParserBox との完全分離 - Fail-Fast: 明示的エラー、フォールバック無し - 遅延SG: _functions キャッシュ、オンデマンド計算 **発見された課題** - JsonParserBox._parse_number() 無限ループ問題(次タスクで対処) - VM ステップ予算超過でフル MIR JSON テスト一時ブロック Status: Phase 161-2 80%完了(コア実装OK、テスト検証はJsonParser修正後) Next: _parse_number() 修正 → Phase 161-2 テスト完了 → Phase 161-3 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 19:55:55 +09:00
local block_id = phi.get("block_id")
local dest = phi.get("dest")
local incoming_count = phi.get("incoming_count")
print(" PHI details: block=" + block_id + " dest=r" + dest + " incoming=" + incoming_count)
if block_id == 10 {
print(" [OK] PHI in correct block (10)")
pass_count = pass_count + 1
}
if incoming_count == 2 {
print(" [OK] Correct incoming count (2)")
pass_count = pass_count + 1
}
} else {
print(" [FAIL] Incorrect PHI count (expected 1, got " + phi_count + ")")
}
// Test 4: Count loops
print("")
print("Step 5: Counting loops...")
local loop_count = analyzer.count_loops(0)
print("Loop count: " + loop_count)
if loop_count == 0 {
print(" [OK] No loops detected (correct)")
pass_count = pass_count + 1
} else {
print(" [FAIL] Loops incorrectly detected (expected 0, got " + loop_count + ")")
}
// Final results
print("")
print("=======================================================")
print("Test Results: " + pass_count + " / 9 checks passed")
print("=======================================================")
if pass_count == 9 {
print("SUCCESS: All tests passed!")
return 0
} else {
print("PARTIAL: Some tests failed")
return 1
}
}
}