* *.c, scm_validate.h: Use SCM_VALIDATE_NIM, not SCM_VALIDATE_NIMP
[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 GUILE_PROC (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 GUILE_PROC(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 GUILE_PROC(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_NIMP (obj) && SCM_CLOSUREP (obj));
212 }
213 #undef FUNC_NAME
214
215 GUILE_PROC(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 GUILE_PROC(scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
264 (SCM proc),
265 "")
266 #define FUNC_NAME s_scm_procedure_documentation
267 {
268 SCM code;
269 SCM_ASSERT (SCM_BOOL_T == scm_procedure_p (proc) && SCM_NIMP (proc) && SCM_TYP7 (proc) != scm_tc7_contin,
270 proc, SCM_ARG1, FUNC_NAME);
271 switch (SCM_TYP7 (proc))
272 {
273 case scm_tcs_closures:
274 code = SCM_CDR (SCM_CODE (proc));
275 if (SCM_IMP (SCM_CDR (code)))
276 return SCM_BOOL_F;
277 code = SCM_CAR (code);
278 if (SCM_IMP (code))
279 return SCM_BOOL_F;
280 if (SCM_STRINGP (code))
281 return code;
282 default:
283 return SCM_BOOL_F;
284 /*
285 case scm_tcs_subrs:
286 #ifdef CCLO
287 case scm_tc7_cclo:
288 #endif
289 */
290 }
291 }
292 #undef FUNC_NAME
293
294
295 /* Procedure-with-setter
296 */
297
298 GUILE_PROC (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
299 (SCM obj),
300 "")
301 #define FUNC_NAME s_scm_procedure_with_setter_p
302 {
303 return SCM_BOOL(SCM_NIMP (obj) && SCM_PROCEDURE_WITH_SETTER_P (obj));
304 }
305 #undef FUNC_NAME
306
307 GUILE_PROC (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
308 (SCM procedure, SCM setter),
309 "")
310 #define FUNC_NAME s_scm_make_procedure_with_setter
311 {
312 SCM z;
313 SCM_VALIDATE_PROC(1,procedure);
314 SCM_VALIDATE_PROC(2,setter);
315 SCM_NEWCELL (z);
316 SCM_ENTER_A_SECTION;
317 SCM_SETCDR (z, scm_cons (procedure, setter));
318 SCM_SETCAR (z, scm_tc7_pws);
319 SCM_EXIT_A_SECTION;
320 return z;
321 }
322 #undef FUNC_NAME
323
324 GUILE_PROC (scm_procedure, "procedure", 1, 0, 0,
325 (SCM proc),
326 "")
327 #define FUNC_NAME s_scm_procedure
328 {
329 SCM_VALIDATE_NIM (1,proc);
330 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
331 return SCM_PROCEDURE (proc);
332 else if (SCM_STRUCTP (proc))
333 {
334 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
335 return proc;
336 }
337 SCM_WRONG_TYPE_ARG (1, proc);
338 return 0; /* not reached */
339 }
340 #undef FUNC_NAME
341
342 SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
343
344 SCM
345 scm_setter (SCM proc)
346 {
347 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
348 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
349 return SCM_SETTER (proc);
350 else if (SCM_STRUCTP (proc))
351 {
352 SCM setter;
353 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
354 g_setter, proc, SCM_ARG1, s_setter);
355 setter = (SCM_I_ENTITYP (proc)
356 ? SCM_ENTITY_SETTER (proc)
357 : SCM_OPERATOR_SETTER (proc));
358 if (SCM_NIMP (setter))
359 return setter;
360 /* fall through */
361 }
362 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
363 return 0;
364 }
365
366
367 void
368 scm_init_iprocs(const scm_iproc *subra, int type)
369 {
370 for(;subra->scm_string; subra++)
371 scm_make_subr(subra->scm_string,
372 type,
373 subra->cproc);
374 }
375
376
377 void
378 scm_init_subr_table ()
379 {
380 scm_subr_table
381 = ((scm_subr_entry *)
382 scm_must_malloc (sizeof (scm_subr_entry) * scm_subr_table_room,
383 "scm_subr_table"));
384 }
385
386 void
387 scm_init_procs ()
388 {
389 #include "procs.x"
390 }