diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-04-03 12:36:14 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-04-03 12:36:14 -0700 |
commit | ab409383337440bd12f9ce12e512572b2b19170e (patch) | |
tree | a7cc8d658829d7dcb51e516d96a194113a7a703a /cppawk-include | |
parent | 20394dfa62464334b202dcce7e592025baf7ffbd (diff) | |
download | cppawk-ab409383337440bd12f9ce12e512572b2b19170e.tar.gz cppawk-ab409383337440bd12f9ce12e512572b2b19170e.tar.bz2 cppawk-ab409383337440bd12f9ce12e512572b2b19170e.zip |
cons: split into private/public.
A couple of loop clause macros now match the public macros,
requiring a little adjustment.
Diffstat (limited to 'cppawk-include')
-rw-r--r-- | cppawk-include/cons-priv.h | 724 | ||||
-rw-r--r-- | cppawk-include/cons.h | 738 | ||||
-rw-r--r-- | cppawk-include/iter.h | 30 | ||||
-rw-r--r-- | cppawk-include/varg-priv.h | 68 |
4 files changed, 821 insertions, 739 deletions
diff --git a/cppawk-include/cons-priv.h b/cppawk-include/cons-priv.h new file mode 100644 index 0000000..f6acf8c --- /dev/null +++ b/cppawk-include/cons-priv.h @@ -0,0 +1,724 @@ +// 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_CONS_PRIV_H +#define __CPPAWK_CONS_PRIV_H + +#ifndef __CPPAWK_BASE_H +#include "base.h" +#endif + +#ifndef __CPPAWK_ITER_H +#include "iter.h" +#endif + +#ifndef __CPPAWK_CASE_PRIV_H +#include "case-priv.h" +#endif + +#ifndef __CPPAWK_VARG_PRIV_H +#include "varg-priv.h" +#endif + +#ifndef __CPPAWK_NARG_PRIV_H +#include "narg-priv.h" +#endif + +#if __have_indirect_functions +#ifndef __CPPAWK_FUN_H +#include "fun.h" +#endif +#endif + +#define __nil "" + +#define __prog_first(expr) ((expr) || 1) +#define __prog_next(prev, \ + expr) prev && ((expr) || 1) +#define __prog(...) (__varexpand(__prog_first, __prog_next, \ + __VA_ARGS__)) + +#define __progn_first(expr) (expr) +#define __progn_next(prev, \ + next) ((prev || 1) ? (next) : 0) +#define __progn(...) __varexpand(__progn_first, __progn_next, \ + __VA_ARGS__) + +#define __or_first(expr) (expr) +#define __or_next(prev, \ + next) (__false(__g(or) = prev) ? (next) : __g(or)) +#define __or(...) __varexpand(__or_first, __or_next, \ + __VA_ARGS__) + +#define __and_first(expr) (expr) +#define __and_next(prev, \ + next) (__false(prev) ? __nil : next) +#define __and(...) __varexpand(__and_first, __and_next, \ + __VA_ARGS__) + + +#define __push(item, list) (list = __cons(item, list)) + +#define __pop(list) (__progn(__g(ol) = (list), \ + list = __cdr(__g(ol)), \ + __car(__g(ol)))) + +#define __dolist(item, list) \ + for (__g(i) = list; \ + !__endp(__g(i)) && ((item = __car(__g(i))) || 1); \ + __g(i) = __cdr(__g(i))) + +#define __dolisti(item, index, list) \ + for (__prog(__g(i) = list, index = 0); \ + !__endp(__g(i)) && ((item = __car(__g(i))) || 1); \ + __prog(__g(i) = __cdr(__g(i)), index++)) + +#define __list_begin() "" +#define __list_add(stk, item) __pack(stk, item) +#define __list_end(stk) __unpack(stk) + +#define __bag_init_first(x) (x = __list_begin()) +#define __bag_init_next(p, x) (p || 1) && __bag_init_first(x) + +#define __bag_done_first(x) (x = __list_end(x)) +#define __bag_done_next(p, x) (p || 1) && __bag_done_first(x) + +#define __bags(...) for ((__g(once) = 1) && \ + __varexpand(__bag_init_first, \ + __bag_init_next, \ + __VA_ARGS__); \ + __g(once) || \ + __varexpand(__bag_done_first, \ + __bag_done_next, \ + __VA_ARGS__) && 0; \ + __g(once) = 0) +#define __bag(bag, expr) bag = __list_add(bag, expr) + +#define __typecode(obj) (substr(obj, 1, 1)) + +function __consp(__obj) +{ + return __typecode(__obj) == "C" +} +#define __consp(obj) (__typecode(obj) == "C") + +function __atom(__obj) +{ + return __typecode(__obj) != "C" +} +#define __atom(obj) (__typecode(obj) != "C") + +function __null(__obj) +{ + return __obj == __nil +} +#define __null(obj) ((obj) == __nil) + +function __endp(__obj) +{ + if (__obj == __nil) + return 1 + if (__consp(__obj)) + return __nil + __error("endp: a proper list ends with nil, not %s", __obj) +} + +#if __config_strict_false +function __false(__obj) +{ + return __obj == __nil +} +#define __false(obj) __null(obj) +#else +function __false(__obj) +{ + return !obj +} +#define __false(obj) (!(obj)) +#endif + +function __true(__obj) +{ + return !__false(__obj) +} +#define __true(obj) (!__false(obj)) + + +function __bs_esc(raw) +{ + gsub(/[\\\042]/, "\\\\&", raw) + return raw +} + +function __nump(__obj) +{ + return __obj == 0 || __obj + 0 != 0 +} +#define __nump(obj) ((obj) == 0 || (obj) + 0 != 0) + +function __box(__raw, + __case_temps) +{ + __case (__typecode(__raw)) { + __of ("T", "S", "C") + __cret ("T" __raw) // represent as unescaped string + __otherwise + __cret (__raw) + } +} + +function __unbox(__obj, + __case_temps) +{ + __case (__typecode(__obj)) { + __of ("T", "S") + __cret (substr(__obj, 2)) + __of ("C") + __error("unbox: %s looks like a cons", __obj, i) + __cbreak + __otherwise + __cret (__obj) + } +} + +function __sym(__name) +{ + return "S" __name +} + +function __cons(__a, __d) +{ + return "C" length(__a) "," length(__d) ":" __a __d +} + +function __car(__cell, + __col, __com, len) +{ + if (__consp(__cell)) { + __col = match(__cell, /:/) + __com = match(__cell, /,/) + if (__col == 0 || __com == 0 || __col <= __com) + __error("car: %s has a malformed cons header", __cell) + len = substr(__cell, 2, __com - 2 + 1) + return substr(__cell, __col + 1, len) + } else if (__null(__cell)) { + return __nil + } else { + __error("car: %s isn't a cons", __cell) + } +} + +function __cdr(__cell, + __col, __com, __alen, __dlen) +{ + if (__consp(__cell)) { + __col = match(__cell, /:/) + __com = match(__cell, /,/) + if (__col == 0 || __com == 0 || __col <= __com) + __error("car: %s has a malformed cons header", __cell) + __alen = substr(__cell, 2, __com - 2 + 1) + __dlen = substr(__cell, __com + 1, __col - __com) + return substr(__cell, __col + 1 + __alen, __dlen) + } else if (__null(__cell)) { + return __nil + } else { + __error("car: %s isn't a cons", __cell) + } +} + +function __sexp(__obj, + __d, __x, __y, __case_temps) +{ + __case (__typecode(__obj)) { + __of ("C") + __d = __cdr(__obj) + __x = __sexp(__car(__obj)) + if (__null(__d)) { + return "(" __x ")" + } else { + __y = __sexp(__d) + if (__typecode(__d) == "C") + return "(" __x " " substr(__y, 2) + else + return "(" __x " . " __y ")" + } + __cbreak + __of ("T") + __cret ("\"" __bs_esc(substr(__obj, 2)) "\"") + __of ("S") + __cret (__bs_esc(substr(__obj, 2))) + } + + if (__nump(__obj)) { + return __obj + } else if (__null(__obj)) { + return "nil" + } else { + return "\"" __bs_esc(__obj) "\"" + } +} + +function __reverse(__lst, + __tmp) +{ + __tmp = __nil + + while (!__endp(__lst)) { + __push(__car(__lst), __tmp); + __lst = __cdr(__lst); + } + + return __tmp; +} + +function __atol(__arr, + __tmp, __key, __value) +{ + __tmp = __list_begin() + + doarray (__key, __value, __arr) + __tmp = __list_add(__tmp,__value) + + return __list_end(__tmp) +} + +function __ftol( __tmp, __i, __val) +{ + __tmp = __list_begin() + + dofields (__i, __val) + __tmp = __list_add(__tmp, __val) + + return __list_end(__tmp) +} + +function __ltof(__lst, __start, + __tmp, __item, __n) +{ + if (!__present(__start)) + __start = 1 + + __dolisti (__item, __n, __lst) + $(__n + __start) = __item + + NF = __n + __start - 1 + + return __n +} + +function __keys(__array, + __key, __tmp) +{ + __tmp = __list_begin() + + for (__key in __array) + __tmp = __list_add(__tmp, __key) + + return __list_end(__tmp) +} + +function __slow_equal(__obj1, __obj2, + __tc1, __tc2, __case_temps) +{ + __tc1 = __typecode(__obj1) + __tc2 = __typecode(__obj2) + + __case (__tc1 __tc2) { + __of ("CC") + __cret (__equal(__car(__obj1), __car(__obj2)) && + __equal(__cdr(__obj1), __cdr(__obj2))) + __matching (/[TSC][TSC]/) + __cret (0); + } + + if (__tc1 == "T") + return __unbox(__obj1) == __obj2; + + if (__tc2 == "T") + return __obj1 == __unbox(__obj2); + + if (__nump(__obj1)) + return __obj1 + 0 == __obj2 + + return 0; +} + +function __equal(__obj1, __obj2) +{ + if (__obj1 == __obj2) + return 1; + return __slow_equal(__obj1, __obj2) +} + +#define __equal(obj1, obj2) ((obj1) == (obj2) ? 1 : __slow_equal(obj1, obj2)) + +function __pack(__stk, __item) +{ + return length(__item) ":" __item __stk +} + +function __unpack(__stk, + __col, __out) +{ + __out = __nil + + while (__stk != "") { + __col = match(__stk, /:/) + if (__col == 0) + break; + __push(substr(__stk, __col + 1, __stk), __out) + __stk = substr(__stk, __col + 1 + __stk); + } + + return __out +} + + +function __list(__rest, + __check) +{ + return __arglist("list", __rest, __check) +} +#define __list_first(item) __cons(item, nil) +#define __list_next(list, item) __cons(item, list) +#define __li(...) __varexpand(__list_first, \ + __list_next, \ + __revarg(__VA_ARGS__)) + +#define __listar_first(item) item +#define __listar(...) __varexpand(__listar_first, \ + __list_next, \ + __revarg(__VA_ARGS__)) + +function __append2(__left, __right, + tmp) +{ + tmp = __reverse(__left) + while (!__endp(tmp)) + __right = __cons(__pop(tmp), __right) + return __right +} + +function __append(__rest, + __count, __out) +{ + __varg_count("append", __count, __count) + __out = __nil + __case (__count) { + __of (32) + __out = __r32 + __cbreak + __of (31) + __out = __r31 + __cbreak + __of (30) + __out = __r30 + __cbreak + __of (29) + __out = __r29 + __cbreak + __of (28) + __out = __r28 + __cbreak + __of (27) + __out = __r27 + __cbreak + __of (26) + __out = __r26 + __cbreak + __of (25) + __out = __r25 + __cbreak + __of (24) + __out = __r24 + __cbreak + __of (23) + __out = __r23 + __cbreak + __of (22) + __out = __r22 + __cbreak + __of (21) + __out = __r21 + __cbreak + __of (20) + __out = __r20 + __cbreak + __of (19) + __out = __r19 + __cbreak + __of (18) + __out = __r18 + __cbreak + __of (17) + __out = __r17 + __cbreak + __of (16) + __out = __r16 + __cbreak + __of (15) + __out = __r15 + __cbreak + __of (14) + __out = __r14 + __cbreak + __of (13) + __out = __r13 + __cbreak + __of (12) + __out = __r12 + __cbreak + __of (11) + __out = __r11 + __cbreak + __of (10) + __out = __r10 + __cbreak + __of (9) + __out = __r9 + __cbreak + __of (8) + __out = __r8 + __cbreak + __of (7) + __out = __r7 + __cbreak + __of (6) + __out = __r6 + __cbreak + __of (5) + __out = __r5 + __cbreak + __of (4) + __out = __r4 + __cbreak + __of (3) + __out = __r3 + __cbreak + __of (2) + __out = __r2 + __cbreak + __of (1) + __out = __r1 + __cbreak + } + __case (__count - 1) { + __of (31) + __out = __append2(__r31, __out) + __cfall + __of (30) + __out = __append2(__r30, __out) + __cfall + __of (29) + __out = __append2(__r29, __out) + __cfall + __of (28) + __out = __append2(__r28, __out) + __cfall + __of (27) + __out = __append2(__r27, __out) + __cfall + __of (26) + __out = __append2(__r26, __out) + __cfall + __of (25) + __out = __append2(__r25, __out) + __cfall + __of (24) + __out = __append2(__r24, __out) + __cfall + __of (23) + __out = __append2(__r23, __out) + __cfall + __of (22) + __out = __append2(__r22, __out) + __cfall + __of (21) + __out = __append2(__r21, __out) + __cfall + __of (20) + __out = __append2(__r20, __out) + __cfall + __of (19) + __out = __append2(__r19, __out) + __cfall + __of (18) + __out = __append2(__r18, __out) + __cfall + __of (17) + __out = __append2(__r17, __out) + __cfall + __of (16) + __out = __append2(__r16, __out) + __cfall + __of (15) + __out = __append2(__r15, __out) + __cfall + __of (14) + __out = __append2(__r14, __out) + __cfall + __of (13) + __out = __append2(__r13, __out) + __cfall + __of (12) + __out = __append2(__r12, __out) + __cfall + __of (11) + __out = __append2(__r11, __out) + __cfall + __of (10) + __out = __append2(__r10, __out) + __cfall + __of (9) + __out = __append2(__r9, __out) + __cfall + __of (8) + __out = __append2(__r8, __out) + __cfall + __of (7) + __out = __append2(__r7, __out) + __cfall + __of (6) + __out = __append2(__r6, __out) + __cfall + __of (5) + __out = __append2(__r5, __out) + __cfall + __of (4) + __out = __append2(__r4, __out) + __cfall + __of (3) + __out = __append2(__r3, __out) + __cfall + __of (2) + __out = __append2(__r2, __out) + __cfall + __of (1) + __out = __append2(__r1, __out) + __cfall + } + + return __out +} + +function __member(__item, __lst) +{ + while (!__endp(__lst)) { + if (__equal(__item, __car(__lst))) + return __lst; + __lst = __cdr(__lst) + } + + return __nil +} + +function __position(__item, __lst, + __pos) +{ + for (__pos = 0; !__endp(__lst); __pos++) { + if (__equal(__item, __car(__lst))) + return __pos; + __lst = __cdr(__lst) + } + + return __nil +} + +function __nth(__pos, __lst) +{ + for (; __pos > 0 && !__endp(__lst); __pos--) + __lst = __cdr(__lst) + return __car(__lst) +} + +function __nthcdr(__pos, __lst) +{ + for (; __pos > 0 && !__endp(__lst); __pos--) + __lst = __cdr(__lst) + return __lst +} + +function __iota(__from, __to, __step, + __i, __out) +{ + __out = __list_begin(); + + if (__from < __to) { + if (!__present(__step)) + __step = 1 + if (__step > 0) + for (__i = __from; __i <= __to; __i += __step) + __out = __list_add(__out, __i) + } else { + if (!__present(__step)) + __step = -1 + if (__step < 0) + for (__i = __from; __i >= __to; __i += __step) + __out = __list_add(__out, __i) + } + + return __list_end(__out) +} + +function __uniq(__lst, + __out, __item, __seen) +{ + __out = __list_begin(); + + __dolist(__item, __lst) { + if (__item in __seen) + continue + __seen[__item] + __out = __list_add(__out, __item) + } + + return __list_end(__out) +} + +#if __have_indirect_functions + +function __mapcar(__fun, __lst, + __temp, __item, out) +{ + __temp = __list_begin() + __dolist(__item, __lst) + __temp = __list_add(__temp, call(__fun, __item)) + return __list_end(__temp) +} + +function __mappend(__fun, __lst, + __temp, __item, out) +{ + __temp = __nil + __dolist(__item, __lst) + __temp = __append2(__temp, call(__fun, __item)) + return __temp +} + +#endif + +#endif diff --git a/cppawk-include/cons.h b/cppawk-include/cons.h index aaea3bf..3f4af02 100644 --- a/cppawk-include/cons.h +++ b/cppawk-include/cons.h @@ -28,697 +28,55 @@ #ifndef __CPPAWK_CONS_H #define __CPPAWK_CONS_H -#ifndef __CPPAWK_BASE_H -#include "base.h" +#ifndef __CPPAWK_CONS_PRIV_H +#include "cons-priv.h" #endif -#ifndef __CPPAWK_ITER_H -#include "iter.h" -#endif - -#ifndef __CPPAWK_CASE_PRIV_H -#include "case-priv.h" -#endif - -#ifndef __CPPAWK_VARG_PRIV_H -#include "varg-priv.h" -#endif - -#ifndef __CPPAWK_NARG_PRIV_H -#include "narg-priv.h" -#endif - -#if __have_indirect_functions -#ifndef __CPPAWK_FUN_H -#include "fun.h" -#endif -#endif - -#define nil "" - -#define __prog_first(expr) ((expr) || 1) -#define __prog_next(prev, \ - expr) prev && ((expr) || 1) -#define prog(...) (__varexpand(__prog_first, __prog_next, \ - __VA_ARGS__)) - -#define __progn_first(expr) (expr) -#define __progn_next(prev, \ - next) ((prev || 1) ? (next) : 0) -#define progn(...) __varexpand(__progn_first, __progn_next, \ - __VA_ARGS__) - -#define __or_first(expr) (expr) -#define __or_next(prev, \ - next) (false(__g(or) = prev) ? (next) : __g(or)) -#define or(...) __varexpand(__or_first, __or_next, \ - __VA_ARGS__) - -#define __and_first(expr) (expr) -#define __and_next(prev, \ - next) (false(prev) ? nil : next) -#define and(...) __varexpand(__and_first, __and_next, \ - __VA_ARGS__) - - -#define push(item, list) (list = cons(item, list)) - -#define pop(list) (progn(__g(ol) = (list), \ - list = cdr(__g(ol)), \ - car(__g(ol)))) - -#define dolist(item, list) \ - for (__g(i) = list; \ - !endp(__g(i)) && ((item = car(__g(i))) || 1); \ - __g(i) = cdr(__g(i))) - -#define dolisti(item, index, list) \ - for (prog(__g(i) = list, index = 0); \ - !endp(__g(i)) && ((item = car(__g(i))) || 1); \ - prog(__g(i) = cdr(__g(i)), index++)) - -#define list_begin() "" -#define list_add(stk, item) __pack(stk, item) -#define list_end(stk) __unpack(stk) - -#define __bag_init_first(x) (x = list_begin()) -#define __bag_init_next(p, x) (p || 1) && __bag_init_first(x) - -#define __bag_done_first(x) (x = list_end(x)) -#define __bag_done_next(p, x) (p || 1) && __bag_done_first(x) - -#define bags(...) for ((__g(once) = 1) && \ - __varexpand(__bag_init_first, \ - __bag_init_next, \ - __VA_ARGS__); \ - __g(once) || \ - __varexpand(__bag_done_first, \ - __bag_done_next, \ - __VA_ARGS__) && 0; \ - __g(once) = 0) -#define bag(bag, expr) bag = list_add(bag, expr) - -#define typecode(obj) (substr(obj, 1, 1)) - -function consp(__obj) -{ - return typecode(__obj) == "C" -} -#define consp(obj) (typecode(obj) == "C") - -function atom(__obj) -{ - return typecode(__obj) != "C" -} -#define atom(obj) (typecode(obj) != "C") - -function null(__obj) -{ - return __obj == nil -} -#define null(obj) ((obj) == nil) - -function endp(__obj) -{ - if (__obj == nil) - return 1 - if (consp(__obj)) - return nil - __error("endp: a proper list ends with nil, not %s", __obj) -} - -#if __config_strict_false -function false(__obj) -{ - return __obj == nil -} -#define false(obj) null(obj) -#else -function false(__obj) -{ - return !obj -} -#define false(obj) (!(obj)) -#endif - -function true(__obj) -{ - return !false(__obj) -} -#define true(obj) (!false(obj)) - - -function __bs_esc(raw) -{ - gsub(/[\\\042]/, "\\\\&", raw) - return raw -} - -function nump(__obj) -{ - return __obj == 0 || __obj + 0 != 0 -} -#define nump(obj) ((obj) == 0 || (obj) + 0 != 0) - -function box(__raw, - __case_temps) -{ - __case (typecode(__raw)) { - __of ("T", "S", "C") - __cret ("T" __raw) // represent as unescaped string - __otherwise - __cret (__raw) - } -} - -function unbox(__obj, - __case_temps) -{ - __case (typecode(__obj)) { - __of ("T", "S") - __cret (substr(__obj, 2)) - __of ("C") - __error("unbox: %s looks like a cons", __obj, i) - __cbreak - __otherwise - __cret (__obj) - } -} - -function sym(__name) -{ - return "S" __name -} - -function cons(__a, __d) -{ - return "C" length(__a) "," length(__d) ":" __a __d -} - -function car(__cons, - __col, __com, len) -{ - if (consp(__cons)) { - __col = match(__cons, /:/) - __com = match(__cons, /,/) - if (__col == 0 || __com == 0 || __col <= __com) - __error("car: %s has a malformed cons header", __cons) - len = substr(__cons, 2, __com - 2 + 1) - return substr(__cons, __col + 1, len) - } else if (null(__cons)) { - return nil - } else { - __error("car: %s isn't a cons", __cons) - } -} - -function cdr(__cons, - __col, __com, __alen, __dlen) -{ - if (consp(__cons)) { - __col = match(__cons, /:/) - __com = match(__cons, /,/) - if (__col == 0 || __com == 0 || __col <= __com) - __error("car: %s has a malformed cons header", __cons) - __alen = substr(__cons, 2, __com - 2 + 1) - __dlen = substr(__cons, __com + 1, __col - __com) - return substr(__cons, __col + 1 + __alen, __dlen) - } else if (null(__cons)) { - return nil - } else { - __error("car: %s isn't a cons", __cons) - } -} - -function sexp(__obj, - __d, __x, __y, __case_temps) -{ - __case (typecode(__obj)) { - __of ("C") - __d = cdr(__obj) - __x = sexp(car(__obj)) - if (null(__d)) { - return "(" __x ")" - } else { - __y = sexp(__d) - if (typecode(__d) == "C") - return "(" __x " " substr(__y, 2) - else - return "(" __x " . " __y ")" - } - __cbreak - __of ("T") - __cret ("\"" __bs_esc(substr(__obj, 2)) "\"") - __of ("S") - __cret (__bs_esc(substr(__obj, 2))) - } - - if (nump(__obj)) { - return __obj - } else if (null(__obj)) { - return "nil" - } else { - return "\"" __bs_esc(__obj) "\"" - } -} - -function reverse(__list, - __tmp) -{ - __tmp = nil - - while (!endp(__list)) { - push(car(__list), __tmp); - __list = cdr(__list); - } - - return __tmp; -} - -function atol(__arr, - __tmp, __key, __value) -{ - __tmp = list_begin() - - doarray (__key, __value, __arr) - __tmp = list_add(__tmp,__value) - - return list_end(__tmp) -} - -function ftol( __tmp, __i, __val) -{ - __tmp = list_begin() - - dofields (__i, __val) - __tmp = list_add(__tmp, __val) - - return list_end(__tmp) -} - -function ltof(__list, __start, - __tmp, __item, __n) -{ - if (!__present(__start)) - __start = 1 - - dolisti (__item, __n, __list) - $(__n + __start) = __item - - NF = __n + __start - 1 - - return __n -} - -function keys(__array, - __key, __tmp) -{ - __tmp = list_begin() - - for (__key in __array) - __tmp = list_add(__tmp, __key) - - return list_end(__tmp) -} - -function __slow_equal(__obj1, __obj2, - __tc1, __tc2, __case_temps) -{ - __tc1 = typecode(__obj1) - __tc2 = typecode(__obj2) - - __case (__tc1 __tc2) { - __of ("CC") - __cret (equal(car(__obj1), car(__obj2)) && - equal(cdr(__obj1), cdr(__obj2))) - __matching (/[TSC][TSC]/) - __cret (0); - } - - if (__tc1 == "T") - return unbox(__obj1) == __obj2; - - if (__tc2 == "T") - return __obj1 == unbox(__obj2); - - if (nump(__obj1)) - return __obj1 + 0 == __obj2 - - return 0; -} - -function equal(__obj1, __obj2) -{ - if (__obj1 == __obj2) - return 1; - return __slow_equal(__obj1, __obj2) -} - -#define equal(obj1, obj2) ((obj1) == (obj2) ? 1 : __slow_equal(obj1, obj2)) - -function __pack(__stk, __item) -{ - return length(__item) ":" __item __stk -} - -function __unpack(__stk, - __col, __out) -{ - __out = nil - - while (__stk != "") { - __col = match(__stk, /:/) - if (__col == 0) - break; - push(substr(__stk, __col + 1, __stk), __out) - __stk = substr(__stk, __col + 1 + __stk); - } - - return __out -} - - -function list(__rest, - __check) -{ - return __arglist("list", __rest, __check) -} -#define __list_first(item) cons(item, nil) -#define __list_next(list, item) cons(item, list) -#define li(...) __varexpand(__list_first, \ - __list_next, \ - __revarg(__VA_ARGS__)) - -#define __listar_first(item) item -#define listar(...) __varexpand(__listar_first, \ - __list_next, \ - __revarg(__VA_ARGS__)) - -function __append(__left, __right, - tmp) -{ - tmp = reverse(__left) - while (!endp(tmp)) - __right = cons(pop(tmp), __right) - return __right -} - -function append(__rest, - __count, __out) -{ - __varg_count("append", __count, __count) - __out = nil - __case (__count) { - __of (32) - __out = __r32 - __cbreak - __of (31) - __out = __r31 - __cbreak - __of (30) - __out = __r30 - __cbreak - __of (29) - __out = __r29 - __cbreak - __of (28) - __out = __r28 - __cbreak - __of (27) - __out = __r27 - __cbreak - __of (26) - __out = __r26 - __cbreak - __of (25) - __out = __r25 - __cbreak - __of (24) - __out = __r24 - __cbreak - __of (23) - __out = __r23 - __cbreak - __of (22) - __out = __r22 - __cbreak - __of (21) - __out = __r21 - __cbreak - __of (20) - __out = __r20 - __cbreak - __of (19) - __out = __r19 - __cbreak - __of (18) - __out = __r18 - __cbreak - __of (17) - __out = __r17 - __cbreak - __of (16) - __out = __r16 - __cbreak - __of (15) - __out = __r15 - __cbreak - __of (14) - __out = __r14 - __cbreak - __of (13) - __out = __r13 - __cbreak - __of (12) - __out = __r12 - __cbreak - __of (11) - __out = __r11 - __cbreak - __of (10) - __out = __r10 - __cbreak - __of (9) - __out = __r9 - __cbreak - __of (8) - __out = __r8 - __cbreak - __of (7) - __out = __r7 - __cbreak - __of (6) - __out = __r6 - __cbreak - __of (5) - __out = __r5 - __cbreak - __of (4) - __out = __r4 - __cbreak - __of (3) - __out = __r3 - __cbreak - __of (2) - __out = __r2 - __cbreak - __of (1) - __out = __r1 - __cbreak - } - __case (__count - 1) { - __of (31) - __out = __append(__r31, __out) - __cfall - __of (30) - __out = __append(__r30, __out) - __cfall - __of (29) - __out = __append(__r29, __out) - __cfall - __of (28) - __out = __append(__r28, __out) - __cfall - __of (27) - __out = __append(__r27, __out) - __cfall - __of (26) - __out = __append(__r26, __out) - __cfall - __of (25) - __out = __append(__r25, __out) - __cfall - __of (24) - __out = __append(__r24, __out) - __cfall - __of (23) - __out = __append(__r23, __out) - __cfall - __of (22) - __out = __append(__r22, __out) - __cfall - __of (21) - __out = __append(__r21, __out) - __cfall - __of (20) - __out = __append(__r20, __out) - __cfall - __of (19) - __out = __append(__r19, __out) - __cfall - __of (18) - __out = __append(__r18, __out) - __cfall - __of (17) - __out = __append(__r17, __out) - __cfall - __of (16) - __out = __append(__r16, __out) - __cfall - __of (15) - __out = __append(__r15, __out) - __cfall - __of (14) - __out = __append(__r14, __out) - __cfall - __of (13) - __out = __append(__r13, __out) - __cfall - __of (12) - __out = __append(__r12, __out) - __cfall - __of (11) - __out = __append(__r11, __out) - __cfall - __of (10) - __out = __append(__r10, __out) - __cfall - __of (9) - __out = __append(__r9, __out) - __cfall - __of (8) - __out = __append(__r8, __out) - __cfall - __of (7) - __out = __append(__r7, __out) - __cfall - __of (6) - __out = __append(__r6, __out) - __cfall - __of (5) - __out = __append(__r5, __out) - __cfall - __of (4) - __out = __append(__r4, __out) - __cfall - __of (3) - __out = __append(__r3, __out) - __cfall - __of (2) - __out = __append(__r2, __out) - __cfall - __of (1) - __out = __append(__r1, __out) - __cfall - } - - return __out -} - -function member(__item, __list) -{ - while (!endp(__list)) { - if (equal(__item, car(__list))) - return __list; - __list = cdr(__list) - } - - return nil -} - -function position(__item, __list, - __pos) -{ - for (__pos = 0; !endp(__list); __pos++) { - if (equal(__item, car(__list))) - return __pos; - __list = cdr(__list) - } - - return nil -} - -function nth(__pos, __list) -{ - for (; __pos > 0 && !endp(__list); __pos--) - __list = cdr(__list) - return car(__list) -} - -function nthcdr(__pos, __list) -{ - for (; __pos > 0 && !endp(__list); __pos--) - __list = cdr(__list) - return __list -} - -function iota(__from, __to, __step, - i, __out) -{ - __out = list_begin(); - - if (__from < __to) { - if (!__present(__step)) - __step = 1 - if (__step > 0) - for (__i = __from; __i <= __to; __i += __step) - __out = list_add(__out, __i) - } else { - if (!__present(__step)) - __step = -1 - if (__step < 0) - for (__i = __from; __i >= __to; __i += __step) - __out = list_add(__out, __i) - } - - return list_end(__out) -} - -function uniq(__list, - __out, __item, __seen) -{ - __out = list_begin(); - - dolist(__item, __list) { - if (__item in __seen) - continue - __seen[__item] - __out = list_add(__out, __item) - } - - return list_end(__out) -} - -#if __have_indirect_functions - -function mapcar(__fun, __list, - __temp, __item, out) -{ - __temp = list_begin() - dolist(__item, __list) - __temp = list_add(__temp, call(__fun, __item)) - return list_end(__temp) -} - -function mappend(__fun, __list, - __temp, __item, out) -{ - __temp = nil - dolist(__item, __list) - __temp = __append(__temp, call(__fun, __item)) - return __temp -} - -#endif +#define nil __nil +#define prog(...) __prog(__VA_ARGS__) +#define progn(...) __progn(__VA_ARGS__) +#define or(...) __varexpand(__VA_ARGS__) +#define and(...) __varexpand(__VA_ARGS__) +#define push(item, list) __push(item, list) +#define pop(list) __pop(list) +#define dolist(item, list) __dolist(item, list) +#define dolisti(item, index, list) __dolisti(item, index, list) +#define list_begin() __list_begin() +#define list_add(stk, item) __list_add(stk, item) +#define list_end(stk) __list_end(stk) +#define bags(...) __bags(__VA_ARGS__) +#define bag(bag, expr) __bag(bag, expr) +#define consp __consp +#define atom __atom +#define null __null +#define endp __endp +#define false __false +#define true __true +#define nump __nump +#define box __box +#define unbox __unbox +#define sym __sym +#define cons __cons +#define car __car +#define cdr __cdr +#define sexp __sexp +#define reverse __reverse +#define atol __atol +#define ftol __ftol +#define ltof __ltof +#define keys __keys +#define equal __equal +#define list __list +#define li __li +#define listar __listar +#define append __append +#define member __member +#define position __position +#define nth __nth +#define nthcdr __nthcdr +#define iota __iota +#define uniq __uniq +#define mapcar __mapcar +#define mappend __mappend #endif diff --git a/cppawk-include/iter.h b/cppawk-include/iter.h index 72f8acb..1cc3c7b 100644 --- a/cppawk-include/iter.h +++ b/cppawk-include/iter.h @@ -62,7 +62,7 @@ #define __loop_init(...) prog(__varexpand(__init_first, __init_next, \ __VA_ARGS__)) -#define __loop_test(...) __and(__varexpand(__test_first, __test_next, \ +#define __loop_test(...) __nd(__varexpand(__test_first, __test_next, \ __VA_ARGS__)) #define __loop_prep(...) prog(__varexpand(__prep_first, __prep_next, \ __VA_ARGS__)) @@ -71,7 +71,7 @@ #define __loop_step(...) prog(__varexpand(__step_first, __step_next, \ __VA_ARGS__)) -#define __and(...) __varexpand(__nd_first, __nd_next, __VA_ARGS__) +#define __nd(...) __varexpand(__nd_first, __nd_next, __VA_ARGS__) #define __nd_first(x) (x) #define __nd_next(p, x) p && (x) @@ -137,11 +137,11 @@ #define for_var(var, expr) first_then(var, expr, expr) -#define __init_list(iter, var, list) iter = (list) -#define __test_list(iter, var, list) !endp(iter) -#define __prep_list(iter, var, list) var = car(iter) -#define __fini_list(iter, var, list) 0 -#define __step_list(iter, var, list) iter = cdr(iter) +#define __init___list(iter, var, list) iter = (list) +#define __test___list(iter, var, list) !endp(iter) +#define __prep___list(iter, var, list) var = car(iter) +#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 @@ -149,11 +149,11 @@ #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___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 @@ -247,11 +247,11 @@ function __loop_argmin(a, arga, b, argb) #define __init_lockstep(...) prog(__varexpand3(__init_first, __init_next, \ __VA_ARGS__)) -#define __test_lockstep(...) __and(__varexpand3(__test_first, __test_next, \ +#define __test_lockstep(...) __nd(__varexpand3(__test_first, __test_next, \ __VA_ARGS__)) -#define __prep_lockstep(...) (__and(__varexpand3(__prep_first, __prep_next, \ +#define __prep_lockstep(...) (__nd(__varexpand3(__prep_first, __prep_next, \ __VA_ARGS__)) || 1) -#define __fini_lockstep(...) (__and(__varexpand3(__fini_first, __fini_next, \ +#define __fini_lockstep(...) (__nd(__varexpand3(__fini_first, __fini_next, \ __VA_ARGS__)) && 0) #define __step_lockstep(...) prog(__varexpand3(__step_first, __step_next, \ __VA_ARGS__)) diff --git a/cppawk-include/varg-priv.h b/cppawk-include/varg-priv.h index 4bb9a47..fc4e432 100644 --- a/cppawk-include/varg-priv.h +++ b/cppawk-include/varg-priv.h @@ -158,105 +158,105 @@ } \ } -#define __varg_list(name, check, count, out) \ - __varg_count(name, check, count); \ +#define __varg_list(name, check, count, out) \ + __varg_count(name, check, count); \ out = nil; \ __case (count) { \ __of (32) \ - out = cons(__r32, out); \ + out = __cons(__r32, out); \ __cfall; \ __of (31) \ - out = cons(__r31, out); \ + out = __cons(__r31, out); \ __cfall; \ __of (30) \ - out = cons(__r30, out); \ + out = __cons(__r30, out); \ __cfall; \ __of (29) \ - out = cons(__r29, out); \ + out = __cons(__r29, out); \ __cfall; \ __of (28) \ - out = cons(__r28, out); \ + out = __cons(__r28, out); \ __cfall; \ __of (27) \ - out = cons(__r27, out); \ + out = __cons(__r27, out); \ __cfall; \ __of (26) \ - out = cons(__r26, out); \ + out = __cons(__r26, out); \ __cfall; \ __of (25) \ - out = cons(__r25, out); \ + out = __cons(__r25, out); \ __cfall; \ __of (24) \ - out = cons(__r24, out); \ + out = __cons(__r24, out); \ __cfall; \ __of (23) \ - out = cons(__r23, out); \ + out = __cons(__r23, out); \ __cfall; \ __of (22) \ - out = cons(__r22, out); \ + out = __cons(__r22, out); \ __cfall; \ __of (21) \ - out = cons(__r21, out); \ + out = __cons(__r21, out); \ __cfall; \ __of (20) \ - out = cons(__r20, out); \ + out = __cons(__r20, out); \ __cfall; \ __of (19) \ - out = cons(__r19, out); \ + out = __cons(__r19, out); \ __cfall; \ __of (18) \ - out = cons(__r18, out); \ + out = __cons(__r18, out); \ __cfall; \ __of (17) \ - out = cons(__r17, out); \ + out = __cons(__r17, out); \ __cfall; \ __of (16) \ - out = cons(__r16, out); \ + out = __cons(__r16, out); \ __cfall; \ __of (15) \ - out = cons(__r15, out); \ + out = __cons(__r15, out); \ __cfall; \ __of (14) \ - out = cons(__r14, out); \ + out = __cons(__r14, out); \ __cfall; \ __of (13) \ - out = cons(__r13, out); \ + out = __cons(__r13, out); \ __cfall; \ __of (12) \ - out = cons(__r12, out); \ + out = __cons(__r12, out); \ __cfall; \ __of (11) \ - out = cons(__r11, out); \ + out = __cons(__r11, out); \ __cfall; \ __of (10) \ - out = cons(__r10, out); \ + out = __cons(__r10, out); \ __cfall; \ __of (9) \ - out = cons(__r9, out); \ + out = __cons(__r9, out); \ __cfall; \ __of (8) \ - out = cons(__r8, out); \ + out = __cons(__r8, out); \ __cfall; \ __of (7) \ - out = cons(__r7, out); \ + out = __cons(__r7, out); \ __cfall; \ __of (6) \ - out = cons(__r6, out); \ + out = __cons(__r6, out); \ __cfall; \ __of (5) \ - out = cons(__r5, out); \ + out = __cons(__r5, out); \ __cfall; \ __of (4) \ - out = cons(__r4, out); \ + out = __cons(__r4, out); \ __cfall; \ __of (3) \ - out = cons(__r3, out); \ + out = __cons(__r3, out); \ __cfall; \ __of (2) \ - out = cons(__r2, out); \ + out = __cons(__r2, out); \ __cfall; \ __of (1) \ - out = cons(__r1, out); \ + out = __cons(__r1, out); \ __cfall; \ } |