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

@ -124,6 +124,20 @@ static box JsonFragBox {
if idx < 0 { return -1 }
return p2 + idx
}
last_index_of_from(hay, needle, pos) {
// VM fallback: reverse search from pos backwards to start
if hay == null || needle == null { return -1 }
local s = "" + hay
local n = s.length()
local p2 = pos
if p2 < 0 { return -1 }
if p2 >= n { p2 = n - 1 }
// Extract substring from 0 to pos (inclusive)
local substr = s.substring(0, p2 + 1)
// Find last occurrence of needle in substring
local idx = substr.lastIndexOf(needle)
return idx
}
read_digits(text, pos) { return StringHelpers.read_digits(text, pos) }
_str_to_int(s) { return StringHelpers.to_i64(s) }
_to_bool10(ch) { if ch == "t" { return 1 } if ch == "f" { return 0 } return null }