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