view test/call_suite/mk-cases.lua @ 496:da5232da6270

- test code: rand-sig now spitting out dyncallish sigs (with ')' end delim and rettype at end), purely for consistency
author Tassilo Philipp
date Mon, 21 Mar 2022 18:11:38 +0100
parents 8e22b70d3ee4
children f263eb7a206e
line wrap: on
line source

require"math"
local max = math.max
local maxargs = 0

function trim(l) return l:gsub("^%s+",""):gsub("%s+$","") end
function mkcase(id,sig)
  local sig = trim(sig)
  local h = { "/* ",id,":",sig," */ ",sig:sub(-1), " f", id,"(","" }
  local t = { "" }
  local pos = 0
  maxargs = max(maxargs, #sig-2)
  for i = 1, #sig-2 do 
    pos = tostring(i)
    local name = "a"..pos
    local ch   = sig:sub(i,i)
    h[#h+1] = ch.." "..name
    h[#h+1] = ","
    t[#t+1] = "V_"..ch.."["..pos.."]="..name..";"
  end
  h[#h] = "){"
  t[#t+1] = "ret_"..sig:sub(-1).."("..pos..")}\n"
  return table.concat(h,"")..table.concat(t,"")
end

function mkfuntab(n)
  local s = { "funptr G_funtab[] = {\n"}
  for i = 0, n-1 do
    s[#s+1] = "\t(funptr)&f"..i..",\n"
  end
  s[#s+1] = "};\n"
  return table.concat(s,"")
end

function mksigtab(sigs)
  local s = { "char const * G_sigtab[] = {\n"}
  for k,v in pairs(sigs) do
    s[#s+1] = '\t"'..v:sub(-1)..v:sub(1,-3)..'",\n'
  end
  s[#s+1] = "};\n"
  return table.concat(s,"")
end

function mkall()
  local lineno = 0
  local sigtab = { }
  for line in io.lines() do
    local sig = trim(line)
    io.write(mkcase(lineno,sig))
    sigtab[#sigtab+1] = sig
    lineno = lineno + 1
  end
  io.write(mkfuntab(lineno))
  io.write(mksigtab(sigtab))
  io.write("int G_maxargs = "..maxargs..";\n")
end

mkall()