Commit | Line | Data |
---|---|---|
ac51e74b | 1 | /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
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. | |
0f2d19dd | 7 | * |
73be1d9e MV |
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. | |
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 | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
dbb605f5 LC |
20 | #ifdef HAVE_CONFIG_H |
21 | # include <config.h> | |
22 | #endif | |
0f2d19dd | 23 | |
a0599745 | 24 | #include "libguile/_scm.h" |
0f2d19dd | 25 | |
a0599745 MD |
26 | #include "libguile/objects.h" |
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" | |
0f2d19dd JB |
34 | \f |
35 | ||
36 | ||
37 | /* {Procedures} | |
38 | */ | |
39 | ||
9de33deb | 40 | |
ac51e74b | 41 | SCM |
c014a02e | 42 | scm_c_make_subr (const char *name, long type, SCM (*fcn) ()) |
0f2d19dd | 43 | { |
0f2d19dd | 44 | register SCM z; |
ac51e74b | 45 | SCM *meta_info; |
9de33deb | 46 | |
e0923570 | 47 | meta_info = scm_gc_malloc (2 * sizeof (*meta_info), "subr meta-info"); |
ac51e74b LC |
48 | meta_info[0] = scm_from_locale_symbol (name); |
49 | meta_info[1] = SCM_EOL; /* properties */ | |
50 | ||
51 | z = scm_double_cell ((scm_t_bits) type, (scm_t_bits) fcn, | |
52 | 0 /* generic */, (scm_t_bits) meta_info); | |
9de33deb | 53 | |
0f2d19dd JB |
54 | return z; |
55 | } | |
56 | ||
c88a8162 | 57 | SCM |
c014a02e | 58 | scm_c_define_subr (const char *name, long type, SCM (*fcn) ()) |
c88a8162 MV |
59 | { |
60 | SCM subr = scm_c_make_subr (name, type, fcn); | |
cce8b2ce | 61 | scm_define (SCM_SNAME (subr), subr); |
c88a8162 MV |
62 | return subr; |
63 | } | |
64 | ||
9de33deb MD |
65 | /* This function isn't currently used since subrs are never freed. */ |
66 | /* *fixme* Need mutex here. */ | |
67 | void | |
68 | scm_free_subr_entry (SCM subr) | |
69 | { | |
ac51e74b LC |
70 | scm_gc_free (SCM_SUBR_META_INFO (subr), 2 * sizeof (SCM), |
71 | "subr meta-info"); | |
9de33deb | 72 | } |
1cc91f1b | 73 | |
c88a8162 MV |
74 | SCM |
75 | scm_c_make_subr_with_generic (const char *name, | |
c014a02e | 76 | long type, SCM (*fcn) (), SCM *gf) |
0f2d19dd | 77 | { |
c88a8162 | 78 | SCM subr = scm_c_make_subr (name, type, fcn); |
feccd2d3 | 79 | SCM_SET_SUBR_GENERIC_LOC (subr, gf); |
c88a8162 | 80 | return subr; |
0f2d19dd JB |
81 | } |
82 | ||
9de33deb | 83 | SCM |
c88a8162 | 84 | scm_c_define_subr_with_generic (const char *name, |
c014a02e | 85 | long type, SCM (*fcn) (), SCM *gf) |
9de33deb | 86 | { |
c88a8162 | 87 | SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf); |
cce8b2ce | 88 | scm_define (SCM_SNAME (subr), subr); |
9de33deb MD |
89 | return subr; |
90 | } | |
91 | ||
9de33deb | 92 | |
3b3b36dd | 93 | SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0, |
8cf97abf MG |
94 | (SCM obj), |
95 | "Return @code{#t} if @var{obj} is a procedure.") | |
1bbd0b84 | 96 | #define FUNC_NAME s_scm_procedure_p |
0f2d19dd JB |
97 | { |
98 | if (SCM_NIMP (obj)) | |
99 | switch (SCM_TYP7 (obj)) | |
100 | { | |
904a077d | 101 | case scm_tcs_struct: |
bdc88419 MD |
102 | if (!SCM_I_OPERATORP (obj)) |
103 | break; | |
0f2d19dd | 104 | case scm_tcs_closures: |
0f2d19dd | 105 | case scm_tcs_subrs: |
b4cd6492 | 106 | case scm_tc7_pws: |
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: | |
d2e53ed6 | 137 | return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj))); |
b4cd6492 MD |
138 | case scm_tc7_subr_0: |
139 | case scm_tc7_subr_1o: | |
140 | case scm_tc7_lsubr: | |
141 | case scm_tc7_rpsubr: | |
142 | case scm_tc7_asubr: | |
b4cd6492 | 143 | return SCM_BOOL_T; |
e20d7001 LC |
144 | case scm_tc7_gsubr: |
145 | return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj)) == 0); | |
b4cd6492 MD |
146 | case scm_tc7_pws: |
147 | obj = SCM_PROCEDURE (obj); | |
148 | goto again; | |
149 | default: | |
150 | ; | |
151 | } | |
152 | } | |
44bd53b9 MD |
153 | return SCM_BOOL_F; |
154 | } | |
1bbd0b84 | 155 | #undef FUNC_NAME |
44bd53b9 | 156 | |
9de33deb MD |
157 | /* Only used internally. */ |
158 | int | |
159 | scm_subr_p (SCM obj) | |
160 | { | |
161 | if (SCM_NIMP (obj)) | |
162 | switch (SCM_TYP7 (obj)) | |
163 | { | |
164 | case scm_tcs_subrs: | |
165 | return 1; | |
166 | default: | |
167 | ; | |
168 | } | |
169 | return 0; | |
170 | } | |
171 | ||
3b3b36dd | 172 | SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0, |
1bbd0b84 | 173 | (SCM proc), |
b380b885 MD |
174 | "Return the documentation string associated with @code{proc}. By\n" |
175 | "convention, if a procedure contains more than one expression and the\n" | |
176 | "first expression is a string constant, that string is assumed to contain\n" | |
177 | "documentation for that procedure.") | |
1bbd0b84 | 178 | #define FUNC_NAME s_scm_procedure_documentation |
c2c82fba MD |
179 | { |
180 | SCM code; | |
bc36d050 | 181 | SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), |
9a09deb1 | 182 | proc, SCM_ARG1, FUNC_NAME); |
c2c82fba MD |
183 | switch (SCM_TYP7 (proc)) |
184 | { | |
185 | case scm_tcs_closures: | |
f9450cdb | 186 | code = SCM_CLOSURE_BODY (proc); |
d2e53ed6 | 187 | if (scm_is_null (SCM_CDR (code))) |
c2c82fba MD |
188 | return SCM_BOOL_F; |
189 | code = SCM_CAR (code); | |
7f9994d9 | 190 | if (scm_is_string (code)) |
c2c82fba | 191 | return code; |
5c9e7dad DH |
192 | else |
193 | return SCM_BOOL_F; | |
c2c82fba MD |
194 | default: |
195 | return SCM_BOOL_F; | |
c2c82fba MD |
196 | } |
197 | } | |
1bbd0b84 | 198 | #undef FUNC_NAME |
c2c82fba | 199 | |
0f2d19dd | 200 | |
b4cd6492 MD |
201 | /* Procedure-with-setter |
202 | */ | |
203 | ||
a1ec6916 | 204 | SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0, |
1bbd0b84 | 205 | (SCM obj), |
8cf97abf MG |
206 | "Return @code{#t} if @var{obj} is a procedure with an\n" |
207 | "associated setter procedure.") | |
1bbd0b84 | 208 | #define FUNC_NAME s_scm_procedure_with_setter_p |
b4cd6492 | 209 | { |
7888309b | 210 | return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj)); |
b4cd6492 | 211 | } |
1bbd0b84 | 212 | #undef FUNC_NAME |
b4cd6492 | 213 | |
a1ec6916 | 214 | SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0, |
1bbd0b84 | 215 | (SCM procedure, SCM setter), |
8cf97abf MG |
216 | "Create a new procedure which behaves like @var{procedure}, but\n" |
217 | "with the associated setter @var{setter}.") | |
1bbd0b84 | 218 | #define FUNC_NAME s_scm_make_procedure_with_setter |
b4cd6492 | 219 | { |
e1f8eb2b MD |
220 | SCM_VALIDATE_PROC (1, procedure); |
221 | SCM_VALIDATE_PROC (2, setter); | |
228a24ef DH |
222 | return scm_double_cell (scm_tc7_pws, |
223 | SCM_UNPACK (procedure), | |
224 | SCM_UNPACK (setter), 0); | |
b4cd6492 | 225 | } |
1bbd0b84 | 226 | #undef FUNC_NAME |
b4cd6492 | 227 | |
a1ec6916 | 228 | SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0, |
1bbd0b84 | 229 | (SCM proc), |
8cf97abf MG |
230 | "Return the procedure of @var{proc}, which must be either a\n" |
231 | "procedure with setter, or an operator struct.") | |
1bbd0b84 | 232 | #define FUNC_NAME s_scm_procedure |
b4cd6492 | 233 | { |
e1f8eb2b | 234 | SCM_VALIDATE_NIM (1, proc); |
b4cd6492 MD |
235 | if (SCM_PROCEDURE_WITH_SETTER_P (proc)) |
236 | return SCM_PROCEDURE (proc); | |
237 | else if (SCM_STRUCTP (proc)) | |
238 | { | |
1bbd0b84 | 239 | SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME); |
b4cd6492 MD |
240 | return proc; |
241 | } | |
1bbd0b84 | 242 | SCM_WRONG_TYPE_ARG (1, proc); |
4260a7fc | 243 | return SCM_BOOL_F; /* not reached */ |
b4cd6492 | 244 | } |
1bbd0b84 | 245 | #undef FUNC_NAME |
b4cd6492 | 246 | |
f5267231 | 247 | SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter); |
b4cd6492 MD |
248 | |
249 | SCM | |
250 | scm_setter (SCM proc) | |
251 | { | |
f5267231 | 252 | SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter); |
b4cd6492 MD |
253 | if (SCM_PROCEDURE_WITH_SETTER_P (proc)) |
254 | return SCM_SETTER (proc); | |
255 | else if (SCM_STRUCTP (proc)) | |
256 | { | |
c72a774a | 257 | SCM setter; |
f5267231 MD |
258 | SCM_GASSERT1 (SCM_I_OPERATORP (proc), |
259 | g_setter, proc, SCM_ARG1, s_setter); | |
c72a774a MD |
260 | setter = (SCM_I_ENTITYP (proc) |
261 | ? SCM_ENTITY_SETTER (proc) | |
262 | : SCM_OPERATOR_SETTER (proc)); | |
263 | if (SCM_NIMP (setter)) | |
264 | return setter; | |
265 | /* fall through */ | |
b4cd6492 | 266 | } |
f5267231 | 267 | SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter); |
b038d983 | 268 | return SCM_BOOL_F; /* not reached */ |
b4cd6492 | 269 | } |
1cc91f1b | 270 | |
ac51e74b | 271 | \f |
0f2d19dd JB |
272 | void |
273 | scm_init_procs () | |
0f2d19dd | 274 | { |
a0599745 | 275 | #include "libguile/procs.x" |
0f2d19dd | 276 | } |
89e00824 ML |
277 | |
278 | /* | |
279 | Local Variables: | |
280 | c-file-style: "gnu" | |
281 | End: | |
282 | */ |