-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathclass_gen.lua
547 lines (519 loc) · 16.3 KB
/
class_gen.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
-----------------------------------------------
-- script to build lua classes
-- expects lua 5.1 or luajit
-- expects "../cimgui/generator/definitions.lua" to be generated in cimgui (master_auto2 branch)
-----------------------------------------------
package.path = package.path.."../cimgui/generator/?.lua"
local cpp2ffi = require"cpp2ffi"
--utility functions
function strsplit(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
table.insert(t,cap)
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
elseif str:sub(-1)==pat then
table.insert(t, "")
end
return t
end
function deleteOuterPars(def)
local w = def:match("^%b()$")
if w then
w = w:gsub("^%((.+)%)$","%1")
return w
else
return def
end
end
function CleanImU32(def)
def = def:gsub("%(ImU32%)","")
--quitar () de numeros
def = def:gsub("%((%d+)%)","%1")
def = deleteOuterPars(def)
local bb=strsplit(def,"|")
for i=1,#bb do
local val = deleteOuterPars(bb[i])
if val:match"<<" then
local v1,v2 = val:match("(%d+)%s*<<%s*(%d+)")
val = v1*2^v2
bb[i] = val
end
assert(type(bb[i])=="number")
end
local res = 0
for i=1,#bb do res = res + bb[i] end
return res
end
-------------------------------------------------
-------------------------------------------------
local enumsvalues = {}
--[[ tests
require"anima.utils" --gives us prtable
-- prtable(structs.ImFontConfig)
-- prtable(fundefs.igCombo)
local defaults = {}
for fun,defs in pairs(fundefs) do
for i,def in ipairs(defs) do
for k,v in pairs(def.defaults) do
defaults[v]=true
end
end
end
prtable(defaults)
do return end
--]]
--test correctness of generated lua code
local function testcode(codestr)
local fl,err = loadstring(codestr)
if not fl then
local linenum = err:match(":(%d+):")
linenum = tonumber(linenum)
--print("error on:",linenum,code[linenum])
local lineN,codelines = 1,{}
for line in codestr:gmatch("([^\n\r]*)\r?\n") do
print(lineN, line)
table.insert(codelines, line)
lineN = lineN + 1
end
print("error is ",err)
print("in line: ", codelines[linenum])
error("error in testcode",2)
end
end
--this replaces reserved lua words and not valid tokens
function sanitize_reserved(def)
local words = {["in"]="_in",["repeat"]="_repeat"}
for k,w in pairs(words) do
local pat = "([%(,])("..k..")([,%)])"
if def.call_args:match(pat) then
--print("found",def.cimguiname,def.call_args,def.call_args:match(pat))
def.call_args = def.call_args:gsub(pat,"%1"..w.."%3")
--print(def.call_args)
--sanitize defaults
if def.defaults[k] then
def.defaults[w] = def.defaults[k]
def.defaults[k] = nil
end
end
end
--correct default vals
for k,v in pairs(def.defaults) do
--do only if not a c string
local is_cstring = v:sub(1,1)=='"' and v:sub(-1,-1) =='"'
if not is_cstring then
if v:match"::" then --could be nested enum
local enumname = v:gsub("[%w:]-::([%w]+)","%1")
local ok,val = pcall(cpp2ffi.parse_enum_value,enumname,enumsvalues)
if ok then
def.defaults[k] = val
else
print("deleting default ",v)
def.defaults[k] = nil
end
elseif enumsvalues[v] then
def.defaults[k] = enumsvalues[v]
else
local ok,val = pcall(cpp2ffi.parse_enum_value,v,enumsvalues,true)
if ok then
def.defaults[k] = val
else
--numbers without f in the end
def.defaults[k] = v:gsub("([%d%.%-]+)f","%1")
--+ in front of numbers
def.defaults[k] = def.defaults[k]:gsub("^%+([%d%.%-]+)","%1")
--FLT_MAX
def.defaults[k] = def.defaults[k]:gsub("FLT_MAX","M.FLT_MAX")
def.defaults[k] = def.defaults[k]:gsub("ImDrawCornerFlags_All","lib.ImDrawCornerFlags_All")
def.defaults[k] = def.defaults[k]:gsub("sizeof%((%w+)%)",[[ffi.sizeof("%1")]])
def.defaults[k] = def.defaults[k]:gsub("%(%(void%s*%*%)0%)","nil")
def.defaults[k] = def.defaults[k]:gsub("NULL","nil")
def.defaults[k] = def.defaults[k]:gsub("nullptr","nil")
if def.defaults[k]:match"%(ImU32%)" then
def.defaults[k] = CleanImU32(def.defaults[k])
end
end
--if def.defaults[k]:match"~" then
-- def.defaults[k] = bit.bnot
--end
end
end
end
end
local function make_function(method,def)
if def.skipped then return "" end
if def.is_static_function then method = nil end
sanitize_reserved(def)
local fname = def.ov_cimguiname or def.cimguiname --overloaded or original
local fname_m = method and fname:match(def.stname.."_(.*)") or fname:match("^ig(.*)") or fname --drop struct name part
fname_m = fname_m:match("(.*)_nonUDT$") or fname_m --drop "_nonUDT" suffix
if fname_m == "end" then fname_m = "_end" end
--dump function code
if def.nonUDT == 1 or next(def.defaults) then
local call_args = def.call_args:gsub("%*","")
local code = {}
local args, fname_lua
local empty = call_args:match("^%(%)") --no args
if method and not def.is_static_function then
args = call_args:gsub("^%(","(self"..(empty and "" or ","))
fname_lua = def.stname..":"..fname_m
empty = false
else
args = call_args
fname_lua = "M."..fname_m
end
table.insert(code,"function "..fname_lua..call_args)
--set defaults
cpp2ffi.table_do_sorted(def.defaults, function(k,v)
--for k,v in pairs(def.defaults) do
if v == 'true' then
table.insert(code," if "..k.." == nil then "..k.." = "..v.." end")
else
table.insert(code," "..k.." = "..k.." or "..v)
end
--end
end)
if def.nonUDT == 1 then
--allocate variable for return value
local out_type = def.argsT[1].type:gsub("*", "")
table.insert(code,' local nonUDT_out = ffi.new("'..out_type..'")')
--prepend nonUDT_out to args
args = args:gsub("%(", "(nonUDT_out" .. (empty and "" or ","), 1)
--call cimgui and return value of out variable
table.insert(code," lib."..fname..args)
table.insert(code," return nonUDT_out")
else
--call cimgui
table.insert(code," return lib."..fname..args)
end
table.insert(code,"end")
return table.concat(code,"\n")
end
--for no nonUDT and no defaults
return (method and def.stname or "M").."."..fname_m.." = lib."..fname
end
--struct constructor generator
local function constructor_gen(code,def)
sanitize_reserved(def)
--dump function code
if def.cimguiname == def.ov_cimguiname then --default constructor
local args = (def.call_args == "()") and "(ctype)" or "(ctype,"..def.call_args:sub(2)
table.insert(code,"function "..def.stname..".__new"..args)
else
local name = def.ov_cimguiname:match(def.stname.."_(.*)") --drop struct name part
table.insert(code,"function "..def.stname.."."..name..def.call_args)
end
--set defaults
cpp2ffi.table_do_sorted(def.defaults, function(k,v)
--for k,v in pairs(def.defaults) do
table.insert(code," if "..k.." == nil then "..k.." = "..v.." end")
--end
end)
local fname = def.ov_cimguiname or def.cimguiname
table.insert(code," local ptr = lib."..fname..def.call_args)
table.insert(code," return ffi.gc(ptr,lib."..def.stname.."_destroy)")
table.insert(code,"end")
end
--struct function generator
local function struct_function_gen(code,def)
table.insert(code,make_function(true,def))
end
--top level function generator (ImGui namespace)
local function function_gen(code,def)
table.insert(code,make_function(false,def))
end
local cdefs = dofile("./imgui/cdefs.lua")
local ffi = require"ffi"
ffi.cdef(cdefs)
local function checktype(typ,va)
if ffi.typeof(typ)==ffi.typeof"int" or
ffi.typeof(typ)==ffi.typeof"const int" or
ffi.typeof(typ)==ffi.typeof"float" or
ffi.typeof(typ)==ffi.typeof"double" then
return "(ffi.istype('"..typ.."',"..va..") or type("..va..")=='number')"
elseif ffi.typeof(typ)==ffi.typeof"bool" then
return "(ffi.istype('"..typ.."',"..va..") or type("..va..")=='boolean')"
elseif ffi.typeof(typ)==ffi.typeof"const char*" then
return "(ffi.istype('"..typ.."',"..va..") or type("..va..")=='string')"
elseif ffi.typeof(typ)==ffi.typeof"const float*" then
return "(ffi.istype('"..typ.."',"..va..") or ffi.istype('float[]',"..va.."))"
elseif ffi.typeof(typ)==ffi.typeof"const double*" then
return "(ffi.istype('"..typ.."',"..va..") or ffi.istype('double[]',"..va.."))"
elseif ffi.typeof(typ)==ffi.typeof"int*" then
return "(ffi.istype('"..typ.."',"..va..") or ffi.istype('int[]',"..va.."))"
elseif ffi.typeof(typ)==ffi.typeof"void*" or ffi.typeof(typ)==ffi.typeof"const void*" then
return "ffi.istype('"..typ.."',"..va..")"
else
if typ:match"%*" and not typ:match"%(%*%)" then --pointer not function pointer
local typsinptr = typ:gsub("(%*)","")
local extra = " or ffi.istype('"..typsinptr.."',"..va..")"
local extra2 = " or ffi.istype('"..typsinptr.."[]',"..va..")"
return "(ffi.istype('"..typ.."',"..va..")"..extra..extra2..")"
end
return "ffi.istype('"..typ.."',"..va..")"
end
end
local function gen_args(method,n,minvararg)
local args = ""
local isvararg
if minvararg < math.huge and n >= minvararg then
n = minvararg - 1
isvararg = true
end
if method then
for i=2,n do
args = args.."a"..i..","
end
else
for i=1,n do
args = args.."a"..i..","
end
end
if isvararg then
args = args .. "...,"
end
args = args:sub(1,-2) --drop last
return args
end
--require"anima.utils" --gives us prtable
local function create_generic(code,defs,method)
if defs[1].skipped then return end
if defs[1].is_static_function then
method = nil
end
local methodnotconst = method and not defs[1].constructor
--find max number of arguments
local maxnargs = -1
local minvararg = math.huge
local is_vararg
for i,def in ipairs(defs) do
if #def.argsT> 0 and def.argsT[#def.argsT].type == "..." then
minvararg = minvararg > #def.argsT and #def.argsT or minvararg
--print("...",def.cimguiname,#def.argsT)
end
maxnargs = maxnargs < #def.argsT and #def.argsT or maxnargs
end
is_vararg = minvararg < math.huge
--print("maxnargs",defs[1].cimguiname,maxnargs,minvararg)
--[[
for i,def in ipairs(defs) do
io.write(def.ov_cimguiname," , ")
end
print()
for i=1,maxnargs do
io.write(i," ")
for j,def in ipairs(defs) do
io.write(def.argsT[i] and def.argsT[i].type or "nil")
io.write", "
end
print()
end
--]]
--find first different arg
local keys = {}
local done = {}
local check = {}
for i=1,maxnargs do
keys[i] = {}
for j=1,#defs do
if not done[j] then
local tt = defs[j].argsT[i] and defs[j].argsT[i].type or "nil"
keys[i][tt] = (keys[i][tt] or 0) + 1
end
end
local keycount = 0
for k,v in pairs(keys[i]) do keycount = keycount + 1 end
--print("keycount",i,keycount,#defs)
for j=1,#defs do
if not done[j] then
local tt = defs[j].argsT[i] and defs[j].argsT[i].type or "nil"
if keycount > 1 then
check[j] = check[j] or {}
check[j][i]=tt
end
if keys[i][tt] == 1 then
done[j]= true;
--print(j,defs[j].ov_cimguiname,"done")
end
end
end
end
--cpp2ffi.prtable(keys,done,check)
--do generic--------------
local code2 = {}
--create args
if is_vararg then maxnargs = minvararg-1 end
local args = "" --method and "self," or ""
if methodnotconst then
for i=2,maxnargs do
args = args.."a"..i..","
end
elseif method then --constructor
args = maxnargs==0 and "ctype" or "ctype,"
for i=1,maxnargs do args = args.."a"..i.."," end
else
for i=1,maxnargs do
args = args.."a"..i..","
end
end
if is_vararg then args = args.."...," end
args = args:sub(1,-2) --drop last ,
local fname = defs[1].cimguiname
local fname_e = method and fname:match(defs[1].stname.."_(.*)") or fname:match("^ig(.*)") or fname--drop struct name part
fname = method and (methodnotconst and defs[1].stname..":"..fname_e or defs[1].stname..".__new") or "M."..fname_e
table.insert(code2, "function "..fname.."("..args..") -- generic version")
for i=1,#check do
local chk = check[i]
table.insert(code2,"\n if ")
local addand = false
for k,v in pairs(chk) do
assert(k < minvararg)
if addand then table.insert(code2," and ") end
if v=="nil" then
table.insert(code2,"a"..k.."==nil")
else
local strcode = checktype(v,"a"..k)
--if has a default take nil as valid check
--print("defs[i].defaults[k]",defs[i].ov_cimguiname,defs[i].defaults[defs[i].argsT[k].name])
if defs[i].defaults[defs[i].argsT[k].name]~=nil then
strcode = "("..strcode.." or type(a"..k..")=='nil')"
end
table.insert(code2 , strcode)
---table.insert(code2,"ffi.istype('"..v.."',a"..k..")")
end
addand = true
end
local fname2 = defs[i].ov_cimguiname
local fname2_e = method and fname2:match(defs[1].stname.."_(.*)") or fname2:match("^ig(.*)") or fname2 --drop struct name part
fname2 = method and (methodnotconst and "self:"..fname2_e or defs[1].stname.."."..fname2_e) or "M."..fname2_e
table.insert(code2," then return "..fname2.."("..gen_args(methodnotconst,#defs[i].argsT,minvararg)..") end")
if fname_e == fname2_e then
print("--------error cimguiname equals ov_cimguiname in overloaded function",fname)
--error"cimguiname equals ov_cimguiname"
end
end
table.insert(code2,"\n print("..args..")")
table.insert(code2,"\n error'"..fname.." could not find overloaded'\nend")
table.insert(code, table.concat(code2))
end
--struct code generator
local function code_for_struct(st,fundefs,structs)
local funs = structs[st]
table.sort(funs)
local code = {}
table.insert(code,"--------------------------"..st.."----------------------------")
--declare struct
table.insert(code,"local "..st.."= {}")
table.insert(code,st..".__index = "..st)
for _,f in ipairs(funs) do
local defs = fundefs[f]
for _,def in ipairs(defs) do
if not def.destructor then
if def.constructor then
constructor_gen(code,def)
else
struct_function_gen(code,def)
end
end
end
if #defs > 1 then
create_generic(code,defs,true)
end
end
table.insert(code,[[M.]]..st..[[ = ffi.metatype("]]..st..[[",]]..st..")")
local codestr = table.concat(code,"\n")
--test correctness of code
testcode(codestr)
return codestr
end
--ImGui namespace generator
local function code_for_imguifuns(st,fundefs,structs)
local funs = structs[st]
table.sort(funs)
local code = {}
table.insert(code,"--------------------------"..st.."----------------------------")
for _,f in ipairs(funs) do
local defs = fundefs[f]
for _,def in ipairs(defs) do
def.stname = "M"
function_gen(code,def)
end
--if has overloading create the generic
if #defs > 1 then
create_generic(code, defs)
end
end
local codestr = table.concat(code,"\n")
--test correctness of code
testcode(codestr)
return codestr
end
local function make_enums(sources)
for i,v in ipairs(sources) do
local standenu = dofile([[../]]..v..[[/generator/output/structs_and_enums.lua]])
for k,enu in pairs(standenu.enums) do
for i,v in ipairs(enu) do
assert(v.calc_value)
enumsvalues[v.name] = v.calc_value
end
end
end
end
local function make_funcdefs(sources)
local fundefs = {}
for i,v in ipairs(sources) do
local fundefs1 = dofile([[../]]..v..[[/generator/output/definitions.lua]])
for fun,defs in pairs(fundefs1) do
fundefs[fun] = defs
end
end
return fundefs
end
--------------------------------------------------------------
local function class_gen(sources)
--firs get enumsvalues table
make_enums(sources)
local fundefs = make_funcdefs(sources)
--group them by structs
local structs = {}
local skipped = {}
for fun,defs in pairs(fundefs) do
local stname = defs[1].stname
if not defs[1].templated then
structs[stname] = structs[stname] or {}
table.insert(structs[stname],fun)
else
skipped[stname] = true
end
end
print"---skipped templated structs"
for k,_ in pairs(skipped) do
print(k)
end
--delete templated
structs.ImVec4 = nil
structs.ImVector = nil
structs.ImChunkStream = nil
structs.ImPool = nil
---------------------------------------------------------------
local strout = {}
--Do generation
table.insert(strout,"----------BEGIN_AUTOGENERATED_LUA---------------------------")
cpp2ffi.table_do_sorted(structs, function(struct,funs)
if struct ~= "ImGui" and struct ~= "ImVec2" and struct ~= "" then
table.insert(strout,code_for_struct(struct,fundefs, structs))
end
end)
table.insert(strout,code_for_imguifuns("",fundefs, structs))--("ImGui"))
table.insert(strout,"return M")
table.insert(strout,"----------END_AUTOGENERATED_LUA-----------------------------")
return table.concat(strout,"\n")
end
return class_gen