* *.[ch]: Whitespace changes -- added space after SCM_VALIDATE_*
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <stdio.h>
48 #include "_scm.h"
49
50 #include "objects.h"
51
52 #include "scm_validate.h"
53 #include "procs.h"
54 \f
55
56
57 /* {Procedures}
58 */
59
60 scm_subr_entry *scm_subr_table;
61
62 /* libguile contained approx. 700 primitive procedures on 24 Aug 1999. */
63
64 int scm_subr_table_size = 0;
65 int scm_subr_table_room = 750;
66
67 SCM
68 scm_make_subr_opt (const char *name, int type, SCM (*fcn) (), int set)
69 {
70 SCM symcell;
71 register SCM z;
72 int entry;
73
74 if (scm_subr_table_size == scm_subr_table_room)
75 {
76 scm_sizet new_size = scm_subr_table_room * 3 / 2;
77 void *new_table
78 = scm_must_realloc ((char *) scm_subr_table,
79 sizeof (scm_subr_entry) * scm_subr_table_room,
80 sizeof (scm_subr_entry) * new_size,
81 "scm_make_subr_opt");
82 scm_subr_table = new_table;
83 scm_subr_table_room = new_size;
84 }
85
86 SCM_NEWCELL (z);
87 symcell = set ? scm_sysintern0 (name) : scm_intern0 (name);
88
89 entry = scm_subr_table_size;
90 scm_subr_table[entry].handle = z;
91 scm_subr_table[entry].name = SCM_CAR (symcell);
92 scm_subr_table[entry].generic = 0;
93 scm_subr_table[entry].properties = SCM_EOL;
94 scm_subr_table[entry].documentation = SCM_BOOL_F;
95
96 SCM_SUBRF (z) = fcn;
97 SCM_SETCAR (z, (entry << 8) + type);
98 scm_subr_table_size++;
99
100 if (set)
101 SCM_SETCDR (symcell, z);
102
103 return z;
104 }
105
106 /* This function isn't currently used since subrs are never freed. */
107 /* *fixme* Need mutex here. */
108 void
109 scm_free_subr_entry (SCM subr)
110 {
111 int entry = SCM_SUBRNUM (subr);
112 /* Move last entry in table to the free position */
113 scm_subr_table[entry] = scm_subr_table[scm_subr_table_size - 1];
114 SCM_SET_SUBRNUM (scm_subr_table[entry].handle, entry);
115 scm_subr_table_size--;
116 }
117
118 SCM
119 scm_make_subr (const char *name, int type, SCM (*fcn) ())
120 {
121 return scm_make_subr_opt (name, type, fcn, 1);
122 }
123
124 SCM
125 scm_make_subr_with_generic (const char *name, int type, SCM (*fcn) (), SCM *gf)
126 {
127 SCM subr = scm_make_subr_opt (name, type, fcn, 1);
128 scm_subr_table[scm_subr_table_size - 1].generic = gf;
129 return subr;
130 }
131
132 void
133 scm_mark_subr_table ()
134 {
135 int i;
136 for (i = 0; i < scm_subr_table_size; ++i)
137 {
138 SCM_SETGC8MARK (scm_subr_table[i].name);
139 if (scm_subr_table[i].generic && *scm_subr_table[i].generic)
140 scm_gc_mark (*scm_subr_table[i].generic);
141 if (SCM_NIMP (scm_subr_table[i].properties))
142 scm_gc_mark (scm_subr_table[i].properties);
143 if (SCM_NIMP (scm_subr_table[i].documentation))
144 scm_gc_mark (scm_subr_table[i].documentation);
145 }
146 }
147
148
149 #ifdef CCLO
150 SCM
151 scm_makcclo (SCM proc, long len)
152 {
153 SCM s;
154 SCM_NEWCELL (s);
155 SCM_DEFER_INTS;
156 SCM_SETCHARS (s, scm_must_malloc (len * sizeof (SCM), "compiled-closure"));
157 SCM_SETLENGTH (s, len, scm_tc7_cclo);
158 while (--len)
159 SCM_VELTS (s)[len] = SCM_UNSPECIFIED;
160 SCM_CCLO_SUBR (s) = proc;
161 SCM_ALLOW_INTS;
162 return s;
163 }
164
165 /* Undocumented debugging procedure */
166 #ifdef GUILE_DEBUG
167 SCM_DEFINE (scm_make_cclo, "make-cclo", 2, 0, 0,
168 (SCM proc, SCM len),
169 "")
170 #define FUNC_NAME s_scm_make_cclo
171 {
172 return scm_makcclo (proc, SCM_INUM (len));
173 }
174 #undef FUNC_NAME
175 #endif
176 #endif
177
178
179
180 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
181 (SCM obj),
182 "")
183 #define FUNC_NAME s_scm_procedure_p
184 {
185 if (SCM_NIMP (obj))
186 switch (SCM_TYP7 (obj))
187 {
188 case scm_tcs_cons_gloc:
189 if (!SCM_I_OPERATORP (obj))
190 break;
191 case scm_tcs_closures:
192 case scm_tc7_contin:
193 case scm_tcs_subrs:
194 #ifdef CCLO
195 case scm_tc7_cclo:
196 #endif
197 case scm_tc7_pws:
198 return SCM_BOOL_T;
199 default:
200 return SCM_BOOL_F;
201 }
202 return SCM_BOOL_F;
203 }
204 #undef FUNC_NAME
205
206 SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
207 (SCM obj),
208 "")
209 #define FUNC_NAME s_scm_closure_p
210 {
211 return SCM_BOOL(SCM_CLOSUREP (obj));
212 }
213 #undef FUNC_NAME
214
215 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
216 (SCM obj),
217 "")
218 #define FUNC_NAME s_scm_thunk_p
219 {
220 if (SCM_NIMP (obj))
221 {
222 again:
223 switch (SCM_TYP7 (obj))
224 {
225 case scm_tcs_closures:
226 if (SCM_NULLP (SCM_CAR (SCM_CODE (obj))))
227 return SCM_BOOL_T;
228 case scm_tc7_subr_0:
229 case scm_tc7_subr_1o:
230 case scm_tc7_lsubr:
231 case scm_tc7_rpsubr:
232 case scm_tc7_asubr:
233 #ifdef CCLO
234 case scm_tc7_cclo:
235 #endif
236 return SCM_BOOL_T;
237 case scm_tc7_pws:
238 obj = SCM_PROCEDURE (obj);
239 goto again;
240 default:
241 ;
242 }
243 }
244 return SCM_BOOL_F;
245 }
246 #undef FUNC_NAME
247
248 /* Only used internally. */
249 int
250 scm_subr_p (SCM obj)
251 {
252 if (SCM_NIMP (obj))
253 switch (SCM_TYP7 (obj))
254 {
255 case scm_tcs_subrs:
256 return 1;
257 default:
258 ;
259 }
260 return 0;
261 }
262
263 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
264 (SCM proc),
265 "Return the documentation string associated with @code{proc}. By
266 convention, if a procedure contains more than one expression and the
267 first expression is a string constant, that string is assumed to contain
268 documentation for that procedure.")
269 #define FUNC_NAME s_scm_procedure_documentation
270 {
271 SCM code;
272 SCM_ASSERT (SCM_BOOL_T == scm_procedure_p (proc) && SCM_NIMP (proc) && SCM_TYP7 (proc) != scm_tc7_contin,
273 proc, SCM_ARG1, FUNC_NAME);
274 switch (SCM_TYP7 (proc))
275 {
276 case scm_tcs_closures:
277 code = SCM_CDR (SCM_CODE (proc));
278 if (SCM_IMP (SCM_CDR (code)))
279 return SCM_BOOL_F;
280 code = SCM_CAR (code);
281 if (SCM_IMP (code))
282 return SCM_BOOL_F;
283 if (SCM_STRINGP (code))
284 return code;
285 default:
286 return SCM_BOOL_F;
287 /*
288 case scm_tcs_subrs:
289 #ifdef CCLO
290 case scm_tc7_cclo:
291 #endif
292 */
293 }
294 }
295 #undef FUNC_NAME
296
297
298 /* Procedure-with-setter
299 */
300
301 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
302 (SCM obj),
303 "")
304 #define FUNC_NAME s_scm_procedure_with_setter_p
305 {
306 return SCM_BOOL(SCM_PROCEDURE_WITH_SETTER_P (obj));
307 }
308 #undef FUNC_NAME
309
310 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
311 (SCM procedure, SCM setter),
312 "")
313 #define FUNC_NAME s_scm_make_procedure_with_setter
314 {
315 SCM z;
316 SCM_VALIDATE_PROC (1,procedure);
317 SCM_VALIDATE_PROC (2,setter);
318 SCM_NEWCELL (z);
319 SCM_ENTER_A_SECTION;
320 SCM_SETCDR (z, scm_cons (procedure, setter));
321 SCM_SETCAR (z, scm_tc7_pws);
322 SCM_EXIT_A_SECTION;
323 return z;
324 }
325 #undef FUNC_NAME
326
327 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
328 (SCM proc),
329 "")
330 #define FUNC_NAME s_scm_procedure
331 {
332 SCM_VALIDATE_NIM (1,proc);
333 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
334 return SCM_PROCEDURE (proc);
335 else if (SCM_STRUCTP (proc))
336 {
337 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
338 return proc;
339 }
340 SCM_WRONG_TYPE_ARG (1, proc);
341 return 0; /* not reached */
342 }
343 #undef FUNC_NAME
344
345 SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
346
347 SCM
348 scm_setter (SCM proc)
349 {
350 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
351 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
352 return SCM_SETTER (proc);
353 else if (SCM_STRUCTP (proc))
354 {
355 SCM setter;
356 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
357 g_setter, proc, SCM_ARG1, s_setter);
358 setter = (SCM_I_ENTITYP (proc)
359 ? SCM_ENTITY_SETTER (proc)
360 : SCM_OPERATOR_SETTER (proc));
361 if (SCM_NIMP (setter))
362 return setter;
363 /* fall through */
364 }
365 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
366 return 0;
367 }
368
369
370 void
371 scm_init_iprocs(const scm_iproc *subra, int type)
372 {
373 for(;subra->scm_string; subra++)
374 scm_make_subr(subra->scm_string,
375 type,
376 subra->cproc);
377 }
378
379
380 void
381 scm_init_subr_table ()
382 {
383 scm_subr_table
384 = ((scm_subr_entry *)
385 scm_must_malloc (sizeof (scm_subr_entry) * scm_subr_table_room,
386 "scm_subr_table"));
387 }
388
389 void
390 scm_init_procs ()
391 {
392 #include "procs.x"
393 }