comparison test/callback_suite/mk-cases.lua @ 505:049e04af13c8

test/callback_suite: - greatly simplified - refactored to look more like other test cases (especially call_suite{,_aggrs} for consistency/maintainablity/future code sharing
author Tassilo Philipp
date Sat, 09 Apr 2022 13:53:58 +0200
parents
children 5a3c07a0f376
comparison
equal deleted inserted replaced
504:f263eb7a206e 505:049e04af13c8
1 require "config"
2
3 function trim(l) return l:gsub("^%s+",""):gsub("%s+$","") end
4 function mkcase(id,sig)
5 local nargs = string.len(sig) - 2 -- @@@STRUCT wrong, b/c ignores callconv prefixes
6 local rtype = string.sub(sig, nargs + 2, nargs + 2)
7 local s = "F" .. nargs .. "(f" .. id .. "," .. rtype
8 for i = 1, nargs do
9 local type = string.sub(sig, i, i)
10 s = s .. "," .. type
11 end
12 s = s .. ")\n"
13 return s
14 end
15
16 -- @@@STRUCT same as in call_suite{,_aggrs}, share?
17 function mkfuntab(n)
18 local s = { "funptr G_funtab[] = {\n"}
19 for i = 0, n-1 do
20 s[#s+1] = "\t&f"..i..",\n"
21 end
22 s[#s+1] = "};\n"
23 return table.concat(s,"")
24 end
25
26 -- @@@STRUCT same as in call_suite{,_aggrs}, share?
27 function mksigtab(sigs)
28 local s = { "const char * G_sigtab[] = {\n"}
29 for k,v in pairs(sigs) do
30 s[#s+1] = '\t"'..v..'",\n'
31 end
32 s[#s+1] = "};\n"
33 return table.concat(s,"")
34 end
35
36 function mkall()
37 -- force minargs for ordered mode @@@STRUCT why?
38 if mode == "ordered" then
39 minargs = 0
40 end
41
42 -- case macros
43 local i
44 for i = minargs, maxargs do
45 local line = "#define F" .. i .. "(ID,R"
46 local argdef = ""
47 local argset = ""
48 if i > 0 then
49 line = line .. ","
50 local j
51 for j = 0, i-1 do
52 if j > 0 then
53 argdef = argdef .. ","
54 argset = argset .. ","
55 end
56 argdef = argdef .. "M" .. j
57 argset = argset .. "M[" .. j .. "].M" .. j
58 end
59 end
60 line = line .. argdef .. ") void ID(void* addr) { Result.R = ((CONFIG_API R(*)(" .. argdef .. "))addr)(" .. argset .. ");}\n"
61 io.write(line)
62 end
63
64 -- cases
65 local lineno = 0
66 local sigtab = { }
67 local cases = ''
68 for line in io.lines() do
69 local sig = trim(line)
70 cases = cases..mkcase(lineno,sig)
71 sigtab[#sigtab+1] = sig
72 lineno = lineno + 1
73 end
74
75 io.write(cases)
76 io.write(mkfuntab(lineno))
77 io.write(mksigtab(sigtab))
78 io.write("int G_maxargs = "..maxargs..";\n")
79 end
80
81 mkall()
82