Commit | Line | Data |
---|---|---|
81ba12d7 | 1 | /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
a0599745 | 25 | #include "libguile/_scm.h" |
0f2d19dd | 26 | |
a0599745 MD |
27 | #include "libguile/strings.h" |
28 | #include "libguile/vectors.h" | |
0717dfd8 | 29 | #include "libguile/smob.h" |
c88a8162 | 30 | #include "libguile/deprecation.h" |
bdc88419 | 31 | |
a0599745 MD |
32 | #include "libguile/validate.h" |
33 | #include "libguile/procs.h" | |
3fd8807e | 34 | #include "libguile/procprop.h" |
53e28ed9 | 35 | #include "libguile/objcodes.h" |
7e580328 | 36 | #include "libguile/programs.h" |
0f2d19dd JB |
37 | \f |
38 | ||
39 | ||
40 | /* {Procedures} | |
41 | */ | |
42 | ||
9de33deb | 43 | |
ac51e74b | 44 | SCM |
c014a02e | 45 | scm_c_make_subr (const char *name, long type, SCM (*fcn) ()) |
0f2d19dd | 46 | { |
0f2d19dd | 47 | register SCM z; |
17df23e3 | 48 | SCM sname; |
ac51e74b | 49 | SCM *meta_info; |
9de33deb | 50 | |
e0923570 | 51 | meta_info = scm_gc_malloc (2 * sizeof (*meta_info), "subr meta-info"); |
17df23e3 AW |
52 | sname = scm_from_locale_symbol (name); |
53 | meta_info[0] = sname; | |
ac51e74b LC |
54 | meta_info[1] = SCM_EOL; /* properties */ |
55 | ||
56 | z = scm_double_cell ((scm_t_bits) type, (scm_t_bits) fcn, | |
57 | 0 /* generic */, (scm_t_bits) meta_info); | |
9de33deb | 58 | |
17df23e3 AW |
59 | scm_remember_upto_here_1 (sname); |
60 | ||
0f2d19dd JB |
61 | return z; |
62 | } | |
63 | ||
c88a8162 | 64 | SCM |
c014a02e | 65 | scm_c_define_subr (const char *name, long type, SCM (*fcn) ()) |
c88a8162 MV |
66 | { |
67 | SCM subr = scm_c_make_subr (name, type, fcn); | |
ce471ab8 | 68 | scm_define (SCM_SUBR_NAME (subr), subr); |
c88a8162 MV |
69 | return subr; |
70 | } | |
71 | ||
c88a8162 MV |
72 | SCM |
73 | scm_c_make_subr_with_generic (const char *name, | |
c014a02e | 74 | long type, SCM (*fcn) (), SCM *gf) |
0f2d19dd | 75 | { |
c88a8162 | 76 | SCM subr = scm_c_make_subr (name, type, fcn); |
feccd2d3 | 77 | SCM_SET_SUBR_GENERIC_LOC (subr, gf); |
c88a8162 | 78 | return subr; |
0f2d19dd JB |
79 | } |
80 | ||
9de33deb | 81 | SCM |
c88a8162 | 82 | scm_c_define_subr_with_generic (const char *name, |
c014a02e | 83 | long type, SCM (*fcn) (), SCM *gf) |
9de33deb | 84 | { |
c88a8162 | 85 | SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf); |
ce471ab8 | 86 | scm_define (SCM_SUBR_NAME (subr), subr); |
9de33deb MD |
87 | return subr; |
88 | } | |
89 | ||
9de33deb | 90 | |
3b3b36dd | 91 | SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0, |
8cf97abf MG |
92 | (SCM obj), |
93 | "Return @code{#t} if @var{obj} is a procedure.") | |
1bbd0b84 | 94 | #define FUNC_NAME s_scm_procedure_p |
0f2d19dd JB |
95 | { |
96 | if (SCM_NIMP (obj)) | |
97 | switch (SCM_TYP7 (obj)) | |
98 | { | |
904a077d | 99 | case scm_tcs_struct: |
b6cf4d02 AW |
100 | if (!((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC) |
101 | || SCM_STRUCT_APPLICABLE_P (obj))) | |
bdc88419 | 102 | break; |
0f2d19dd | 103 | case scm_tcs_closures: |
f36878ba | 104 | case scm_tc7_gsubr: |
b4cd6492 | 105 | case scm_tc7_pws: |
2fb924f6 | 106 | case scm_tc7_program: |
0f2d19dd | 107 | return SCM_BOOL_T; |
0717dfd8 | 108 | case scm_tc7_smob: |
7888309b | 109 | return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply); |
0f2d19dd JB |
110 | default: |
111 | return SCM_BOOL_F; | |
112 | } | |
113 | return SCM_BOOL_F; | |
114 | } | |
1bbd0b84 | 115 | #undef FUNC_NAME |
0f2d19dd | 116 | |
3b3b36dd | 117 | SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0, |
1bbd0b84 | 118 | (SCM obj), |
8cf97abf | 119 | "Return @code{#t} if @var{obj} is a closure.") |
1bbd0b84 | 120 | #define FUNC_NAME s_scm_closure_p |
ecdb5eb2 | 121 | { |
7888309b | 122 | return scm_from_bool (SCM_CLOSUREP (obj)); |
ecdb5eb2 | 123 | } |
1bbd0b84 | 124 | #undef FUNC_NAME |
ecdb5eb2 | 125 | |
3b3b36dd | 126 | SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0, |
8cf97abf MG |
127 | (SCM obj), |
128 | "Return @code{#t} if @var{obj} is a thunk.") | |
1bbd0b84 | 129 | #define FUNC_NAME s_scm_thunk_p |
44bd53b9 MD |
130 | { |
131 | if (SCM_NIMP (obj)) | |
b4cd6492 MD |
132 | { |
133 | again: | |
134 | switch (SCM_TYP7 (obj)) | |
135 | { | |
136 | case scm_tcs_closures: | |
b7742c6b | 137 | return scm_from_bool (SCM_CLOSURE_NUM_REQUIRED_ARGS (obj) == 0); |
e20d7001 LC |
138 | case scm_tc7_gsubr: |
139 | return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj)) == 0); | |
2fb924f6 | 140 | case scm_tc7_program: |
56164a5a AW |
141 | { |
142 | int a, o, r; | |
143 | if (scm_i_program_arity (obj, &a, &o, &r)) | |
144 | return scm_from_bool (a == 0); | |
145 | else | |
146 | return SCM_BOOL_F; | |
147 | } | |
b4cd6492 MD |
148 | case scm_tc7_pws: |
149 | obj = SCM_PROCEDURE (obj); | |
150 | goto again; | |
151 | default: | |
56164a5a | 152 | return SCM_BOOL_F; |
b4cd6492 MD |
153 | } |
154 | } | |
44bd53b9 MD |
155 | return SCM_BOOL_F; |
156 | } | |
1bbd0b84 | 157 | #undef FUNC_NAME |
44bd53b9 | 158 | |
9de33deb MD |
159 | /* Only used internally. */ |
160 | int | |
161 | scm_subr_p (SCM obj) | |
162 | { | |
163 | if (SCM_NIMP (obj)) | |
164 | switch (SCM_TYP7 (obj)) | |
165 | { | |
f36878ba | 166 | case scm_tc7_gsubr: |
9de33deb MD |
167 | return 1; |
168 | default: | |
169 | ; | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
2a0db0e3 AW |
174 | SCM_SYMBOL (sym_documentation, "documentation"); |
175 | ||
3b3b36dd | 176 | SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0, |
1bbd0b84 | 177 | (SCM proc), |
b380b885 MD |
178 | "Return the documentation string associated with @code{proc}. By\n" |
179 | "convention, if a procedure contains more than one expression and the\n" | |
180 | "first expression is a string constant, that string is assumed to contain\n" | |
181 | "documentation for that procedure.") | |
1bbd0b84 | 182 | #define FUNC_NAME s_scm_procedure_documentation |
c2c82fba MD |
183 | { |
184 | SCM code; | |
bc36d050 | 185 | SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), |
9a09deb1 | 186 | proc, SCM_ARG1, FUNC_NAME); |
2a0db0e3 AW |
187 | if (SCM_PROGRAM_P (proc)) |
188 | return scm_assq_ref (scm_program_properties (proc), sym_documentation); | |
c2c82fba MD |
189 | switch (SCM_TYP7 (proc)) |
190 | { | |
191 | case scm_tcs_closures: | |
f9450cdb | 192 | code = SCM_CLOSURE_BODY (proc); |
d2e53ed6 | 193 | if (scm_is_null (SCM_CDR (code))) |
c2c82fba MD |
194 | return SCM_BOOL_F; |
195 | code = SCM_CAR (code); | |
7f9994d9 | 196 | if (scm_is_string (code)) |
c2c82fba | 197 | return code; |
5c9e7dad DH |
198 | else |
199 | return SCM_BOOL_F; | |
c2c82fba MD |
200 | default: |
201 | return SCM_BOOL_F; | |
c2c82fba MD |
202 | } |
203 | } | |
1bbd0b84 | 204 | #undef FUNC_NAME |
c2c82fba | 205 | |
0f2d19dd | 206 | |
b4cd6492 MD |
207 | /* Procedure-with-setter |
208 | */ | |
209 | ||
a1ec6916 | 210 | SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0, |
1bbd0b84 | 211 | (SCM obj), |
8cf97abf MG |
212 | "Return @code{#t} if @var{obj} is a procedure with an\n" |
213 | "associated setter procedure.") | |
1bbd0b84 | 214 | #define FUNC_NAME s_scm_procedure_with_setter_p |
b4cd6492 | 215 | { |
7888309b | 216 | return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj)); |
b4cd6492 | 217 | } |
1bbd0b84 | 218 | #undef FUNC_NAME |
b4cd6492 | 219 | |
a1ec6916 | 220 | SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0, |
1bbd0b84 | 221 | (SCM procedure, SCM setter), |
8cf97abf MG |
222 | "Create a new procedure which behaves like @var{procedure}, but\n" |
223 | "with the associated setter @var{setter}.") | |
1bbd0b84 | 224 | #define FUNC_NAME s_scm_make_procedure_with_setter |
b4cd6492 | 225 | { |
3fd8807e | 226 | SCM name, ret; |
e1f8eb2b MD |
227 | SCM_VALIDATE_PROC (1, procedure); |
228 | SCM_VALIDATE_PROC (2, setter); | |
3fd8807e AW |
229 | ret = scm_double_cell (scm_tc7_pws, |
230 | SCM_UNPACK (procedure), | |
231 | SCM_UNPACK (setter), 0); | |
232 | /* don't use procedure_name, because don't care enough to do a reverse | |
233 | lookup */ | |
234 | switch (SCM_TYP7 (procedure)) { | |
f36878ba | 235 | case scm_tc7_gsubr: |
ce471ab8 | 236 | name = SCM_SUBR_NAME (procedure); |
3fd8807e AW |
237 | break; |
238 | default: | |
239 | name = scm_procedure_property (procedure, scm_sym_name); | |
240 | break; | |
241 | } | |
242 | if (scm_is_true (name)) | |
243 | scm_set_procedure_property_x (ret, scm_sym_name, name); | |
244 | return ret; | |
b4cd6492 | 245 | } |
1bbd0b84 | 246 | #undef FUNC_NAME |
b4cd6492 | 247 | |
a1ec6916 | 248 | SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0, |
1bbd0b84 | 249 | (SCM proc), |
8cf97abf | 250 | "Return the procedure of @var{proc}, which must be either a\n" |
b6cf4d02 | 251 | "procedure with setter, or an applicable struct.") |
1bbd0b84 | 252 | #define FUNC_NAME s_scm_procedure |
b4cd6492 | 253 | { |
e1f8eb2b | 254 | SCM_VALIDATE_NIM (1, proc); |
b4cd6492 MD |
255 | if (SCM_PROCEDURE_WITH_SETTER_P (proc)) |
256 | return SCM_PROCEDURE (proc); | |
257 | else if (SCM_STRUCTP (proc)) | |
258 | { | |
b6cf4d02 | 259 | SCM_ASSERT (SCM_PUREGENERICP (proc) || SCM_STRUCT_APPLICABLE_P (proc), |
11561496 | 260 | proc, SCM_ARG1, FUNC_NAME); |
b4cd6492 MD |
261 | return proc; |
262 | } | |
1bbd0b84 | 263 | SCM_WRONG_TYPE_ARG (1, proc); |
4260a7fc | 264 | return SCM_BOOL_F; /* not reached */ |
b4cd6492 | 265 | } |
1bbd0b84 | 266 | #undef FUNC_NAME |
b4cd6492 | 267 | |
f5267231 | 268 | SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter); |
b4cd6492 MD |
269 | |
270 | SCM | |
271 | scm_setter (SCM proc) | |
272 | { | |
f5267231 | 273 | SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter); |
b4cd6492 MD |
274 | if (SCM_PROCEDURE_WITH_SETTER_P (proc)) |
275 | return SCM_SETTER (proc); | |
276 | else if (SCM_STRUCTP (proc)) | |
277 | { | |
b6cf4d02 AW |
278 | SCM setter = SCM_BOOL_F; |
279 | if (SCM_PUREGENERICP (proc)) | |
280 | setter = SCM_GENERIC_SETTER (proc); | |
281 | else if (SCM_STRUCT_SETTER_P (proc)) | |
282 | setter = SCM_STRUCT_SETTER (proc); | |
c72a774a MD |
283 | if (SCM_NIMP (setter)) |
284 | return setter; | |
285 | /* fall through */ | |
b4cd6492 | 286 | } |
f5267231 | 287 | SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter); |
b038d983 | 288 | return SCM_BOOL_F; /* not reached */ |
b4cd6492 | 289 | } |
1cc91f1b | 290 | |
ac51e74b | 291 | \f |
0f2d19dd JB |
292 | void |
293 | scm_init_procs () | |
0f2d19dd | 294 | { |
a0599745 | 295 | #include "libguile/procs.x" |
0f2d19dd | 296 | } |
89e00824 ML |
297 | |
298 | /* | |
299 | Local Variables: | |
300 | c-file-style: "gnu" | |
301 | End: | |
302 | */ |