Files
hakorune/lang/src/compiler/parser/expr/parser_peek_box.hako
nyash-codex 6a452b2dca fix(mir): PHI検証panic修正 - update_cfg()を検証前に呼び出し
A案実装: debug_verify_phi_inputs呼び出し前にCFG predecessorを更新

修正箇所(7箇所):
- src/mir/builder/phi.rs:50, 73, 132, 143
- src/mir/builder/ops.rs:273, 328, 351

根本原因:
- Branch/Jump命令でsuccessorは即座に更新
- predecessorはupdate_cfg()で遅延再構築
- PHI検証が先に実行されてpredecessor未更新でpanic

解決策:
- 各debug_verify_phi_inputs呼び出し前に
  if let Some(func) = self.current_function.as_mut() {
      func.update_cfg();
  }
  を挿入してCFGを同期

影響: if/else文、論理演算子(&&/||)のPHI生成が正常動作
2025-11-01 13:28:56 +09:00

105 lines
3.2 KiB
Plaintext

// Moved from apps/selfhost-compiler/boxes/parser/expr/parser_peek_box.hako
// ParserPeekBox — peek expression parser (peek <expr> { "label" => <expr>, ..., else => <expr> })
// Responsibility: Parse peek expressions (pattern-matching syntax)
// API: parse(src, i, ctx) -> JSON string
static box ParserPeekBox {
parse(src, i, ctx) {
// ctx is ParserBox for delegation
local j = i
local n = src.length()
// Parse scrutinee expression
local scr = ctx.parse_expr2(src, j)
j = ctx.gpos_get()
j = ctx.skip_ws(src, j)
// Enter arms block
if src.substring(j, j+1) == "{" { j = j + 1 }
j = ctx.skip_ws(src, j)
local arms_json = "["
local first_arm = 1
local else_json = null
local contp = 1
local guardp = 0
local maxp = 400000
loop(contp == 1) {
if guardp > maxp { contp = 0 } else { guardp = guardp + 1 }
j = ctx.skip_ws(src, j)
if j >= n {
contp = 0
} else {
if src.substring(j, j+1) == "}" {
j = j + 1
contp = 0
} else {
// else arm or labeled arm
if ctx.starts_with_kw(src, j, "else") == 1 {
j = j + 4
j = ctx.skip_ws(src, j)
if src.substring(j, j+2) == "=>" { j = j + 2 }
j = ctx.skip_ws(src, j)
// else body may be a block or bare expr
if src.substring(j, j+1) == "{" {
j = j + 1
j = ctx.skip_ws(src, j)
else_json = ctx.parse_expr2(src, j)
j = ctx.gpos_get()
j = ctx.skip_ws(src, j)
if src.substring(j, j+1) == "}" { j = j + 1 }
} else {
else_json = ctx.parse_expr2(src, j)
j = ctx.gpos_get()
}
} else {
// labeled arm: string literal label
if src.substring(j, j+1) != "\"" {
// degrade safely to avoid infinite loop
j = j + 1
continue
}
local label_raw = ctx.read_string_lit(src, j)
j = ctx.gpos_get()
j = ctx.skip_ws(src, j)
if src.substring(j, j+2) == "=>" { j = j + 2 }
j = ctx.skip_ws(src, j)
// arm expr: block or bare expr
local expr_json = "{\"type\":\"Int\",\"value\":0}"
if src.substring(j, j+1) == "{" {
j = j + 1
j = ctx.skip_ws(src, j)
expr_json = ctx.parse_expr2(src, j)
j = ctx.gpos_get()
j = ctx.skip_ws(src, j)
if src.substring(j, j+1) == "}" { j = j + 1 }
} else {
expr_json = ctx.parse_expr2(src, j)
j = ctx.gpos_get()
}
local arm_json = "{\"label\":\"" + ctx.esc_json(label_raw) + "\",\"expr\":" + expr_json + "}"
if first_arm == 1 {
arms_json = arms_json + arm_json
first_arm = 0
} else {
arms_json = arms_json + "," + arm_json
}
}
}
}
}
arms_json = arms_json + "]"
if else_json == null { else_json = "{\"type\":\"Null\"}" }
ctx.gpos_set(j)
return "{\"type\":\"Peek\",\"scrutinee\":" + scr + ",\"arms\":" + arms_json + ",\"else\":" + else_json + "}"
}
}