1 /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008 Free Software Foundation, Inc.
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.
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.
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
24 #include "libguile/_scm.h"
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"
32 #include "libguile/validate.h"
33 #include "libguile/procs.h"
40 scm_t_subr_entry *scm_subr_table;
42 /* libguile contained approx. 700 primitive procedures on 24 Aug 1999. */
44 /* Increased to 800 on 2001-05-07 -- Guile now has 779 primitives on
45 startup, 786 with guile-readline. 'martin */
47 long scm_subr_table_size = 0;
48 long scm_subr_table_room = 800;
51 scm_c_make_subr (const char *name, long type, SCM (*fcn) ())
56 if (scm_subr_table_size == scm_subr_table_room)
58 long new_size = scm_subr_table_room * 3 / 2;
60 = scm_realloc ((char *) scm_subr_table,
61 sizeof (scm_t_subr_entry) * new_size);
62 scm_subr_table = new_table;
63 scm_subr_table_room = new_size;
66 entry = scm_subr_table_size;
67 z = scm_cell ((entry << 8) + type, (scm_t_bits) fcn);
68 scm_subr_table[entry].handle = z;
69 scm_subr_table[entry].name = scm_from_locale_symbol (name);
70 scm_subr_table[entry].generic = 0;
71 scm_subr_table[entry].properties = SCM_EOL;
72 scm_subr_table_size++;
78 scm_c_define_subr (const char *name, long type, SCM (*fcn) ())
80 SCM subr = scm_c_make_subr (name, type, fcn);
81 scm_define (SCM_SUBR_ENTRY(subr).name, subr);
85 /* This function isn't currently used since subrs are never freed. */
86 /* *fixme* Need mutex here. */
88 scm_free_subr_entry (SCM subr)
90 long entry = SCM_SUBRNUM (subr);
91 /* Move last entry in table to the free position */
92 scm_subr_table[entry] = scm_subr_table[scm_subr_table_size - 1];
93 SCM_SET_SUBRNUM (scm_subr_table[entry].handle, entry);
94 scm_subr_table_size--;
98 scm_c_make_subr_with_generic (const char *name,
99 long type, SCM (*fcn) (), SCM *gf)
101 SCM subr = scm_c_make_subr (name, type, fcn);
102 SCM_SUBR_ENTRY(subr).generic = gf;
107 scm_c_define_subr_with_generic (const char *name,
108 long type, SCM (*fcn) (), SCM *gf)
110 SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf);
111 scm_define (SCM_SUBR_ENTRY(subr).name, subr);
116 scm_mark_subr_table ()
119 for (i = 0; i < scm_subr_table_size; ++i)
121 scm_gc_mark (scm_subr_table[i].name);
122 if (scm_subr_table[i].generic && *scm_subr_table[i].generic)
123 scm_gc_mark (*scm_subr_table[i].generic);
124 if (SCM_NIMP (scm_subr_table[i].properties))
125 scm_gc_mark (scm_subr_table[i].properties);
132 scm_makcclo (SCM proc, size_t len)
134 scm_t_bits *base = scm_gc_malloc (len * sizeof (scm_t_bits),
139 for (i = 0; i < len; ++i)
140 base [i] = SCM_UNPACK (SCM_UNSPECIFIED);
142 s = scm_cell (SCM_MAKE_CCLO_TAG (len), (scm_t_bits) base);
143 SCM_SET_CCLO_SUBR (s, proc);
147 /* Undocumented debugging procedure */
149 SCM_DEFINE (scm_make_cclo, "make-cclo", 2, 0, 0,
151 "Create a compiled closure for @var{proc}, which reserves\n"
152 "@var{len} objects for its usage.")
153 #define FUNC_NAME s_scm_make_cclo
155 return scm_makcclo (proc, scm_to_size_t (len));
163 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
165 "Return @code{#t} if @var{obj} is a procedure.")
166 #define FUNC_NAME s_scm_procedure_p
169 switch (SCM_TYP7 (obj))
172 if (!SCM_I_OPERATORP (obj))
174 case scm_tcs_closures:
182 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
190 SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
192 "Return @code{#t} if @var{obj} is a closure.")
193 #define FUNC_NAME s_scm_closure_p
195 return scm_from_bool (SCM_CLOSUREP (obj));
199 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
201 "Return @code{#t} if @var{obj} is a thunk.")
202 #define FUNC_NAME s_scm_thunk_p
207 switch (SCM_TYP7 (obj))
209 case scm_tcs_closures:
210 return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj)));
212 case scm_tc7_subr_1o:
221 obj = SCM_PROCEDURE (obj);
231 /* Only used internally. */
236 switch (SCM_TYP7 (obj))
246 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
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
255 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
256 proc, SCM_ARG1, FUNC_NAME);
257 switch (SCM_TYP7 (proc))
259 case scm_tcs_closures:
260 code = SCM_CLOSURE_BODY (proc);
261 if (scm_is_null (SCM_CDR (code)))
263 code = SCM_CAR (code);
264 if (scm_is_string (code))
281 /* Procedure-with-setter
284 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
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
290 return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj));
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
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);
308 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
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
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))
319 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
322 SCM_WRONG_TYPE_ARG (1, proc);
323 return SCM_BOOL_F; /* not reached */
327 SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
330 scm_setter (SCM proc)
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))
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))
347 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
348 return SCM_BOOL_F; /* not reached */
353 scm_init_subr_table ()
356 = ((scm_t_subr_entry *)
357 scm_malloc (sizeof (scm_t_subr_entry) * scm_subr_table_room));
363 #include "libguile/procs.x"