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
|
// cppawk: C preprocessor wrapper around awk
// 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_ITER_H
#ifndef __CPPAWK_BASE_H
#include "base.h"
#endif
#ifndef __CPPAWK_CONS_H
#include "cons.h"
#endif
#ifndef __CPPAWK_NARG_PRIV_H
#include "narg-priv.h"
#endif
#define doarray(key, value, arr) \
for (key in arr) \
if ((value = arr[key]) || 1)
#define dostring(index, chr, str) \
__g(s) = str; \
__g(l) = length(__g(s)); \
for (index = 1; index <= __g(l); i++) \
if ((chr = substr(__g(s), index, 1)) || 1)
#define dofields(index, val) \
for (index = 1; index <= NF; index++) \
if ((val = $ index) || 1)
#define loop(...) for (__loop_init(__VA_ARGS__); \
(__loop_test(__VA_ARGS__) || \
__loop_fini(__VA_ARGS__)) && \
__loop_prep(__VA_ARGS__); \
__loop_step(__VA_ARGS__))
#define __loop_init(...) prog(__varexpand(__init_first, __init_next, \
__VA_ARGS__))
#define __loop_test(...) __and(__varexpand(__test_first, __test_next, \
__VA_ARGS__))
#define __loop_prep(...) prog(__varexpand(__prep_first, __prep_next, \
__VA_ARGS__))
#define __loop_fini(...) prog(__varexpand(__fini_first, __fini_next, \
__VA_ARGS__)) && 0
#define __loop_step(...) prog(__varexpand(__step_first, __step_next, \
__VA_ARGS__))
#define __and(...) __varexpand(__nd_first, __nd_next, __VA_ARGS__)
#define __nd_first(x) (x)
#define __nd_next(p, x) p && (x)
#define loop_cross(...) __varexpand2(__cross_first, __cross_next, __VA_ARGS__)
#define __cross_first(args) for (__init_ ## args; \
((__test_ ## args) || \
(__fini_ ## args) && 0) && \
__prep_ ## args; \
__step_ ## args)
#define __cross_next(prev, args) prev __cross_first(args)
#define __init_first(args) __init_ ## args
#define __init_next(prev, args) prev, __init_ ## args
#define __test_first(args) __test_ ## args
#define __test_next(prev, args) prev, __test_ ## args
#define __prep_first(args) __prep_ ## args
#define __prep_next(prev, args) prev, __prep_ ## args
#define __fini_first(args) __fini_ ## args
#define __fini_next(prev, args) prev, __fini_ ## args
#define __step_first(args) __step_ ## args
#define __step_next(prev, args) prev, __step_ ## args
#define __init_str(idx, ch, str) idx = 1
#define __test_str(idx, ch, str) idx <= length(str)
#define __prep_str(idx, ch, str) ch = substr(str, idx, 1)
#define __fini_str(idx, ch, str) 1
#define __step_str(idx, ch, str) idx++
#define __init_range(idx, from, to) idx = (from)
#define __test_range(idx, from, to) idx <= (to)
#define __prep_range(idx, from, to) 1
#define __fini_range(idx, from, to) 1
#define __step_range(idx, from, to) idx++
#define __init_range_step(idx, from, to, step) idx = (from)
#define __test_range_step(idx, from, to, step) idx <= (to)
#define __prep_range_step(idx, from, to, step) 1
#define __fini_range_step(idx, from, to, step) 1
#define __step_range_step(idx, from, to, step) idx += (step)
#define __init_from(idx, from) idx = (from)
#define __test_from(idx, from) 1
#define __prep_from(idx, from) 1
#define __fini_from(idx, from) 1
#define __step_from(idx, from) idx++
#define __init_from_step(idx, from, step) idx = (from)
#define __test_from_step(idx, from, step) 1
#define __prep_from_step(idx, from, step) 1
#define __fini_from_step(idx, from, step) 1
#define __step_from_step(idx, from, step) idx += (step)
#define __init_list(iter, var, list) iter = (list)
#define __test_list(iter, var, list) !endp(iter)
#define __prep_list(iter, var, list) var = car(list)
#define __fini_list(iter, var, list) 0
#define __step_list(iter, var, list) iter = cdr(iter)
#define __init_fields(var) __g(var) = 1
#define __test_fields(var) __g(var) <= NF
#define __prep_fields(var) var = $ __g(var)
#define __fini_fields(var) 1
#define __step_fields(var) __g(var)++
#define __init_keys(key, array) __g(key) = keys(array)
#define __test_keys(key, array) !endp(__g(key))
#define __prep_keys(key, array) key = car(__g(key))
#define __fini_keys(key, array) 1
#define __step_keys(key, array) __g(key) = cdr(__g(key))
#define __init_collect(var, expr) var = list_begin()
#define __test_collect(var, expr) 1
#define __prep_collect(var, expr) var = list_add(var, expr)
#define __fini_collect(var, expr) var = list_end(var)
#define __step_collect(var, expr) 1
#define __init_lockstep(...) prog(__varexpand3(__init_first, __init_next, \
__VA_ARGS__))
#define __test_lockstep(...) __and(__varexpand3(__test_first, __test_next, \
__VA_ARGS__))
#define __prep_lockstep(...) (__and(__varexpand3(__prep_first, __prep_next, \
__VA_ARGS__)) || 1)
#define __fini_lockstep(...) (__and(__varexpand3(__fini_first, __fini_next, \
__VA_ARGS__)) && 0)
#define __step_lockstep(...) prog(__varexpand3(__step_first, __step_next, \
__VA_ARGS__))
#endif
|