*.[ch]: Replace GUILE_PROC w/ SCM_DEFINE.
[bpt/guile.git] / libguile / procs.c
CommitLineData
78a0461a 1/* Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
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
0f2d19dd
JB
45\f
46
47#include <stdio.h>
48#include "_scm.h"
49
bdc88419
MD
50#include "objects.h"
51
1bbd0b84 52#include "scm_validate.h"
20e6290e 53#include "procs.h"
0f2d19dd
JB
54\f
55
56
57/* {Procedures}
58 */
59
9de33deb
MD
60scm_subr_entry *scm_subr_table;
61
98f9c984 62/* libguile contained approx. 700 primitive procedures on 24 Aug 1999. */
9de33deb
MD
63
64int scm_subr_table_size = 0;
65int scm_subr_table_room = 750;
1cc91f1b 66
0f2d19dd 67SCM
1bbd0b84 68scm_make_subr_opt (const char *name, int type, SCM (*fcn) (), int set)
0f2d19dd
JB
69{
70 SCM symcell;
0f2d19dd 71 register SCM z;
9de33deb
MD
72 int entry;
73
74 if (scm_subr_table_size == scm_subr_table_room)
75 {
98f9c984
JB
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");
9de33deb
MD
82 scm_subr_table = new_table;
83 scm_subr_table_room = new_size;
84 }
85
0f2d19dd 86 SCM_NEWCELL (z);
9de33deb
MD
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
0f2d19dd 96 SCM_SUBRF (z) = fcn;
9de33deb
MD
97 SCM_SETCAR (z, (entry << 8) + type);
98 scm_subr_table_size++;
99
0f2d19dd 100 if (set)
a6c64c3c 101 SCM_SETCDR (symcell, z);
9de33deb 102
0f2d19dd
JB
103 return z;
104}
105
9de33deb
MD
106/* This function isn't currently used since subrs are never freed. */
107/* *fixme* Need mutex here. */
108void
109scm_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}
1cc91f1b 117
0f2d19dd 118SCM
1bbd0b84 119scm_make_subr (const char *name, int type, SCM (*fcn) ())
0f2d19dd
JB
120{
121 return scm_make_subr_opt (name, type, fcn, 1);
122}
123
9de33deb
MD
124SCM
125scm_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
132void
133scm_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}
1cc91f1b 147
9de33deb
MD
148
149#ifdef CCLO
0f2d19dd 150SCM
1bbd0b84 151scm_makcclo (SCM proc, long len)
0f2d19dd
JB
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}
d88094f9
MD
164
165/* Undocumented debugging procedure */
166#ifdef GUILE_DEBUG
a1ec6916 167SCM_DEFINE (scm_make_cclo, "make-cclo", 2, 0, 0,
1bbd0b84
GB
168 (SCM proc, SCM len),
169"")
170#define FUNC_NAME s_scm_make_cclo
d88094f9
MD
171{
172 return scm_makcclo (proc, SCM_INUM (len));
173}
1bbd0b84 174#undef FUNC_NAME
d88094f9 175#endif
0f2d19dd
JB
176#endif
177
178
179
a1ec6916 180SCM_DEFINE(scm_procedure_p, "procedure?", 1, 0, 0,
1bbd0b84
GB
181 (SCM obj),
182"")
183#define FUNC_NAME s_scm_procedure_p
0f2d19dd
JB
184{
185 if (SCM_NIMP (obj))
186 switch (SCM_TYP7 (obj))
187 {
bdc88419
MD
188 case scm_tcs_cons_gloc:
189 if (!SCM_I_OPERATORP (obj))
190 break;
0f2d19dd
JB
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
b4cd6492 197 case scm_tc7_pws:
0f2d19dd
JB
198 return SCM_BOOL_T;
199 default:
200 return SCM_BOOL_F;
201 }
202 return SCM_BOOL_F;
203}
1bbd0b84 204#undef FUNC_NAME
0f2d19dd 205
a1ec6916 206SCM_DEFINE(scm_closure_p, "closure?", 1, 0, 0,
1bbd0b84
GB
207 (SCM obj),
208"")
209#define FUNC_NAME s_scm_closure_p
ecdb5eb2 210{
0c95b57d 211 return SCM_BOOL(SCM_CLOSUREP (obj));
ecdb5eb2 212}
1bbd0b84 213#undef FUNC_NAME
ecdb5eb2 214
a1ec6916 215SCM_DEFINE(scm_thunk_p, "thunk?", 1, 0, 0,
1bbd0b84
GB
216 (SCM obj),
217"")
218#define FUNC_NAME s_scm_thunk_p
44bd53b9
MD
219{
220 if (SCM_NIMP (obj))
b4cd6492
MD
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:
44bd53b9 233#ifdef CCLO
b4cd6492 234 case scm_tc7_cclo:
44bd53b9 235#endif
b4cd6492
MD
236 return SCM_BOOL_T;
237 case scm_tc7_pws:
238 obj = SCM_PROCEDURE (obj);
239 goto again;
240 default:
241 ;
242 }
243 }
44bd53b9
MD
244 return SCM_BOOL_F;
245}
1bbd0b84 246#undef FUNC_NAME
44bd53b9 247
9de33deb
MD
248/* Only used internally. */
249int
250scm_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
a1ec6916 263SCM_DEFINE(scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
1bbd0b84 264 (SCM proc),
4079f87e
GB
265"Return the documentation string associated with @code{proc}. By
266convention, if a procedure contains more than one expression and the
267first expression is a string constant, that string is assumed to contain
268documentation for that procedure.")
1bbd0b84 269#define FUNC_NAME s_scm_procedure_documentation
c2c82fba
MD
270{
271 SCM code;
272 SCM_ASSERT (SCM_BOOL_T == scm_procedure_p (proc) && SCM_NIMP (proc) && SCM_TYP7 (proc) != scm_tc7_contin,
1bbd0b84 273 proc, SCM_ARG1, FUNC_NAME);
c2c82fba
MD
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}
1bbd0b84 295#undef FUNC_NAME
c2c82fba 296
0f2d19dd 297
b4cd6492
MD
298/* Procedure-with-setter
299 */
300
a1ec6916 301SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
1bbd0b84 302 (SCM obj),
717050c8 303"")
1bbd0b84 304#define FUNC_NAME s_scm_procedure_with_setter_p
b4cd6492 305{
0c95b57d 306 return SCM_BOOL(SCM_PROCEDURE_WITH_SETTER_P (obj));
b4cd6492 307}
1bbd0b84 308#undef FUNC_NAME
b4cd6492 309
a1ec6916 310SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
1bbd0b84 311 (SCM procedure, SCM setter),
717050c8 312"")
1bbd0b84 313#define FUNC_NAME s_scm_make_procedure_with_setter
b4cd6492
MD
314{
315 SCM z;
1bbd0b84
GB
316 SCM_VALIDATE_PROC(1,procedure);
317 SCM_VALIDATE_PROC(2,setter);
b4cd6492
MD
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}
1bbd0b84 325#undef FUNC_NAME
b4cd6492 326
a1ec6916 327SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
1bbd0b84 328 (SCM proc),
717050c8 329"")
1bbd0b84 330#define FUNC_NAME s_scm_procedure
b4cd6492 331{
6b5a304f 332 SCM_VALIDATE_NIM (1,proc);
b4cd6492
MD
333 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
334 return SCM_PROCEDURE (proc);
335 else if (SCM_STRUCTP (proc))
336 {
1bbd0b84 337 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
b4cd6492
MD
338 return proc;
339 }
1bbd0b84 340 SCM_WRONG_TYPE_ARG (1, proc);
b4cd6492
MD
341 return 0; /* not reached */
342}
1bbd0b84 343#undef FUNC_NAME
b4cd6492 344
f5267231 345SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
b4cd6492
MD
346
347SCM
348scm_setter (SCM proc)
349{
f5267231 350 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
b4cd6492
MD
351 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
352 return SCM_SETTER (proc);
353 else if (SCM_STRUCTP (proc))
354 {
c72a774a 355 SCM setter;
f5267231
MD
356 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
357 g_setter, proc, SCM_ARG1, s_setter);
c72a774a
MD
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 */
b4cd6492 364 }
f5267231 365 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
b4cd6492
MD
366 return 0;
367}
1cc91f1b 368
9de33deb 369
0f2d19dd 370void
1bbd0b84 371scm_init_iprocs(const scm_iproc *subra, int type)
0f2d19dd
JB
372{
373 for(;subra->scm_string; subra++)
374 scm_make_subr(subra->scm_string,
375 type,
376 subra->cproc);
377}
378
379
9de33deb
MD
380void
381scm_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}
1cc91f1b 388
0f2d19dd
JB
389void
390scm_init_procs ()
0f2d19dd
JB
391{
392#include "procs.x"
393}