stage-b (P0): stabilize entry — compiler_stageb.hako now emits Stage‑1 Program(JSON v0) directly (one-line), avoiding heavy MIR path; FlowEntry prefers v1→v0 first; noisy debug prints in pipeline with_usings gated. Quick core/stageb canaries PASS.

This commit is contained in:
nyash-codex
2025-11-02 07:22:40 +09:00
parent 4ee61b1477
commit 7180579cf8
3 changed files with 27 additions and 17 deletions

View File

@ -1,7 +1,6 @@
// Stage-B compiler entry — ParserBox → FlowEntry emit-only
using lang.compiler.parser.box as ParserBox
using lang.compiler.pipeline_v2.flow_entry as FlowEntryBox
static box StageBMain {
// Minimal StageB driver: parse args/env, extract main body if wrapped in `box Main { static method main(){...} }`,
@ -68,13 +67,11 @@ static box StageBMain {
if body_src == null { body_src = src }
// 5) Parse and emit MIR(JSON v0)
// 5) Parse and emit Stage1 JSON v0 (Program)
// Bridge(JSON v0) が Program v0 を受け取り MIR に lowering するため、ここでは AST(JSON v0) を出力する。
// 既定で MIR 直出力は行わない(重い経路を避け、一行出力を保証)。
local ast_json = p.parse_program2(body_src)
local prefer = 1
local out = FlowEntryBox.emit_v0_from_ast_with_context(ast_json, prefer, usings_json, null, externs_json)
if out == null || out == "" { out = FlowEntryBox.emit_v0_from_ast(ast_json, prefer) }
// TTL OFF: do not fallback to dummy JSON when emit fails. Empty output surfaces failure to caller.
print(out)
print(ast_json)
return 0
}
}

View File

@ -16,6 +16,13 @@ static box FlowEntryBox {
// Emit v0 with optional using / extern metadataStageB entry
emit_v0_from_ast_with_context(ast_json, prefer_cfg, usings_json, modules_json, externs_json) {
// 1) Prefer lightweight v1→v0 経路(簡単な return/binop/compare/call/new はここで完結)
{
local j1 = PipelineV2.lower_stage1_to_mir_v1(ast_json, prefer_cfg)
if j1 != null { return MirJsonV1Adapter.to_v0(j1) }
}
// 2) using 情報があれば using 解決つきの v0 経路を試す
local used_with_usings = 0
local out = null
if usings_json != null && usings_json != "" && usings_json != "[]" {
@ -25,9 +32,10 @@ static box FlowEntryBox {
if out == null {
out = PipelineV2.lower_stage1_to_mir(ast_json, prefer_cfg)
}
// 3) externs が指定され、まだ v1 経路を使っていない場合は v1+meta → v0 を試す
if externs_json != null && externs_json != "" && externs_json != "[]" && used_with_usings == 0 {
local j1 = PipelineV2.lower_stage1_to_mir_v1_with_meta(ast_json, prefer_cfg, externs_json)
if j1 != null { out = MirJsonV1Adapter.to_v0(j1) }
local j2 = PipelineV2.lower_stage1_to_mir_v1_with_meta(ast_json, prefer_cfg, externs_json)
if j2 != null { out = MirJsonV1Adapter.to_v0(j2) }
}
return out
}

View File

@ -457,10 +457,15 @@ flow PipelineV2 {
}
// Overload: resolve names via UsingResolverBox before emit
lower_stage1_to_mir_with_usings(ast_json, prefer_cfg, usings_json, modules_json) {
// Debug logs are noisy: emit only when prefer_cfg is a large debug value (>=9)
local _dbg = 0
if prefer_cfg != null { if (""+prefer_cfg) == "9" { _dbg = 1 } }
if _dbg == 1 {
print("[DEBUG] === lower_stage1_to_mir_with_usings ENTRY ===")
print("[DEBUG] ast_json length=" + ast_json.length())
print("[DEBUG] usings_json=" + usings_json)
print("[DEBUG] modules_json=" + modules_json)
}
if ast_json == null { return PipelineV2.lower_stage1_to_mir(ast_json, prefer_cfg) }
// Build resolver context
local r = UsingResolverBox.state_new()
@ -468,7 +473,7 @@ flow PipelineV2 {
if modules_json != null { UsingResolverBox.load_modules_json(r, modules_json) }
// Upgrade alias→namespace mapping now that modules are loaded (ModuleFirst)
UsingResolverBox.upgrade_aliases(r)
print("[DEBUG] upgrade_aliases complete")
if _dbg == 1 { print("[DEBUG] upgrade_aliases complete") }
// Prefer Call/Method/New branches with name normalization; otherwise fallback to default
{
@ -479,12 +484,12 @@ flow PipelineV2 {
local scan0 = Stage1JsonScannerBox.extract_name_args(ast_json, kq)
if scan0 != null {
local raw_head = scan0.get("name")
print("[DEBUG] preflight raw_head=" + raw_head)
if _dbg == 1 { print("[DEBUG] preflight raw_head=" + raw_head) }
local dot = RegexFlow.find_from(raw_head, ".", 0)
if dot >= 0 {
local head = raw_head.substring(0, dot)
local resolved = UsingResolverBox.resolve_namespace_alias(r, head)
print("[DEBUG] preflight head=" + head + " resolved=" + resolved)
if _dbg == 1 { print("[DEBUG] preflight head=" + head + " resolved=" + resolved) }
if resolved == null {
print("[ERROR] Unresolved using alias: " + head)
return null
@ -514,7 +519,7 @@ flow PipelineV2 {
if kc != null {
local kn = NormalizerBox.normalize_call_ints(kc)
if kn == null {
print("[DEBUG] normalize_call_ints returned null")
if _dbg == 1 { print("[DEBUG] normalize_call_ints returned null") }
return null
}
local raw_name = kn.get("name")