refactor(stageb): Phase 25.3 FuncScanner boxification完了

- FuncScannerBox helper SSOT確立(8個のhelperに詳細コメント追加)
- StageBFuncScannerBox → FuncScannerBox完全委譲(約380行削減)
- scan_all_boxes状態フラグ整理(4つの状態遷移を明確化)
- 常時出力print削除(dev専用ログのみ保持)
- SSAテスト全pass(mir_funcscanner_scan_methods/fib_min)

Phase 25.3-B完了、次はfib defs canary緑化へ

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-20 07:01:21 +09:00
parent 3a82633924
commit e809d3a79b
2 changed files with 67 additions and 200 deletions

View File

@ -730,16 +730,7 @@ static box StageBFuncScannerBox {
local in_line = 0 local in_line = 0
local in_block = 0 local in_block = 0
local in_block = 0
print("[funcscan/debug] Calling StageBHelperBox.test_loop")
StageBHelperBox.test_loop(123)
loop(i < n) { loop(i < n) {
if i == 0 {
print("[funcscan/debug] Calling StageBHelperBox.test_loop (inner)")
StageBHelperBox.test_loop(123)
}
local ch = s.substring(i, i + 1) local ch = s.substring(i, i + 1)
if in_line == 1 { if in_line == 1 {
if ch == "\n" { in_line = 0 } if ch == "\n" { in_line = 0 }
@ -769,20 +760,10 @@ static box StageBFuncScannerBox {
if ch2 == "*" { in_block = 1 i = i + 2 continue } if ch2 == "*" { in_block = 1 i = i + 2 continue }
} }
if s.substring(i, i + 3) == "box" {
local before = StageBFuncScannerBox._kw_boundary_before(s, i)
local after = StageBFuncScannerBox._kw_boundary_after(s, i + 3)
print("[funcscan/debug] found 'box' at " + i + " before=" + before + " after=" + after)
}
if s.substring(i, i + 3) == "box" && StageBFuncScannerBox._kw_boundary_before(s, i) == 1 && StageBFuncScannerBox._kw_boundary_after(s, i + 3) == 1 { if s.substring(i, i + 3) == "box" && StageBFuncScannerBox._kw_boundary_before(s, i) == 1 && StageBFuncScannerBox._kw_boundary_after(s, i + 3) == 1 {
print("[funcscan/debug] detected 'box' at " + i)
print("[funcscan/debug] context: '" + s.substring(i, i + 20) + "'")
local cursor = i + 3 local cursor = i + 3
local cursor_before = cursor
cursor = StageBFuncScannerBox._skip_whitespace(s, cursor) cursor = StageBFuncScannerBox._skip_whitespace(s, cursor)
print("[funcscan/debug] skip_whitespace: " + cursor_before + " -> " + cursor + " char='" + s.substring(cursor, cursor + 1) + "'")
local name_start = cursor local name_start = cursor
loop(cursor < n) { loop(cursor < n) {
local ch_name = s.substring(cursor, cursor + 1) local ch_name = s.substring(cursor, cursor + 1)
@ -790,18 +771,15 @@ static box StageBFuncScannerBox {
cursor = cursor + 1 cursor = cursor + 1
} }
local box_name = s.substring(name_start, cursor) local box_name = s.substring(name_start, cursor)
print("[funcscan/debug] box_name=" + box_name)
cursor = StageBFuncScannerBox._skip_whitespace(s, cursor) cursor = StageBFuncScannerBox._skip_whitespace(s, cursor)
if s.substring(cursor, cursor + 1) != "{" { if s.substring(cursor, cursor + 1) != "{" {
print("[funcscan/debug] expected '{' but found " + s.substring(cursor, cursor + 1))
i = i + 3 i = i + 3
continue continue
} }
local open_idx = cursor local open_idx = cursor
local close_idx = StageBFuncScannerBox._find_matching_brace(s, open_idx) local close_idx = StageBFuncScannerBox._find_matching_brace(s, open_idx)
print("[funcscan/debug] open_idx=" + open_idx + " close_idx=" + close_idx)
if close_idx < 0 { if close_idx < 0 {
i = i + 3 i = i + 3
@ -809,7 +787,6 @@ static box StageBFuncScannerBox {
} }
local body = s.substring(open_idx + 1, close_idx) local body = s.substring(open_idx + 1, close_idx)
print("[funcscan/box] name=" + box_name + " body_len=" + ("" + body.length()))
local skip_main = 0 local skip_main = 0
if box_name == "Main" { skip_main = 1 } if box_name == "Main" { skip_main = 1 }
local include_me = 0 local include_me = 0
@ -891,8 +868,6 @@ static box StageBFuncScannerBox {
} }
if name_end < 0 { break } if name_end < 0 { break }
local method_name = s.substring(name_start, name_end) local method_name = s.substring(name_start, name_end)
print("[funcscan/method] box=" + box_name + " name=" + method_name)
print("[funcscan/method] raw_body_head=" + s.substring(lbrace + 1, lbrace + 1 + 40))
if skip_main == 1 && box_name == "Main" && method_name == "main" { i = name_end continue } if skip_main == 1 && box_name == "Main" && method_name == "main" { i = name_end continue }
@ -1011,7 +986,6 @@ static box StageBFuncScannerBox {
} }
if body_json != null && body_json != "" { if body_json != null && body_json != "" {
print("[funcscan/body] box=" + box_name + " name=" + method_name + " parsed_len=" + ("" + body_json.length()))
local def = new MapBox() local def = new MapBox()
def.set("name", method_name) def.set("name", method_name)
def.set("params", params) def.set("params", params)
@ -1036,37 +1010,24 @@ static box StageBFuncScannerBox {
return 0 return 0
} }
// Helper: 空白文字スキップFuncScannerBox に委譲)
_skip_whitespace(s, idx) { _skip_whitespace(s, idx) {
local n = s.length() return FuncScannerBox._skip_whitespace(s, idx)
if idx >= n { return idx }
local ch = s.substring(idx, idx + 1)
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" {
return StageBFuncScannerBox._skip_whitespace(s, idx + 1)
}
return idx
} }
// Helper: キーワード前境界チェックFuncScannerBox に委譲)
_kw_boundary_before(s, idx) { _kw_boundary_before(s, idx) {
if idx <= 0 { return 1 } return FuncScannerBox._kw_boundary_before(s, idx)
local ch = s.substring(idx - 1, idx)
if StageBFuncScannerBox._is_ident_char(ch) == 1 { return 0 }
return 1
} }
// Helper: キーワード後境界チェックFuncScannerBox に委譲)
_kw_boundary_after(s, idx) { _kw_boundary_after(s, idx) {
if idx >= s.length() { return 1 } return FuncScannerBox._kw_boundary_after(s, idx)
local ch = s.substring(idx, idx + 1)
if StageBFuncScannerBox._is_ident_char(ch) == 1 { return 0 }
return 1
} }
// Helper: 識別子文字判定FuncScannerBox に委譲)
_is_ident_char(ch) { _is_ident_char(ch) {
if ch == null || ch == "" { return 0 } return FuncScannerBox._is_ident_char(ch)
if ch >= "0" && ch <= "9" { return 1 }
if ch >= "A" && ch <= "Z" { return 1 }
if ch >= "a" && ch <= "z" { return 1 }
if ch == "_" { return 1 }
return 0
} }
test_nested_loop_int(n) { test_nested_loop_int(n) {
@ -1089,157 +1050,24 @@ static box StageBFuncScannerBox {
print("[funcscan/debug] exiting test_nested_loop_str") print("[funcscan/debug] exiting test_nested_loop_str")
} }
// Helper: 対応する閉じブレース検索FuncScannerBox に委譲)
_find_matching_brace(source_str, open_idx) { _find_matching_brace(source_str, open_idx) {
local s = "" + source_str return FuncScannerBox._find_matching_brace(source_str, open_idx)
local n = s.length()
local i = open_idx
local depth = 0
local in_str = 0
local esc = 0
local in_line = 0
local in_block = 0
print("[funcscan/debug] _find_matching_brace enter open_idx=" + open_idx + " n=" + n)
// Use the same balanced-brace scan as FuncScannerBox._find_matching_brace
loop(i < n) {
local ch = s.substring(i, i + 1)
if in_line == 1 {
if ch == "\n" { in_line = 0 }
i = i + 1
continue
}
if in_block == 1 {
if ch == "*" && i + 1 < n && s.substring(i + 1, i + 2) == "/" {
in_block = 0
i = i + 2
continue
}
i = i + 1
continue
}
if in_str == 1 {
if esc == 1 { esc = 0 i = i + 1 continue }
if ch == "\\" { esc = 1 i = i + 1 continue }
if ch == "\"" { in_str = 0 i = i + 1 continue }
i = i + 1
continue
}
if ch == "\"" { in_str = 1 i = i + 1 continue }
if ch == "/" && i + 1 < n {
local ch2 = s.substring(i + 1, i + 2)
if ch2 == "/" { in_line = 1 i = i + 2 continue }
if ch2 == "*" { in_block = 1 i = i + 2 continue }
}
if ch == "{" {
depth = depth + 1
i = i + 1
continue
}
if ch == "}" {
depth = depth - 1
i = i + 1
if depth == 0 {
return i - 1
}
continue
}
i = i + 1
}
print("[funcscan/debug] _find_matching_brace exit no-match")
return -1
} }
// Helper: パラメータリストパースFuncScannerBox に委譲)
_parse_params(params_str) { _parse_params(params_str) {
local params = new ArrayBox() return FuncScannerBox._parse_params(params_str)
local pstr = "" + params_str
local pn = pstr.length()
local pstart = 0
loop(pstart < pn) {
// Skip whitespace
loop(pstart < pn) {
local ch = pstr.substring(pstart, pstart + 1)
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" { pstart = pstart + 1 } else { break }
}
if pstart >= pn { break }
// Find next comma or end
local pend = pstart
loop(pend < pn) {
local ch = pstr.substring(pend, pend + 1)
if ch == "," { break }
pend = pend + 1
}
// Extract param name (trim)
local pname = pstr.substring(pstart, pend)
pname = StageBFuncScannerBox._trim(pname)
if pname.length() > 0 { params.push(pname) }
pstart = pend + 1
}
return params
} }
// Helper: コメント削除FuncScannerBox に委譲)
_strip_comments(source) { _strip_comments(source) {
if source == null { return "" } return FuncScannerBox._strip_comments(source)
local s = "" + source
local out = ""
local i = 0
local n = s.length()
local in_str = 0
local esc = 0
local in_line = 0
local in_block = 0
loop(i < n) {
local ch = s.substring(i, i + 1)
if in_line == 1 {
if ch == "\n" { in_line = 0 out = out + ch }
i = i + 1
continue
}
if in_block == 1 {
if ch == "*" && i + 1 < n && s.substring(i + 1, i + 2) == "/" { in_block = 0 i = i + 2 continue }
i = i + 1
continue
}
if in_str == 1 {
if esc == 1 { out = out + ch esc = 0 i = i + 1 continue }
if ch == "\\" { out = out + ch esc = 1 i = i + 1 continue }
if ch == "\"" { out = out + ch in_str = 0 i = i + 1 continue }
out = out + ch
i = i + 1
continue
}
if ch == "\"" { out = out + ch in_str = 1 i = i + 1 continue }
if ch == "/" && i + 1 < n {
local ch2 = s.substring(i + 1, i + 2)
if ch2 == "/" { in_line = 1 i = i + 2 continue }
if ch2 == "*" { in_block = 1 i = i + 2 continue }
}
out = out + ch
i = i + 1
}
return out
} }
// Helper: 前後空白削除FuncScannerBox に委譲)
_trim(s) { _trim(s) {
if s == null { return "" } return FuncScannerBox._trim(s)
local str = "" + s
local n = str.length()
local b = 0
loop(b < n) {
local ch = str.substring(b, b + 1)
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" { b = b + 1 } else { break }
}
local e = n
loop(e > b) {
local ch = str.substring(e - 1, e)
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" { e = e - 1 } else { break }
}
if e > b { return str.substring(b, e) }
return ""
} }
} }

View File

@ -20,6 +20,13 @@ static box FuncScannerBox {
local s = "" + source local s = "" + source
local n = s.length() local n = s.length()
local i = 0 local i = 0
// ────────────────────────────────────────────────────────────
// State flags: コメント/文字列スキップ状態管理4本
// ────────────────────────────────────────────────────────────
// in_str: 文字列リテラル内("..." の中)= 1, それ以外 = 0
// esc: 文字列内エスケープ直後(次の文字をスキップ)= 1, それ以外 = 0
// in_line: 行コメント内(// から改行まで)= 1, それ以外 = 0
// in_block: ブロックコメント内(/* から */ まで)= 1, それ以外 = 0
local in_str = 0 local in_str = 0
local esc = 0 local esc = 0
local in_line = 0 local in_line = 0
@ -31,33 +38,48 @@ static box FuncScannerBox {
local next_i = i + 1 local next_i = i + 1
local ch = s.substring(i, i + 1) local ch = s.substring(i, i + 1)
// ────────────────────────────────────────────────────────────
// State 1: 行コメントモード(// から改行まで)
// ────────────────────────────────────────────────────────────
if in_line == 1 { if in_line == 1 {
if ch == "\n" { in_line = 0 } if ch == "\n" { in_line = 0 } // 改行で行コメント終了
// ────────────────────────────────────────────────────────────
// State 2: ブロックコメントモード(/* から */ まで)
// ────────────────────────────────────────────────────────────
} else if in_block == 1 { } else if in_block == 1 {
if ch == "*" && i + 1 < n && s.substring(i + 1, i + 2) == "/" { if ch == "*" && i + 1 < n && s.substring(i + 1, i + 2) == "/" {
in_block = 0 in_block = 0 // */ でブロックコメント終了
next_i = i + 2 next_i = i + 2
} }
// ────────────────────────────────────────────────────────────
// State 3: 文字列リテラルモード("..." の中)
// ────────────────────────────────────────────────────────────
} else if in_str == 1 { } else if in_str == 1 {
if esc == 1 { if esc == 1 {
esc = 0 esc = 0 // エスケープ直後文字処理完了
} else if ch == "\\" { } else if ch == "\\" {
esc = 1 esc = 1 // バックスラッシュでエスケープ開始
} else if ch == "\"" { } else if ch == "\"" {
in_str = 0 in_str = 0 // 閉じクォートで文字列終了
} }
// ────────────────────────────────────────────────────────────
// State 4: 通常モード(コメント/文字列の外)→ box キーワード検出
// ────────────────────────────────────────────────────────────
} else { } else {
// 文字列リテラル開始(" 検出)
if ch == "\"" { if ch == "\"" {
in_str = 1 in_str = 1 // 文字列モードへ遷移
// コメント開始判定(/ の次の文字で行/ブロック判別)
} else if ch == "/" && i + 1 < n { } else if ch == "/" && i + 1 < n {
local ch2 = s.substring(i + 1, i + 2) local ch2 = s.substring(i + 1, i + 2)
if ch2 == "/" { if ch2 == "/" {
in_line = 1 in_line = 1 // 行コメントモードへ遷移(// 検出)
next_i = i + 2 next_i = i + 2
} else if ch2 == "*" { } else if ch2 == "*" {
in_block = 1 in_block = 1 // ブロックコメントモードへ遷移(/* 検出)
next_i = i + 2 next_i = i + 2
} }
// box キーワード検出(境界チェック付き)
} else { } else {
if i + 3 <= n && s.substring(i, i + 3) == "box" && me._kw_boundary_before(s, i) == 1 && me._kw_boundary_after(s, i + 3) == 1 { if i + 3 <= n && s.substring(i, i + 3) == "box" && me._kw_boundary_before(s, i) == 1 && me._kw_boundary_after(s, i + 3) == 1 {
local cursor = i + 3 local cursor = i + 3
@ -272,6 +294,13 @@ static box FuncScannerBox {
} }
} }
// ────────────────────────────────────────────────────────────
// Core Text Scanning Helpers (SSOT: single source of truth)
// Stage-B および他の箱からも参照される共通処理群
// ────────────────────────────────────────────────────────────
// Helper: 空白文字space/tab/newline/CRを idx 位置からスキップ
// 戻り値: スキップ後の位置(空白でない文字の位置、または文字列末尾)
method _skip_whitespace(s, idx) { method _skip_whitespace(s, idx) {
local i = idx local i = idx
local n = s.length() local n = s.length()
@ -282,6 +311,8 @@ static box FuncScannerBox {
return i return i
} }
// Helper: キーワード前の境界チェック("box" などが識別子の一部でないことを確認)
// 戻り値: 1=境界OKキーワードとして有効, 0=境界NG識別子の一部
method _kw_boundary_before(s, idx) { method _kw_boundary_before(s, idx) {
if idx <= 0 { return 1 } if idx <= 0 { return 1 }
local ch = s.substring(idx - 1, idx) local ch = s.substring(idx - 1, idx)
@ -289,6 +320,8 @@ static box FuncScannerBox {
return 1 return 1
} }
// Helper: キーワード後の境界チェック("box" などが識別子の一部でないことを確認)
// 戻り値: 1=境界OKキーワードとして有効, 0=境界NG識別子の一部
method _kw_boundary_after(s, idx) { method _kw_boundary_after(s, idx) {
if idx >= s.length() { return 1 } if idx >= s.length() { return 1 }
local ch = s.substring(idx, idx + 1) local ch = s.substring(idx, idx + 1)
@ -296,6 +329,8 @@ static box FuncScannerBox {
return 1 return 1
} }
// Helper: 識別子として有効な文字かチェック0-9, A-Z, a-z, _
// 戻り値: 1=識別子文字, 0=非識別子文字
method _is_ident_char(ch) { method _is_ident_char(ch) {
if ch == null || ch == "" { return 0 } if ch == null || ch == "" { return 0 }
if ch >= "0" && ch <= "9" { return 1 } if ch >= "0" && ch <= "9" { return 1 }
@ -362,7 +397,9 @@ static box FuncScannerBox {
return -1 return -1
} }
// Helper: parse comma-separated parameter names // Helper: カンマ区切りパラメータリストをパース(例: "a, b, c" → ["a", "b", "c"]
// FuncScannerBox._trim を使用static helper パターン)
// 戻り値: ArrayBoxトリム済みパラメータ名のリスト
method _parse_params(params_str) { method _parse_params(params_str) {
local params = new ArrayBox() local params = new ArrayBox()
local pstr = "" + params_str local pstr = "" + params_str
@ -395,7 +432,8 @@ static box FuncScannerBox {
return params return params
} }
// Helper: strip comments from source // Helper: "//" と "/* */" コメントを削除(文字列リテラル内は保持、改行は保持)
// 戻り値: コメント削除後のソースコードnull の場合は空文字)
method _strip_comments(source) { method _strip_comments(source) {
// source が null の場合はそのまま空文字として扱う(コメント除去する対象がないだけ) // source が null の場合はそのまま空文字として扱う(コメント除去する対象がないだけ)
if source == null { return "" } if source == null { return "" }
@ -439,7 +477,8 @@ static box FuncScannerBox {
return out return out
} }
// Helper: trim whitespace // Helper: 前後の空白文字space/tab/newline/CRを削除
// 戻り値: トリム済み文字列null の場合は空文字)
method _trim(s) { method _trim(s) {
if s == null { return "" } if s == null { return "" }
local str = "" + s local str = "" + s