make thunk? understand programs
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006 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
21 #include "libguile/_scm.h"
22
23 #include "libguile/objects.h"
24 #include "libguile/strings.h"
25 #include "libguile/vectors.h"
26 #include "libguile/smob.h"
27 #include "libguile/deprecation.h"
28
29 #include "libguile/validate.h"
30 #include "libguile/procs.h"
31 #include "libguile/programs.h"
32 \f
33
34
35 /* {Procedures}
36 */
37
38 scm_t_subr_entry *scm_subr_table;
39
40 /* libguile contained approx. 700 primitive procedures on 24 Aug 1999. */
41
42 /* Increased to 800 on 2001-05-07 -- Guile now has 779 primitives on
43 startup, 786 with guile-readline. 'martin */
44
45 long scm_subr_table_size = 0;
46 long scm_subr_table_room = 800;
47
48 SCM
49 scm_c_make_subr (const char *name, long type, SCM (*fcn) ())
50 {
51 register SCM z;
52 long entry;
53
54 if (scm_subr_table_size == scm_subr_table_room)
55 {
56 long new_size = scm_subr_table_room * 3 / 2;
57 void *new_table
58 = scm_realloc ((char *) scm_subr_table,
59 sizeof (scm_t_subr_entry) * new_size);
60 scm_subr_table = new_table;
61 scm_subr_table_room = new_size;
62 }
63
64 entry = scm_subr_table_size;
65 z = scm_cell ((entry << 8) + type, (scm_t_bits) fcn);
66 scm_subr_table[entry].handle = z;
67 scm_subr_table[entry].name = scm_from_locale_symbol (name);
68 scm_subr_table[entry].generic = 0;
69 scm_subr_table[entry].properties = SCM_EOL;
70 scm_subr_table_size++;
71
72 return z;
73 }
74
75 SCM
76 scm_c_define_subr (const char *name, long type, SCM (*fcn) ())
77 {
78 SCM subr = scm_c_make_subr (name, type, fcn);
79 scm_define (SCM_SUBR_ENTRY(subr).name, subr);
80 return subr;
81 }
82
83 /* This function isn't currently used since subrs are never freed. */
84 /* *fixme* Need mutex here. */
85 void
86 scm_free_subr_entry (SCM subr)
87 {
88 long entry = SCM_SUBRNUM (subr);
89 /* Move last entry in table to the free position */
90 scm_subr_table[entry] = scm_subr_table[scm_subr_table_size - 1];
91 SCM_SET_SUBRNUM (scm_subr_table[entry].handle, entry);
92 scm_subr_table_size--;
93 }
94
95 SCM
96 scm_c_make_subr_with_generic (const char *name,
97 long type, SCM (*fcn) (), SCM *gf)
98 {
99 SCM subr = scm_c_make_subr (name, type, fcn);
100 SCM_SUBR_ENTRY(subr).generic = gf;
101 return subr;
102 }
103
104 SCM
105 scm_c_define_subr_with_generic (const char *name,
106 long type, SCM (*fcn) (), SCM *gf)
107 {
108 SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf);
109 scm_define (SCM_SUBR_ENTRY(subr).name, subr);
110 return subr;
111 }
112
113 void
114 scm_mark_subr_table ()
115 {
116 long i;
117 for (i = 0; i < scm_subr_table_size; ++i)
118 {
119 scm_gc_mark (scm_subr_table[i].name);
120 if (scm_subr_table[i].generic && *scm_subr_table[i].generic)
121 scm_gc_mark (*scm_subr_table[i].generic);
122 if (SCM_NIMP (scm_subr_table[i].properties))
123 scm_gc_mark (scm_subr_table[i].properties);
124 }
125 }
126
127
128 #ifdef CCLO
129 SCM
130 scm_makcclo (SCM proc, size_t len)
131 {
132 scm_t_bits *base = scm_gc_malloc (len * sizeof (scm_t_bits),
133 "compiled closure");
134 unsigned long i;
135 SCM s;
136
137 for (i = 0; i < len; ++i)
138 base [i] = SCM_UNPACK (SCM_UNSPECIFIED);
139
140 s = scm_cell (SCM_MAKE_CCLO_TAG (len), (scm_t_bits) base);
141 SCM_SET_CCLO_SUBR (s, proc);
142 return s;
143 }
144
145 /* Undocumented debugging procedure */
146 #ifdef GUILE_DEBUG
147 SCM_DEFINE (scm_make_cclo, "make-cclo", 2, 0, 0,
148 (SCM proc, SCM len),
149 "Create a compiled closure for @var{proc}, which reserves\n"
150 "@var{len} objects for its usage.")
151 #define FUNC_NAME s_scm_make_cclo
152 {
153 return scm_makcclo (proc, scm_to_size_t (len));
154 }
155 #undef FUNC_NAME
156 #endif
157 #endif
158
159
160
161 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
162 (SCM obj),
163 "Return @code{#t} if @var{obj} is a procedure.")
164 #define FUNC_NAME s_scm_procedure_p
165 {
166 if (SCM_NIMP (obj))
167 switch (SCM_TYP7 (obj))
168 {
169 case scm_tcs_struct:
170 if (!SCM_I_OPERATORP (obj))
171 break;
172 case scm_tcs_closures:
173 case scm_tcs_subrs:
174 #ifdef CCLO
175 case scm_tc7_cclo:
176 #endif
177 case scm_tc7_pws:
178 return SCM_BOOL_T;
179 case scm_tc7_smob:
180 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
181 default:
182 return SCM_BOOL_F;
183 }
184 return SCM_BOOL_F;
185 }
186 #undef FUNC_NAME
187
188 SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
189 (SCM obj),
190 "Return @code{#t} if @var{obj} is a closure.")
191 #define FUNC_NAME s_scm_closure_p
192 {
193 return scm_from_bool (SCM_CLOSUREP (obj));
194 }
195 #undef FUNC_NAME
196
197 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
198 (SCM obj),
199 "Return @code{#t} if @var{obj} is a thunk.")
200 #define FUNC_NAME s_scm_thunk_p
201 {
202 if (SCM_NIMP (obj))
203 {
204 again:
205 switch (SCM_TYP7 (obj))
206 {
207 case scm_tcs_closures:
208 return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj)));
209 case scm_tc7_subr_0:
210 case scm_tc7_subr_1o:
211 case scm_tc7_lsubr:
212 case scm_tc7_rpsubr:
213 case scm_tc7_asubr:
214 #ifdef CCLO
215 case scm_tc7_cclo:
216 #endif
217 return SCM_BOOL_T;
218 case scm_tc7_pws:
219 obj = SCM_PROCEDURE (obj);
220 goto again;
221 default:
222 if (SCM_PROGRAM_P (obj) && SCM_PROGRAM_DATA (obj)->nargs == 0)
223 return SCM_BOOL_T;
224 /* otherwise fall through */
225 }
226 }
227 return SCM_BOOL_F;
228 }
229 #undef FUNC_NAME
230
231 /* Only used internally. */
232 int
233 scm_subr_p (SCM obj)
234 {
235 if (SCM_NIMP (obj))
236 switch (SCM_TYP7 (obj))
237 {
238 case scm_tcs_subrs:
239 return 1;
240 default:
241 ;
242 }
243 return 0;
244 }
245
246 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
247 (SCM proc),
248 "Return the documentation string associated with @code{proc}. By\n"
249 "convention, if a procedure contains more than one expression and the\n"
250 "first expression is a string constant, that string is assumed to contain\n"
251 "documentation for that procedure.")
252 #define FUNC_NAME s_scm_procedure_documentation
253 {
254 SCM code;
255 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
256 proc, SCM_ARG1, FUNC_NAME);
257 switch (SCM_TYP7 (proc))
258 {
259 case scm_tcs_closures:
260 code = SCM_CLOSURE_BODY (proc);
261 if (scm_is_null (SCM_CDR (code)))
262 return SCM_BOOL_F;
263 code = SCM_CAR (code);
264 if (scm_is_string (code))
265 return code;
266 else
267 return SCM_BOOL_F;
268 default:
269 return SCM_BOOL_F;
270 /*
271 case scm_tcs_subrs:
272 #ifdef CCLO
273 case scm_tc7_cclo:
274 #endif
275 */
276 }
277 }
278 #undef FUNC_NAME
279
280
281 /* Procedure-with-setter
282 */
283
284 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
285 (SCM obj),
286 "Return @code{#t} if @var{obj} is a procedure with an\n"
287 "associated setter procedure.")
288 #define FUNC_NAME s_scm_procedure_with_setter_p
289 {
290 return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj));
291 }
292 #undef FUNC_NAME
293
294 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
295 (SCM procedure, SCM setter),
296 "Create a new procedure which behaves like @var{procedure}, but\n"
297 "with the associated setter @var{setter}.")
298 #define FUNC_NAME s_scm_make_procedure_with_setter
299 {
300 SCM_VALIDATE_PROC (1, procedure);
301 SCM_VALIDATE_PROC (2, setter);
302 return scm_double_cell (scm_tc7_pws,
303 SCM_UNPACK (procedure),
304 SCM_UNPACK (setter), 0);
305 }
306 #undef FUNC_NAME
307
308 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
309 (SCM proc),
310 "Return the procedure of @var{proc}, which must be either a\n"
311 "procedure with setter, or an operator struct.")
312 #define FUNC_NAME s_scm_procedure
313 {
314 SCM_VALIDATE_NIM (1, proc);
315 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
316 return SCM_PROCEDURE (proc);
317 else if (SCM_STRUCTP (proc))
318 {
319 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
320 return proc;
321 }
322 SCM_WRONG_TYPE_ARG (1, proc);
323 return SCM_BOOL_F; /* not reached */
324 }
325 #undef FUNC_NAME
326
327 SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
328
329 SCM
330 scm_setter (SCM proc)
331 {
332 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
333 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
334 return SCM_SETTER (proc);
335 else if (SCM_STRUCTP (proc))
336 {
337 SCM setter;
338 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
339 g_setter, proc, SCM_ARG1, s_setter);
340 setter = (SCM_I_ENTITYP (proc)
341 ? SCM_ENTITY_SETTER (proc)
342 : SCM_OPERATOR_SETTER (proc));
343 if (SCM_NIMP (setter))
344 return setter;
345 /* fall through */
346 }
347 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
348 return SCM_BOOL_F; /* not reached */
349 }
350
351
352 void
353 scm_init_subr_table ()
354 {
355 scm_subr_table
356 = ((scm_t_subr_entry *)
357 scm_malloc (sizeof (scm_t_subr_entry) * scm_subr_table_room));
358 }
359
360 void
361 scm_init_procs ()
362 {
363 #include "libguile/procs.x"
364 }
365
366 /*
367 Local Variables:
368 c-file-style: "gnu"
369 End:
370 */