// Mini-VM: function-based entry for branching
// Local static box (duplicated from mini_vm_lib for now to avoid include gate issues)
using selfhost.shared.common.string_helpers as StringHelpers
static box MiniVm {
  read_digits(json, pos) { return StringHelpers.read_digits(json, pos) }
  parse_first_int(json) {
    @key = "\"value\":{\"type\":\"int\",\"value\":"
    @idx = json.lastIndexOf(key)
    if idx < 0 { return "0" }
    @start = idx + key.length()
    return me.read_digits(json, start)
  }
  run_branch(json) {
    @n = me.parse_first_int(json)
    if n == "0" || n == "1" || n == "2" || n == "3" || n == "4" { print("10") return 0 }
    print("20")
    return 0
  }
}

// Program entry: embedded JSON (value=1 → print 10; else → 20)
static box Main {
  main(args) {
    @vm = new MiniVm()
    @json = "{\"kind\":\"Program\",\"statements\":[{\"kind\":\"Print\",\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":1}}}]}"
    // If provided, override by argv[0]
    if args {
      if args.length() > 0 {
        @s = args.get(0)
        if s { json = s }
      }
    }
    return MiniVm.run_branch(json)
  }
}

// Top-level fallback entry for current runner
function main(args) {
  @vm = new MiniVm()
  @json = "{\"kind\":\"Program\",\"statements\":[{\"kind\":\"Print\",\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":1}}}]}"
  if args {
    if args.length() > 0 {
      @s = args.get(0)
      if s { json = s }
    }
  }
  @n = MiniVm.parse_first_int(json)
  if n == "0" || n == "1" || n == "2" || n == "3" || n == "4" { print("10") return 0 }
  print("20")
  return 0
}
