-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.py
executable file
·169 lines (161 loc) · 4.51 KB
/
codegen.py
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
#!/usr/bin/env python
import sys,re
ignoreCmds = frozenset({
('logger', 'log'),
('plugin', 'getPointer'),
('plugin', 'setPointer'),
('plugin', 'addCallback'),
})
id = lambda x:x
ntypes = {
'const char*': ('std::string', 'String', lambda x: x+'.c_str()'),
'float': ('float', 'Float', id),
'double': ('double', 'Double', id),
'int64_t': ('int64_t', 'Int64', id),
'int': ('int32_t', 'Int32', id),
'short': ('int16_t', 'Int16', id),
'unsigned char': ('uint8_t', 'Int8', id),
'char': ('int8_t', 'Int8', id),
'bool': ('bool', 'Bool', id)
}
jtypes = {
'std::string': ('String', 'String', 'String'),
'float': ('float', 'Float', 'Float'),
'double': ('double', 'Double', 'Double'),
'int64_t': ('long', 'Long', 'Long'),
'int32_t': ('int', 'Int', 'Int'),
'int16_t': ('short', 'Short', 'Short'),
'uint8_t': ('short', 'Byte', 'UnsignedByte'),
'int8_t': ('byte', 'Byte', 'Byte'),
'bool': ('boolean', 'Bool', 'Bool')
}
def nCallArg(narg):
if narg[2] == 'out':
return '&'+narg[1]
else:
return narg[0][2](narg[1])
def jDecArg(jarg):
type = jarg[0][0]
if jarg[2] == 'out':
type = 'ByRef_'+type
return type+" "+jarg[1]
f = open(sys.argv[1], 'r');
ncode = open('ncode', 'w');
jcode = open('jcode', 'w');
enum = open('enum', 'w');
lines = f.readlines()
i = 0
while i < len(lines):
m = re.search('^struct\s+(\w+)_pointer_struct$', lines[i])
if m == None:
i+=1
continue
struct = m.group(1)
print "struct: "+struct
i+=1
m = None
while True:
if re.search('};', lines[i]) != None:
i+=1
break
if re.search('#else', lines[i]) != None:
i+=1
while re.search('#endif', lines[i]) == None:
i+=1
m = re.search('^\s*(\w(\w|\s)+?\*?)\s+\(\*(\w+)\)\s*\((.*)\);.*$', lines[i])
if m == None:
i+=1
continue
rawline = m.group(0)
ret = m.group(1)
name = m.group(3)
if (struct, name) in ignoreCmds:
i+=1
continue
args = [[x.strip() for x in aDec.split()] for aDec in m.group(4).split(',')]
args = [(" ".join(x[:-1]), x[-1:][0]) for x in args]
if args == [('', 'void')]:
args = []
args2 = []
for arg in args:
if arg[1][0] == '*':
args2.append((arg[0]+'*', arg[1][1:]))
else:
args2.append(arg)
args = args2
infoline = 'struct="%s", ret="%s", name="%s", args=%s' % (struct,ret,name,args)
print infoline
enumName = struct+'_'+name
enum.write(enumName+',\n')
nins = []
nouts = []
nargs = []
err = False
for arg in args:
if ntypes.has_key(arg[0]):
nins.append((ntypes[arg[0]], arg[1]))
nargs.append((ntypes[arg[0]], arg[1], "in"))
elif arg[0][-1:] == '*' and ntypes.has_key(arg[0][:-1]):
nouts.append((ntypes[arg[0][:-1]], arg[1]))
nargs.append((ntypes[arg[0][:-1]], arg[1], "out"))
else:
txt = 'Unknown argument type: "'+arg[0]+'" for argument "'+arg[1]+'". Not generating code!'
err = True
break
try:
if ret != 'void':
nret = ntypes[ret]
except KeyError as e:
txt = 'Unknown return type: "'+ret+'". Not generating code!'
err = True
if err == True:
comments = '//'+rawline+'\n'
comments += '//'+infoline+'\n'
comments += '//'+txt+'\n\n'
print txt
ncode.write(comments)
jcode.write(comments)
i+=1
continue
jins = [(jtypes[arg[0][0]], arg[1]) for arg in nins]
jouts = [(jtypes[arg[0][0]], arg[1]) for arg in nouts]
jargs = [(jtypes[arg[0][0]], arg[1], arg[2]) for arg in nargs]
if ret != 'void':
jret = jtypes[nret[0]]
nc = " case ClientCommand::"+enumName+":\n"
nc += " {\n"
for narg in nins:
#int32_t type = readInt32();
nc += " %s %s = read%s();\n" % (narg[0][0], narg[1], narg[0][1])
for narg in nouts:
nc += " %s %s;\n" % (narg[0][0], narg[1])
ncc = ", ".join([nCallArg(narg) for narg in nargs])
retdec = ""
if ret != 'void':
retdec = nret[0]+" retval = "
nc += " "+retdec+"ms->"+struct+"."+name+"("+ncc+");\n"
for narg in nouts:
nc += " write%s(%s);\n" % (narg[0][1], narg[1])
if ret != 'void':
nc += " write%s(retval);\n" % nret[1]
nc += " break;\n"
nc += " }\n"
ncode.write(nc)
jdec = ", ".join([jDecArg(jarg) for jarg in jargs])
if ret != 'void':
jretType = jret[0]
else:
jretType = 'void'
jc = " public "+jretType+" "+enumName+"("+jdec+")\n"
jc += " {\n"
jc += " writeInt(ClientCommand."+enumName+".ordinal());\n"
for jarg in jins:
jc += " write%s(%s);\n" % (jarg[0][1], jarg[1])
jc += " flush();\n"
for jarg in jouts:
jc += " %s.value = read%s();\n" % (jarg[1], jarg[0][2])
if ret != 'void':
jc += " return read"+jret[2]+"();\n"
jc += " }\n\n"
jcode.write(jc)
i+=1