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:
@ -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 == "==" {
|
||||
|
||||
@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user