Commit | Line | Data |
---|---|---|
dee01b01 | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc. |
c0fa6561 | 2 | * |
0f2d19dd JB |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
c0fa6561 | 7 | * |
0f2d19dd JB |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
c0fa6561 | 12 | * |
0f2d19dd JB |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
0f2d19dd JB |
42 | \f |
43 | ||
44 | #include <stdio.h> | |
a0599745 MD |
45 | #include "libguile/_scm.h" |
46 | #include "libguile/procprop.h" | |
47 | #include "libguile/root.h" | |
0f2d19dd | 48 | |
a0599745 | 49 | #include "libguile/gsubr.h" |
1be6b49c | 50 | #include "libguile/deprecation.h" |
0f2d19dd JB |
51 | \f |
52 | /* | |
53 | * gsubr.c | |
54 | * Provide `gsubrs' -- subrs taking a prescribed number of required, optional, | |
55 | * and rest arguments. | |
56 | */ | |
57 | ||
952bedad | 58 | /* #define GSUBR_TEST */ |
0f2d19dd | 59 | |
85db4a2c DH |
60 | SCM_GLOBAL_SYMBOL (scm_sym_name, "name"); |
61 | ||
dc0938d8 | 62 | SCM scm_f_gsubr_apply; |
1cc91f1b | 63 | |
9d78586f | 64 | static SCM |
c0fa6561 | 65 | create_gsubr (int define, const char *name, |
9d78586f | 66 | int req, int opt, int rst, SCM (*fcn)()) |
0f2d19dd | 67 | { |
9d78586f MV |
68 | SCM subr; |
69 | ||
70 | switch (SCM_GSUBR_MAKTYPE (req, opt, rst)) | |
0f2d19dd | 71 | { |
9d78586f MV |
72 | case SCM_GSUBR_MAKTYPE(0, 0, 0): |
73 | subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn); | |
74 | goto create_subr; | |
75 | case SCM_GSUBR_MAKTYPE(1, 0, 0): | |
76 | subr = scm_c_make_subr (name, scm_tc7_subr_1, fcn); | |
77 | goto create_subr; | |
78 | case SCM_GSUBR_MAKTYPE(0, 1, 0): | |
79 | subr = scm_c_make_subr (name, scm_tc7_subr_1o, fcn); | |
80 | goto create_subr; | |
81 | case SCM_GSUBR_MAKTYPE(1, 1, 0): | |
82 | subr = scm_c_make_subr (name, scm_tc7_subr_2o, fcn); | |
83 | goto create_subr; | |
84 | case SCM_GSUBR_MAKTYPE(2, 0, 0): | |
85 | subr = scm_c_make_subr (name, scm_tc7_subr_2, fcn); | |
86 | goto create_subr; | |
87 | case SCM_GSUBR_MAKTYPE(3, 0, 0): | |
88 | subr = scm_c_make_subr (name, scm_tc7_subr_3, fcn); | |
89 | goto create_subr; | |
90 | case SCM_GSUBR_MAKTYPE(0, 0, 1): | |
91 | subr = scm_c_make_subr (name, scm_tc7_lsubr, fcn); | |
92 | goto create_subr; | |
93 | case SCM_GSUBR_MAKTYPE(2, 0, 1): | |
94 | subr = scm_c_make_subr (name, scm_tc7_lsubr_2, fcn); | |
95 | create_subr: | |
96 | if (define) | |
97 | scm_define (SCM_SUBR_ENTRY(subr).name, subr); | |
98 | return subr; | |
99 | default: | |
100 | { | |
101 | SCM cclo = scm_makcclo (scm_f_gsubr_apply, 3L); | |
102 | SCM subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn); | |
103 | SCM sym = SCM_SUBR_ENTRY(subr).name; | |
104 | if (SCM_GSUBR_MAX < req + opt + rst) | |
105 | { | |
c0fa6561 TTN |
106 | fprintf (stderr, |
107 | "ERROR in scm_c_make_gsubr: too many args (%d) to %s\n", | |
108 | req + opt + rst, name); | |
9d78586f MV |
109 | exit (1); |
110 | } | |
111 | SCM_SET_GSUBR_PROC (cclo, subr); | |
112 | SCM_SET_GSUBR_TYPE (cclo, | |
113 | SCM_MAKINUM (SCM_GSUBR_MAKTYPE (req, opt, rst))); | |
abae3119 | 114 | #ifdef DEBUG_EXTENSIONS |
9d78586f MV |
115 | if (SCM_REC_PROCNAMES_P) |
116 | scm_set_procedure_property_x (cclo, scm_sym_name, sym); | |
abae3119 | 117 | #endif |
9d78586f MV |
118 | if (define) |
119 | scm_define (sym, cclo); | |
0f2d19dd | 120 | return cclo; |
9d78586f | 121 | } |
0f2d19dd | 122 | } |
0f2d19dd JB |
123 | } |
124 | ||
9de33deb | 125 | SCM |
9d78586f | 126 | scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)()) |
9de33deb | 127 | { |
9d78586f MV |
128 | return create_gsubr (0, name, req, opt, rst, fcn); |
129 | } | |
130 | ||
131 | SCM | |
132 | scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)()) | |
133 | { | |
134 | return create_gsubr (1, name, req, opt, rst, fcn); | |
135 | } | |
136 | ||
137 | static SCM | |
138 | create_gsubr_with_generic (int define, | |
139 | const char *name, | |
140 | int req, | |
141 | int opt, | |
142 | int rst, | |
143 | SCM (*fcn)(), | |
144 | SCM *gf) | |
145 | { | |
146 | SCM subr; | |
147 | ||
148 | switch (SCM_GSUBR_MAKTYPE(req, opt, rst)) | |
149 | { | |
150 | case SCM_GSUBR_MAKTYPE(0, 0, 0): | |
151 | subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_0, fcn, gf); | |
152 | goto create_subr; | |
153 | case SCM_GSUBR_MAKTYPE(1, 0, 0): | |
154 | subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1, fcn, gf); | |
155 | goto create_subr; | |
156 | case SCM_GSUBR_MAKTYPE(0, 1, 0): | |
157 | subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1o, fcn, gf); | |
158 | goto create_subr; | |
159 | case SCM_GSUBR_MAKTYPE(1, 1, 0): | |
160 | subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2o, fcn, gf); | |
161 | goto create_subr; | |
162 | case SCM_GSUBR_MAKTYPE(2, 0, 0): | |
163 | subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2, fcn, gf); | |
164 | goto create_subr; | |
165 | case SCM_GSUBR_MAKTYPE(3, 0, 0): | |
166 | subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_3, fcn, gf); | |
167 | goto create_subr; | |
168 | case SCM_GSUBR_MAKTYPE(0, 0, 1): | |
169 | subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr, fcn, gf); | |
170 | goto create_subr; | |
171 | case SCM_GSUBR_MAKTYPE(2, 0, 1): | |
172 | subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr_2, fcn, gf); | |
173 | create_subr: | |
174 | if (define) | |
175 | scm_define (SCM_SUBR_ENTRY(subr).name, subr); | |
176 | return subr; | |
177 | default: | |
178 | ; | |
179 | } | |
180 | scm_misc_error ("scm_c_make_gsubr_with_generic", | |
9de33deb MD |
181 | "can't make primitive-generic with this arity", |
182 | SCM_EOL); | |
4260a7fc | 183 | return SCM_BOOL_F; /* never reached */ |
9de33deb MD |
184 | } |
185 | ||
9d78586f MV |
186 | SCM |
187 | scm_c_make_gsubr_with_generic (const char *name, | |
188 | int req, | |
189 | int opt, | |
190 | int rst, | |
191 | SCM (*fcn)(), | |
192 | SCM *gf) | |
193 | { | |
194 | return create_gsubr_with_generic (0, name, req, opt, rst, fcn, gf); | |
195 | } | |
196 | ||
197 | SCM | |
198 | scm_c_define_gsubr_with_generic (const char *name, | |
199 | int req, | |
200 | int opt, | |
201 | int rst, | |
202 | SCM (*fcn)(), | |
203 | SCM *gf) | |
204 | { | |
205 | return create_gsubr_with_generic (1, name, req, opt, rst, fcn, gf); | |
206 | } | |
207 | ||
0f2d19dd | 208 | |
6fa73e72 GB |
209 | SCM |
210 | scm_gsubr_apply (SCM args) | |
a4bb4e6d | 211 | #define FUNC_NAME "scm_gsubr_apply" |
0f2d19dd | 212 | { |
1be6b49c ML |
213 | SCM self = SCM_CAR (args); |
214 | SCM (*fcn)() = SCM_SUBRF (SCM_GSUBR_PROC (self)); | |
a4bb4e6d | 215 | SCM v[SCM_GSUBR_MAX]; |
c014a02e ML |
216 | long typ = SCM_INUM (SCM_GSUBR_TYPE (self)); |
217 | long i, n = SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ) + SCM_GSUBR_REST (typ); | |
adc02cce | 218 | #if 0 |
a4bb4e6d | 219 | if (n > SCM_GSUBR_MAX) |
c0fa6561 TTN |
220 | scm_misc_error (FUNC_NAME, |
221 | "Function ~S has illegal arity ~S.", | |
1afff620 | 222 | scm_list_2 (self, SCM_MAKINUM (n))); |
adc02cce | 223 | #endif |
1be6b49c ML |
224 | args = SCM_CDR (args); |
225 | for (i = 0; i < SCM_GSUBR_REQ (typ); i++) { | |
cf7c17e9 | 226 | #ifndef SCM_RECKLESS |
a4bb4e6d DH |
227 | if (SCM_NULLP (args)) |
228 | scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self))); | |
0f2d19dd JB |
229 | #endif |
230 | v[i] = SCM_CAR(args); | |
231 | args = SCM_CDR(args); | |
232 | } | |
1be6b49c ML |
233 | for (; i < SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ); i++) { |
234 | if (SCM_NIMP (args)) { | |
235 | v[i] = SCM_CAR (args); | |
0f2d19dd JB |
236 | args = SCM_CDR(args); |
237 | } | |
238 | else | |
239 | v[i] = SCM_UNDEFINED; | |
240 | } | |
dc0938d8 | 241 | if (SCM_GSUBR_REST(typ)) |
0f2d19dd | 242 | v[i] = args; |
a4bb4e6d DH |
243 | else if (!SCM_NULLP (args)) |
244 | scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self))); | |
0f2d19dd | 245 | switch (n) { |
0f2d19dd JB |
246 | case 2: return (*fcn)(v[0], v[1]); |
247 | case 3: return (*fcn)(v[0], v[1], v[2]); | |
248 | case 4: return (*fcn)(v[0], v[1], v[2], v[3]); | |
249 | case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]); | |
250 | case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]); | |
251 | case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]); | |
252 | case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); | |
253 | case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]); | |
254 | case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]); | |
255 | } | |
4260a7fc | 256 | return SCM_BOOL_F; /* Never reached. */ |
0f2d19dd | 257 | } |
a4bb4e6d | 258 | #undef FUNC_NAME |
0f2d19dd JB |
259 | |
260 | ||
261 | #ifdef GSUBR_TEST | |
262 | /* A silly example, taking 2 required args, 1 optional, and | |
c0fa6561 | 263 | a scm_list of rest args |
0f2d19dd JB |
264 | */ |
265 | SCM | |
1bbd0b84 | 266 | gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst) |
0f2d19dd | 267 | { |
b7f3516f | 268 | scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp); |
0f2d19dd | 269 | scm_display(req1, scm_cur_outp); |
b7f3516f | 270 | scm_puts ("\n req2: ", scm_cur_outp); |
0f2d19dd | 271 | scm_display(req2, scm_cur_outp); |
b7f3516f | 272 | scm_puts ("\n opt: ", scm_cur_outp); |
0f2d19dd | 273 | scm_display(opt, scm_cur_outp); |
b7f3516f | 274 | scm_puts ("\n rest: ", scm_cur_outp); |
0f2d19dd JB |
275 | scm_display(rst, scm_cur_outp); |
276 | scm_newline(scm_cur_outp); | |
277 | return SCM_UNSPECIFIED; | |
278 | } | |
279 | #endif | |
280 | ||
281 | ||
0f2d19dd JB |
282 | void |
283 | scm_init_gsubr() | |
0f2d19dd | 284 | { |
9d78586f MV |
285 | scm_f_gsubr_apply = scm_c_make_subr ("gsubr-apply", scm_tc7_lsubr, |
286 | scm_gsubr_apply); | |
0f2d19dd | 287 | #ifdef GSUBR_TEST |
9d78586f | 288 | scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */ |
0f2d19dd | 289 | #endif |
85db4a2c DH |
290 | |
291 | #ifndef SCM_MAGIC_SNARFER | |
292 | #include "libguile/gsubr.x" | |
293 | #endif | |
0f2d19dd | 294 | } |
89e00824 ML |
295 | |
296 | /* | |
297 | Local Variables: | |
298 | c-file-style: "gnu" | |
299 | End: | |
300 | */ |