Debian Codegraph @

src/c/mapfun.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * mapfun.d - mapping
6 *
7 * Copyright (c) 1993 Giuseppe Attardi
8 * Copyright (c) 2001 Juan Jose Garcia Ripoll
9 *
10 * See file 'LICENSE' for the copyright details.
11 *
12 */
13
14 #include <ecl/ecl.h>
15 #include <ecl/internal.h>
16 #include <string.h>
17
18 #define PREPARE_MAP(env, list, cdrs_frame, cars_frame, narg) \
19 struct ecl_stack_frame frames_aux[2]; \
20 const cl_object cdrs_frame = (cl_object)frames_aux; \
21 const cl_object cars_frame = (cl_object)(frames_aux+1); \
22 ECL_STACK_FRAME_FROM_VA_LIST(env,cdrs_frame,list); \
23 ECL_STACK_FRAME_COPY(cars_frame, cdrs_frame); \
24 narg = cars_frame->frame.size; \
25 if (ecl_unlikely(narg == 0)) { \
26 FEprogram_error("MAP*: Too few arguments", 0); \
27 }
28
29 @(defun mapcar (fun &rest lists)
30 cl_object res, *val = &res;
31 @ {
32 PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
33 res = ECL_NIL;
34 while (TRUE) {
35 cl_index i;
36 for (i = 0; i < narg; i++) {
37 cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
38 if (ecl_unlikely(!LISTP(cdr)))
39 FEwrong_type_nth_arg(@[mapcar], i+2, cdr, @[list]);
40 if (Null(cdr)) {
41 ecl_stack_frame_close(cars_frame);
42 ecl_stack_frame_close(cdrs_frame);
43 @(return res)
44 }
45 ECL_STACK_FRAME_SET(cars_frame, i, ECL_CONS_CAR(cdr));
46 ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
47 }
48 *val = ecl_list1(ecl_apply_from_stack_frame(cars_frame, fun));
49 val = &ECL_CONS_CDR(*val);
50 }
51 } @)
52
53 @(defun maplist (fun &rest lists)
54 cl_object res, *val = &res;
55 @ {
56 PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
57 res = ECL_NIL;
58 while (TRUE) {
59 cl_index i;
60 for (i = 0; i < narg; i++) {
61 cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
62 if (ecl_unlikely(!LISTP(cdr)))
63 FEwrong_type_nth_arg(@[maplist], i+2, cdr, @[list]);
64 if (Null(cdr)) {
65 ecl_stack_frame_close(cars_frame);
66 ecl_stack_frame_close(cdrs_frame);
67 @(return res)
68 }
69 ECL_STACK_FRAME_SET(cars_frame, i, cdr);
70 ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
71 }
72 *val = ecl_list1(ecl_apply_from_stack_frame(cars_frame, fun));
73 val = &ECL_CONS_CDR(*val);
74 }
75 } @)
76
77 @(defun mapc (fun &rest lists)
78 cl_object onelist;
79 @ {
80 PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
81 onelist = ECL_STACK_FRAME_REF(cdrs_frame, 0);
82 while (TRUE) {
83 cl_index i;
84 for (i = 0; i < narg; i++) {
85 cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
86 if (ecl_unlikely(!LISTP(cdr)))
87 FEwrong_type_nth_arg(@[mapc], i+2, cdr, @[list]);
88 if (Null(cdr)) {
89 ecl_stack_frame_close(cars_frame);
90 ecl_stack_frame_close(cdrs_frame);
91 @(return onelist)
92 }
93 ECL_STACK_FRAME_SET(cars_frame, i, ECL_CONS_CAR(cdr));
94 ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
95 }
96 ecl_apply_from_stack_frame(cars_frame, fun);
97 }
98 } @)
99
100 @(defun mapl (fun &rest lists)
101 cl_object onelist;
102 @ {
103 PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
104 onelist = ECL_STACK_FRAME_REF(cdrs_frame, 0);
105 while (TRUE) {
106 cl_index i;
107 for (i = 0; i < narg; i++) {
108 cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
109 if (ecl_unlikely(!LISTP(cdr)))
110 FEwrong_type_nth_arg(@[mapl], i+2, cdr, @[list]);
111 if (Null(cdr)) {
112 ecl_stack_frame_close(cars_frame);
113 ecl_stack_frame_close(cdrs_frame);
114 @(return onelist)
115 }
116 ECL_STACK_FRAME_SET(cars_frame, i, cdr);
117 ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
118 }
119 ecl_apply_from_stack_frame(cars_frame, fun);
120 }
121 } @)
122
123 @(defun mapcan (fun &rest lists)
124 cl_object res, *val = &res;
125 @ {
126 PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
127 res = ECL_NIL;
128 while (TRUE) {
129 cl_index i;
130 for (i = 0; i < narg; i++) {
131 cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
132 if (ecl_unlikely(!LISTP(cdr)))
133 FEwrong_type_nth_arg(@[mapcan], i+2, cdr, @[list]);
134 if (Null(cdr)) {
135 ecl_stack_frame_close(cars_frame);
136 ecl_stack_frame_close(cdrs_frame);
137 @(return res)
138 }
139 ECL_STACK_FRAME_SET(cars_frame, i, ECL_CONS_CAR(cdr));
140 ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
141 }
142 *val = ecl_apply_from_stack_frame(cars_frame, fun);
143 while (CONSP(*val))
144 val = &ECL_CONS_CDR(*val);
145 }
146 } @)
147
148 @(defun mapcon (fun &rest lists)
149 cl_object res, *val = &res;
150 @ {
151 PREPARE_MAP(the_env, lists, cdrs_frame, cars_frame, narg);
152 res = ECL_NIL;
153 while (TRUE) {
154 cl_index i;
155 for (i = 0; i < narg; i++) {
156 cl_object cdr = ECL_STACK_FRAME_REF(cdrs_frame, i);
157 if (ecl_unlikely(!LISTP(cdr)))
158 FEwrong_type_nth_arg(@[mapcon], i+2, cdr, @[list]);
159 if (Null(cdr)) {
160 ecl_stack_frame_close(cars_frame);
161 ecl_stack_frame_close(cdrs_frame);
162 @(return res)
163 }
164 ECL_STACK_FRAME_SET(cars_frame, i, cdr);
165 ECL_STACK_FRAME_SET(cdrs_frame, i, ECL_CONS_CDR(cdr));
166 }
167 *val = ecl_apply_from_stack_frame(cars_frame, fun);
168 while (CONSP(*val))
169 val = &ECL_CONS_CDR(*val);
170 }
171 } @)