aboutsummaryrefslogtreecommitdiffstats
path: root/cppawk-include/varg-priv.h
blob: d71b500842dbe35e6a50aa0fac484c60b0794f59 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// cppawk: C preprocessor wrapper around awk
// Copyright 2022 Kaz Kylheku <kaz@kylheku.com>
//
// BSD-2 License
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef __CPPAWK_VARG_PRIV_H
#define __CPPAWK_VARG_PRIV_H

#ifndef __CPPAWK_BASE_H
#include "base.h"
#endif

#ifndef __CPPAWK_CASE_PRIV_H
#include "case-priv.h"
#endif

#define __rest                                                                \
  __r1, __r2, __r3, __r4, __r5, __r6, __r7, __r8, __r9, __r10,  __r11,        \
  __r12, __r13, __r14, __r15, __r16, __r17, __r18, __r19, __r20, __r21,       \
  __r22, __r23, __r24, __r25, __r26, __r27, __r28, __r29, __r30, __r31, __r32

#define __varg_count(name, check, count)                                      \
  if (__present(check))                                                       \
    __error(name ": too many arguments");                                     \
  count = 0;                                                                  \
  if (__present(__r16)) {                                                     \
    if (__present(__r24)) {                                                   \
      if (__present(__r28)) {                                                 \
        if (__present(__r30)) {                                               \
          if (__present(__r31)) {                                             \
            if (__present(__r32))                                             \
              count = 32;                                                     \
            else                                                              \
              count = 31;                                                     \
          } else {                                                            \
            count = 30;                                                       \
          }                                                                   \
        } else { /* !__present(__r30) */                                      \
          if (__present(__r29))                                               \
            count = 29;                                                       \
          else                                                                \
            count = 28;                                                       \
        }                                                                     \
      } else { /* !__present(__r28) */                                        \
        if (__present(__r26)) {                                               \
          if (__present(__r27))                                               \
            count = 27;                                                       \
          else                                                                \
            count = 26;                                                       \
        } else { /* !__present(__r26) */                                      \
          if (__present(__r25))                                               \
            count = 25;                                                       \
          else                                                                \
            count = 24;                                                       \
        }                                                                     \
      }                                                                       \
    } else { /* !__present(__r24) */                                          \
      if (__present(__r20)) {                                                 \
        if (__present(__r22)) {                                               \
          if (__present(__r23))                                               \
            count = 23;                                                       \
          else                                                                \
            count = 22;                                                       \
        } else { /* !__present(__r22) */                                      \
          if (__present(__r21))                                               \
            count = 21;                                                       \
          else                                                                \
            count = 20;                                                       \
        }                                                                     \
      } else { /* !__present(__r20) */                                        \
        if (__present(__r18)) {                                               \
          if (__present(__r19))                                               \
            count = 19;                                                       \
          else                                                                \
            count = 18;                                                       \
        } else { /* !__present(__r18) */                                      \
          if (__present(__r17))                                               \
            count = 17;                                                       \
          else                                                                \
            count = 16;                                                       \
        }                                                                     \
      }                                                                       \
    }                                                                         \
  } else { /*!__present(__r16) */                                             \
    if (__present(__r8)) {                                                    \
      if (__present(__r12)) {                                                 \
        if (__present(__r14)) {                                               \
          if (__present(__r15)) {                                             \
            count = 15;                                                       \
          } else {                                                            \
            count = 14;                                                       \
          }                                                                   \
        } else { /* !__present(__r14) */                                      \
          if (__present(__r13))                                               \
            count = 13;                                                       \
          else                                                                \
            count = 12;                                                       \
        }                                                                     \
      } else { /* !__present(__r12) */                                        \
        if (__present(__r10)) {                                               \
          if (__present(__r11))                                               \
            count = 11;                                                       \
          else                                                                \
            count = 10;                                                       \
        } else { /* !__present(__r10) */                                      \
          if (__present(__r9))                                                \
            count = 9;                                                        \
          else                                                                \
            count = 8;                                                        \
        }                                                                     \
      }                                                                       \
    } else { /* !__present(__r8) */                                           \
      if (__present(__r4)) {                                                  \
        if (__present(__r6)) {                                                \
          if (__present(__r7))                                                \
            count = 7;                                                        \
          else                                                                \
            count = 6;                                                        \
        } else { /* !__present(__r6) */                                       \
          if (__present(__r5))                                                \
            count = 5;                                                        \
          else                                                                \
            count = 4;                                                        \
        }                                                                     \
      } else { /* !__present(__r4) */                                         \
        if (__present(__r2)) {                                                \
          if (__present(__r3))                                                \
            count = 3;                                                        \
          else                                                                \
            count = 2;                                                        \
        } else { /* !__present(__r2) */                                       \
          if (__present(__r1))                                                \
            count = 1;                                                        \
        }                                                                     \
      }                                                                       \
    }                                                                         \
  }

