-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro_expansion_generator.py
271 lines (220 loc) · 9.05 KB
/
macro_expansion_generator.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
import sys
def generate_macros(n):
# Generate GET_ARG macros
get_arg_macros = ""
for i in range(1, n + 1):
args = (
", ".join([f"arg{j}" for j in range(1, i + 1)]) + ", ..."
if i > 1
else "arg1, ..."
)
get_arg_macros += f"#define TRLC_GET_ARG_{i}({args}) arg{i}\n"
# Generate COUNT_ARGS macros
count_args_macro = f"#define TRLC_COUNT_ARGS(...) TRLC_COUNT_ARGS_IMPL(__VA_ARGS__, {', '.join(map(str, range(n, 0, -1)))})"
count_args_impl_macro = f"#define TRLC_COUNT_ARGS_IMPL({', '.join([f'_{i}' for i in range(1, n + 1)])}, N, ...) N"
# Generate recursive APPLY_* macros
apply_impl_macros = ""
for i in range(1, n + 1):
args = ", ".join([f"arg{j}" for j in range(1, i + 1)])
apply_impl_macros += f"#define TRLC_APPLY_{i}(MACRO, {args}) "
apply_impl_macros += "".join([f"MACRO(arg{j}); " for j in range(1, i + 1)])
apply_impl_macros += "\n"
# Generate APPLY and recursive expansion macros with indexing
apply_impl_macros_with_index = "\n".join(
[
f"#define TRLC_APPLY_WITH_INDEX_{i}(MACRO, "
+ ", ".join([f"arg{j}" for j in range(1, i + 1)])
+ ") "
+ (
"MACRO(arg1, 1); "
+ "".join([f"MACRO(arg{j}, {j}); " for j in range(2, i + 1)])
if i > 1
else "MACRO(arg1, 1);"
)
for i in range(1, n + 1)
]
)
# Generate recursive EXPAND_* macros
expand_impl_macros = ""
for i in range(1, n + 1):
args = ", ".join([f"arg{j}" for j in range(1, i + 1)])
expand_impl_macros += f"#define TRLC_EXPAND_{i}(MACRO, {args}) "
# Use a generator expression to join MACRO calls without a trailing comma
expand_impl_macros += ", ".join([f"MACRO(arg{j})" for j in range(1, i + 1)])
expand_impl_macros += "\n"
# Generate EXPAND and recursive expansion macros with indexing
expand_impl_macros_with_index = "\n".join(
[
f"#define TRLC_EXPAND_WITH_INDEX_{i}(MACRO, "
+ ", ".join([f"arg{j}" for j in range(1, i + 1)])
+ ") "
+ (
"MACRO(arg1, 1)"
+ "".join([f", MACRO(arg{j}, {j})" for j in range(2, i + 1)])
if i > 1
else "MACRO(arg1, 1)"
)
for i in range(1, n + 1)
]
)
# Generate recursive APPEND_* macros
append_impl_macros = ""
for i in range(1, n + 1):
args = ",".join([f"arg{j}" for j in range(1, i + 1)])
append_impl_macros += f"#define TRLC_APPEND_{i}(MACRO, delim, {args}) "
# Use a generator expression to join MACRO calls without a trailing comma
append_impl_macros += "MACRO(" + "##delim##".join([f"arg{j}" for j in range(1, i + 1)])
append_impl_macros += ")\n"
# Generate APPLY macros
apply_macros = "#define TRLC_APPLY(MACRO, ...) TRLC_APPLY_IMPL(MACRO, TRLC_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)\n"
apply_macros += "#define TRLC_APPLY_IMPL(MACRO, COUNT, ...) TRLC_APPLY_IMPL2(MACRO, COUNT, __VA_ARGS__)\n"
apply_macros += "#define TRLC_APPLY_IMPL2(MACRO, COUNT, ...) TRLC_APPLY_##COUNT(MACRO, __VA_ARGS__)"
# Generate EXPAND macros
expand_macros = "#define TRLC_EXPAND(MACRO, ...) TRLC_EXPAND_IMPL(MACRO, TRLC_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)\n"
expand_macros += "#define TRLC_EXPAND_IMPL(MACRO, COUNT, ...) TRLC_EXPAND_IMPL2(MACRO, COUNT, __VA_ARGS__)\n"
expand_macros += "#define TRLC_EXPAND_IMPL2(MACRO, COUNT, ...) TRLC_EXPAND_##COUNT(MACRO, __VA_ARGS__)"
# Generate APPEND macros
append_macros = "#define TRLC_APPEND(MACRO, delim,...) TRLC_APPEND_IMPL(MACRO, delim, TRLC_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)\n"
append_macros += "#define TRLC_APPEND_IMPL(MACRO, delim, COUNT, ...) TRLC_APPEND_IMPL2(MACRO, delim, COUNT, __VA_ARGS__)\n"
append_macros += "#define TRLC_APPEND_IMPL2(MACRO, delim, COUNT,...) TRLC_APPEND_##COUNT(MACRO, delim, __VA_ARGS__)"
# Generate APPLY_WITH_INDEX macros
apply_with_index_macros = "#define TRLC_APPLY_WITH_INDEX(MACRO, ...) TRLC_APPLY_WITH_INDEX_IMPL(MACRO, TRLC_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)\n"
apply_with_index_macros += "#define TRLC_APPLY_WITH_INDEX_IMPL(MACRO, COUNT, ...) TRLC_APPLY_WITH_INDEX_IMPL2(MACRO, COUNT, __VA_ARGS__)\n"
apply_with_index_macros += "#define TRLC_APPLY_WITH_INDEX_IMPL2(MACRO, COUNT, ...) TRLC_APPLY_WITH_INDEX_##COUNT(MACRO, __VA_ARGS__)"
# Generate EXPAND_WITH_INDEX macros
expand_with_index_macros = "#define TRLC_EXPAND_WITH_INDEX(MACRO, ...) TRLC_EXPAND_WITH_INDEX_IMPL(MACRO, TRLC_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)\n"
expand_with_index_macros += "#define TRLC_EXPAND_WITH_INDEX_IMPL(MACRO, COUNT, ...) TRLC_EXPAND_WITH_INDEX_IMPL2(MACRO, COUNT, __VA_ARGS__)\n"
expand_with_index_macros += "#define TRLC_EXPAND_WITH_INDEX_IMPL2(MACRO, COUNT, ...) TRLC_EXPAND_WITH_INDEX_##COUNT(MACRO, __VA_ARGS__)"
header = """
/**
* This file is automatically generated from a Python script.
*
* If you wish to change the number of recursive argument expansions or the output directory,
* please rerun the script and replace this file with the newly generated version.
*
* Note: Modifications made directly to this file will be overwritten
* the next time the script is executed.
*
* To run the script, use the following command:
*
* python3 macro_expansion_generator.py <N> [directory]
*
* Where <N> is the number of recursive expansions you want (e.g., 125).
* [directory] is optional and specifies the output directory for the generated files.
*/
#pragma once
// clang-format off
"""
footer = """
// clang-format on
"""
# Combine all parts of the generated macros
count_args_file = f"""
{header}
// Macro to get the number of arguments
{count_args_macro}
{count_args_impl_macro}
{footer}
"""
apply_args_file = f"""
{header}
#include "count_args.hpp"
// Macro to apply a function to each argument
{apply_macros}
// Recursive apply of arguments based on count
{apply_impl_macros}
{footer}
"""
apply_args_with_index_file = f"""
{header}
#include "count_args.hpp"
// Macro to apply a function to each argument with index
{apply_with_index_macros}
// Recursive apply of arguments based on count with index
{apply_impl_macros_with_index}
{footer}
"""
expand_args_file = f"""
{header}
#include "count_args.hpp"
// Macro to expand a expr to each argument
{expand_macros}
// Recursive expansion of arguments based on count
{expand_impl_macros}
{footer}
"""
append_args_file = f"""
{header}
#include "count_args.hpp"
// Macro to append a expr to each argument
{append_macros}
// Recursive appending of arguments based on count
{append_impl_macros}
{footer}
"""
expand_args_with_index_file = f"""
{header}
#include "count_args.hpp"
// Macro to expand a expr to each with index
{expand_with_index_macros}
// Recursive expansion of arguments based on count with index
{expand_impl_macros_with_index}
{footer}
"""
return (
count_args_file,
apply_args_file,
append_args_file,
apply_args_with_index_file,
expand_args_file,
expand_args_with_index_file,
)
def write_to_file(n, directory):
# Generate the different parts of macros
(
count_args_file,
apply_args_file,
append_args_file,
apply_args_with_index_file,
expand_args_file,
expand_args_with_index_file,
) = generate_macros(n)
# Write COUNT_ARGS macros to a separate file
with open(f"{directory}/count_args.hpp", "w") as f:
f.write(count_args_file)
# Write APPLY macros to a separate file
with open(f"{directory}/apply_args.hpp", "w") as f:
f.write(apply_args_file)
# Write APPLY macros with index to a separate file
with open(f"{directory}/apply_args_with_index.hpp", "w") as f:
f.write(apply_args_with_index_file)
# Write EXPAND macros to a separate file
with open(f"{directory}/expand_args.hpp", "w") as f:
f.write(expand_args_file)
# Write EXPAND macros with index to a separate file
with open(f"{directory}/expand_args_with_index.hpp", "w") as f:
f.write(expand_args_with_index_file)
# Write APPEND macros to a separate file
with open(f"{directory}/append_args.hpp", "w") as f:
f.write(append_args_file)
print(
f"Macros generated and written to {directory}/count_args.hpp, {directory}/apply_args.hpp, "
f"{directory}/apply_args_with_index.hpp, {directory}/expand_args.hpp, "
f"{directory}/expand_args_with_index.hpp",
f"{directory}/append_args.hpp",
)
def main():
if len(sys.argv) < 2 or len(sys.argv) > 3:
print(
"Usage: python3 macro_expansion_generator.py <number_of_recursive_expansions> [directory]"
)
return
try:
num_expansions = int(sys.argv[1])
except ValueError:
print("Please provide a valid number for the recursive expansions.")
return
directory = sys.argv[2] if len(sys.argv) == 3 else "."
write_to_file(num_expansions, directory)
if __name__ == "__main__":
main()