// MirCallBox — JSON v1 unified call emitters（薄い箱）
// 目的: v1（mir_call）を第一級に扱う最小APIを集中させる。実行は含まない（emit-only）。

using selfhost.shared.json.mir_builder_min as MirJsonBuilderMin
using lang.compiler.pipeline_v2.regex_flow as RegexFlow
using lang.compiler.emit.common.mir_emit as MirEmitBox
using lang.compiler.emit.common.call_emit as CallEmitBox
using lang.compiler.emit.common.header_emit as HeaderEmitBox
using selfhost.shared.mir.json_emit as JsonEmitBox

static box MirCallBox {
  // Global(name, args:int[])
  emit_call_v1(name, args) {
    if name == null { name = "" }
    if args == null { args = [] }
    local s = "" + args
    local pos = 0
    local n = 0
    local insts = []
    // materialize const args r1..rN
    loop (true) {
      local ds = RegexFlow.digits_from(s, pos)
      if ds == "" { pos = pos + 1 } else { insts.push(MirEmitBox.make_const(1 + n, RegexFlow.to_int(ds)))  n = n + 1  pos = pos + ds.length() }
      if pos >= s.length() { break }
    }
    local dst = n + 1
    // args 1..n
    local arg_ids = new ArrayBox()
    { local i=0  loop(i<n) { arg_ids.push(1+i)  i=i+1 } }
    insts.push(CallEmitBox.make_mir_call_global(name, arg_ids, dst))
    insts.push(MirEmitBox.make_ret(dst))
    local blocks = [HeaderEmitBox.make_block(0, insts)]
    local fns = [HeaderEmitBox.make_function_main(blocks)]
    return JsonEmitBox.to_json(HeaderEmitBox.make_module_with_functions(fns))
  }

  // Method(method, recv:int, args:int[])
  emit_method_v1(method, recv_val, args) {
    if method == null { method = "" }
    if args == null { args = [] }
    local s = "" + args
    local pos = 0
    local n = 0
    local insts = []
    insts.push(MirEmitBox.make_const(1, recv_val))
    // materialize args r2..r(n+1)
    { local i = 0  loop(true) {
        local ds = RegexFlow.digits_from(s, pos)
        if ds == "" { pos = pos + 1 } else { insts.push(MirEmitBox.make_const(2 + i, RegexFlow.to_int(ds)))  i = i + 1  n = i  pos = pos + ds.length() }
        if pos >= s.length() { break }
      }
    }
    local dst = n + 2
    // args 2..(n+1)
    local arg_ids = new ArrayBox()
    { local i=0  loop(i<n) { arg_ids.push(2+i)  i=i+1 } }
    insts.push(CallEmitBox.make_mir_call_method(method, 1, arg_ids, dst))
    insts.push(MirEmitBox.make_ret(dst))
    local blocks = [HeaderEmitBox.make_block(0, insts)]
    local fns = [HeaderEmitBox.make_function_main(blocks)]
    return JsonEmitBox.to_json(HeaderEmitBox.make_module_with_functions(fns))
  }

  // Constructor(class, args:int[])
  emit_newbox_v1(class_name, args) {
    if class_name == null { class_name = "" }
    if args == null { args = [] }
    local s = "" + args
    local pos = 0
    local n = 0
    local insts = []
    // materialize args r1..rN
    { local i = 0  loop(true) {
        local ds = RegexFlow.digits_from(s, pos)
        if ds == "" { pos = pos + 1 } else { insts.push(MirEmitBox.make_const(1 + i, RegexFlow.to_int(ds)))  i = i + 1  n = i  pos = pos + ds.length() }
        if pos >= s.length() { break }
      }
    }
    local dst = n + 1
    // args 1..n
    local arg_ids = new ArrayBox()
    { local i=0  loop(i<n) { arg_ids.push(1+i)  i=i+1 } }
    insts.push(CallEmitBox.make_mir_call_constructor(class_name, arg_ids, dst))
    insts.push(MirEmitBox.make_ret(dst))
    local blocks = [HeaderEmitBox.make_block(0, insts)]
    local fns = [HeaderEmitBox.make_function_main(blocks)]
    return JsonEmitBox.to_json(HeaderEmitBox.make_module_with_functions(fns))
  }
}

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