#define __varg_list(name, check, count, out)                                  \
  __varg_count(name, check, count);                                           \
  out = __nil;                                                                \
  __case (count) {                                                            \
    __of (32)                                                                 \
      out = __cons(__r32, out);                                               \
      __cfall;                                                                \
    __of (31)                                                                 \
      out = __cons(__r31, out);                                               \
      __cfall;                                                                \
    __of (30)                                                                 \
      out = __cons(__r30, out);                                               \
      __cfall;                                                                \
    __of (29)                                                                 \
      out = __cons(__r29, out);                                               \
      __cfall;                                                                \
    __of (28)                                                                 \
      out = __cons(__r28, out);                                               \
      __cfall;                                                                \
    __of (27)                                                                 \
      out = __cons(__r27, out);                                               \
      __cfall;                                                                \
    __of (26)                                                                 \
      out = __cons(__r26, out);                                               \
      __cfall;                                                                \
    __of (25)                                                                 \
      out = __cons(__r25, out);                                               \
      __cfall;                                                                \
    __of (24)                                                                 \
      out = __cons(__r24, out);                                               \
      __cfall;                                                                \
    __of (23)                                                                 \
      out = __cons(__r23, out);                                               \
      __cfall;                                                                \
    __of (22)                                                                 \
      out = __cons(__r22, out);                                               \
      __cfall;                                                                \
    __of (21)                                                                 \
      out = __cons(__r21, out);                                               \
      __cfall;                                                                \
    __of (20)                                                                 \
      out = __cons(__r20, out);                                               \
      __cfall;                                                                \
    __of (19)                                                                 \
      out = __cons(__r19, out);                                               \
      __cfall;                                                                \
    __of (18)                                                                 \
      out = __cons(__r18, out);                                               \
      __cfall;                                                                \
    __of (17)                                                                 \
      out = __cons(__r17, out);                                               \
      __cfall;                                                                \
    __of (16)                                                                 \
      out = __cons(__r16, out);                                               \
      __cfall;                                                                \
    __of (15)                                                                 \
      out = __cons(__r15, out);                                               \
      __cfall;                                                                \
    __of (14)                                                                 \
      out = __cons(__r14, out);                                               \
      __cfall;                                                                \
    __of (13)                                                                 \
      out = __cons(__r13, out);                                               \
      __cfall;                                                                \
    __of (12)                                                                 \
      out = __cons(__r12, out);                                               \
      __cfall;                                                                \
    __of (11)                                                                 \
      out = __cons(__r11, out);                                               \
      __cfall;                                                                \
    __of (10)                                                                 \
      out = __cons(__r10, out);                                               \
      __cfall;                                                                \
    __of (9)                                                                  \
      out = __cons(__r9, out);                                                \
      __cfall;                                                                \
    __of (8)                                                                  \
      out = __cons(__r8, out);                                                \
      __cfall;                                                                \
    __of (7)                                                                  \
      out = __cons(__r7, out);                                                \
      __cfall;                                                                \
    __of (6)                                                                  \
      out = __cons(__r6, out);                                                \
      __cfall;                                                                \
    __of (5)                                                                  \
      out = __cons(__r5, out);                                                \
      __cfall;                                                                \
    __of (4)                                                                  \
      out = __cons(__r4, out);                                                \
      __cfall;                                                                \
    __of (3)                                                                  \
      out = __cons(__r3, out);                                                \
      __cfall;                                                                \
    __of (2)                                                                  \
      out = __cons(__r2, out);                                                \
      __cfall;                                                                \
    __of (1)                                                                  \
      out = __cons(__r1, out);                                                \
      __cfall;                                                                \
  }

