aboutsummaryrefslogtreecommitdiffstats
path: root/cppawk-narg.1
blob: 409a602a96a25c22e3da5a288d9d6c01936e1ccc (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
CPPAWK-NARG(1)                              Case Macro                             CPPAWK-NARG(1)

NAME
       narg - macros for writing variable argument macros

SYNOPSIS
         #include <narg.h>

         #define narg(...)
         #define splice(args)
         #define varexpand(first_mac, rest_mac, ...)
         #define revarg(...)

DESCRIPTION
       The <narg.h> header provides several macros which are useful to macro writers.  In partic-
       ular, these macros make it easy to develop variable argument macros which take one or more
       argument, and have complex expansions.

       In this manual, the -> (arrow) notation means "expands to". For instance

         foo(bar) -> 42  // the macro call foo(bar) expands to 42

       A description of each macro follows:

       narg   This  macro takes one or more arguments, and expands to a decimal integer which in-
              dicates how many arguments there are.

                narg(x) -> 1
                narg(x, y) -> 2
                narg(x, y, z) -> 3

              The narg macro can be called with up to 32 (thirty-two) arguments. If it is  called
              with  between 33 to 48 arguments, it expands to an unspecified token sequence which
              generates a syntax error in Awk. The token sequence begins with an  identifier  and
              therefore  may  appear as the right operand of the token-pasting ## operator, oppo-
              site to an identifier token.

              If more than 48 arguments are given, the behavior is unspecified.

       splice The splice macro provides a shim for inserting  a  parenthesized  argument  into  a
              macro expansion, such that the argument turns into individual arguments. Suppose we
              have a macro like this:

                 #define vmac(a, b, ...) ...

              For some reason, we need to write fixed a macro like this:

                 #define fmac(x, y, args) vmac(x, y, ???)

              where the args argument is a parenthesized list of arguments that must  become  the
              ...   argument  of the vmac macro. That is to say, fmac is to be invoked like this,
              with the indicated expansion:

                 fmac(1, 2, (3, 4, 5)) -> vmac(1, 2, 3, 4, 5)

              The splice macro solves the question of what to write into the  position  indicated
              by the ??? question marks to achieve this:

                 #define fmac(x, y, args) vmac(x, y, splice(args))

              Example: produce the following macro:

                csum((a, b, c), (x, y))   -> (sqrt(sumsq(a, b, c)) +
                                              sqrt(sumsq(x, y)))

              This is a trick example: splice is not required at all:

                #define csum(left, right) (sqrt(sumsq left) + \
                                           sqrt(sumsq right))

              The splice macro is not required because the parenthesized arguments constitute the
              entire argument list of sumsq.  However, suppose the requirement is this, requiring
              the  parenthesized  arguments to be inserted into an argument list containing other
              arguments:

                csum(t, (a, b, c), (x, y))  -> (sqrt(sumsq(t, a, b, c)) +
                                                sqrt(sumsq(t, x, y)))

              Now we need:

                #define csum(parm, left, right) (sqrt(sumsq(parm, \
                                                            splice(left)) + \
                                                      sumsq(parm, \
                                                            splice(right))))

       revarg This macro expands to a comma-separated list of its arguments, which appear in  re-
              verse.

                 revarg(1) -> 1
                 revarg(1, 2) -> 2, 1
                 revarg(1, 2, 3) -> 3, 2, 1

              Like  narg,  the  revarg  macro can be called with up to 32 arguments, beyond which
              there is some overflow detection up to 48 arguments, followed by unspecified behav-
              ior for 49 or more arguments.

       varexpand
              The most complex macro in the <narg.h> header is varexpand.

              This  macro  is  used  for writing variadic macros with complex expansions, using a
              compact specification.

              The varexpand macro uses "higher order macro" programming: it has  arguments  which
              are themselves macros.  To understand varexpand it helps to understand the Lisp re-
              duce function, or the similar fold function found in functional  languages.  Recall
              that the prototype of the varexpand macro is this:

                #define varexpand(first_mac, rest_mac, ...)

              To  use  varexpand you must first write two macros: a one-argument macro whose name
              is passed as the first_mac argument, and two argument  macro  to  be  used  as  the
              rest_mac argument.

              Most  variadic  macros  written  with varexpand will pass through their __VA_ARGS__
              list as the ...  parameter; however, the splice macro can also  be  used  to  place
              parenthesized argument lists into that position

              Up  to  32 variadic arguments are accepted by varexpand beyond which there is over-
              flow detection up to 48 arguments, followed by unspecified behavior for 49 or  more
              arguments.

              Example: suppose we want to write a macro with an expansion like this:

                add(1) -> 1
                add(1, 2) -> 1 + 2
                add(1, 2, 3) -> 1 + 2 + 3

              First,  we must write a macro for handling the base case of the induction, which is
              used for the leftmost argument. The expansion is trivial:

                #define add_first(x) x

              The second macro is more complex. It takes two arguments. The left argument is  the
              accumulated  expansion  so far, of all the arguments previous to that argument. The
              right argument is the next argument to be added to the expansion.

                #define add_next(acc, x) acc + x

              For instance, if the arguments 1, 2 have already been expanded to 1  +  2  and  the
              next  argument is 3, then acc takes on the tokens 1 + 2, and x takes on 3. Thus the
              expansion is:

                add_next(1 + 2, 3) -> 1 + 2 + 3

              With these two macros, we can then write add like this:

                #define add(...) varexpand(add_first, add_next, __VA_ARGS__)

              More complex example: suppose we want an inline sum-of-squares  macro  which  works
              like this:

                sumsq(x)       -> ((x)*(x))
                sumsq(x, y, z) -> ((x)*(x) + (y)*(y) + (z)*(z))

              Note  the  detail that there are outer parentheses around the entire expansion, but
              the individual terms are not parenthesized, only the products. We write the  helper
              macros like this:

                #define sumsq_first(x)   (x)*(x)
                #define sumsq_next(a, x) a + sumsq_first(x)

              Note  that  sumsq_next reuses sumsq_first to avoid repeating the (x)*(x) term. Then
              we complete the implementation:

                #define sumsq(...) (varexpand(sumsq_first, \
                                              sumsq_next,\
                                              __VA_ARGS__))

              The outer parentheses are written around the varexpand call. In general,  varexpand
              can  be  just  a  small component of a larger macro expansion, and can be used more
              than one time in a macro expansion.

              Example: rlist macro which generates a  left-associative  nested  expression,  like
              this:

                rlist(1)        -> cons(1, nil)
                rlist(1, 2)     -> cons(2, cons(1, nil))
                rlist(1, 2, 3)  -> cons(3, cons(2, cons(1, nil)))

              Implementation:

                #define rlist_first(x)    cons(x, nil)
                #define rlist_next(a, x)  cons(x, a)

                #define rlist(...)        varexpand(rlist_first, rlist_next, \
                                                    __VA_ARGS__)

              What  if  we  want  the consing to produce the list in order via right association,
              rather than in reverse? So that is to say:

                list(1, 2, 3)   -> cons(1, cons(2, cons(3, nil)))

              Here we simply take advantage of the revarg macro to reverse the arguments:

                #define list(...)         rlist(revarg(__VA_ARG__))

BUGS
       As noted in the DESCRIPTION, the narg, revarg and varexpand macros are limited to handling
       32 variadic arguments, beyond which there is a 16 argument safety margin with error detec-
       tion, followed by unspecified behavior.

       The C preprocessor doesn't support macro recursion, which forbids  some  complex  uses  of
       varexpand  whereby  the first_mac and next_mac macros themselves make use of varexpand.  A
       possible workaround is to clone the implementation of varexpand to  produce  an  identical
       macro  called  varexpand2.   This then allows for two "recursion" levels, whereby each one
       uses the macro under a different name.

AUTHOR
       Kaz Kylheku <kaz@kylheku.com>

COPYRIGHT
       Copyright 2022, BSD2 License.

cppawk Libraries                          29 March 2022                            CPPAWK-NARG(1)