1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
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.
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.
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
28 #include "libguile/_scm.h"
29 #include "libguile/procprop.h"
30 #include "libguile/root.h"
32 #include "libguile/gsubr.h"
33 #include "libguile/deprecation.h"
35 #include "libguile/private-options.h"
39 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
43 /* #define GSUBR_TEST */
45 SCM_GLOBAL_SYMBOL (scm_sym_name
, "name");
48 create_gsubr (int define
, const char *name
,
49 unsigned int req
, unsigned int opt
, unsigned int rst
,
54 switch (SCM_GSUBR_MAKTYPE (req
, opt
, rst
))
56 case SCM_GSUBR_MAKTYPE(0, 0, 0):
57 subr
= scm_c_make_subr (name
, scm_tc7_subr_0
, fcn
);
59 case SCM_GSUBR_MAKTYPE(1, 0, 0):
60 subr
= scm_c_make_subr (name
, scm_tc7_subr_1
, fcn
);
62 case SCM_GSUBR_MAKTYPE(0, 1, 0):
63 subr
= scm_c_make_subr (name
, scm_tc7_subr_1o
, fcn
);
65 case SCM_GSUBR_MAKTYPE(1, 1, 0):
66 subr
= scm_c_make_subr (name
, scm_tc7_subr_2o
, fcn
);
68 case SCM_GSUBR_MAKTYPE(2, 0, 0):
69 subr
= scm_c_make_subr (name
, scm_tc7_subr_2
, fcn
);
71 case SCM_GSUBR_MAKTYPE(3, 0, 0):
72 subr
= scm_c_make_subr (name
, scm_tc7_subr_3
, fcn
);
74 case SCM_GSUBR_MAKTYPE(0, 0, 1):
75 subr
= scm_c_make_subr (name
, scm_tc7_lsubr
, fcn
);
77 case SCM_GSUBR_MAKTYPE(2, 0, 1):
78 subr
= scm_c_make_subr (name
, scm_tc7_lsubr_2
, fcn
);
84 type
= SCM_GSUBR_MAKTYPE (req
, opt
, rst
);
85 if (SCM_GSUBR_REQ (type
) != req
86 || SCM_GSUBR_OPT (type
) != opt
87 || SCM_GSUBR_REST (type
) != rst
)
88 scm_out_of_range ("create_gsubr", scm_from_uint (req
+ opt
+ rst
));
90 subr
= scm_c_make_subr (name
, scm_tc7_gsubr
| (type
<< 8U),
96 scm_define (SCM_SNAME (subr
), subr
);
102 scm_c_make_gsubr (const char *name
, int req
, int opt
, int rst
, SCM (*fcn
)())
104 return create_gsubr (0, name
, req
, opt
, rst
, fcn
);
108 scm_c_define_gsubr (const char *name
, int req
, int opt
, int rst
, SCM (*fcn
)())
110 return create_gsubr (1, name
, req
, opt
, rst
, fcn
);
114 create_gsubr_with_generic (int define
,
124 switch (SCM_GSUBR_MAKTYPE(req
, opt
, rst
))
126 case SCM_GSUBR_MAKTYPE(0, 0, 0):
127 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_0
, fcn
, gf
);
129 case SCM_GSUBR_MAKTYPE(1, 0, 0):
130 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_1
, fcn
, gf
);
132 case SCM_GSUBR_MAKTYPE(0, 1, 0):
133 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_1o
, fcn
, gf
);
135 case SCM_GSUBR_MAKTYPE(1, 1, 0):
136 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_2o
, fcn
, gf
);
138 case SCM_GSUBR_MAKTYPE(2, 0, 0):
139 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_2
, fcn
, gf
);
141 case SCM_GSUBR_MAKTYPE(3, 0, 0):
142 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_3
, fcn
, gf
);
144 case SCM_GSUBR_MAKTYPE(0, 0, 1):
145 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_lsubr
, fcn
, gf
);
147 case SCM_GSUBR_MAKTYPE(2, 0, 1):
148 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_lsubr_2
, fcn
, gf
);
151 scm_define (SCM_SNAME (subr
), subr
);
156 scm_misc_error ("scm_c_make_gsubr_with_generic",
157 "can't make primitive-generic with this arity",
159 return SCM_BOOL_F
; /* never reached */
163 scm_c_make_gsubr_with_generic (const char *name
,
170 return create_gsubr_with_generic (0, name
, req
, opt
, rst
, fcn
, gf
);
174 scm_c_define_gsubr_with_generic (const char *name
,
181 return create_gsubr_with_generic (1, name
, req
, opt
, rst
, fcn
, gf
);
184 /* Apply PROC, a gsubr, to the ARGC arguments in ARGV. ARGC is expected to
185 match the number of arguments of the underlying C function. */
187 gsubr_apply_raw (SCM proc
, unsigned int argc
, const SCM
*argv
)
190 unsigned int type
, argc_max
;
192 type
= SCM_GSUBR_TYPE (proc
);
193 argc_max
= SCM_GSUBR_REQ (type
) + SCM_GSUBR_OPT (type
)
194 + SCM_GSUBR_REST (type
);
196 if (SCM_UNLIKELY (argc
!= argc_max
))
197 /* We expect the exact argument count. */
198 scm_wrong_num_args (SCM_SNAME (proc
));
200 fcn
= SCM_SUBRF (proc
);
207 return (*fcn
) (argv
[0]);
209 return (*fcn
) (argv
[0], argv
[1]);
211 return (*fcn
) (argv
[0], argv
[1], argv
[2]);
213 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3]);
215 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3], argv
[4]);
217 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5]);
219 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
222 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
225 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
226 argv
[6], argv
[7], argv
[8]);
228 return (*fcn
) (argv
[0], argv
[1], argv
[2], argv
[3], argv
[4], argv
[5],
229 argv
[6], argv
[7], argv
[8], argv
[9]);
231 scm_misc_error ((char *) SCM_SNAME (proc
),
232 "gsubr invocation with more than 10 arguments not implemented",
236 return SCM_BOOL_F
; /* Never reached. */
239 /* Apply PROC, a gsubr, to the given arguments. Missing optional arguments
240 are added, and rest arguments are turned into a list. */
242 scm_i_gsubr_apply (SCM proc
, SCM arg
, ...)
244 unsigned int type
, argc
, argc_max
;
248 type
= SCM_GSUBR_TYPE (proc
);
249 argc_max
= SCM_GSUBR_REQ (type
) + SCM_GSUBR_OPT (type
);
250 argv
= alloca ((argc_max
+ SCM_GSUBR_REST (type
)) * sizeof (*argv
));
252 va_start (arg_list
, arg
);
255 !SCM_UNBNDP (arg
) && argc
< argc_max
;
256 argc
++, arg
= va_arg (arg_list
, SCM
))
259 if (SCM_UNLIKELY (argc
< SCM_GSUBR_REQ (type
)))
260 scm_wrong_num_args (SCM_SNAME (proc
));
262 /* Fill in optional arguments that were not passed. */
263 while (argc
< argc_max
)
264 argv
[argc
++] = SCM_UNDEFINED
;
266 if (SCM_GSUBR_REST (type
))
268 /* Accumulate rest arguments in a list. */
271 argv
[argc_max
] = SCM_EOL
;
273 for (rest_loc
= &argv
[argc_max
];
275 rest_loc
= SCM_CDRLOC (*rest_loc
), arg
= va_arg (arg_list
, SCM
))
276 *rest_loc
= scm_cons (arg
, SCM_EOL
);
283 return gsubr_apply_raw (proc
, argc
, argv
);
286 /* Apply SELF, a gsubr, to the arguments listed in ARGS. Missing optional
287 arguments are added, and rest arguments are kept into a list. */
289 scm_i_gsubr_apply_list (SCM self
, SCM args
)
290 #define FUNC_NAME "scm_i_gsubr_apply"
292 SCM v
[SCM_GSUBR_MAX
];
293 unsigned int typ
= SCM_GSUBR_TYPE (self
);
294 long i
, n
= SCM_GSUBR_REQ (typ
) + SCM_GSUBR_OPT (typ
) + SCM_GSUBR_REST (typ
);
296 for (i
= 0; i
< SCM_GSUBR_REQ (typ
); i
++) {
297 if (scm_is_null (args
))
298 scm_wrong_num_args (SCM_SNAME (self
));
299 v
[i
] = SCM_CAR(args
);
300 args
= SCM_CDR(args
);
302 for (; i
< SCM_GSUBR_REQ (typ
) + SCM_GSUBR_OPT (typ
); i
++) {
303 if (SCM_NIMP (args
)) {
304 v
[i
] = SCM_CAR (args
);
305 args
= SCM_CDR(args
);
308 v
[i
] = SCM_UNDEFINED
;
310 if (SCM_GSUBR_REST(typ
))
312 else if (!scm_is_null (args
))
313 scm_wrong_num_args (SCM_SNAME (self
));
315 return gsubr_apply_raw (self
, n
, v
);
321 /* A silly example, taking 2 required args, 1 optional, and
322 a scm_list of rest args
325 gsubr_21l(SCM req1
, SCM req2
, SCM opt
, SCM rst
)
327 scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp
);
328 scm_display(req1
, scm_cur_outp
);
329 scm_puts ("\n req2: ", scm_cur_outp
);
330 scm_display(req2
, scm_cur_outp
);
331 scm_puts ("\n opt: ", scm_cur_outp
);
332 scm_display(opt
, scm_cur_outp
);
333 scm_puts ("\n rest: ", scm_cur_outp
);
334 scm_display(rst
, scm_cur_outp
);
335 scm_newline(scm_cur_outp
);
336 return SCM_UNSPECIFIED
;
345 scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l
); /* example */
348 #include "libguile/gsubr.x"