aboutsummaryrefslogtreecommitdiffstats
path: root/cppawk-include/iter.h
diff options
context:
space:
mode:
Diffstat (limited to 'cppawk-include/iter.h')
-rw-r--r--cppawk-include/iter.h166
1 files changed, 166 insertions, 0 deletions
diff --git a/cppawk-include/iter.h b/cppawk-include/iter.h
new file mode 100644
index 0000000..57ff81e
--- /dev/null
+++ b/cppawk-include/iter.h
@@ -0,0 +1,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