Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 */
17
18
19 \f
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25
26 #include "libguile/objects.h"
27 #include "libguile/strings.h"
28 #include "libguile/vectors.h"
29 #include "libguile/smob.h"
30 #include "libguile/deprecation.h"
31
32 #include "libguile/validate.h"
33 #include "libguile/procs.h"
34 #include "libguile/procprop.h"
35 #include "libguile/objcodes.h"
36 #include "libguile/programs.h"
37 \f
38
39
40 /* {Procedures}
41 */
42
43
44 SCM
45 scm_c_make_subr (const char *name, long type, SCM (*fcn) ())
46 {
47 register SCM z;
48 SCM *meta_info;
49
50 meta_info = scm_gc_malloc (2 * sizeof (*meta_info), "subr meta-info");
51 meta_info[0] = scm_from_locale_symbol (name);
52 meta_info[1] = SCM_EOL; /* properties */
53
54 z = scm_double_cell ((scm_t_bits) type, (scm_t_bits) fcn,
55 0 /* generic */, (scm_t_bits) meta_info);
56
57 return z;
58 }
59
60 SCM
61 scm_c_define_subr (const char *name, long type, SCM (*fcn) ())
62 {
63 SCM subr = scm_c_make_subr (name, type, fcn);
64 scm_define (SCM_SNAME (subr), subr);
65 return subr;
66 }
67
68 SCM
69 scm_c_make_subr_with_generic (const char *name,
70 long type, SCM (*fcn) (), SCM *gf)
71 {
72 SCM subr = scm_c_make_subr (name, type, fcn);
73 SCM_SET_SUBR_GENERIC_LOC (subr, gf);
74 return subr;
75 }
76
77 SCM
78 scm_c_define_subr_with_generic (const char *name,
79 long type, SCM (*fcn) (), SCM *gf)
80 {
81 SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf);
82 scm_define (SCM_SNAME (subr), subr);
83 return subr;
84 }
85
86
87 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
88 (SCM obj),
89 "Return @code{#t} if @var{obj} is a procedure.")
90 #define FUNC_NAME s_scm_procedure_p
91 {
92 if (SCM_NIMP (obj))
93 switch (SCM_TYP7 (obj))
94 {
95 case scm_tcs_struct:
96 if (!SCM_I_OPERATORP (obj))
97 break;
98 case scm_tcs_closures:
99 case scm_tcs_subrs:
100 case scm_tc7_pws:
101 return SCM_BOOL_T;
102 case scm_tc7_smob:
103 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
104 default:
105 return SCM_BOOL_F;
106 }
107 return SCM_BOOL_F;
108 }
109 #undef FUNC_NAME
110
111 SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
112 (SCM obj),
113 "Return @code{#t} if @var{obj} is a closure.")
114 #define FUNC_NAME s_scm_closure_p
115 {
116 return scm_from_bool (SCM_CLOSUREP (obj));
117 }
118 #undef FUNC_NAME
119
120 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
121 (SCM obj),
122 "Return @code{#t} if @var{obj} is a thunk.")
123 #define FUNC_NAME s_scm_thunk_p
124 {
125 if (SCM_NIMP (obj))
126 {
127 again:
128 switch (SCM_TYP7 (obj))
129 {
130 case scm_tcs_closures:
131 return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj)));
132 case scm_tc7_subr_0:
133 case scm_tc7_subr_1o:
134 case scm_tc7_lsubr:
135 case scm_tc7_rpsubr:
136 case scm_tc7_asubr:
137 return SCM_BOOL_T;
138 case scm_tc7_gsubr:
139 return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj)) == 0);
140 case scm_tc7_pws:
141 obj = SCM_PROCEDURE (obj);
142 goto again;
143 default:
144 if (SCM_PROGRAM_P (obj) && SCM_PROGRAM_DATA (obj)->nargs == 0)
145 return SCM_BOOL_T;
146 /* otherwise fall through */
147 }
148 }
149 return SCM_BOOL_F;
150 }
151 #undef FUNC_NAME
152
153 /* Only used internally. */
154 int
155 scm_subr_p (SCM obj)
156 {
157 if (SCM_NIMP (obj))
158 switch (SCM_TYP7 (obj))
159 {
160 case scm_tcs_subrs:
161 return 1;
162 default:
163 ;
164 }
165 return 0;
166 }
167
168 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
169 (SCM proc),
170 "Return the documentation string associated with @code{proc}. By\n"
171 "convention, if a procedure contains more than one expression and the\n"
172 "first expression is a string constant, that string is assumed to contain\n"
173 "documentation for that procedure.")
174 #define FUNC_NAME s_scm_procedure_documentation
175 {
176 SCM code;
177 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
178 proc, SCM_ARG1, FUNC_NAME);
179 switch (SCM_TYP7 (proc))
180 {
181 case scm_tcs_closures:
182 code = SCM_CLOSURE_BODY (proc);
183 if (scm_is_null (SCM_CDR (code)))
184 return SCM_BOOL_F;
185 code = SCM_CAR (code);
186 if (scm_is_string (code))
187 return code;
188 else
189 return SCM_BOOL_F;
190 default:
191 return SCM_BOOL_F;
192 }
193 }
194 #undef FUNC_NAME
195
196
197 /* Procedure-with-setter
198 */
199
200 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
201 (SCM obj),
202 "Return @code{#t} if @var{obj} is a procedure with an\n"
203 "associated setter procedure.")
204 #define FUNC_NAME s_scm_procedure_with_setter_p
205 {
206 return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj));
207 }
208 #undef FUNC_NAME
209
210 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
211 (SCM procedure, SCM setter),
212 "Create a new procedure which behaves like @var{procedure}, but\n"
213 "with the associated setter @var{setter}.")
214 #define FUNC_NAME s_scm_make_procedure_with_setter
215 {
216 SCM name, ret;
217 SCM_VALIDATE_PROC (1, procedure);
218 SCM_VALIDATE_PROC (2, setter);
219 ret = scm_double_cell (scm_tc7_pws,
220 SCM_UNPACK (procedure),
221 SCM_UNPACK (setter), 0);
222 /* don't use procedure_name, because don't care enough to do a reverse
223 lookup */
224 switch (SCM_TYP7 (procedure)) {
225 case scm_tcs_subrs:
226 name = SCM_SNAME (procedure);
227 break;
228 default:
229 name = scm_procedure_property (procedure, scm_sym_name);
230 break;
231 }
232 if (scm_is_true (name))
233 scm_set_procedure_property_x (ret, scm_sym_name, name);
234 return ret;
235 }
236 #undef FUNC_NAME
237
238 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
239 (SCM proc),
240 "Return the procedure of @var{proc}, which must be either a\n"
241 "procedure with setter, or an operator struct.")
242 #define FUNC_NAME s_scm_procedure
243 {
244 SCM_VALIDATE_NIM (1, proc);
245 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
246 return SCM_PROCEDURE (proc);
247 else if (SCM_STRUCTP (proc))
248 {
249 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
250 return proc;
251 }
252 SCM_WRONG_TYPE_ARG (1, proc);
253 return SCM_BOOL_F; /* not reached */
254 }
255 #undef FUNC_NAME
256
257 SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
258
259 SCM
260 scm_setter (SCM proc)
261 {
262 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
263 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
264 return SCM_SETTER (proc);
265 else if (SCM_STRUCTP (proc))
266 {
267 SCM setter;
268 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
269 g_setter, proc, SCM_ARG1, s_setter);
270 setter = (SCM_I_ENTITYP (proc)
271 ? SCM_ENTITY_SETTER (proc)
272 : SCM_OPERATOR_SETTER (proc));
273 if (SCM_NIMP (setter))
274 return setter;
275 /* fall through */
276 }
277 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
278 return SCM_BOOL_F; /* not reached */
279 }
280
281 \f
282 void
283 scm_init_procs ()
284 {
285 #include "libguile/procs.x"
286 }
287
288 /*
289 Local Variables:
290 c-file-style: "gnu"
291 End:
292 */