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
24 #include "libguile/_scm.h"
25 #include "libguile/procprop.h"
26 #include "libguile/root.h"
28 #include "libguile/gsubr.h"
29 #include "libguile/deprecation.h"
31 #include "libguile/private-options.h"
35 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
39 /* #define GSUBR_TEST */
41 SCM_GLOBAL_SYMBOL (scm_sym_name
, "name");
44 create_gsubr (int define
, const char *name
,
45 unsigned int req
, unsigned int opt
, unsigned int rst
,
50 switch (SCM_GSUBR_MAKTYPE (req
, opt
, rst
))
52 case SCM_GSUBR_MAKTYPE(0, 0, 0):
53 subr
= scm_c_make_subr (name
, scm_tc7_subr_0
, fcn
);
55 case SCM_GSUBR_MAKTYPE(1, 0, 0):
56 subr
= scm_c_make_subr (name
, scm_tc7_subr_1
, fcn
);
58 case SCM_GSUBR_MAKTYPE(0, 1, 0):
59 subr
= scm_c_make_subr (name
, scm_tc7_subr_1o
, fcn
);
61 case SCM_GSUBR_MAKTYPE(1, 1, 0):
62 subr
= scm_c_make_subr (name
, scm_tc7_subr_2o
, fcn
);
64 case SCM_GSUBR_MAKTYPE(2, 0, 0):
65 subr
= scm_c_make_subr (name
, scm_tc7_subr_2
, fcn
);
67 case SCM_GSUBR_MAKTYPE(3, 0, 0):
68 subr
= scm_c_make_subr (name
, scm_tc7_subr_3
, fcn
);
70 case SCM_GSUBR_MAKTYPE(0, 0, 1):
71 subr
= scm_c_make_subr (name
, scm_tc7_lsubr
, fcn
);
73 case SCM_GSUBR_MAKTYPE(2, 0, 1):
74 subr
= scm_c_make_subr (name
, scm_tc7_lsubr_2
, fcn
);
80 type
= SCM_GSUBR_MAKTYPE (req
, opt
, rst
);
81 if (SCM_GSUBR_REQ (type
) != req
82 || SCM_GSUBR_OPT (type
) != opt
83 || SCM_GSUBR_REST (type
) != rst
)
84 scm_out_of_range ("create_gsubr", scm_from_uint (req
+ opt
+ rst
));
86 subr
= scm_c_make_subr (name
, scm_tc7_gsubr
| (type
<< 8U),
92 scm_define (SCM_SNAME (subr
), subr
);
98 scm_c_make_gsubr (const char *name
, int req
, int opt
, int rst
, SCM (*fcn
)())
100 return create_gsubr (0, name
, req
, opt
, rst
, fcn
);
104 scm_c_define_gsubr (const char *name
, int req
, int opt
, int rst
, SCM (*fcn
)())
106 return create_gsubr (1, name
, req
, opt
, rst
, fcn
);
110 create_gsubr_with_generic (int define
,
120 switch (SCM_GSUBR_MAKTYPE(req
, opt
, rst
))
122 case SCM_GSUBR_MAKTYPE(0, 0, 0):
123 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_0
, fcn
, gf
);
125 case SCM_GSUBR_MAKTYPE(1, 0, 0):
126 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_1
, fcn
, gf
);
128 case SCM_GSUBR_MAKTYPE(0, 1, 0):
129 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_1o
, fcn
, gf
);
131 case SCM_GSUBR_MAKTYPE(1, 1, 0):
132 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_2o
, fcn
, gf
);
134 case SCM_GSUBR_MAKTYPE(2, 0, 0):
135 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_2
, fcn
, gf
);
137 case SCM_GSUBR_MAKTYPE(3, 0, 0):
138 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_subr_3
, fcn
, gf
);
140 case SCM_GSUBR_MAKTYPE(0, 0, 1):
141 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_lsubr
, fcn
, gf
);
143 case SCM_GSUBR_MAKTYPE(2, 0, 1):
144 subr
= scm_c_make_subr_with_generic (name
, scm_tc7_lsubr_2
, fcn
, gf
);
147 scm_define (SCM_SNAME (subr
), subr
);
152 scm_misc_error ("scm_c_make_gsubr_with_generic",
153 "can't make primitive-generic with this arity",
155 return SCM_BOOL_F
; /* never reached */
159 scm_c_make_gsubr_with_generic (const char *name
,
166 return create_gsubr_with_generic (0, name
, req
, opt
, rst
, fcn
, gf
);
170 scm_c_define_gsubr_with_generic (const char *name
,
177 return create_gsubr_with_generic (1, name
, req
, opt
, rst
, fcn
, gf
);
182 scm_gsubr_apply (SCM args
)
183 #define FUNC_NAME "scm_gsubr_apply"
185 SCM self
= SCM_CAR (args
);
186 SCM (*fcn
)() = SCM_SUBRF (self
);
187 SCM v
[SCM_GSUBR_MAX
];
188 unsigned int typ
= SCM_GSUBR_TYPE (self
);
189 long i
, n
= SCM_GSUBR_REQ (typ
) + SCM_GSUBR_OPT (typ
) + SCM_GSUBR_REST (typ
);
191 args
= SCM_CDR (args
);
192 for (i
= 0; i
< SCM_GSUBR_REQ (typ
); i
++) {
193 if (scm_is_null (args
))
194 scm_wrong_num_args (SCM_SNAME (self
));
195 v
[i
] = SCM_CAR(args
);
196 args
= SCM_CDR(args
);
198 for (; i
< SCM_GSUBR_REQ (typ
) + SCM_GSUBR_OPT (typ
); i
++) {
199 if (SCM_NIMP (args
)) {
200 v
[i
] = SCM_CAR (args
);
201 args
= SCM_CDR(args
);
204 v
[i
] = SCM_UNDEFINED
;
206 if (SCM_GSUBR_REST(typ
))
208 else if (!scm_is_null (args
))
209 scm_wrong_num_args (SCM_SNAME (self
));
211 case 2: return (*fcn
)(v
[0], v
[1]);
212 case 3: return (*fcn
)(v
[0], v
[1], v
[2]);
213 case 4: return (*fcn
)(v
[0], v
[1], v
[2], v
[3]);
214 case 5: return (*fcn
)(v
[0], v
[1], v
[2], v
[3], v
[4]);
215 case 6: return (*fcn
)(v
[0], v
[1], v
[2], v
[3], v
[4], v
[5]);
216 case 7: return (*fcn
)(v
[0], v
[1], v
[2], v
[3], v
[4], v
[5], v
[6]);
217 case 8: return (*fcn
)(v
[0], v
[1], v
[2], v
[3], v
[4], v
[5], v
[6], v
[7]);
218 case 9: return (*fcn
)(v
[0], v
[1], v
[2], v
[3], v
[4], v
[5], v
[6], v
[7], v
[8]);
219 case 10: return (*fcn
)(v
[0], v
[1], v
[2], v
[3], v
[4], v
[5], v
[6], v
[7], v
[8], v
[9]);
221 scm_misc_error ((char *) SCM_SNAME (self
),
222 "gsubr invocation with more than 10 arguments not implemented",
225 return SCM_BOOL_F
; /* Never reached. */
231 /* A silly example, taking 2 required args, 1 optional, and
232 a scm_list of rest args
235 gsubr_21l(SCM req1
, SCM req2
, SCM opt
, SCM rst
)
237 scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp
);
238 scm_display(req1
, scm_cur_outp
);
239 scm_puts ("\n req2: ", scm_cur_outp
);
240 scm_display(req2
, scm_cur_outp
);
241 scm_puts ("\n opt: ", scm_cur_outp
);
242 scm_display(opt
, scm_cur_outp
);
243 scm_puts ("\n rest: ", scm_cur_outp
);
244 scm_display(rst
, scm_cur_outp
);
245 scm_newline(scm_cur_outp
);
246 return SCM_UNSPECIFIED
;
255 scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l
); /* example */
258 #include "libguile/gsubr.x"