Use `SCM_SNAME ()' when requesting the name of a subr.
[bpt/guile.git] / libguile / gsubr.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 \f
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include "libguile/_scm.h"
25 #include "libguile/procprop.h"
26 #include "libguile/root.h"
27
28 #include "libguile/gsubr.h"
29 #include "libguile/deprecation.h"
30
31 #include "libguile/private-options.h"
32 \f
33 /*
34 * gsubr.c
35 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
36 * and rest arguments.
37 */
38
39 /* #define GSUBR_TEST */
40
41 SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
42
43 SCM scm_f_gsubr_apply;
44
45 static SCM
46 create_gsubr (int define, const char *name,
47 int req, int opt, int rst, SCM (*fcn)())
48 {
49 SCM subr;
50
51 switch (SCM_GSUBR_MAKTYPE (req, opt, rst))
52 {
53 case SCM_GSUBR_MAKTYPE(0, 0, 0):
54 subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
55 goto create_subr;
56 case SCM_GSUBR_MAKTYPE(1, 0, 0):
57 subr = scm_c_make_subr (name, scm_tc7_subr_1, fcn);
58 goto create_subr;
59 case SCM_GSUBR_MAKTYPE(0, 1, 0):
60 subr = scm_c_make_subr (name, scm_tc7_subr_1o, fcn);
61 goto create_subr;
62 case SCM_GSUBR_MAKTYPE(1, 1, 0):
63 subr = scm_c_make_subr (name, scm_tc7_subr_2o, fcn);
64 goto create_subr;
65 case SCM_GSUBR_MAKTYPE(2, 0, 0):
66 subr = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
67 goto create_subr;
68 case SCM_GSUBR_MAKTYPE(3, 0, 0):
69 subr = scm_c_make_subr (name, scm_tc7_subr_3, fcn);
70 goto create_subr;
71 case SCM_GSUBR_MAKTYPE(0, 0, 1):
72 subr = scm_c_make_subr (name, scm_tc7_lsubr, fcn);
73 goto create_subr;
74 case SCM_GSUBR_MAKTYPE(2, 0, 1):
75 subr = scm_c_make_subr (name, scm_tc7_lsubr_2, fcn);
76 create_subr:
77 if (define)
78 scm_define (SCM_SNAME (subr), subr);
79 return subr;
80 default:
81 {
82 SCM cclo = scm_makcclo (scm_f_gsubr_apply, 3L);
83 SCM subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
84 SCM sym = SCM_SNAME (subr);
85 if (SCM_GSUBR_MAX < req + opt + rst)
86 {
87 fprintf (stderr,
88 "ERROR in scm_c_make_gsubr: too many args (%d) to %s\n",
89 req + opt + rst, name);
90 exit (1);
91 }
92 SCM_SET_GSUBR_PROC (cclo, subr);
93 SCM_SET_GSUBR_TYPE (cclo,
94 scm_from_int (SCM_GSUBR_MAKTYPE (req, opt, rst)));
95 if (SCM_REC_PROCNAMES_P)
96 scm_set_procedure_property_x (cclo, scm_sym_name, sym);
97 if (define)
98 scm_define (sym, cclo);
99 return cclo;
100 }
101 }
102 }
103
104 SCM
105 scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
106 {
107 return create_gsubr (0, name, req, opt, rst, fcn);
108 }
109
110 SCM
111 scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
112 {
113 return create_gsubr (1, name, req, opt, rst, fcn);
114 }
115
116 static SCM
117 create_gsubr_with_generic (int define,
118 const char *name,
119 int req,
120 int opt,
121 int rst,
122 SCM (*fcn)(),
123 SCM *gf)
124 {
125 SCM subr;
126
127 switch (SCM_GSUBR_MAKTYPE(req, opt, rst))
128 {
129 case SCM_GSUBR_MAKTYPE(0, 0, 0):
130 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_0, fcn, gf);
131 goto create_subr;
132 case SCM_GSUBR_MAKTYPE(1, 0, 0):
133 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1, fcn, gf);
134 goto create_subr;
135 case SCM_GSUBR_MAKTYPE(0, 1, 0):
136 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1o, fcn, gf);
137 goto create_subr;
138 case SCM_GSUBR_MAKTYPE(1, 1, 0):
139 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2o, fcn, gf);
140 goto create_subr;
141 case SCM_GSUBR_MAKTYPE(2, 0, 0):
142 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2, fcn, gf);
143 goto create_subr;
144 case SCM_GSUBR_MAKTYPE(3, 0, 0):
145 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_3, fcn, gf);
146 goto create_subr;
147 case SCM_GSUBR_MAKTYPE(0, 0, 1):
148 subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr, fcn, gf);
149 goto create_subr;
150 case SCM_GSUBR_MAKTYPE(2, 0, 1):
151 subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr_2, fcn, gf);
152 create_subr:
153 if (define)
154 scm_define (SCM_SNAME (subr), subr);
155 return subr;
156 default:
157 ;
158 }
159 scm_misc_error ("scm_c_make_gsubr_with_generic",
160 "can't make primitive-generic with this arity",
161 SCM_EOL);
162 return SCM_BOOL_F; /* never reached */
163 }
164
165 SCM
166 scm_c_make_gsubr_with_generic (const char *name,
167 int req,
168 int opt,
169 int rst,
170 SCM (*fcn)(),
171 SCM *gf)
172 {
173 return create_gsubr_with_generic (0, name, req, opt, rst, fcn, gf);
174 }
175
176 SCM
177 scm_c_define_gsubr_with_generic (const char *name,
178 int req,
179 int opt,
180 int rst,
181 SCM (*fcn)(),
182 SCM *gf)
183 {
184 return create_gsubr_with_generic (1, name, req, opt, rst, fcn, gf);
185 }
186
187
188 SCM
189 scm_gsubr_apply (SCM args)
190 #define FUNC_NAME "scm_gsubr_apply"
191 {
192 SCM self = SCM_CAR (args);
193 SCM (*fcn)() = SCM_SUBRF (SCM_GSUBR_PROC (self));
194 SCM v[SCM_GSUBR_MAX];
195 int typ = scm_to_int (SCM_GSUBR_TYPE (self));
196 long i, n = SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ) + SCM_GSUBR_REST (typ);
197 #if 0
198 if (n > SCM_GSUBR_MAX)
199 scm_misc_error (FUNC_NAME,
200 "Function ~S has illegal arity ~S.",
201 scm_list_2 (self, scm_from_int (n)));
202 #endif
203 args = SCM_CDR (args);
204 for (i = 0; i < SCM_GSUBR_REQ (typ); i++) {
205 if (scm_is_null (args))
206 scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self)));
207 v[i] = SCM_CAR(args);
208 args = SCM_CDR(args);
209 }
210 for (; i < SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ); i++) {
211 if (SCM_NIMP (args)) {
212 v[i] = SCM_CAR (args);
213 args = SCM_CDR(args);
214 }
215 else
216 v[i] = SCM_UNDEFINED;
217 }
218 if (SCM_GSUBR_REST(typ))
219 v[i] = args;
220 else if (!scm_is_null (args))
221 scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self)));
222 switch (n) {
223 case 2: return (*fcn)(v[0], v[1]);
224 case 3: return (*fcn)(v[0], v[1], v[2]);
225 case 4: return (*fcn)(v[0], v[1], v[2], v[3]);
226 case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]);
227 case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]);
228 case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
229 case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
230 case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
231 case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
232 }
233 return SCM_BOOL_F; /* Never reached. */
234 }
235 #undef FUNC_NAME
236
237
238 #ifdef GSUBR_TEST
239 /* A silly example, taking 2 required args, 1 optional, and
240 a scm_list of rest args
241 */
242 SCM
243 gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst)
244 {
245 scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp);
246 scm_display(req1, scm_cur_outp);
247 scm_puts ("\n req2: ", scm_cur_outp);
248 scm_display(req2, scm_cur_outp);
249 scm_puts ("\n opt: ", scm_cur_outp);
250 scm_display(opt, scm_cur_outp);
251 scm_puts ("\n rest: ", scm_cur_outp);
252 scm_display(rst, scm_cur_outp);
253 scm_newline(scm_cur_outp);
254 return SCM_UNSPECIFIED;
255 }
256 #endif
257
258
259 void
260 scm_init_gsubr()
261 {
262 scm_f_gsubr_apply = scm_c_make_subr ("gsubr-apply", scm_tc7_lsubr,
263 scm_gsubr_apply);
264 #ifdef GSUBR_TEST
265 scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */
266 #endif
267
268 #include "libguile/gsubr.x"
269 }
270
271 /*
272 Local Variables:
273 c-file-style: "gnu"
274 End:
275 */