aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cppawk-cons.1111
-rw-r--r--cppawk-include/cons.h1
-rw-r--r--testcases-cons25
3 files changed, 137 insertions, 0 deletions
diff --git a/cppawk-cons.1 b/cppawk-cons.1
index 1833c8f..0fa8f7a 100644
--- a/cppawk-cons.1
+++ b/cppawk-cons.1
@@ -118,6 +118,9 @@ cons \- Lisp-like data representation and control flow macros
dolisti(\fIitem\fP, \fIindex\fP, \fIlist\fP)
\fIstatement\fP
+ doconses(\fIsuffix\fP, \fIlist\fP)
+ \fIstatement\fP
+
\fI// stack-like list manipulation\fP
push(\fIy\fP, \fIx\fP) \fI// push item y onto x, updating location x\fP
@@ -2022,6 +2025,114 @@ contains boxed values, then those boxed values become fields.
print $\fIi\fP
.ft R
+.SH LIST ITERATION
+.bk
+.SS Macros \fIdolist\fP, \fIdolisti\fP and \fIdoconses\fP
+.bk
+Syntax:
+
+.ft B
+ dolist(\fIitem\fP, \fIlist\fP)
+ \fIstatement\fP
+
+ dolisti(\fIitem\fP, \fIindex\fP, \fIlist\fP)
+ \fIstatement\fP
+
+ doconses(\fIsuffix\fP, \fIlist\fP)
+ \fIstatement\fP
+.ft R
+
+.B Description
+
+The
+.BR dolist ,
+.BR dolisti
+and
+.B doconses
+macros provide an iteration construct for traversing lists.
+
+The
+.B dolist
+and
+.B dolisti
+macros require
+.I list
+to be an expression evaluating to a list. If the iteration is
+permitted to traverse the entire list (no early break out of the loop
+takes place), then
+.I list
+must be a proper list. The
+.I item
+and
+.I index
+arguments must be identifiers suitable for use as a variable name.
+The macro invocations must be immediately followed by a
+.IR statement ,
+which must be syntactically a statement.
+
+The
+.B dolisti
+macro evaluates the
+.I list
+expression and initializes an internal iterator. It then enters
+into a loop which visits every element of the list, and for
+each element, executes the
+.IR statement .
+On each iteration, the
+.I item
+variable is set to the next available element of the list, and
+.I index
+is set to a successive integer, starting from 0. Thus each
+.I item
+value is accompanied by an
+.I index
+value indicating that value's zero-based position in the list.
+
+The
+.B dolist
+macro is similar, except it takes no
+.I index
+argument, and consequently does not provide the index values.
+
+The
+.I item
+and
+.I index
+variables are assigned. If such variables are already visible, those
+variables are used. Moreover, after these loops execute, the variables
+remain visible, with their most recent values. If it is desirable for these
+variables to be local, the program must arrange that in the surrounding code.
+
+The
+.B doconses
+macro allows improper lists. It iterates the
+.I list
+over a
+.I statement
+exactly like
+.B dolist
+and
+.BR dolisti .
+The
+.I suffix
+argument must be an identifier suitable for use as a variable name,
+The
+.B doconses
+provides access to the conses of the list rather than the items.
+On the first iteration, the
+.I suffix
+variable is first set to the entire list. On the second iteration,
+it is set to the rest of the list. Then to the rest of that and
+so forth. For instance if the input list is
+.B "(1 2 3 . 4)"
+then
+.I suffix
+is stepped over the values
+.BR "(1 2 3 . 4)" ,
+.B "(2 3 . 4)"
+and finally the last cons cell
+.BR "(3 . 4)" .
+
.SH "SEE ALSO"
cppawk(1), cppawk-fun(1)
diff --git a/cppawk-include/cons.h b/cppawk-include/cons.h
index 6b7774e..fed6ffb 100644
--- a/cppawk-include/cons.h
+++ b/cppawk-include/cons.h
@@ -41,6 +41,7 @@
#define pop(list) __pop(list)
#define dolist(item, list) __dolist(item, list)
#define dolisti(item, index, list) __dolisti(item, index, list)
+#define doconses(iter, list) __doconses(iter, list)
#define list_begin() __list_begin()
#define list_add(stk, item) __list_add(stk, item)
#define list_end(stk) __list_end(stk)
diff --git a/testcases-cons b/testcases-cons
index ec7e6cb..4dc2788 100644
--- a/testcases-cons
+++ b/testcases-cons
@@ -774,3 +774,28 @@ BEGIN {
C1,1:12
foo
Tfoo
+--
+41:
+$cppawk '
+#include <cons.h>
+
+BEGIN {
+ dolist (x, list(1, 2, 3))
+ print x
+ print x
+ dolisti (x, i, list(1, 2, 3))
+ print x, i
+ doconses (s, listar(1, 2, 3, 4))
+ print sexp(s)
+}'
+:
+1
+2
+3
+3
+1 0
+2 1
+3 2
+(1 2 3 . 4)
+(2 3 . 4)
+(3 . 4)