-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathc2swift.py
51 lines (40 loc) · 1.58 KB
/
c2swift.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
from pdb import set_trace
import re
def lower1(s): return s[:1].lower() + s[1:]
float_swift = ['Float', 'Double']
float_c = ['float', 'double']
int_swift = ['Int', 'Int32']
int_c = ['long', 'int']
float_types = list(zip(float_swift,float_c))
int_types = list(zip(int_swift,int_c))
type_replace = {'double':'Double', 'float':'Float', 'int':'Int32', 'long':'Int',
'void':'Void', '#':'#', '':''}
type_replace_rev = {v:k for k,v in type_replace.items()}
def word(name): return fr'(?P<{name}>[\w#]+)'
param_re = re.compile(fr'(?P<const>const *)?{word("t")} *(?P<ptr>\*?) *{word("name")}(?P<arr> *\[\])?')
def parse_type(s):
m = param_re.search(s)
if not m: raise Exception(f"Failed to parse: {s}")
name = m.group('name')
is_ptr = m.group('ptr') or m.group('arr')
t = m.group('t')
t = type_replace.get(t,t)
if is_ptr:
if t=='Void': t = 'UnsafeRawPointer'
else:
p_type ='UnsafePointer' if m.group('const') else 'UnsafeMutablePointer'
t = f"{p_type}<{t}>"
return (name,t)
def parse_types(s):
if not s: return {}
s = re.split(r',\s*', s)
return dict([parse_type(o) for o in s])
def test_parse() :
in1 = "const long n,const float a[], double r[], const int N, const float *ex, # f"
ex1 = dict([("n","Int"), ("a","UnsafePointer<Float>"), ("r","UnsafeMutablePointer<Double>"), ("N","Int32"),
("ex","UnsafePointer<Float>"), ("f","#")])
res = parse_types(in1)
for r,exp in zip(res.items(),ex1.items()):
assert r == exp, f'{r}\n{exp}'
print("done")
if __name__=='__main__': test_parse()