-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmake-macro-n.pl
More file actions
96 lines (75 loc) · 1.97 KB
/
Copy pathmake-macro-n.pl
File metadata and controls
96 lines (75 loc) · 1.97 KB
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
#!/usr/bin/perl -w
use constant MAXN => 50;
print "/* automatically generated by $0 */
#define __VA_NARGS(";
for $n (1 .. MAXN) {
print "_$n, ";
print "\\\n " if ($n % 10 == 0);
}
print "N, ...) N
#define VA_NARGS(...) __VA_NARGS(__VA_ARGS__";
for ($n = MAXN; $n >= 1; --$n) {
print ", ";
print "\\\n " if ($n % 10 == 0);
print "$n";
}
print ")
#define VA_NPAIRS(...) __VA_NARGS(__VA_ARGS__";
for ($n = MAXN / 2; $n >= 1; --$n) {
print ", ";
print "\\\n " if ($n % 5 == 0);
print "$n, X";
}
print ")
";
for $n (1 .. MAXN) {
my $p = $n - 1;
print "#define _INC$p $n\n";
}
print "#define _INC(n) _INC ## n
#define INC(n) _INC(n)
";
for $n (1 .. MAXN) {
my $p = $n - 1;
print "#define _DEC$n $p\n";
}
print "#define _DEC(n) _DEC ## n
#define DEC(n) _DEC(n)
#define _FOR_NA1(n, op, cbarg, sep, arg, ...) op(n, cbarg, arg)
";
for $n (2 .. MAXN) {
my $p = $n - 1;
print "#define _FOR_NA$n(n, op, cbarg, sep, arg, ...) \\
op(n, cbarg, arg) sep() _FOR_NA$p(INC(n), op, cbarg, sep, __VA_ARGS__)
";
}
print "#define __FOR_NARGS(n, op, cbarg, sep, ...) \\
_FOR_NA ## n(1, op, cbarg, sep, __VA_ARGS__)
#define _FOR_NARGS(n, op, cbarg, sep, ...) \\
__FOR_NARGS(n, op, cbarg, sep, __VA_ARGS__)
#define FOR_NARGS(op, cbarg, sep, ...) \\
_FOR_NARGS(VA_NARGS(__VA_ARGS__), op, cbarg, sep, __VA_ARGS__)
";
for $n (1 .. MAXN) {
print "#define ARGN$n(";
for $a (1 .. $n) {
print "_$a, ";
print "\\\n " if ($a % 10 == 0);
}
print "...) _$n\n";
}
print "
#define __FORP1(op, sep, a, b) op(a, b)
";
for (my $n = 2; $n <= MAXN / 2; ++$n) {
my $p = $n - 1;
print "#define __FORP$n(op, sep, a, b, ...) \\
op(a, b) sep() __FORP$p(op, sep, __VA_ARGS__)\n";
}
print "
#define __FORP(n, op, sep, ...) __FORP ## n(op, sep, __VA_ARGS__)
#define _FOR_PAIRS(n, op, sep, ...) \\
__FORP(n, op, sep, __VA_ARGS__)
#define FOR_PAIRS(op, sep, ...) \\
_FOR_PAIRS(VA_NPAIRS(__VA_ARGS__), op, sep, __VA_ARGS__)
";