Commit | Line | Data |
---|---|---|
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" |
0f2d19dd JB |
53 | \f |
54 | /* | |
55 | * gsubr.c | |
56 | * Provide `gsubrs' -- subrs taking a prescribed number of required, optional, | |
57 | * and rest arguments. | |
58 | */ | |
59 | ||
952bedad | 60 | /* #define GSUBR_TEST */ |
0f2d19dd | 61 | |
85db4a2c DH |
62 | SCM_GLOBAL_SYMBOL (scm_sym_name, "name"); |
63 | ||
dc0938d8 | 64 | SCM scm_f_gsubr_apply; |
1cc91f1b | 65 | |
0f2d19dd | 66 | SCM |
1bbd0b84 | 67 | scm_make_gsubr(const char *name,int req,int opt,int rst,SCM (*fcn)()) |
0f2d19dd | 68 | { |
dc0938d8 MD |
69 | switch SCM_GSUBR_MAKTYPE(req, opt, rst) { |
70 | case SCM_GSUBR_MAKTYPE(0, 0, 0): return scm_make_subr(name, scm_tc7_subr_0, fcn); | |
71 | case SCM_GSUBR_MAKTYPE(1, 0, 0): return scm_make_subr(name, scm_tc7_subr_1, fcn); | |
72 | case SCM_GSUBR_MAKTYPE(0, 1, 0): return scm_make_subr(name, scm_tc7_subr_1o, fcn); | |
73 | case SCM_GSUBR_MAKTYPE(1, 1, 0): return scm_make_subr(name, scm_tc7_subr_2o, fcn); | |
74 | case SCM_GSUBR_MAKTYPE(2, 0, 0): return scm_make_subr(name, scm_tc7_subr_2, fcn); | |
75 | case SCM_GSUBR_MAKTYPE(3, 0, 0): return scm_make_subr(name, scm_tc7_subr_3, fcn); | |
76 | case SCM_GSUBR_MAKTYPE(0, 0, 1): return scm_make_subr(name, scm_tc7_lsubr, fcn); | |
77 | case SCM_GSUBR_MAKTYPE(2, 0, 1): return scm_make_subr(name, scm_tc7_lsubr_2, fcn); | |
0f2d19dd JB |
78 | default: |
79 | { | |
73d6b4df MD |
80 | SCM symcell = scm_sysintern (name, SCM_UNDEFINED); |
81 | SCM cclo = scm_makcclo (scm_f_gsubr_apply, 3L); | |
dc0938d8 | 82 | if (SCM_GSUBR_MAX < req + opt + rst) { |
0f2d19dd JB |
83 | fputs("ERROR in scm_make_gsubr: too many args\n", stderr); |
84 | exit (1); | |
85 | } | |
74cc8503 DH |
86 | SCM_SET_GSUBR_PROC (cclo, scm_make_subr_opt (name, scm_tc7_subr_0, fcn, 0)); |
87 | SCM_SET_GSUBR_TYPE (cclo, SCM_MAKINUM (SCM_GSUBR_MAKTYPE (req, opt, rst))); | |
a6c64c3c | 88 | SCM_SETCDR (symcell, cclo); |
abae3119 MD |
89 | #ifdef DEBUG_EXTENSIONS |
90 | if (SCM_REC_PROCNAMES_P) | |
55b0ed69 | 91 | scm_set_procedure_property_x (cclo, scm_sym_name, SCM_CAR (symcell)); |
abae3119 | 92 | #endif |
0f2d19dd JB |
93 | return cclo; |
94 | } | |
95 | } | |
96 | } | |
97 | ||
9de33deb MD |
98 | SCM |
99 | scm_make_gsubr_with_generic (const char *name, | |
100 | int req, | |
101 | int opt, | |
102 | int rst, | |
103 | SCM (*fcn)(), | |
104 | SCM *gf) | |
105 | { | |
106 | switch SCM_GSUBR_MAKTYPE(req, opt, rst) { | |
107 | case SCM_GSUBR_MAKTYPE(0, 0, 0): | |
108 | return scm_make_subr_with_generic(name, scm_tc7_subr_0, fcn, gf); | |
109 | case SCM_GSUBR_MAKTYPE(1, 0, 0): | |
110 | return scm_make_subr_with_generic(name, scm_tc7_subr_1, fcn, gf); | |
111 | case SCM_GSUBR_MAKTYPE(0, 1, 0): | |
112 | return scm_make_subr_with_generic(name, scm_tc7_subr_1o, fcn, gf); | |
113 | case SCM_GSUBR_MAKTYPE(1, 1, 0): | |
114 | return scm_make_subr_with_generic(name, scm_tc7_subr_2o, fcn, gf); | |
115 | case SCM_GSUBR_MAKTYPE(2, 0, 0): | |
116 | return scm_make_subr_with_generic(name, scm_tc7_subr_2, fcn, gf); | |
117 | case SCM_GSUBR_MAKTYPE(3, 0, 0): | |
118 | return scm_make_subr_with_generic(name, scm_tc7_subr_3, fcn, gf); | |
119 | case SCM_GSUBR_MAKTYPE(0, 0, 1): | |
120 | return scm_make_subr_with_generic(name, scm_tc7_lsubr, fcn, gf); | |
121 | case SCM_GSUBR_MAKTYPE(2, 0, 1): | |
122 | return scm_make_subr_with_generic(name, scm_tc7_lsubr_2, fcn, gf); | |
123 | default: | |
124 | ; | |
125 | } | |
126 | scm_misc_error ("scm_make_gsubr_with_generic", | |
127 | "can't make primitive-generic with this arity", | |
128 | SCM_EOL); | |
4260a7fc | 129 | return SCM_BOOL_F; /* never reached */ |
9de33deb MD |
130 | } |
131 | ||
0f2d19dd | 132 | |
6fa73e72 GB |
133 | SCM |
134 | scm_gsubr_apply (SCM args) | |
a4bb4e6d | 135 | #define FUNC_NAME "scm_gsubr_apply" |
0f2d19dd JB |
136 | { |
137 | SCM self = SCM_CAR(args); | |
dc0938d8 | 138 | SCM (*fcn)() = SCM_SUBRF(SCM_GSUBR_PROC(self)); |
a4bb4e6d | 139 | SCM v[SCM_GSUBR_MAX]; |
dc0938d8 MD |
140 | int typ = SCM_INUM(SCM_GSUBR_TYPE(self)); |
141 | int i, n = SCM_GSUBR_REQ(typ) + SCM_GSUBR_OPT(typ) + SCM_GSUBR_REST(typ); | |
adc02cce | 142 | #if 0 |
a4bb4e6d DH |
143 | if (n > SCM_GSUBR_MAX) |
144 | scm_misc_error (FUNC_NAME, | |
145 | "Function ~S has illegal arity ~S.", | |
146 | SCM_LIST2 (self, SCM_MAKINUM (n))); | |
adc02cce | 147 | #endif |
0f2d19dd | 148 | args = SCM_CDR(args); |
dc0938d8 | 149 | for (i = 0; i < SCM_GSUBR_REQ(typ); i++) { |
cf7c17e9 | 150 | #ifndef SCM_RECKLESS |
a4bb4e6d DH |
151 | if (SCM_NULLP (args)) |
152 | scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self))); | |
0f2d19dd JB |
153 | #endif |
154 | v[i] = SCM_CAR(args); | |
155 | args = SCM_CDR(args); | |
156 | } | |
dc0938d8 | 157 | for (; i < SCM_GSUBR_REQ(typ) + SCM_GSUBR_OPT(typ); i++) { |
0f2d19dd JB |
158 | if (SCM_NIMP(args)) { |
159 | v[i] = SCM_CAR(args); | |
160 | args = SCM_CDR(args); | |
161 | } | |
162 | else | |
163 | v[i] = SCM_UNDEFINED; | |
164 | } | |
dc0938d8 | 165 | if (SCM_GSUBR_REST(typ)) |
0f2d19dd | 166 | v[i] = args; |
a4bb4e6d DH |
167 | else if (!SCM_NULLP (args)) |
168 | scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self))); | |
0f2d19dd | 169 | switch (n) { |
0f2d19dd JB |
170 | case 2: return (*fcn)(v[0], v[1]); |
171 | case 3: return (*fcn)(v[0], v[1], v[2]); | |
172 | case 4: return (*fcn)(v[0], v[1], v[2], v[3]); | |
173 | case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]); | |
174 | case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]); | |
175 | case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]); | |
176 | case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); | |
177 | case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]); | |
178 | case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]); | |
179 | } | |
4260a7fc | 180 | return SCM_BOOL_F; /* Never reached. */ |
0f2d19dd | 181 | } |
a4bb4e6d | 182 | #undef FUNC_NAME |
0f2d19dd JB |
183 | |
184 | ||
185 | #ifdef GSUBR_TEST | |
186 | /* A silly example, taking 2 required args, 1 optional, and | |
187 | a scm_list of rest args | |
188 | */ | |
189 | SCM | |
1bbd0b84 | 190 | gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst) |
0f2d19dd | 191 | { |
b7f3516f | 192 | scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp); |
0f2d19dd | 193 | scm_display(req1, scm_cur_outp); |
b7f3516f | 194 | scm_puts ("\n req2: ", scm_cur_outp); |
0f2d19dd | 195 | scm_display(req2, scm_cur_outp); |
b7f3516f | 196 | scm_puts ("\n opt: ", scm_cur_outp); |
0f2d19dd | 197 | scm_display(opt, scm_cur_outp); |
b7f3516f | 198 | scm_puts ("\n rest: ", scm_cur_outp); |
0f2d19dd JB |
199 | scm_display(rst, scm_cur_outp); |
200 | scm_newline(scm_cur_outp); | |
201 | return SCM_UNSPECIFIED; | |
202 | } | |
203 | #endif | |
204 | ||
205 | ||
1cc91f1b | 206 | |
0f2d19dd JB |
207 | void |
208 | scm_init_gsubr() | |
0f2d19dd | 209 | { |
6fa73e72 | 210 | scm_f_gsubr_apply = scm_make_subr_opt("gsubr-apply", scm_tc7_lsubr, scm_gsubr_apply, 0); |
85db4a2c | 211 | |
0f2d19dd JB |
212 | #ifdef GSUBR_TEST |
213 | scm_make_gsubr("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */ | |
214 | #endif | |
85db4a2c DH |
215 | |
216 | #ifndef SCM_MAGIC_SNARFER | |
217 | #include "libguile/gsubr.x" | |
218 | #endif | |
0f2d19dd | 219 | } |
89e00824 ML |
220 | |
221 | /* | |
222 | Local Variables: | |
223 | c-file-style: "gnu" | |
224 | End: | |
225 | */ |