feat(phase22.1): JsonFrag.last_index_of_from() unified search refactor

- Add: JsonFragBox.last_index_of_from(hay, needle, pos) method
  - VM fallback: simple reverse search using substring + lastIndexOf
  - Replaces hand-written lastIndexOf calls in MIR builder

- Refactor: lower_loop_sum_bc_box.hako uses unified method
  - Line 75: Break sentinel backward search
  - Line 113: Continue sentinel backward search
  - Eliminates 2 hand-written lastIndexOf calls

- Test: json_frag_last_index_of_from_canary_vm.sh
  - Loop with break(i==3) and continue(i==2)
  - Expect: 0+1+4 = 5 (skip 2, break at 3)
  - Status: PASS 

Phase 22.1 ultrathink cleanup: code consolidation complete
This commit is contained in:
nyash-codex
2025-11-09 23:56:46 +09:00
parent b0898fcd7b
commit fc5706e3f2
3 changed files with 62 additions and 2 deletions

View File

@ -72,7 +72,7 @@ static box LowerLoopSumBcBox {
local kb = JsonFragBox.index_of_from(s, "\"type\":\"Break\"", k_loop)
if kb >= 0 {
// Find nearest previous Compare and grab rhs Int
local kbc = s.lastIndexOf("\"type\":\"Compare\"", kb)
local kbc = JsonFragBox.last_index_of_from(s, "\"type\":\"Compare\"", kb)
if kbc >= 0 {
// Ensure op=="==" and lhs Var i
local kop = JsonFragBox.index_of_from(s, "\"op\":", kbc); local bop = null; if kop >= 0 { bop = JsonFragBox.read_string_after(s, kop + 5) }
@ -110,7 +110,7 @@ static box LowerLoopSumBcBox {
{
local kc = JsonFragBox.index_of_from(s, "\"type\":\"Continue\"", k_loop)
if kc >= 0 {
local kcc = s.lastIndexOf("\"type\":\"Compare\"", kc)
local kcc = JsonFragBox.last_index_of_from(s, "\"type\":\"Compare\"", kc)
if kcc >= 0 {
local kop2 = JsonFragBox.index_of_from(s, "\"op\":", kcc); local cop = null; if kop2 >= 0 { cop = JsonFragBox.read_string_after(s, kop2 + 5) }
if cop != null && cop == "==" {