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