FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mlx-c/python/generator.py at main · Open-Less/mlx-c · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Open-Less
/
mlx-c
Public
forked from
jimersylee/mlx-c
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mlx-c
/
python
/
generator.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
156 lines (136 loc) · 4.69 KB
Breadcrumbs
mlx-c
/
python
/
generator.py
Copy path
File metadata and controls
156 lines (136 loc) · 4.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Copyright © 2023-2024 Apple Inc.
import
cxxheaderparser
from
cxxheaderparser
.
simple
import
parse_string
import
argparse
import
os
import
re
parser
=
argparse
.
ArgumentParser
(
"MLX C bindings generator"
,
add_help
=
False
)
parser
.
add_argument
(
"--header"
,
type
=
str
)
parser
.
add_argument
(
"--implementation"
,
default
=
False
,
action
=
"store_true"
)
parser
.
add_argument
(
"--language"
,
default
=
"C"
,
type
=
str
)
parser
.
add_argument
(
"--docstring"
,
default
=
""
,
type
=
str
)
parser
.
add_argument
(
"--headername"
,
default
=
""
,
type
=
str
)
args
=
parser
.
parse_args
()
if
args
.
headername
:
headername
=
args
.
headername
else
:
headername
=
os
.
path
.
basename
(
args
.
header
)
if
headername
.
endswith
(
".h"
):
headername
=
headername
[:
-
2
]
else
:
raise
RuntimeError
(
"are you sure you are providing a header?"
)
def
getname
(
t
):
if
type
(
t
)
==
cxxheaderparser
.
types
.
TemplateArgument
:
return
getname
(
t
.
arg
)
elif
type
(
t
)
==
cxxheaderparser
.
types
.
Reference
:
return
getname
(
t
.
ref_to
)
elif
type
(
t
)
==
cxxheaderparser
.
types
.
MoveReference
:
return
getname
(
t
.
moveref_to
)
elif
type
(
t
)
==
cxxheaderparser
.
types
.
PQName
:
res
=
[]
for
s
in
t
.
segments
:
res
.
append
(
getname
(
s
))
return
"::"
.
join
(
res
)
elif
type
(
t
)
==
cxxheaderparser
.
types
.
FundamentalSpecifier
:
return
t
.
name
elif
type
(
t
)
==
cxxheaderparser
.
types
.
NameSpecifier
:
res
=
t
.
name
if
t
.
specialization
is
not
None
:
res
+=
getname
(
t
.
specialization
)
return
res
elif
type
(
t
)
==
cxxheaderparser
.
types
.
Type
:
return
getname
(
t
.
typename
)
elif
type
(
t
)
==
cxxheaderparser
.
types
.
TemplateSpecialization
:
res
=
[]
for
s
in
t
.
args
:
res
.
append
(
getname
(
s
))
return
"<"
+
", "
.
join
(
res
)
+
">"
elif
type
(
t
)
==
cxxheaderparser
.
types
.
FunctionType
:
return_t
=
getname
(
t
.
return_type
)
params_t
=
[]
for
p
in
t
.
parameters
:
params_t
.
append
(
getname
(
p
.
type
))
res
=
return_t
+
"("
+
","
.
join
(
params_t
)
+
")"
return
res
elif
type
(
t
)
==
cxxheaderparser
.
types
.
Pointer
:
# circumvents parser crashing on pointers
res
=
"*("
+
getname
(
t
.
ptr_to
)
+
")"
return
res
raise
RuntimeError
(
"unsupported type: "
+
str
(
t
))
def
get_default_value
(
d
):
if
d
is
None
:
return
d
res
=
[]
for
tok
in
d
.
tokens
:
res
.
append
(
tok
.
value
)
return
""
.
join
(
res
)
funcs
=
{}
enums
=
{}
def
preprocess_header
(
content
):
"""Simple preprocessor that strips MLX_API macro without resolving includes."""
# Remove MLX_API macro (appears as MLX_API or MLX_API followed by space)
content
=
re
.
sub
(
r"\bMLX_API\s*"
,
""
,
content
)
return
content
for
header
in
args
.
header
.
split
(
";"
):
# Read file and preprocess to strip MLX_API
with
open
(
header
,
"r"
)
as
f
:
content
=
f
.
read
()
content
=
preprocess_header
(
content
)
Z
=
parse_string
(
content
)
def
process_namespace
(
l
,
namespace
,
funcs
,
enums
):
namespace
=
namespace
.
lstrip
(
"::"
)
for
e
in
l
.
enums
:
name
=
getname
(
e
.
typename
)
values
=
[
v
.
name
for
v
in
e
.
values
]
enums
[
namespace
+
"::"
+
name
]
=
{
"name"
:
name
,
"namespace"
:
namespace
,
"values"
:
values
,
}
for
f
in
l
.
functions
:
name
=
getname
(
f
.
name
)
if
name
.
startswith
(
"operator"
):
continue
params_t
=
[]
params_name
=
[]
params_default
=
[]
return_t
=
getname
(
f
.
return_type
)
if
return_t
==
"Stream"
:
# unsupported
continue
for
p
in
f
.
parameters
:
params_t
.
append
(
getname
(
p
.
type
))
params_name
.
append
(
p
.
name
)
params_default
.
append
(
get_default_value
(
p
.
default
))
func
=
{
"name"
:
name
,
"params_t"
:
params_t
,
"params_name"
:
params_name
,
"return_t"
:
return_t
,
"namespace"
:
namespace
,
"params_default"
:
params_default
,
}
ns_name
=
namespace
+
"::"
+
name
if
ns_name
in
funcs
:
funcs
[
ns_name
].
append
(
func
)
else
:
funcs
[
ns_name
]
=
[
func
]
for
subnamespace
in
l
.
namespaces
:
process_namespace
(
l
.
namespaces
[
subnamespace
],
namespace
+
"::"
+
subnamespace
,
funcs
,
enums
,
)
process_namespace
(
Z
.
namespace
,
""
,
funcs
,
enums
)
if
args
.
language
==
"C"
:
from
c
import
generate
else
:
raise
RuntimeError
(
"Unsupported language"
)
generate
(
funcs
,
enums
,
header
,
headername
,
args
.
implementation
,
args
.
docstring
,
)
Back
|
FazBrowse Home
|
New Git URL