5
5
import os
6
6
import re
7
7
8
- select_re = re .compile (" LAPACK_(\w)_SELECT(\d)" )
8
+ select_re = re .compile (' LAPACK_(\w)_SELECT(\d)' )
9
9
10
10
def is_const (name , cty ):
11
- return " *const" in cty
11
+ return ' *const' in cty
12
12
13
13
def is_mut (name , cty ):
14
- return " *mut" in cty
14
+ return ' *mut' in cty
15
15
16
16
def is_scalar (name , cty , f ):
17
17
return (
18
- " c_char" in cty or
18
+ ' c_char' in cty or
19
19
name in [
20
- " abnrm" ,
21
- " abstol" ,
22
- " amax" ,
23
- " anorm" ,
24
- " bbnrm" ,
25
- " colcnd" ,
26
- " dif" ,
27
- " ihi" ,
28
- "il" ,
29
- " ilo" ,
30
- " info" ,
31
- " iter" ,
32
- "iu" ,
33
- "l" ,
34
- " liwork" ,
35
- " lrwork" ,
36
- " lwork" ,
37
- "m" ,
38
- "mm" ,
39
- "n" ,
40
- " n_err_bnds" ,
41
- "nb" ,
42
- " nrhs" ,
43
- " rank" ,
44
- " rcond" ,
45
- " rowcnd" ,
46
- " rpvgrw" ,
47
- " sdim" ,
48
- " tryrac" ,
49
- "vu" ,
20
+ ' abnrm' ,
21
+ ' abstol' ,
22
+ ' amax' ,
23
+ ' anorm' ,
24
+ ' bbnrm' ,
25
+ ' colcnd' ,
26
+ ' dif' ,
27
+ ' ihi' ,
28
+ 'il' ,
29
+ ' ilo' ,
30
+ ' info' ,
31
+ ' iter' ,
32
+ 'iu' ,
33
+ 'l' ,
34
+ ' liwork' ,
35
+ ' lrwork' ,
36
+ ' lwork' ,
37
+ 'm' ,
38
+ 'mm' ,
39
+ 'n' ,
40
+ ' n_err_bnds' ,
41
+ 'nb' ,
42
+ ' nrhs' ,
43
+ ' rank' ,
44
+ ' rcond' ,
45
+ ' rowcnd' ,
46
+ ' rpvgrw' ,
47
+ ' sdim' ,
48
+ ' tryrac' ,
49
+ 'vu' ,
50
50
] or
51
- name == "q" and 'lapack_int' in cty or
51
+ name == 'q' and 'lapack_int' in cty or
52
52
not (
53
53
'geev' in f .name or
54
54
'tgsna' in f .name or
55
55
'trsna' in f .name
56
56
) and name in [
57
- "vl" ,
58
- "vr" ,
57
+ 'vl' ,
58
+ 'vr' ,
59
59
] or
60
60
not ('tgevc' in f .name ) and name in [
61
- "p" ,
61
+ 'p' ,
62
62
] or
63
- name .startswith (" alpha" ) or
64
- name .startswith (" beta" ) or
65
- name .startswith (" inc" ) or
66
- name .startswith ("k" ) or
67
- name .startswith ("ld" ) or
68
- name .startswith (" tol" ) or
69
- name .startswith (" vers" )
63
+ name .startswith (' alpha' ) or
64
+ name .startswith (' beta' ) or
65
+ name .startswith (' inc' ) or
66
+ name .startswith ('k' ) or
67
+ name .startswith ('ld' ) or
68
+ name .startswith (' tol' ) or
69
+ name .startswith (' vers' )
70
70
)
71
71
72
72
def translate_argument (name , cty , f ):
@@ -75,123 +75,123 @@ def translate_argument(name, cty, f):
75
75
if is_scalar (name , cty , f ):
76
76
return base
77
77
else :
78
- return " &[{}]" .format (base )
78
+ return ' &[{}]' .format (base )
79
79
elif is_mut (name , cty ):
80
80
base = translate_type_base (cty , f )
81
81
if is_scalar (name , cty , f ):
82
- return " &mut {}" .format (base )
82
+ return ' &mut {}' .format (base )
83
83
else :
84
- return " &mut [{}]" .format (base )
84
+ return ' &mut [{}]' .format (base )
85
85
86
86
m = select_re .match (cty )
87
87
if m is not None :
88
88
if m .group (1 ) == 'S' :
89
- return " Select{}F32" .format (m .group (2 ))
89
+ return ' Select{}F32' .format (m .group (2 ))
90
90
elif m .group (1 ) == 'D' :
91
- return " Select{}F64" .format (m .group (2 ))
91
+ return ' Select{}F64' .format (m .group (2 ))
92
92
elif m .group (1 ) == 'C' :
93
- return " Select{}C32" .format (m .group (2 ))
93
+ return ' Select{}C32' .format (m .group (2 ))
94
94
elif m .group (1 ) == 'Z' :
95
- return " Select{}C64" .format (m .group (2 ))
95
+ return ' Select{}C64' .format (m .group (2 ))
96
96
97
- assert False , " cannot translate `{}: {}`" .format (name , cty )
97
+ assert False , ' cannot translate `{}: {}`' .format (name , cty )
98
98
99
99
def translate_type_base (cty , f ):
100
- if " c_char" in cty :
101
- return "u8"
102
- elif " lapack_int" in cty or " lapack_logical" in cty :
103
- return " i32"
104
- elif " lapack_complex_double" in cty :
105
- return " c64"
106
- elif " lapack_complex_float" in cty :
107
- return " c32"
108
- elif " c_double" in cty :
109
- return " f64"
110
- elif " c_float" in cty :
111
- return " f32"
112
-
113
- assert False , " cannot translate `{}` in `{}`" .format (cty , f .name )
100
+ if ' c_char' in cty :
101
+ return 'u8'
102
+ elif ' lapack_int' in cty or ' lapack_logical' in cty :
103
+ return ' i32'
104
+ elif ' lapack_complex_double' in cty :
105
+ return ' c64'
106
+ elif ' lapack_complex_float' in cty :
107
+ return ' c32'
108
+ elif ' c_double' in cty :
109
+ return ' f64'
110
+ elif ' c_float' in cty :
111
+ return ' f32'
112
+
113
+ assert False , ' cannot translate `{}` in `{}`' .format (cty , f .name )
114
114
115
115
def translate_body_argument (name , rty ):
116
- if rty == "u8" :
117
- return " &({} as c_char)" .format (name )
118
- elif rty == " &mut u8" :
119
- return " {} as *mut _ as *mut _" .format (name )
120
-
121
- elif rty == " i32" :
122
- return " &{}" .format (name )
123
- elif rty == " &mut i32" :
116
+ if rty == 'u8' :
117
+ return ' &({} as c_char)' .format (name )
118
+ elif rty == ' &mut u8' :
119
+ return ' {} as *mut _ as *mut _' .format (name )
120
+
121
+ elif rty == ' i32' :
122
+ return ' &{}' .format (name )
123
+ elif rty == ' &mut i32' :
124
124
return name
125
- elif rty == " &[i32]" :
126
- return " {}.as_ptr()" .format (name )
127
- elif rty == " &mut [i32]" :
128
- return " {}.as_mut_ptr()" .format (name )
129
-
130
- elif rty .startswith ("f" ):
131
- return " &{}" .format (name )
132
- elif rty .startswith (" &mut f" ):
133
- return "{}" .format (name )
134
- elif rty .startswith (" &[f" ):
135
- return " {}.as_ptr()" .format (name )
136
- elif rty .startswith (" &mut [f" ):
137
- return " {}.as_mut_ptr()" .format (name )
138
-
139
- elif rty .startswith ("c" ):
140
- return " &{} as *const _ as *const _" .format (name )
141
- elif rty .startswith (" &mut c" ):
142
- return " {} as *mut _ as *mut _" .format (name )
143
- elif rty .startswith (" &[c" ):
144
- return " {}.as_ptr() as *const _" .format (name )
145
- elif rty .startswith (" &mut [c" ):
146
- return " {}.as_mut_ptr() as *mut _" .format (name )
147
-
148
- if rty .startswith (" Select" ):
149
- return " transmute({})" .format (name )
150
-
151
- assert False , " cannot translate `{}: {}`" .format (name , rty )
125
+ elif rty == ' &[i32]' :
126
+ return ' {}.as_ptr()' .format (name )
127
+ elif rty == ' &mut [i32]' :
128
+ return ' {}.as_mut_ptr()' .format (name )
129
+
130
+ elif rty .startswith ('f' ):
131
+ return ' &{}' .format (name )
132
+ elif rty .startswith (' &mut f' ):
133
+ return '{}' .format (name )
134
+ elif rty .startswith (' &[f' ):
135
+ return ' {}.as_ptr()' .format (name )
136
+ elif rty .startswith (' &mut [f' ):
137
+ return ' {}.as_mut_ptr()' .format (name )
138
+
139
+ elif rty .startswith ('c' ):
140
+ return ' &{} as *const _ as *const _' .format (name )
141
+ elif rty .startswith (' &mut c' ):
142
+ return ' {} as *mut _ as *mut _' .format (name )
143
+ elif rty .startswith (' &[c' ):
144
+ return ' {}.as_ptr() as *const _' .format (name )
145
+ elif rty .startswith (' &mut [c' ):
146
+ return ' {}.as_mut_ptr() as *mut _' .format (name )
147
+
148
+ if rty .startswith (' Select' ):
149
+ return ' transmute({})' .format (name )
150
+
151
+ assert False , ' cannot translate `{}: {}`' .format (name , rty )
152
152
153
153
def translate_return_type (cty ):
154
- if cty == " c_float" :
155
- return " f32"
156
- elif cty == " c_double" :
157
- return " f64"
154
+ if cty == ' c_float' :
155
+ return ' f32'
156
+ elif cty == ' c_double' :
157
+ return ' f64'
158
158
159
- assert False , " cannot translate `{}`" .format (cty )
159
+ assert False , ' cannot translate `{}`' .format (cty )
160
160
161
161
def format_header (f ):
162
162
args = format_header_arguments (f )
163
163
if f .ret is None :
164
- return " pub unsafe fn {}({})" .format (f .name , args )
164
+ return ' pub unsafe fn {}({})' .format (f .name , args )
165
165
else :
166
- return " pub unsafe fn {}({}) -> {}" .format (f .name , args , translate_return_type (f .ret ))
166
+ return ' pub unsafe fn {}({}) -> {}' .format (f .name , args , translate_return_type (f .ret ))
167
167
168
168
def format_body (f ):
169
- return " ffi::{}_({})" .format (f .name , format_body_arguments (f ))
169
+ return ' ffi::{}_({})' .format (f .name , format_body_arguments (f ))
170
170
171
171
def format_header_arguments (f ):
172
172
s = []
173
173
for arg in f .args :
174
- s .append (" {}: {}" .format (arg [0 ], translate_argument (* arg , f = f )))
175
- return ", " .join (s )
174
+ s .append (' {}: {}' .format (arg [0 ], translate_argument (* arg , f = f )))
175
+ return ', ' .join (s )
176
176
177
177
def format_body_arguments (f ):
178
178
s = []
179
179
for arg in f .args :
180
180
rty = translate_argument (* arg , f = f )
181
181
s .append (translate_body_argument (arg [0 ], rty ))
182
- return ", " .join (s )
182
+ return ', ' .join (s )
183
183
184
184
def prepare (code ):
185
185
lines = filter (lambda line : not re .match (r'^\s*//.*' , line ), code .split ('\n ' ))
186
- lines = re .sub (r'\s+' , ' ' , "" .join (lines )).strip ().split (';' )
186
+ lines = re .sub (r'\s+' , ' ' , '' .join (lines )).strip ().split (';' )
187
187
lines = filter (lambda line : not re .match (r'^\s*$' , line ), lines )
188
188
return [Function .parse (line ) for line in lines ]
189
189
190
190
def do (functions ):
191
191
for f in functions :
192
- print (" \n #[inline]" )
193
- print (format_header (f ) + " {" )
194
- print (" " + format_body (f ) + " \n }" )
192
+ print (' \n #[inline]' )
193
+ print (format_header (f ) + ' {' )
194
+ print (' ' + format_body (f ) + ' \n }' )
195
195
196
196
if __name__ == '__main__' :
197
197
parser = argparse .ArgumentParser ()
0 commit comments