* alist.c, arbiters.c, async.h, backtrace.h, boolean.c, chars.c,
[bpt/guile.git] / libguile / gsubr.c
1 /* Copyright (C) 1995,1996,1997,1998, 1999, 2000, 2001 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
41
42
43 \f
44
45 #include <stdio.h>
46 #include "libguile/_scm.h"
47 #include "libguile/procprop.h"
48 #include "libguile/root.h"
49
50 #include "libguile/gsubr.h"
51 #include "libguile/deprecation.h"
52 \f
53 /*
54 * gsubr.c
55 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
56 * and rest arguments.
57 */
58
59 /* #define GSUBR_TEST */
60
61 SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
62
63 SCM scm_f_gsubr_apply;
64
65 static SCM
66 create_gsubr (int define, const char *name,
67 int req, int opt, int rst, SCM (*fcn)())
68 {
69 SCM subr;
70
71 switch (SCM_GSUBR_MAKTYPE (req, opt, rst))
72 {
73 case SCM_GSUBR_MAKTYPE(0, 0, 0):
74 subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
75 goto create_subr;
76 case SCM_GSUBR_MAKTYPE(1, 0, 0):
77 subr = scm_c_make_subr (name, scm_tc7_subr_1, fcn);
78 goto create_subr;
79 case SCM_GSUBR_MAKTYPE(0, 1, 0):
80 subr = scm_c_make_subr (name, scm_tc7_subr_1o, fcn);
81 goto create_subr;
82 case SCM_GSUBR_MAKTYPE(1, 1, 0):
83 subr = scm_c_make_subr (name, scm_tc7_subr_2o, fcn);
84 goto create_subr;
85 case SCM_GSUBR_MAKTYPE(2, 0, 0):
86 subr = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
87 goto create_subr;
88 case SCM_GSUBR_MAKTYPE(3, 0, 0):
89 subr = scm_c_make_subr (name, scm_tc7_subr_3, fcn);
90 goto create_subr;
91 case SCM_GSUBR_MAKTYPE(0, 0, 1):
92 subr = scm_c_make_subr (name, scm_tc7_lsubr, fcn);
93 goto create_subr;
94 case SCM_GSUBR_MAKTYPE(2, 0, 1):
95 subr = scm_c_make_subr (name, scm_tc7_lsubr_2, fcn);
96 create_subr:
97 if (define)
98 scm_define (SCM_SUBR_ENTRY(subr).name, subr);
99 return subr;
100 default:
101 {
102 SCM cclo = scm_makcclo (scm_f_gsubr_apply, 3L);
103 SCM subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
104 SCM sym = SCM_SUBR_ENTRY(subr).name;
105 if (SCM_GSUBR_MAX < req + opt + rst)
106 {
107 fputs ("ERROR in scm_c_make_gsubr: too many args\n", stderr);
108 exit (1);
109 }
110 SCM_SET_GSUBR_PROC (cclo, subr);
111 SCM_SET_GSUBR_TYPE (cclo,
112 SCM_MAKINUM (SCM_GSUBR_MAKTYPE (req, opt, rst)));
113 #ifdef DEBUG_EXTENSIONS
114 if (SCM_REC_PROCNAMES_P)
115 scm_set_procedure_property_x (cclo, scm_sym_name, sym);
116 #endif
117 if (define)
118 scm_define (sym, cclo);
119 return cclo;
120 }
121 }
122 }
123
124 SCM
125 scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
126 {
127 return create_gsubr (0, name, req, opt, rst, fcn);
128 }
129
130 SCM
131 scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
132 {
133 return create_gsubr (1, name, req, opt, rst, fcn);
134 }
135
136 static SCM
137 create_gsubr_with_generic (int define,
138 const char *name,
139 int req,
140 int opt,
141 int rst,
142 SCM (*fcn)(),
143 SCM *gf)
144 {
145 SCM subr;
146
147 switch (SCM_GSUBR_MAKTYPE(req, opt, rst))
148 {
149 case SCM_GSUBR_MAKTYPE(0, 0, 0):
150 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_0, fcn, gf);
151 goto create_subr;
152 case SCM_GSUBR_MAKTYPE(1, 0, 0):
153 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1, fcn, gf);
154 goto create_subr;
155 case SCM_GSUBR_MAKTYPE(0, 1, 0):
156 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1o, fcn, gf);
157 goto create_subr;
158 case SCM_GSUBR_MAKTYPE(1, 1, 0):
159 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2o, fcn, gf);
160 goto create_subr;
161 case SCM_GSUBR_MAKTYPE(2, 0, 0):
162 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2, fcn, gf);
163 goto create_subr;
164 case SCM_GSUBR_MAKTYPE(3, 0, 0):
165 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_3, fcn, gf);
166 goto create_subr;
167 case SCM_GSUBR_MAKTYPE(0, 0, 1):
168 subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr, fcn, gf);
169 goto create_subr;
170 case SCM_GSUBR_MAKTYPE(2, 0, 1):
171 subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr_2, fcn, gf);
172 create_subr:
173 if (define)
174 scm_define (SCM_SUBR_ENTRY(subr).name, subr);
175 return subr;
176 default:
177 ;
178 }
179 scm_misc_error ("scm_c_make_gsubr_with_generic",
180 "can't make primitive-generic with this arity",
181 SCM_EOL);
182 return SCM_BOOL_F; /* never reached */
183 }
184
185 SCM
186 scm_c_make_gsubr_with_generic (const char *name,
187 int req,
188 int opt,
189 int rst,
190 SCM (*fcn)(),
191 SCM *gf)
192 {
193 return create_gsubr_with_generic (0, name, req, opt, rst, fcn, gf);
194 }
195
196 SCM
197 scm_c_define_gsubr_with_generic (const char *name,
198 int req,
199 int opt,
200 int rst,
201 SCM (*fcn)(),
202 SCM *gf)
203 {
204 return create_gsubr_with_generic (1, name, req, opt, rst, fcn, gf);
205 }
206
207
208 SCM
209 scm_gsubr_apply (SCM args)
210 #define FUNC_NAME "scm_gsubr_apply"
211 {
212 SCM self = SCM_CAR (args);
213 SCM (*fcn)() = SCM_SUBRF (SCM_GSUBR_PROC (self));
214 SCM v[SCM_GSUBR_MAX];
215 long typ = SCM_INUM (SCM_GSUBR_TYPE (self));
216 long i, n = SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ) + SCM_GSUBR_REST (typ);
217 #if 0
218 if (n > SCM_GSUBR_MAX)
219 scm_misc_error (FUNC_NAME,
220 "Function ~S has illegal arity ~S.",
221 scm_list_2 (self, SCM_MAKINUM (n)));
222 #endif
223 args = SCM_CDR (args);
224 for (i = 0; i < SCM_GSUBR_REQ (typ); i++) {
225 #ifndef SCM_RECKLESS
226 if (SCM_NULLP (args))
227 scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self)));
228 #endif
229 v[i] = SCM_CAR(args);
230 args = SCM_CDR(args);
231 }
232 for (; i < SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ); i++) {
233 if (SCM_NIMP (args)) {
234 v[i] = SCM_CAR (args);
235 args = SCM_CDR(args);
236 }
237 else
238 v[i] = SCM_UNDEFINED;
239 }
240 if (SCM_GSUBR_REST(typ))
241 v[i] = args;
242 else if (!SCM_NULLP (args))
243 scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self)));
244 switch (n) {
245 case 2: return (*fcn)(v[0], v[1]);
246 case 3: return (*fcn)(v[0], v[1], v[2]);
247 case 4: return (*fcn)(v[0], v[1], v[2], v[3]);
248 case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]);
249 case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]);
250 case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
251 case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
252 case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
253 case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
254 }
255 return SCM_BOOL_F; /* Never reached. */
256 }
257 #undef FUNC_NAME
258
259
260 #ifdef GSUBR_TEST
261 /* A silly example, taking 2 required args, 1 optional, and
262 a scm_list of rest args
263 */
264 SCM
265 gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst)
266 {
267 scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp);
268 scm_display(req1, scm_cur_outp);
269 scm_puts ("\n req2: ", scm_cur_outp);
270 scm_display(req2, scm_cur_outp);
271 scm_puts ("\n opt: ", scm_cur_outp);
272 scm_display(opt, scm_cur_outp);
273 scm_puts ("\n rest: ", scm_cur_outp);
274 scm_display(rst, scm_cur_outp);
275 scm_newline(scm_cur_outp);
276 return SCM_UNSPECIFIED;
277 }
278 #endif
279
280
281
282 void
283 scm_init_gsubr()
284 {
285 scm_f_gsubr_apply = scm_c_make_subr ("gsubr-apply", scm_tc7_lsubr,
286 scm_gsubr_apply);
287 #ifdef GSUBR_TEST
288 scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */
289 #endif
290
291 #ifndef SCM_MAGIC_SNARFER
292 #include "libguile/gsubr.x"
293 #endif
294 }
295
296 #if SCM_DEBUG_DEPRECATED == 0
297
298 SCM
299 scm_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
300 {
301 scm_c_issue_deprecation_warning
302 ("`scm_make_gsubr' is deprecated. Use `scm_c_define_gsubr' instead.");
303
304 return scm_c_define_gsubr (name, req, opt, rst, fcn);
305 }
306
307 SCM
308 scm_make_gsubr_with_generic (const char *name,
309 int req, int opt, int rst,
310 SCM (*fcn)(), SCM *gf)
311 {
312 scm_c_issue_deprecation_warning
313 ("`scm_make_gsubr_with_generic' is deprecated. "
314 "Use `scm_c_define_gsubr_with_generic' instead.");
315
316 return scm_c_define_gsubr_with_generic (name, req, opt, rst, fcn, gf);
317 }
318
319 #endif /* !SCM_DEBUG_DEPRECATED */
320
321
322 /*
323 Local Variables:
324 c-file-style: "gnu"
325 End:
326 */