#define __varg_array(name, check, count, out)                                 \
  __varg_count(name, check, count);                                           \
  delete out;                                                                 \
  __case (count) {                                                            \
    __of (32)                                                                 \
      out[32] = __r32;                                                        \
      __cfall;                                                                \
    __of (31)                                                                 \
      out[31] = __r31;                                                        \
      __cfall;                                                                \
    __of (30)                                                                 \
      out[30] = __r30;                                                        \
      __cfall;                                                                \
    __of (29)                                                                 \
      out[29] = __r29;                                                        \
      __cfall;                                                                \
    __of (28)                                                                 \
      out[28] = __r28;                                                        \
      __cfall;                                                                \
    __of (27)                                                                 \
      out[27] = __r27;                                                        \
      __cfall;                                                                \
    __of (26)                                                                 \
      out[26] = __r26;                                                        \
      __cfall;                                                                \
    __of (25)                                                                 \
      out[25] = __r25;                                                         \
      __cfall;                                                                \
    __of (24)                                                                 \
      out[24] = __r24;                                                        \
      __cfall;                                                                \
    __of (23)                                                                 \
      out[23] = __r23;                                                        \
      __cfall;                                                                \
    __of (22)                                                                 \
      out[22] = __r22;                                                        \
      __cfall;                                                                \
    __of (21)                                                                 \
      out[21] = __r21;                                                        \
      __cfall;                                                                \
    __of (20)                                                                 \
      out[20] = __r20;                                                        \
      __cfall;                                                                \
    __of (19)                                                                 \
      out[19] = __r19;                                                        \
      __cfall;                                                                \
    __of (18)                                                                 \
      out[18] = __r18;                                                         \
      __cfall;                                                                \
    __of (17)                                                                 \
      out[17] = __r17;                                                        \
      __cfall;                                                                \
    __of (16)                                                                 \
      out[16] = __r16;                                                        \
      __cfall;                                                                \
    __of (15)                                                                 \
      out[15] = __r15;                                                        \
      __cfall;                                                                \
    __of (14)                                                                 \
      out[14] = __r14;                                                        \
      __cfall;                                                                \
    __of (13)                                                                 \
      out[13] = __r13;                                                        \
      __cfall;                                                                \
    __of (12)                                                                 \
      out[12] = __r12;                                                        \
      __cfall;                                                                \
    __of (11)                                                                 \
      out[11] = __r11;                                                        \
      __cfall;                                                                \
    __of (10)                                                                 \
      out[10] = __r10;                                                        \
      __cfall;                                                                \
    __of (9)                                                                  \
      out[9] = __r9;                                                          \
      __cfall;                                                                \
    __of (8)                                                                  \
      out[8] = __r8;                                                          \
      __cfall;                                                                \
    __of (7)                                                                  \
      out[7] = __r7;                                                          \
      __cfall;                                                                \
    __of (6)                                                                  \
      out[6] = __r6;                                                          \
      __cfall;                                                                \
    __of (5)                                                                  \
      out[5] = __r5;                                                          \
      __cfall;                                                                \
    __of (4)                                                                  \
      out[4] = __r4;                                                          \
      __cfall;                                                                \
    __of (3)                                                                  \
      out[3] = __r3;                                                          \
      __cfall;                                                                \
    __of (2)                                                                  \
      out[2] = __r2;                                                          \
      __cfall;                                                                \
    __of (1)                                                                  \
      out[1] = __r1;                                                          \
      __cfall;                                                                \
  }

#ifndef __CPPAWK_CONS_PRIV_H
#include "cons-priv.h"
#endif

function __argcount(__name, __rest,
                    __narg, // check argument; doubles as local count
                    __case_temps)
{
  __varg_count(__name, __narg, __narg)
  return __narg;
}

function __arglist(__name, __rest,
                   __narg,  // check argument; doubles as local count
                   __out, __case_temps)
{
  __varg_list(__name, __narg, __narg, __out)
  return __out;
}

function __argarray(__name, __to_array, __rest,
                    __narg, // check argument; doubles as local count
                    __case_temps)
{
  __varg_array(__name, __narg, __narg, __to_array)
}

#endif