import re

SRC = open('/tmp/opencode/make_runner.py').read()
shim = re.search(r"shim = r'''(.*?)'''", SRC, re.S).group(1)

extra = r'''
function __V(x)
  local t = type(x)
  if t == "string" then local s=x:sub(1,64):gsub("%c","."); return string.format("str#%d:%q",#x,s)
  elseif t == "number" then return string.format("%.10g", x)
  elseif t == "table" then local n=0 for _ in pairs(x) do n=n+1 if n>100000 then break end end return "tbl#"..#x.."/"..n
  elseif t == "function" then return "fn" end
  return t..":"..tostring(x)
end
local function __g(t,k) local ok,v=pcall(function() return t[k] end); if ok then return v end return nil end
__TAG={}
__NINS={}
local function __nins(O) local n=__NINS[O]; if n then return n end; local o3=__g(O,3); if type(o3)=="table" then n=#o3 else n=-1 end; __NINS[O]=n; return n end
function __MAKETAG(O)
  local n=__nins(O)
  local tag=nil
  if n==140 then tag="N140" end
  if n==31 then tag="N31" end
  for _,f in ipairs({7,8,9}) do
    local t=__g(O,f)
    if type(t)=="table" then
      for k,v in pairs(t) do
        if v==16777619 then tag=(tag and (tag.."+FNVPRIME") or "FNVPRIME") end
        if v==2166136261 then tag=(tag and (tag.."+FNVOFF") or "FNVOFF") end
      end
    end
  end
  if tag then __TAG[O]=tag; __realprint("FTAG", tag, tostring(O), "n", n) end
end
__STACK={} __SD=0 __LASTO=nil __LASTPC=0 __PREV=nil
__CALLEEN=0
function __OP(O,pc,z,a,b,c,d,e,f,regs)
  if O~=__LASTO then
    if pc==1 then
      __SD=__SD+1; __STACK[__SD]=O
      local parent=(__SD>=2) and __TAG[__STACK[__SD-1]] or nil
      local tag=__TAG[O]
      if tag then
        local parts={}
        for i=1,math.min(__SD,12) do parts[#parts+1]=tostring(__nins(__STACK[i]))..(__TAG[__STACK[i]] or "") end
        __realprint("ENTRY", tag, table.concat(parts,">"))
      end
      if parent and parent:find("N140") and __CALLEEN<60 then
        __CALLEEN=__CALLEEN+1
        local ap={}
        if __PREV then for i=1,14 do ap[#ap+1]=i.."="..__V(__PREV[i]) end end
        __realprint("CALLEE", "nins"..tostring(__nins(O)), __TAG[O] or "", table.concat(ap," "))
      end
    else
      while __SD>0 and __STACK[__SD]~=O do __SD=__SD-1 end
    end
    __LASTO=O
  end
  __LASTPC=pc
  __PREV=regs
end
'''

shim += extra

raw = open('/tmp/opencode/stage2.lua').read()
rhex = re.search(r'__RHEX__\t([0-9a-f]+)', open('/tmp/opencode/cap4.txt').read()).group(1)
rbytes = bytes.fromhex(rhex)
rlit = '"' + ''.join('\\x%02x' % b for b in rbytes) + '"'

s = raw
s = 'local R=buffer.fromstring(' + rlit + ');' + s[len('local R=...;'):]
old_factory = '(l)[55]=function(O,m,A)'
assert s.count(old_factory) == 1
s = s.replace(old_factory, '(l)[55]=function(O,m,A)__MAKETAG(O);', 1)

head = 'while true do local Z=o[Q];'
assert s.count(head) == 1
s = s.replace(head, 'while true do local Z=o[Q];__OP(O,Q,Z,t[Q],d[Q],P[Q],y[Q],b[Q],p[Q],q);', 1)

tail = r'''
local __ok,__err = pcall(function()
''' + s + r'''
end)
__realprint("EXEC1", __ok, type(__err))
if __ok then
  local __ok2,__err2 = pcall(__err)
  __realprint("EXEC2", __ok2, tostring(__err2))
end
'''
open('/tmp/opencode/probe_ftag.luau','w').write(shim + tail)
print("wrote")
