// terminator_guard_box.hako — TerminatorGuardBox
// Responsibility: Provide a thin, unified guard to prevent emit after terminator.
// Notes: Operates on an instructions ArrayBox of Map nodes with key "op".

using selfhost.shared.common.box_helpers as BoxHelpers

static box TerminatorGuardBox {
  // Return 1 if the given instructions array ends with ret/throw
  block_terminated_insts(insts) {
    if insts == null { return 0 }
    local n = BoxHelpers.array_len(insts)
    if n <= 0 { return 0 }
    local last = BoxHelpers.array_get(insts, n - 1)
    if last == null { return 0 }
    local op = BoxHelpers.map_get(last, "op")
    if op == null { return 0 }
    if op == "ret" || op == "throw" { return 1 }
    return 0
  }

  // Unified message; kept stable for tests
  print_error(opname) {
    if opname == null { opname = "op" }
    print("[ERROR] TerminatorGuard: emit after terminator forbidden (attempt=" + opname + ")")
  }

  // Check before emit; returns 1 if allowed, 0 if blocked (and prints message)
  guard_before_emit_insts(insts, opname) {
    if me.block_terminated_insts(insts) == 1 {
      me.print_error(opname)
      return 0
    }
    return 1
  }
}

static box TerminatorGuardStub { main(args) { return 0 } }
