-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_fortran_zmq_mod.py
executable file
·300 lines (243 loc) · 8.51 KB
/
create_fortran_zmq_mod.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
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
#!/usr/bin/env python3
import sys, re
typesd = {
'void' : 'type(c_ptr)',
'void*' : 'type(c_ptr)',
'char' : 'character(kind=c_signed_char)',
'unsigned char' : 'character(kind=c_char)',
'short' : 'integer(c_short)',
'short int' : 'integer(c_short)',
'int' : 'integer(c_int)',
'long int' : 'integer(c_long)',
'long' : 'integer(c_long)',
'uint8_t' : 'integer(c_int8_t)',
'uint16_t' : 'integer(c_int16_t)',
'int32_t' : 'integer(c_int32_t)',
'size_t' : 'integer(c_size_t)',
}
funchars = r'[\w.,-_/\[\]\(\)\s*]' # excluding some inappropriate chars for fun name and arg list
def breakLine(line):
ll=0
nl=''
for w in line.split():
ll+=len(w)
if ll > 128 :
nl+=" & \n & "+w
ll=len(w)
else:
nl+=w
return nl+"\n"
def getType(typ):
a=None
if typ != None:
a=re.search('\((.*?)\)',typ)
if a != None:
return a.group(1).replace('kind=','')
else:
return None
def processDefines(defines):
cons=''
for define in defines:
dd=define.split()
if dd[1].startswith('__') or dd[1].startswith('ZMQ_MAKE_VERSION') or dd[1] == 'ZMQ_VERSION':
continue
rhs="integer, parameter :: {0:s} = ".format(dd[1])
lh = [ d.replace('(','').replace(')','') for d in dd[2:] ]
lhs=''.join(lh).split('|')
if len(lhs)>1:
c=''
a=''
for i in range(len(lhs)-1):
c+=')'
a+=" ior({0:s},".format(lhs[i])
a+=lhs[-1]+c
ll="{0:s} {1:s}\n".format(rhs,a)
if len(ll)>130:
cons+=breakLine(ll)
else:
cons+=ll
else:
cons+="{0:s} {1:s}\n".format(rhs,lhs[0])
cons+="integer, parameter :: ZMQ_VERSION_K = 10000*ZMQ_VERSION_MAJOR + 100*ZMQ_VERSION_MINOR + ZMQ_VERSION_PATCH\n"
return cons
def listArgs(args):
na=[arg.strip().split()[-1].replace('*','') for arg in args if arg != '']
if na == ['void']: na = ''
return ', '.join(na)
def typeArgs(args, funptrs):
ans=''
imp=[]
h=None
na=[arg.strip().split()[-1] for arg in args if arg != '']
tys=[' '.join(arg.strip().split()[0:-1]) for arg in args if arg != '']
hasSizeT= True if ','.join(tys).find(r'size_t')!=-1 else False
for ty,arg in zip(tys,na):
hasConst= True if ty.find(r'const')!=-1 else False
if hasConst:
ty=ty.replace('const ','')
intent=''
attr=''
### all the stars go to the type
res=re.subn(r'\*','',arg)
narg=res[0]
if res[1] > 0:
nty=ty+'*'*res[1]
else:
nty=ty
if nty[-1]=='*':
if hasConst:
intent=', intent(in)'
if hasSizeT or nty.startswith('char*'):
ast=','.join(re.findall(r'\*',nty))
attr=', dimension({0:s})'.format(ast)
else:
intent=', intent(in)'
attr=', value'
if nty == 'void*':
attr=', value'
nnty=nty.replace('*','')
if nnty not in typesd.keys():
#check if opaque:
if nnty.startswith('struct'):
typ='type({0:s})'.format('c_ptr')
attr=''
else:
typ='type({0:s})'.format(nnty)
else:
typ=typesd[nnty]
if nnty in funptrs:
#typ='type({0:s})'.format('c_funptr')
typ='procedure('+nnty+')'
#attr=', value'
attr = ''
intent=''
h=getType(typ)
ans+=' {0:s}{1:s}{2:s} :: {3:s}\n'.format(typ,intent,attr,narg)
if h is not None:
imp.append(h)
return ans,set(imp)
def processFunctions(exportmark, funexpr, funptrs, blob, indent):
functions = [fun.replace('\n','') for fun in re.findall(r'[\n]'+exportmark+' \s* '+funchars+'*? \s* '+funexpr+' \s* \( '+funchars+'*? \) \s* ; (?isx)', blob)]
funpatrs = re.compile(exportmark+r' \s* ('+funchars+'*?) \s* ('+funexpr+') \s* \( ('+funchars+'*?) \) \s* ; (?isx)')
ans = '\n interface'
for function in functions:
imp=set()
ans+='\n!! {0:s}\n'.format(function)
typ, fname, args = funpatrs.findall(function)[0]
fname = fname.strip('()')
typ = typ.replace('const','').replace(' *','*').strip()
args = args.strip().split(',')
ptrret = ('*' in typ) and (typ != 'void*')
if typ == 'void':
tt='subroutine'
else:
tt='function'
if ptrret:
ffname = fname
fname = fname + '_c'
larg=listArgs(args)
ans+=' '*indent*2 + "{0:s} {1:s}({2:s}) bind(c{3:s})\n".format(tt,fname,larg,(', name="'+ffname+'"') if ptrret else '')
if '*' in typ:
ffun = ' '*indent*2 + "{0:s} {1:s}({2:s})\n".format(tt,ffname,larg)
if larg != '':
#print (fname,larg)
arg,imp=typeArgs(args, funptrs)
imps = imp
z = 'c_ptr' if ptrret else getType(typesd[typ])
imps = (imp|set([z])) if z is not None else imp
ans+=' '*indent*3 + 'import {0:s}\n'.format(', '.join(imps))
if larg != '':
ans+=arg
ftyp = 'type(c_ptr)' if ptrret else typesd[typ]
if typ != 'void':
ans+= ' '*indent*3 + "{0:s} :: {1:s}\n".format(ftyp,fname)
ans+=' '*indent*2 + 'end {0:s} {1:s}\n'.format(tt,fname)
ans+=' '*indent*1 + 'end interface\n'
return ans
def processFunpointers(blob, indent):
funptrs = re.findall(r' \w+ \s* \(\s*(\w+)\s*\) \s* \('+funchars+'*?\) \s* ; (?isx)', blob)
#print (funptrs)
s = processFunctions('typedef', r'\(\w+\)', funptrs, blob, indent)
#print (s)
return funptrs, s
def figure_types(l):
tmp = re.sub(r'[*]\s+(?ixs)','*',l.replace('*', ' *').strip('; ')).split(',')
names = [tmp[0].split()[-1]] + tmp[1:]
size_ptr = re.compile(r' \[.*?\] (?ixs)')
ctype = ' '.join(tmp[0].split()[:-1])
ftype = typesd[ctype] if ctype in typesd else ('*to be def* ' + ctype)
s = ''
for i,name in enumerate(names):
name = name.strip()
bare = size_ptr.sub('',name).strip()
#all this is to legalise illegal in f90 variable names like '_'.
if bare.startswith('_'): bare = 'cfvar%i'%i + bare
if bare.endswith('_'): bare = bare + 'cfvar%i'%i
mo = size_ptr.search(name)
size = ''
if mo != None: size = mo.group(0)
names[i] = bare + size.replace('[','(').replace(']',')')
s += '\n'
s += ftype if not names[i].startswith('*') else 'type(c_ptr)'
s += ' :: '
s += names[i].lstrip('*')
return s.lstrip('\n')
def processStructs(s, indent):
struct_spt = re.compile(r'struct \s* \w* \s* { \s* (.*?) \s* } \s* (\w+) \s*; (?isx)')
structs = struct_spt.findall(s)
fstructs = []
for st in structs:
ft = []
body, name = st
#print (name, body)
ft.append('type, bind(c) :: ' + name)
for l in body.splitlines():
if l.strip().startswith('#'):
ft.append(l)
else:
ft.append(' '*indent + figure_types(l).replace('\n','\n'+' '*indent))
ft.append('end type ' + name)
fstructs.append('\n'.join(ft))
return '\n\n'.join(fstructs)
def clean(s):
s = s.replace("\\\n",'').strip()
s = re.sub(r'// .* (?ixm)', '', s) # delete inline comments
s = re.sub(r'/[*] .*? [*]/ (?ixs)', '', s) # delete multiline comments
s = re.sub(r'\s+ ; (?ix)', ';', s) # bring endline forward
s = re.sub(r'\s+ \[ (?ix)', '[', s) # bring [ forward
i = 0
n = 1
while (n > 0):
s, n = re.subn(r'[*] \s* , (?ix)', '*s%i,'%i, s, count = 1) # put dummy args for *
i += 1
return s
def createmodule():
if len(sys.argv)==2:
inh=sys.argv[1]
else:
inh='/usr/include/zmq.h'
print("No path passed default will be used")
print("process {0:s}".format(inh))
blob = clean(open(inh).read())
lines=blob.split('\n')
defines = [ line for line in lines if line.startswith('#define')]
cons='zmq_constants.F90'
module='zmq.F90'
cF=open(cons,'w')
print(processDefines(defines),file=cF)
cF.close()
indent = 2
head = "module zmq\n use, intrinsic :: iso_c_binding\n implicit none\n include '{0:s}'\n public\n\n".format(cons)
head += ' '*indent + processStructs(blob, indent).replace('\n', '\n'+' '*indent)
# the function finder indents only within its own level
funptrs, funptr_interface = processFunpointers(blob, indent)
head += '\n' + funptr_interface
head += '\n' + processFunctions('ZMQ_EXPORT', r'\w+', funptrs, blob, indent)
head += 'end module\n'
#do not indent preprocessing
head = re.sub(r'^ \s* [#] (?isxm)', '#', head)
mF=open(module,'w')
print(head, file=mF)
mF.close()
if __name__ == '__main__':
createmodule()