diff --git a/lang/src/compiler/parser/parser_box.hako b/lang/src/compiler/parser/parser_box.hako index 9cd1f64b..2b12a4f6 100644 --- a/lang/src/compiler/parser/parser_box.hako +++ b/lang/src/compiler/parser/parser_box.hako @@ -235,6 +235,18 @@ box ParserBox { // === Top-level program parser === parse_program2(src) { + // Shallow recursion guard for Stage‑B / selfhost callers: + // prevent accidental self-recursion in ParserBox.parse_program2. + { + local depth = env.get("HAKO_STAGEB_PARSER_DEPTH") + if depth != null && ("" + depth) != "0" { + print("[stageb/recursion] ParserBox.parse_program2 recursion detected") + // Fail-fast: return an empty Program(JSON) so caller can detect failure. + return "{\"version\":0,\"kind\":\"Program\",\"body\":[]}" + } + env.set("HAKO_STAGEB_PARSER_DEPTH", "1") + } + local trace = me.trace_enabled() // Inline skip_ws to avoid VM bug: method-with-loop called from within loop @@ -355,6 +367,7 @@ box ParserBox { if trace == 1 { print("[parser/trace:program2] done pos=" + ("" + i) + " len=" + ("" + n)) } + env.set("HAKO_STAGEB_PARSER_DEPTH", "0") return "{\"version\":0,\"kind\":\"Program\",\"body\":" + body + "}" } }