// lower_if_then_else_following_return_box.hako
// Pattern: Program body = [ If(cond=Compare(Int,Int), then=[Return(Int)] (no else)), Return(Int) ]
// Lowers to: bb0: const lhs/rhs, compare, branch; bb1: const then, ret; bb2: const else, ret

using selfhost.shared.json.utils.json_frag as JsonFragBox
using "hako.mir.builder.internal.pattern_util" as PatternUtilBox

static box LowerIfThenElseFollowingReturnBox {
  try_lower(program_json) {
    local s = "" + program_json
    // If with Compare(Int,Int)
    local k_if = JsonFragBox.index_of_from(s, "\"type\":\"If\"", 0)
    if k_if < 0 { return null }
    local k_cmp = JsonFragBox.index_of_from(s, "\"type\":\"Compare\"", k_if)
    if k_cmp < 0 { return null }
    local k_op = JsonFragBox.index_of_from(s, "\"op\":", k_cmp); if k_op < 0 { return null }
    local sym = JsonFragBox.read_string_after(s, k_op + 5); if sym == null { return null }
    local op = PatternUtilBox.map_cmp(sym); if op == null { return null }
    // LHS/RHS ints
    local klhs = JsonFragBox.index_of_from(s, "\"lhs\":{", k_cmp); if klhs < 0 { return null }
    local ti1 = JsonFragBox.index_of_from(s, "\"type\":\"Int\"", klhs); if ti1 < 0 { return null }
    { local kv = JsonFragBox.index_of_from(s, "\"value\":", ti1); if kv < 0 { return null }; local lhs_val = JsonFragBox.read_int_after(s, kv + 8); if lhs_val == null { return null } }
    local krhs = JsonFragBox.index_of_from(s, "\"rhs\":{", k_cmp); if krhs < 0 { return null }
    local ti2 = JsonFragBox.index_of_from(s, "\"type\":\"Int\"", krhs); if ti2 < 0 { return null }
    { local kv2 = JsonFragBox.index_of_from(s, "\"value\":", ti2); if kv2 < 0 { return null }; local rhs_val = JsonFragBox.read_int_after(s, kv2 + 8); if rhs_val == null { return null } }
    // then: Return(Int)
    local kth = JsonFragBox.index_of_from(s, "\"then\":", k_if); if kth < 0 { return null }
    local rt = JsonFragBox.index_of_from(s, "\"type\":\"Return\"", kth); if rt < 0 { return null }
    local ti3 = JsonFragBox.index_of_from(s, "\"type\":\"Int\"", rt); if ti3 < 0 { return null }
    { local kv3 = JsonFragBox.index_of_from(s, "\"value\":", ti3); if kv3 < 0 { return null }; local then_val = JsonFragBox.read_int_after(s, kv3 + 8); if then_val == null { return null } }
    // else is omitted → following Return(Int) in Program body
    local k_after = JsonFragBox.index_of_from(s, "\"type\":\"Return\"", k_if + 1); if k_after < 0 { return null }
    local ti4 = JsonFragBox.index_of_from(s, "\"type\":\"Int\"", k_after); if ti4 < 0 { return null }
    { local kv4 = JsonFragBox.index_of_from(s, "\"value\":", ti4); if kv4 < 0 { return null }; local else_val = JsonFragBox.read_int_after(s, kv4 + 8); if else_val == null { return null } }

    // Emit MIR(JSON v0) as string for VM friendliness
    local mir = "{\"functions\":[{\"name\":\"main\",\"params\":[],\"locals\":[],\"blocks\":[" +
      "{\"id\":0,\"instructions\":[{\"op\":\"const\",\"dst\":1,\"value\":{\"type\":\"i64\",\"value\":" + lhs_val + "}}," +
      "{\"op\":\"const\",\"dst\":2,\"value\":{\"type\":\"i64\",\"value\":" + rhs_val + "}}," +
      "{\"op\":\"compare\",\"operation\":\"" + op + "\",\"lhs\":1,\"rhs\":2,\"dst\":3}," +
      "{\"op\":\"branch\",\"cond\":3,\"then\":1,\"else\":2}]}," +
      "{\"id\":1,\"instructions\":[{\"op\":\"const\",\"dst\":4,\"value\":{\"type\":\"i64\",\"value\":" + then_val + "}},{\"op\":\"ret\",\"value\":4}]}," +
      "{\"id\":2,\"instructions\":[{\"op\":\"const\",\"dst\":5,\"value\":{\"type\":\"i64\",\"value\":" + else_val + "}},{\"op\":\"ret\",\"value\":5}]}]}]}"
    return mir
  }
}
