* eval.c: Removed outdated references to "everr". Improved some
[bpt/guile.git] / libguile / eval.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 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 \f
43
44 /* This file is read twice in order to produce debugging versions of
45 * scm_ceval and scm_apply. These functions, scm_deval and
46 * scm_dapply, are produced when we define the preprocessor macro
47 * DEVAL. The file is divided into sections which are treated
48 * differently with respect to DEVAL. The heads of these sections are
49 * marked with the string "SECTION:".
50 */
51
52 /* SECTION: This code is compiled once.
53 */
54
55 #ifndef DEVAL
56
57 /* We need this to get the definitions for HAVE_ALLOCA_H, etc. */
58 #include "libguile/scmconfig.h"
59
60 /* AIX requires this to be the first thing in the file. The #pragma
61 directive is indented so pre-ANSI compilers will ignore it, rather
62 than choke on it. */
63 #ifndef __GNUC__
64 # if HAVE_ALLOCA_H
65 # include <alloca.h>
66 # else
67 # ifdef _AIX
68 # pragma alloca
69 # else
70 # ifndef alloca /* predefined by HP cc +Olibcalls */
71 char *alloca ();
72 # endif
73 # endif
74 # endif
75 #endif
76
77 #include "libguile/_scm.h"
78 #include "libguile/debug.h"
79 #include "libguile/dynwind.h"
80 #include "libguile/alist.h"
81 #include "libguile/eq.h"
82 #include "libguile/continuations.h"
83 #include "libguile/throw.h"
84 #include "libguile/smob.h"
85 #include "libguile/macros.h"
86 #include "libguile/procprop.h"
87 #include "libguile/hashtab.h"
88 #include "libguile/hash.h"
89 #include "libguile/srcprop.h"
90 #include "libguile/stackchk.h"
91 #include "libguile/objects.h"
92 #include "libguile/async.h"
93 #include "libguile/feature.h"
94 #include "libguile/modules.h"
95 #include "libguile/ports.h"
96 #include "libguile/root.h"
97 #include "libguile/vectors.h"
98 #include "libguile/fluids.h"
99 #include "libguile/values.h"
100
101 #include "libguile/validate.h"
102 #include "libguile/eval.h"
103
104 \f
105
106 #define SCM_VALIDATE_NON_EMPTY_COMBINATION(x) \
107 do { \
108 if (SCM_EQ_P ((x), SCM_EOL)) \
109 scm_misc_error (NULL, scm_s_expression, SCM_EOL); \
110 } while (0)
111
112 \f
113
114 /* The evaluator contains a plethora of EVAL symbols.
115 * This is an attempt at explanation.
116 *
117 * The following macros should be used in code which is read twice
118 * (where the choice of evaluator is hard soldered):
119 *
120 * SCM_CEVAL is the symbol used within one evaluator to call itself.
121 * Originally, it is defined to scm_ceval, but is redefined to
122 * scm_deval during the second pass.
123 *
124 * SIDEVAL corresponds to SCM_CEVAL, but is used in situations where
125 * only side effects of expressions matter. All immediates are
126 * ignored.
127 *
128 * SCM_EVALIM is used when it is known that the expression is an
129 * immediate. (This macro never calls an evaluator.)
130 *
131 * EVALCAR evaluates the car of an expression.
132 *
133 * EVALCELLCAR is like EVALCAR, but is used when it is known that the
134 * car is a lisp cell.
135 *
136 * The following macros should be used in code which is read once
137 * (where the choice of evaluator is dynamic):
138 *
139 * SCM_XEVAL takes care of immediates without calling an evaluator. It
140 * then calls scm_ceval *or* scm_deval, depending on the debugging
141 * mode.
142 *
143 * SCM_XEVALCAR corresponds to EVALCAR, but uses scm_ceval *or* scm_deval
144 * depending on the debugging mode.
145 *
146 * The main motivation for keeping this plethora is efficiency
147 * together with maintainability (=> locality of code).
148 */
149
150 #define SCM_CEVAL scm_ceval
151 #define SIDEVAL(x, env) if (SCM_NIMP (x)) SCM_CEVAL((x), (env))
152
153 #define EVALCELLCAR(x, env) (SCM_SYMBOLP (SCM_CAR (x)) \
154 ? *scm_lookupcar (x, env, 1) \
155 : SCM_CEVAL (SCM_CAR (x), env))
156
157 #define EVALCAR(x, env) (SCM_IMP (SCM_CAR (x)) \
158 ? SCM_EVALIM (SCM_CAR (x), env) \
159 : EVALCELLCAR (x, env))
160
161 #define EXTEND_ENV SCM_EXTEND_ENV
162
163 #ifdef MEMOIZE_LOCALS
164
165 SCM *
166 scm_ilookup (SCM iloc, SCM env)
167 {
168 register long ir = SCM_IFRAME (iloc);
169 register SCM er = env;
170 for (; 0 != ir; --ir)
171 er = SCM_CDR (er);
172 er = SCM_CAR (er);
173 for (ir = SCM_IDIST (iloc); 0 != ir; --ir)
174 er = SCM_CDR (er);
175 if (SCM_ICDRP (iloc))
176 return SCM_CDRLOC (er);
177 return SCM_CARLOC (SCM_CDR (er));
178 }
179 #endif
180
181 #ifdef USE_THREADS
182
183 /* The Lookup Car Race
184 - by Eva Luator
185
186 Memoization of variables and special forms is done while executing
187 the code for the first time. As long as there is only one thread
188 everything is fine, but as soon as two threads execute the same
189 code concurrently `for the first time' they can come into conflict.
190
191 This memoization includes rewriting variable references into more
192 efficient forms and expanding macros. Furthermore, macro expansion
193 includes `compiling' special forms like `let', `cond', etc. into
194 tree-code instructions.
195
196 There shouldn't normally be a problem with memoizing local and
197 global variable references (into ilocs and variables), because all
198 threads will mutate the code in *exactly* the same way and (if I
199 read the C code correctly) it is not possible to observe a half-way
200 mutated cons cell. The lookup procedure can handle this
201 transparently without any critical sections.
202
203 It is different with macro expansion, because macro expansion
204 happens outside of the lookup procedure and can't be
205 undone. Therefore the lookup procedure can't cope with it. It has
206 to indicate failure when it detects a lost race and hope that the
207 caller can handle it. Luckily, it turns out that this is the case.
208
209 An example to illustrate this: Suppose that the following form will
210 be memoized concurrently by two threads
211
212 (let ((x 12)) x)
213
214 Let's first examine the lookup of X in the body. The first thread
215 decides that it has to find the symbol "x" in the environment and
216 starts to scan it. Then the other thread takes over and actually
217 overtakes the first. It looks up "x" and substitutes an
218 appropriate iloc for it. Now the first thread continues and
219 completes its lookup. It comes to exactly the same conclusions as
220 the second one and could - without much ado - just overwrite the
221 iloc with the same iloc.
222
223 But let's see what will happen when the race occurs while looking
224 up the symbol "let" at the start of the form. It could happen that
225 the second thread interrupts the lookup of the first thread and not
226 only substitutes a variable for it but goes right ahead and
227 replaces it with the compiled form (#@let* (x 12) x). Now, when
228 the first thread completes its lookup, it would replace the #@let*
229 with a variable containing the "let" binding, effectively reverting
230 the form to (let (x 12) x). This is wrong. It has to detect that
231 it has lost the race and the evaluator has to reconsider the
232 changed form completely.
233
234 This race condition could be resolved with some kind of traffic
235 light (like mutexes) around scm_lookupcar, but I think that it is
236 best to avoid them in this case. They would serialize memoization
237 completely and because lookup involves calling arbitrary Scheme
238 code (via the lookup-thunk), threads could be blocked for an
239 arbitrary amount of time or even deadlock. But with the current
240 solution a lot of unnecessary work is potentially done. */
241
242 /* SCM_LOOKUPCAR1 is was SCM_LOOKUPCAR used to be but is allowed to
243 return NULL to indicate a failed lookup due to some race conditions
244 between threads. This only happens when VLOC is the first cell of
245 a special form that will eventually be memoized (like `let', etc.)
246 In that case the whole lookup is bogus and the caller has to
247 reconsider the complete special form.
248
249 SCM_LOOKUPCAR is still there, of course. It just calls
250 SCM_LOOKUPCAR1 and aborts on receiving NULL. So SCM_LOOKUPCAR
251 should only be called when it is known that VLOC is not the first
252 pair of a special form. Otherwise, use SCM_LOOKUPCAR1 and check
253 for NULL. I think I've found the only places where this
254 applies. */
255
256 #endif /* USE_THREADS */
257
258 SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
259
260 #ifdef USE_THREADS
261 static SCM *
262 scm_lookupcar1 (SCM vloc, SCM genv, int check)
263 #else
264 SCM *
265 scm_lookupcar (SCM vloc, SCM genv, int check)
266 #endif
267 {
268 SCM env = genv;
269 register SCM *al, fl, var = SCM_CAR (vloc);
270 #ifdef MEMOIZE_LOCALS
271 register SCM iloc = SCM_ILOC00;
272 #endif
273 for (; SCM_NIMP (env); env = SCM_CDR (env))
274 {
275 if (!SCM_CONSP (SCM_CAR (env)))
276 break;
277 al = SCM_CARLOC (env);
278 for (fl = SCM_CAR (*al); SCM_NIMP (fl); fl = SCM_CDR (fl))
279 {
280 if (!SCM_CONSP (fl))
281 {
282 if (SCM_EQ_P (fl, var))
283 {
284 #ifdef MEMOIZE_LOCALS
285 #ifdef USE_THREADS
286 if (! SCM_EQ_P (SCM_CAR (vloc), var))
287 goto race;
288 #endif
289 SCM_SET_CELL_WORD_0 (vloc, SCM_UNPACK (iloc) + SCM_ICDR);
290 #endif
291 return SCM_CDRLOC (*al);
292 }
293 else
294 break;
295 }
296 al = SCM_CDRLOC (*al);
297 if (SCM_EQ_P (SCM_CAR (fl), var))
298 {
299 #ifdef MEMOIZE_LOCALS
300 #ifndef SCM_RECKLESS /* letrec inits to SCM_UNDEFINED */
301 if (SCM_UNBNDP (SCM_CAR (*al)))
302 {
303 env = SCM_EOL;
304 goto errout;
305 }
306 #endif
307 #ifdef USE_THREADS
308 if (!SCM_EQ_P (SCM_CAR (vloc), var))
309 goto race;
310 #endif
311 SCM_SETCAR (vloc, iloc);
312 #endif
313 return SCM_CARLOC (*al);
314 }
315 #ifdef MEMOIZE_LOCALS
316 iloc = SCM_PACK (SCM_UNPACK (iloc) + SCM_IDINC);
317 #endif
318 }
319 #ifdef MEMOIZE_LOCALS
320 iloc = SCM_PACK ((~SCM_IDSTMSK) & (SCM_UNPACK(iloc) + SCM_IFRINC));
321 #endif
322 }
323 {
324 SCM top_thunk, real_var;
325 if (SCM_NIMP (env))
326 {
327 top_thunk = SCM_CAR (env); /* env now refers to a
328 top level env thunk */
329 env = SCM_CDR (env);
330 }
331 else
332 top_thunk = SCM_BOOL_F;
333 real_var = scm_sym2var (var, top_thunk, SCM_BOOL_F);
334 if (SCM_FALSEP (real_var))
335 goto errout;
336
337 #ifndef SCM_RECKLESS
338 if (!SCM_NULLP (env) || SCM_UNBNDP (SCM_VARIABLE_REF (real_var)))
339 {
340 errout:
341 if (check)
342 {
343 if (SCM_NULLP (env))
344 scm_error (scm_unbound_variable_key, NULL,
345 "Unbound variable: ~S",
346 scm_list_1 (var), SCM_BOOL_F);
347 else
348 scm_misc_error (NULL, "Damaged environment: ~S",
349 scm_list_1 (var));
350 }
351 else
352 {
353 /* A variable could not be found, but we shall
354 not throw an error. */
355 static SCM undef_object = SCM_UNDEFINED;
356 return &undef_object;
357 }
358 }
359 #endif
360
361 #ifdef USE_THREADS
362 if (!SCM_EQ_P (SCM_CAR (vloc), var))
363 {
364 /* Some other thread has changed the very cell we are working
365 on. In effect, it must have done our job or messed it up
366 completely. */
367 race:
368 var = SCM_CAR (vloc);
369 if (SCM_VARIABLEP (var))
370 return SCM_VARIABLE_LOC (var);
371 #ifdef MEMOIZE_LOCALS
372 if (SCM_ITAG7 (var) == SCM_ITAG7 (SCM_ILOC00))
373 return scm_ilookup (var, genv);
374 #endif
375 /* We can't cope with anything else than variables and ilocs. When
376 a special form has been memoized (i.e. `let' into `#@let') we
377 return NULL and expect the calling function to do the right
378 thing. For the evaluator, this means going back and redoing
379 the dispatch on the car of the form. */
380 return NULL;
381 }
382 #endif /* USE_THREADS */
383
384 SCM_SETCAR (vloc, real_var);
385 return SCM_VARIABLE_LOC (real_var);
386 }
387 }
388
389 #ifdef USE_THREADS
390 SCM *
391 scm_lookupcar (SCM vloc, SCM genv, int check)
392 {
393 SCM *loc = scm_lookupcar1 (vloc, genv, check);
394 if (loc == NULL)
395 abort ();
396 return loc;
397 }
398 #endif
399
400 #define unmemocar scm_unmemocar
401
402 SCM_SYMBOL (sym_three_question_marks, "???");
403
404 SCM
405 scm_unmemocar (SCM form, SCM env)
406 {
407 if (!SCM_CONSP (form))
408 return form;
409 else
410 {
411 SCM c = SCM_CAR (form);
412 if (SCM_VARIABLEP (c))
413 {
414 SCM sym = scm_module_reverse_lookup (scm_env_module (env), c);
415 if (SCM_FALSEP (sym))
416 sym = sym_three_question_marks;
417 SCM_SETCAR (form, sym);
418 }
419 #ifdef MEMOIZE_LOCALS
420 else if (SCM_ILOCP (c))
421 {
422 unsigned long int ir;
423
424 for (ir = SCM_IFRAME (c); ir != 0; --ir)
425 env = SCM_CDR (env);
426 env = SCM_CAAR (env);
427 for (ir = SCM_IDIST (c); ir != 0; --ir)
428 env = SCM_CDR (env);
429 SCM_SETCAR (form, SCM_ICDRP (c) ? env : SCM_CAR (env));
430 }
431 #endif
432 return form;
433 }
434 }
435
436
437 SCM
438 scm_eval_car (SCM pair, SCM env)
439 {
440 return SCM_XEVALCAR (pair, env);
441 }
442
443 \f
444 /*
445 * The following rewrite expressions and
446 * some memoized forms have different syntax
447 */
448
449 const char scm_s_expression[] = "missing or extra expression";
450 const char scm_s_test[] = "bad test";
451 const char scm_s_body[] = "bad body";
452 const char scm_s_bindings[] = "bad bindings";
453 const char scm_s_duplicate_bindings[] = "duplicate bindings";
454 const char scm_s_variable[] = "bad variable";
455 const char scm_s_clauses[] = "bad or missing clauses";
456 const char scm_s_formals[] = "bad formals";
457 const char scm_s_duplicate_formals[] = "duplicate formals";
458 static const char s_splicing[] = "bad (non-list) result for unquote-splicing";
459
460 SCM_GLOBAL_SYMBOL (scm_sym_dot, ".");
461 SCM_GLOBAL_SYMBOL (scm_sym_arrow, "=>");
462 SCM_GLOBAL_SYMBOL (scm_sym_else, "else");
463 SCM_GLOBAL_SYMBOL (scm_sym_unquote, "unquote");
464 SCM_GLOBAL_SYMBOL (scm_sym_uq_splicing, "unquote-splicing");
465
466 SCM scm_f_apply;
467
468 #ifdef DEBUG_EXTENSIONS
469 SCM_GLOBAL_SYMBOL (scm_sym_enter_frame, "enter-frame");
470 SCM_GLOBAL_SYMBOL (scm_sym_apply_frame, "apply-frame");
471 SCM_GLOBAL_SYMBOL (scm_sym_exit_frame, "exit-frame");
472 SCM_GLOBAL_SYMBOL (scm_sym_trace, "trace");
473 #endif
474
475
476 /* Check that the body denoted by XORIG is valid and rewrite it into
477 its internal form. The internal form of a body is just the body
478 itself, but prefixed with an ISYM that denotes to what kind of
479 outer construct this body belongs. A lambda body starts with
480 SCM_IM_LAMBDA, for example, a body of a let starts with SCM_IM_LET,
481 etc. The one exception is a body that belongs to a letrec that has
482 been formed by rewriting internal defines: it starts with
483 SCM_IM_DEFINE. */
484
485 /* XXX - Besides controlling the rewriting of internal defines, the
486 additional ISYM could be used for improved error messages.
487 This is not done yet. */
488
489 static SCM
490 scm_m_body (SCM op, SCM xorig, const char *what)
491 {
492 SCM_ASSYNT (scm_ilength (xorig) >= 1, scm_s_body, what);
493
494 /* Don't add another ISYM if one is present already. */
495 if (SCM_ISYMP (SCM_CAR (xorig)))
496 return xorig;
497
498 /* Retain possible doc string. */
499 if (!SCM_CONSP (SCM_CAR (xorig)))
500 {
501 if (!SCM_NULLP (SCM_CDR (xorig)))
502 return scm_cons (SCM_CAR (xorig),
503 scm_m_body (op, SCM_CDR (xorig), what));
504 return xorig;
505 }
506
507 return scm_cons (op, xorig);
508 }
509
510
511 SCM_SYNTAX (s_quote, "quote", scm_makmmacro, scm_m_quote);
512 SCM_GLOBAL_SYMBOL (scm_sym_quote, s_quote);
513
514 SCM
515 scm_m_quote (SCM xorig, SCM env SCM_UNUSED)
516 {
517 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, scm_s_expression, s_quote);
518 return scm_cons (SCM_IM_QUOTE, SCM_CDR (xorig));
519 }
520
521
522 SCM_SYNTAX (s_begin, "begin", scm_makmmacro, scm_m_begin);
523 SCM_GLOBAL_SYMBOL (scm_sym_begin, s_begin);
524
525 SCM
526 scm_m_begin (SCM xorig, SCM env SCM_UNUSED)
527 {
528 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) >= 0, scm_s_expression, s_begin);
529 return scm_cons (SCM_IM_BEGIN, SCM_CDR (xorig));
530 }
531
532
533 SCM_SYNTAX (s_if, "if", scm_makmmacro, scm_m_if);
534 SCM_GLOBAL_SYMBOL (scm_sym_if, s_if);
535
536 SCM
537 scm_m_if (SCM xorig, SCM env SCM_UNUSED)
538 {
539 long len = scm_ilength (SCM_CDR (xorig));
540 SCM_ASSYNT (len >= 2 && len <= 3, scm_s_expression, "if");
541 return scm_cons (SCM_IM_IF, SCM_CDR (xorig));
542 }
543
544
545 /* Will go into the RnRS module when Guile is factorized.
546 SCM_SYNTAX (scm_s_set_x,"set!", scm_makmmacro, scm_m_set_x); */
547 const char scm_s_set_x[] = "set!";
548 SCM_GLOBAL_SYMBOL (scm_sym_set_x, scm_s_set_x);
549
550 SCM
551 scm_m_set_x (SCM xorig, SCM env SCM_UNUSED)
552 {
553 SCM x = SCM_CDR (xorig);
554 SCM_ASSYNT (scm_ilength (x) == 2, scm_s_expression, scm_s_set_x);
555 SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (x)), scm_s_variable, scm_s_set_x);
556 return scm_cons (SCM_IM_SET_X, x);
557 }
558
559
560 SCM_SYNTAX (s_and, "and", scm_makmmacro, scm_m_and);
561 SCM_GLOBAL_SYMBOL (scm_sym_and, s_and);
562
563 SCM
564 scm_m_and (SCM xorig, SCM env SCM_UNUSED)
565 {
566 long len = scm_ilength (SCM_CDR (xorig));
567 SCM_ASSYNT (len >= 0, scm_s_test, s_and);
568 if (len >= 1)
569 return scm_cons (SCM_IM_AND, SCM_CDR (xorig));
570 else
571 return SCM_BOOL_T;
572 }
573
574
575 SCM_SYNTAX (s_or, "or", scm_makmmacro, scm_m_or);
576 SCM_GLOBAL_SYMBOL (scm_sym_or, s_or);
577
578 SCM
579 scm_m_or (SCM xorig, SCM env SCM_UNUSED)
580 {
581 long len = scm_ilength (SCM_CDR (xorig));
582 SCM_ASSYNT (len >= 0, scm_s_test, s_or);
583 if (len >= 1)
584 return scm_cons (SCM_IM_OR, SCM_CDR (xorig));
585 else
586 return SCM_BOOL_F;
587 }
588
589
590 SCM_SYNTAX (s_case, "case", scm_makmmacro, scm_m_case);
591 SCM_GLOBAL_SYMBOL (scm_sym_case, s_case);
592
593 SCM
594 scm_m_case (SCM xorig, SCM env SCM_UNUSED)
595 {
596 SCM clauses;
597 SCM cdrx = SCM_CDR (xorig);
598 SCM_ASSYNT (scm_ilength (cdrx) >= 2, scm_s_clauses, s_case);
599 clauses = SCM_CDR (cdrx);
600 while (!SCM_NULLP (clauses))
601 {
602 SCM clause = SCM_CAR (clauses);
603 SCM_ASSYNT (scm_ilength (clause) >= 2, scm_s_clauses, s_case);
604 SCM_ASSYNT (scm_ilength (SCM_CAR (clause)) >= 0
605 || (SCM_EQ_P (scm_sym_else, SCM_CAR (clause))
606 && SCM_NULLP (SCM_CDR (clauses))),
607 scm_s_clauses, s_case);
608 clauses = SCM_CDR (clauses);
609 }
610 return scm_cons (SCM_IM_CASE, cdrx);
611 }
612
613
614 SCM_SYNTAX (s_cond, "cond", scm_makmmacro, scm_m_cond);
615 SCM_GLOBAL_SYMBOL (scm_sym_cond, s_cond);
616
617 SCM
618 scm_m_cond (SCM xorig, SCM env SCM_UNUSED)
619 {
620 SCM cdrx = SCM_CDR (xorig);
621 SCM clauses = cdrx;
622 SCM_ASSYNT (scm_ilength (clauses) >= 1, scm_s_clauses, s_cond);
623 while (!SCM_NULLP (clauses))
624 {
625 SCM clause = SCM_CAR (clauses);
626 long len = scm_ilength (clause);
627 SCM_ASSYNT (len >= 1, scm_s_clauses, s_cond);
628 if (SCM_EQ_P (scm_sym_else, SCM_CAR (clause)))
629 {
630 int last_clause_p = SCM_NULLP (SCM_CDR (clauses));
631 SCM_ASSYNT (len >= 2 && last_clause_p, "bad ELSE clause", s_cond);
632 }
633 else if (len >= 2 && SCM_EQ_P (scm_sym_arrow, SCM_CADR (clause)))
634 {
635 SCM_ASSYNT (len > 2, "missing recipient", s_cond);
636 SCM_ASSYNT (len == 3, "bad recipient", s_cond);
637 }
638 clauses = SCM_CDR (clauses);
639 }
640 return scm_cons (SCM_IM_COND, cdrx);
641 }
642
643
644 SCM_SYNTAX (s_lambda, "lambda", scm_makmmacro, scm_m_lambda);
645 SCM_GLOBAL_SYMBOL (scm_sym_lambda, s_lambda);
646
647 /* Return true if OBJ is `eq?' to one of the elements of LIST or to the
648 * cdr of the last cons. (Thus, LIST is not required to be a proper
649 * list and OBJ can also be found in the improper ending.) */
650 static int
651 scm_c_improper_memq (SCM obj, SCM list)
652 {
653 for (; SCM_CONSP (list); list = SCM_CDR (list))
654 {
655 if (SCM_EQ_P (SCM_CAR (list), obj))
656 return 1;
657 }
658 return SCM_EQ_P (list, obj);
659 }
660
661 SCM
662 scm_m_lambda (SCM xorig, SCM env SCM_UNUSED)
663 {
664 SCM formals;
665 SCM x = SCM_CDR (xorig);
666
667 SCM_ASSYNT (SCM_CONSP (x), scm_s_formals, s_lambda);
668
669 formals = SCM_CAR (x);
670 while (SCM_CONSP (formals))
671 {
672 SCM formal = SCM_CAR (formals);
673 SCM_ASSYNT (SCM_SYMBOLP (formal), scm_s_formals, s_lambda);
674 if (scm_c_improper_memq (formal, SCM_CDR (formals)))
675 scm_misc_error (s_lambda, scm_s_duplicate_formals, SCM_EOL);
676 formals = SCM_CDR (formals);
677 }
678 if (!SCM_NULLP (formals) && !SCM_SYMBOLP (formals))
679 scm_misc_error (s_lambda, scm_s_formals, SCM_EOL);
680
681 return scm_cons2 (SCM_IM_LAMBDA, SCM_CAR (x),
682 scm_m_body (SCM_IM_LAMBDA, SCM_CDR (x), s_lambda));
683 }
684
685
686 SCM_SYNTAX (s_letstar, "let*", scm_makmmacro, scm_m_letstar);
687 SCM_GLOBAL_SYMBOL (scm_sym_letstar, s_letstar);
688
689 /* (let* ((v1 i1) (v2 i2) ...) body) with variables v1 .. vk and initializers
690 * i1 .. ik is transformed into the form (#@let* (v1 i1 v2 i2 ...) body*). */
691 SCM
692 scm_m_letstar (SCM xorig, SCM env SCM_UNUSED)
693 {
694 SCM bindings;
695 SCM x = SCM_CDR (xorig);
696 SCM vars = SCM_EOL;
697 SCM *varloc = &vars;
698
699 SCM_ASSYNT (SCM_CONSP (x), scm_s_bindings, s_letstar);
700
701 bindings = SCM_CAR (x);
702 SCM_ASSYNT (scm_ilength (bindings) >= 0, scm_s_bindings, s_letstar);
703 while (!SCM_NULLP (bindings))
704 {
705 SCM binding = SCM_CAR (bindings);
706 SCM_ASSYNT (scm_ilength (binding) == 2, scm_s_bindings, s_letstar);
707 SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (binding)), scm_s_variable, s_letstar);
708 *varloc = scm_list_2 (SCM_CAR (binding), SCM_CADR (binding));
709 varloc = SCM_CDRLOC (SCM_CDR (*varloc));
710 bindings = SCM_CDR (bindings);
711 }
712
713 return scm_cons2 (SCM_IM_LETSTAR, vars,
714 scm_m_body (SCM_IM_LETSTAR, SCM_CDR (x), s_letstar));
715 }
716
717
718 /* DO gets the most radically altered syntax. The order of the vars is
719 * reversed here. In contrast, the order of the inits and steps is reversed
720 * during the evaluation:
721
722 (do ((<var1> <init1> <step1>)
723 (<var2> <init2>)
724 ... )
725 (<test> <return>)
726 <body>)
727
728 ;; becomes
729
730 (#@do (varn ... var2 var1)
731 (<init1> <init2> ... <initn>)
732 (<test> <return>)
733 (<body>)
734 <step1> <step2> ... <stepn>) ;; missing steps replaced by var
735 */
736
737 SCM_SYNTAX(s_do, "do", scm_makmmacro, scm_m_do);
738 SCM_GLOBAL_SYMBOL(scm_sym_do, s_do);
739
740 SCM
741 scm_m_do (SCM xorig, SCM env SCM_UNUSED)
742 {
743 SCM bindings;
744 SCM x = SCM_CDR (xorig);
745 SCM vars = SCM_EOL;
746 SCM inits = SCM_EOL;
747 SCM *initloc = &inits;
748 SCM steps = SCM_EOL;
749 SCM *steploc = &steps;
750 SCM_ASSYNT (scm_ilength (x) >= 2, scm_s_test, "do");
751 bindings = SCM_CAR (x);
752 SCM_ASSYNT (scm_ilength (bindings) >= 0, scm_s_bindings, "do");
753 while (!SCM_NULLP (bindings))
754 {
755 SCM binding = SCM_CAR (bindings);
756 long len = scm_ilength (binding);
757 SCM_ASSYNT (len == 2 || len == 3, scm_s_bindings, "do");
758 {
759 SCM name = SCM_CAR (binding);
760 SCM init = SCM_CADR (binding);
761 SCM step = (len == 2) ? name : SCM_CADDR (binding);
762 SCM_ASSYNT (SCM_SYMBOLP (name), scm_s_variable, "do");
763 vars = scm_cons (name, vars);
764 *initloc = scm_list_1 (init);
765 initloc = SCM_CDRLOC (*initloc);
766 *steploc = scm_list_1 (step);
767 steploc = SCM_CDRLOC (*steploc);
768 bindings = SCM_CDR (bindings);
769 }
770 }
771 x = SCM_CDR (x);
772 SCM_ASSYNT (scm_ilength (SCM_CAR (x)) >= 1, scm_s_test, "do");
773 x = scm_cons2 (SCM_CAR (x), SCM_CDR (x), steps);
774 x = scm_cons2 (vars, inits, x);
775 return scm_cons (SCM_IM_DO, x);
776 }
777
778
779 SCM_SYNTAX (s_quasiquote, "quasiquote", scm_makacro, scm_m_quasiquote);
780 SCM_GLOBAL_SYMBOL (scm_sym_quasiquote, s_quasiquote);
781
782 /* Internal function to handle a quasiquotation: 'form' is the parameter in
783 * the call (quasiquotation form), 'env' is the environment where unquoted
784 * expressions will be evaluated, and 'depth' is the current quasiquotation
785 * nesting level and is known to be greater than zero. */
786 static SCM
787 iqq (SCM form, SCM env, unsigned long int depth)
788 {
789 if (SCM_CONSP (form))
790 {
791 SCM tmp = SCM_CAR (form);
792 if (SCM_EQ_P (tmp, scm_sym_quasiquote))
793 {
794 SCM args = SCM_CDR (form);
795 SCM_ASSYNT (scm_ilength (args) == 1, scm_s_expression, s_quasiquote);
796 return scm_list_2 (tmp, iqq (SCM_CAR (args), env, depth + 1));
797 }
798 else if (SCM_EQ_P (tmp, scm_sym_unquote))
799 {
800 SCM args = SCM_CDR (form);
801 SCM_ASSYNT (scm_ilength (args) == 1, scm_s_expression, s_quasiquote);
802 if (depth - 1 == 0)
803 return scm_eval_car (args, env);
804 else
805 return scm_list_2 (tmp, iqq (SCM_CAR (args), env, depth - 1));
806 }
807 else if (SCM_CONSP (tmp)
808 && SCM_EQ_P (SCM_CAR (tmp), scm_sym_uq_splicing))
809 {
810 SCM args = SCM_CDR (tmp);
811 SCM_ASSYNT (scm_ilength (args) == 1, scm_s_expression, s_quasiquote);
812 if (depth - 1 == 0)
813 {
814 SCM list = scm_eval_car (args, env);
815 SCM rest = SCM_CDR (form);
816 SCM_ASSYNT (scm_ilength (list) >= 0, s_splicing, s_quasiquote);
817 return scm_append (scm_list_2 (list, iqq (rest, env, depth)));
818 }
819 else
820 return scm_cons (iqq (SCM_CAR (form), env, depth - 1),
821 iqq (SCM_CDR (form), env, depth));
822 }
823 else
824 return scm_cons (iqq (SCM_CAR (form), env, depth),
825 iqq (SCM_CDR (form), env, depth));
826 }
827 else if (SCM_VECTORP (form))
828 {
829 size_t i = SCM_VECTOR_LENGTH (form);
830 SCM *data = SCM_VELTS (form);
831 SCM tmp = SCM_EOL;
832 while (i != 0)
833 tmp = scm_cons (data[--i], tmp);
834 scm_remember_upto_here_1 (form);
835 return scm_vector (iqq (tmp, env, depth));
836 }
837 else
838 return form;
839 }
840
841 SCM
842 scm_m_quasiquote (SCM xorig, SCM env)
843 {
844 SCM x = SCM_CDR (xorig);
845 SCM_ASSYNT (scm_ilength (x) == 1, scm_s_expression, s_quasiquote);
846 return iqq (SCM_CAR (x), env, 1);
847 }
848
849
850 SCM_SYNTAX (s_delay, "delay", scm_makmmacro, scm_m_delay);
851 SCM_GLOBAL_SYMBOL (scm_sym_delay, s_delay);
852
853 /* Promises are implemented as closures with an empty parameter list. Thus,
854 * (delay <expression>) is transformed into (#@delay '() <expression>), where
855 * the empty list represents the empty parameter list. This representation
856 * allows for easy creation of the closure during evaluation. */
857 SCM
858 scm_m_delay (SCM xorig, SCM env SCM_UNUSED)
859 {
860 SCM_ASSYNT (scm_ilength (xorig) == 2, scm_s_expression, s_delay);
861 return scm_cons2 (SCM_IM_DELAY, SCM_EOL, SCM_CDR (xorig));
862 }
863
864
865 SCM_SYNTAX(s_define, "define", scm_makmmacro, scm_m_define);
866 SCM_GLOBAL_SYMBOL(scm_sym_define, s_define);
867
868 /* Guile provides an extension to R5RS' define syntax to represent function
869 * currying in a compact way. With this extension, it is allowed to write
870 * (define <nested-variable> <body>), where <nested-variable> has of one of
871 * the forms (<nested-variable> <formals>), (<nested-variable> . <formal>),
872 * (<variable> <formals>) or (<variable> . <formal>). As in R5RS, <formals>
873 * should be either a sequence of zero or more variables, or a sequence of one
874 * or more variables followed by a space-delimited period and another
875 * variable. Each level of argument nesting wraps the <body> within another
876 * lambda expression. For example, the following forms are allowed, each one
877 * followed by an equivalent, more explicit implementation.
878 * Example 1:
879 * (define ((a b . c) . d) <body>) is equivalent to
880 * (define a (lambda (b . c) (lambda d <body>)))
881 * Example 2:
882 * (define (((a) b) c . d) <body>) is equivalent to
883 * (define a (lambda () (lambda (b) (lambda (c . d) <body>))))
884 */
885 /* Dirk:FIXME:: We should provide an implementation for 'define' in the R5RS
886 * module that does not implement this extension. */
887 SCM
888 scm_m_define (SCM x, SCM env)
889 {
890 SCM name;
891 x = SCM_CDR (x);
892 SCM_ASSYNT (scm_ilength (x) >= 2, scm_s_expression, s_define);
893 name = SCM_CAR (x);
894 x = SCM_CDR (x);
895 while (SCM_CONSP (name))
896 {
897 /* This while loop realizes function currying by variable nesting. */
898 SCM formals = SCM_CDR (name);
899 x = scm_list_1 (scm_cons2 (scm_sym_lambda, formals, x));
900 name = SCM_CAR (name);
901 }
902 SCM_ASSYNT (SCM_SYMBOLP (name), scm_s_variable, s_define);
903 SCM_ASSYNT (scm_ilength (x) == 1, scm_s_expression, s_define);
904 if (SCM_TOP_LEVEL (env))
905 {
906 SCM var;
907 x = scm_eval_car (x, env);
908 if (SCM_REC_PROCNAMES_P)
909 {
910 SCM tmp = x;
911 while (SCM_MACROP (tmp))
912 tmp = SCM_MACRO_CODE (tmp);
913 if (SCM_CLOSUREP (tmp)
914 /* Only the first definition determines the name. */
915 && SCM_FALSEP (scm_procedure_property (tmp, scm_sym_name)))
916 scm_set_procedure_property_x (tmp, scm_sym_name, name);
917 }
918 var = scm_sym2var (name, scm_env_top_level (env), SCM_BOOL_T);
919 SCM_VARIABLE_SET (var, x);
920 return SCM_UNSPECIFIED;
921 }
922 else
923 return scm_cons2 (SCM_IM_DEFINE, name, x);
924 }
925
926
927 /* The bindings ((v1 i1) (v2 i2) ... (vn in)) are transformed to the lists
928 * (vn ... v2 v1) and (i1 i2 ... in). That is, the list of variables is
929 * reversed here, the list of inits gets reversed during evaluation. */
930 static void
931 transform_bindings (SCM bindings, SCM *rvarloc, SCM *initloc, const char *what)
932 {
933 SCM rvars = SCM_EOL;
934 *rvarloc = SCM_EOL;
935 *initloc = SCM_EOL;
936
937 SCM_ASSYNT (scm_ilength (bindings) >= 1, scm_s_bindings, what);
938
939 do
940 {
941 SCM binding = SCM_CAR (bindings);
942 SCM_ASSYNT (scm_ilength (binding) == 2, scm_s_bindings, what);
943 SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (binding)), scm_s_variable, what);
944 if (scm_c_improper_memq (SCM_CAR (binding), rvars))
945 scm_misc_error (what, scm_s_duplicate_bindings, SCM_EOL);
946 rvars = scm_cons (SCM_CAR (binding), rvars);
947 *initloc = scm_list_1 (SCM_CADR (binding));
948 initloc = SCM_CDRLOC (*initloc);
949 bindings = SCM_CDR (bindings);
950 }
951 while (!SCM_NULLP (bindings));
952
953 *rvarloc = rvars;
954 }
955
956
957 SCM_SYNTAX(s_letrec, "letrec", scm_makmmacro, scm_m_letrec);
958 SCM_GLOBAL_SYMBOL(scm_sym_letrec, s_letrec);
959
960 SCM
961 scm_m_letrec (SCM xorig, SCM env)
962 {
963 SCM x = SCM_CDR (xorig);
964 SCM_ASSYNT (SCM_CONSP (x), scm_s_bindings, s_letrec);
965
966 if (SCM_NULLP (SCM_CAR (x)))
967 {
968 /* null binding, let* faster */
969 SCM body = scm_m_body (SCM_IM_LETREC, SCM_CDR (x), s_letrec);
970 return scm_m_letstar (scm_cons2 (SCM_CAR (xorig), SCM_EOL, body), env);
971 }
972 else
973 {
974 SCM rvars, inits, body;
975 transform_bindings (SCM_CAR (x), &rvars, &inits, "letrec");
976 body = scm_m_body (SCM_IM_LETREC, SCM_CDR (x), "letrec");
977 return scm_cons2 (SCM_IM_LETREC, rvars, scm_cons (inits, body));
978 }
979 }
980
981
982 SCM_SYNTAX(s_let, "let", scm_makmmacro, scm_m_let);
983 SCM_GLOBAL_SYMBOL(scm_sym_let, s_let);
984
985 SCM
986 scm_m_let (SCM xorig, SCM env)
987 {
988 SCM x = SCM_CDR (xorig);
989 SCM temp;
990
991 SCM_ASSYNT (SCM_CONSP (x), scm_s_bindings, s_let);
992 temp = SCM_CAR (x);
993 if (SCM_NULLP (temp)
994 || (scm_ilength (temp) == 1 && SCM_CONSP (SCM_CAR (temp))))
995 {
996 /* null or single binding, let* is faster */
997 SCM bindings = temp;
998 SCM body = scm_m_body (SCM_IM_LET, SCM_CDR (x), s_let);
999 return scm_m_letstar (scm_cons2 (SCM_CAR (xorig), bindings, body), env);
1000 }
1001 else if (SCM_CONSP (temp))
1002 {
1003 /* plain let */
1004 SCM bindings = temp;
1005 SCM rvars, inits, body;
1006 transform_bindings (bindings, &rvars, &inits, "let");
1007 body = scm_m_body (SCM_IM_LET, SCM_CDR (x), "let");
1008 return scm_cons2 (SCM_IM_LET, rvars, scm_cons (inits, body));
1009 }
1010 else
1011 {
1012 /* named let: Transform (let name ((var init) ...) body ...) into
1013 * ((letrec ((name (lambda (var ...) body ...))) name) init ...) */
1014
1015 SCM name = temp;
1016 SCM vars = SCM_EOL;
1017 SCM *varloc = &vars;
1018 SCM inits = SCM_EOL;
1019 SCM *initloc = &inits;
1020 SCM bindings;
1021
1022 SCM_ASSYNT (SCM_SYMBOLP (name), scm_s_bindings, s_let);
1023 x = SCM_CDR (x);
1024 SCM_ASSYNT (SCM_CONSP (x), scm_s_bindings, s_let);
1025 bindings = SCM_CAR (x);
1026 SCM_ASSYNT (scm_ilength (bindings) >= 0, scm_s_bindings, s_let);
1027 while (!SCM_NULLP (bindings))
1028 { /* vars and inits both in order */
1029 SCM binding = SCM_CAR (bindings);
1030 SCM_ASSYNT (scm_ilength (binding) == 2, scm_s_bindings, s_let);
1031 SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (binding)), scm_s_variable, s_let);
1032 *varloc = scm_list_1 (SCM_CAR (binding));
1033 varloc = SCM_CDRLOC (*varloc);
1034 *initloc = scm_list_1 (SCM_CADR (binding));
1035 initloc = SCM_CDRLOC (*initloc);
1036 bindings = SCM_CDR (bindings);
1037 }
1038
1039 {
1040 SCM lambda_body = scm_m_body (SCM_IM_LET, SCM_CDR (x), "let");
1041 SCM lambda_form = scm_cons2 (scm_sym_lambda, vars, lambda_body);
1042 SCM rvar = scm_list_1 (name);
1043 SCM init = scm_list_1 (lambda_form);
1044 SCM body = scm_m_body (SCM_IM_LET, scm_list_1 (name), "let");
1045 SCM letrec = scm_cons2 (SCM_IM_LETREC, rvar, scm_cons (init, body));
1046 return scm_cons (letrec, inits);
1047 }
1048 }
1049 }
1050
1051
1052 SCM_SYNTAX (s_atapply,"@apply", scm_makmmacro, scm_m_apply);
1053 SCM_GLOBAL_SYMBOL (scm_sym_atapply, s_atapply);
1054 SCM_GLOBAL_SYMBOL (scm_sym_apply, s_atapply + 1);
1055
1056 SCM
1057 scm_m_apply (SCM xorig, SCM env SCM_UNUSED)
1058 {
1059 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 2, scm_s_expression, s_atapply);
1060 return scm_cons (SCM_IM_APPLY, SCM_CDR (xorig));
1061 }
1062
1063
1064 SCM_SYNTAX(s_atcall_cc,"@call-with-current-continuation", scm_makmmacro, scm_m_cont);
1065 SCM_GLOBAL_SYMBOL(scm_sym_atcall_cc,s_atcall_cc);
1066
1067
1068 SCM
1069 scm_m_cont (SCM xorig, SCM env SCM_UNUSED)
1070 {
1071 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1,
1072 scm_s_expression, s_atcall_cc);
1073 return scm_cons (SCM_IM_CONT, SCM_CDR (xorig));
1074 }
1075
1076 /* Multi-language support */
1077
1078 SCM_GLOBAL_SYMBOL (scm_lisp_nil, "nil");
1079 SCM_GLOBAL_SYMBOL (scm_lisp_t, "t");
1080
1081 SCM_SYNTAX (s_nil_cond, "nil-cond", scm_makmmacro, scm_m_nil_cond);
1082
1083 SCM
1084 scm_m_nil_cond (SCM xorig, SCM env SCM_UNUSED)
1085 {
1086 long len = scm_ilength (SCM_CDR (xorig));
1087 SCM_ASSYNT (len >= 1 && (len & 1) == 1, scm_s_expression, "nil-cond");
1088 return scm_cons (SCM_IM_NIL_COND, SCM_CDR (xorig));
1089 }
1090
1091 SCM_SYNTAX (s_nil_ify, "nil-ify", scm_makmmacro, scm_m_nil_ify);
1092
1093 SCM
1094 scm_m_nil_ify (SCM xorig, SCM env SCM_UNUSED)
1095 {
1096 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, scm_s_expression, "nil-ify");
1097 return scm_cons (SCM_IM_NIL_IFY, SCM_CDR (xorig));
1098 }
1099
1100 SCM_SYNTAX (s_t_ify, "t-ify", scm_makmmacro, scm_m_t_ify);
1101
1102 SCM
1103 scm_m_t_ify (SCM xorig, SCM env SCM_UNUSED)
1104 {
1105 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, scm_s_expression, "t-ify");
1106 return scm_cons (SCM_IM_T_IFY, SCM_CDR (xorig));
1107 }
1108
1109 SCM_SYNTAX (s_0_cond, "0-cond", scm_makmmacro, scm_m_0_cond);
1110
1111 SCM
1112 scm_m_0_cond (SCM xorig, SCM env SCM_UNUSED)
1113 {
1114 long len = scm_ilength (SCM_CDR (xorig));
1115 SCM_ASSYNT (len >= 1 && (len & 1) == 1, scm_s_expression, "0-cond");
1116 return scm_cons (SCM_IM_0_COND, SCM_CDR (xorig));
1117 }
1118
1119 SCM_SYNTAX (s_0_ify, "0-ify", scm_makmmacro, scm_m_0_ify);
1120
1121 SCM
1122 scm_m_0_ify (SCM xorig, SCM env SCM_UNUSED)
1123 {
1124 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, scm_s_expression, "0-ify");
1125 return scm_cons (SCM_IM_0_IFY, SCM_CDR (xorig));
1126 }
1127
1128 SCM_SYNTAX (s_1_ify, "1-ify", scm_makmmacro, scm_m_1_ify);
1129
1130 SCM
1131 scm_m_1_ify (SCM xorig, SCM env SCM_UNUSED)
1132 {
1133 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, scm_s_expression, "1-ify");
1134 return scm_cons (SCM_IM_1_IFY, SCM_CDR (xorig));
1135 }
1136
1137 SCM_SYNTAX (s_atfop, "@fop", scm_makmmacro, scm_m_atfop);
1138
1139 SCM
1140 scm_m_atfop (SCM xorig, SCM env SCM_UNUSED)
1141 {
1142 SCM x = SCM_CDR (xorig), var;
1143 SCM_ASSYNT (scm_ilength (x) >= 1, scm_s_expression, "@fop");
1144 var = scm_symbol_fref (SCM_CAR (x));
1145 SCM_ASSYNT (SCM_VARIABLEP (var),
1146 "Symbol's function definition is void", NULL);
1147 SCM_SETCAR (x, var);
1148 return x;
1149 }
1150
1151 /* (@bind ((var exp) ...) body ...)
1152
1153 This will assign the values of the `exp's to the global variables
1154 named by `var's (symbols, not evaluated), creating them if they
1155 don't exist, executes body, and then restores the previous values of
1156 the `var's. Additionally, whenever control leaves body, the values
1157 of the `var's are saved and restored when control returns. It is an
1158 error when a symbol appears more than once among the `var's.
1159 All `exp's are evaluated before any `var' is set.
1160
1161 This of this as `let' for dynamic scope.
1162
1163 It is memoized into (#@bind ((var ...) . (reversed-val ...)) body ...).
1164
1165 XXX - also implement `@bind*'.
1166 */
1167
1168 SCM_SYNTAX (s_atbind, "@bind", scm_makmmacro, scm_m_atbind);
1169
1170 SCM
1171 scm_m_atbind (SCM xorig, SCM env)
1172 {
1173 SCM x = SCM_CDR (xorig);
1174 SCM top_level = scm_env_top_level (env);
1175 SCM vars = SCM_EOL, var;
1176 SCM exps = SCM_EOL;
1177
1178 SCM_ASSYNT (scm_ilength (x) > 1, scm_s_expression, s_atbind);
1179
1180 x = SCM_CAR (x);
1181 while (SCM_NIMP (x))
1182 {
1183 SCM rest;
1184 SCM sym_exp = SCM_CAR (x);
1185 SCM_ASSYNT (scm_ilength (sym_exp) == 2, scm_s_bindings, s_atbind);
1186 SCM_ASSYNT (SCM_SYMBOLP (SCM_CAR (sym_exp)), scm_s_bindings, s_atbind);
1187 x = SCM_CDR (x);
1188 for (rest = x; SCM_NIMP (rest); rest = SCM_CDR (rest))
1189 if (SCM_EQ_P (SCM_CAR (sym_exp), SCM_CAAR (rest)))
1190 scm_misc_error (s_atbind, scm_s_duplicate_bindings, SCM_EOL);
1191 /* The first call to scm_sym2var will look beyond the current
1192 module, while the second call wont. */
1193 var = scm_sym2var (SCM_CAR (sym_exp), top_level, SCM_BOOL_F);
1194 if (SCM_FALSEP (var))
1195 var = scm_sym2var (SCM_CAR (sym_exp), top_level, SCM_BOOL_T);
1196 vars = scm_cons (var, vars);
1197 exps = scm_cons (SCM_CADR (sym_exp), exps);
1198 }
1199 return scm_cons (SCM_IM_BIND,
1200 scm_cons (scm_cons (scm_reverse_x (vars, SCM_EOL), exps),
1201 SCM_CDDR (xorig)));
1202 }
1203
1204 SCM_SYNTAX (s_at_call_with_values, "@call-with-values", scm_makmmacro, scm_m_at_call_with_values);
1205 SCM_GLOBAL_SYMBOL(scm_sym_at_call_with_values, s_at_call_with_values);
1206
1207 SCM
1208 scm_m_at_call_with_values (SCM xorig, SCM env SCM_UNUSED)
1209 {
1210 SCM_ASSYNT (scm_ilength (SCM_CDR (xorig)) == 2,
1211 scm_s_expression, s_at_call_with_values);
1212 return scm_cons (SCM_IM_CALL_WITH_VALUES, SCM_CDR (xorig));
1213 }
1214
1215 SCM
1216 scm_m_expand_body (SCM xorig, SCM env)
1217 {
1218 SCM x = SCM_CDR (xorig), defs = SCM_EOL;
1219 char *what = SCM_ISYMCHARS (SCM_CAR (xorig)) + 2;
1220
1221 while (SCM_NIMP (x))
1222 {
1223 SCM form = SCM_CAR (x);
1224 if (!SCM_CONSP (form))
1225 break;
1226 if (!SCM_SYMBOLP (SCM_CAR (form)))
1227 break;
1228
1229 form = scm_macroexp (scm_cons_source (form,
1230 SCM_CAR (form),
1231 SCM_CDR (form)),
1232 env);
1233
1234 if (SCM_EQ_P (SCM_IM_DEFINE, SCM_CAR (form)))
1235 {
1236 defs = scm_cons (SCM_CDR (form), defs);
1237 x = SCM_CDR (x);
1238 }
1239 else if (!SCM_IMP (defs))
1240 {
1241 break;
1242 }
1243 else if (SCM_EQ_P (SCM_IM_BEGIN, SCM_CAR (form)))
1244 {
1245 x = scm_append (scm_list_2 (SCM_CDR (form), SCM_CDR (x)));
1246 }
1247 else
1248 {
1249 x = scm_cons (form, SCM_CDR (x));
1250 break;
1251 }
1252 }
1253
1254 if (!SCM_NULLP (defs))
1255 {
1256 SCM rvars, inits, body, letrec;
1257 transform_bindings (defs, &rvars, &inits, what);
1258 body = scm_m_body (SCM_IM_DEFINE, x, what);
1259 letrec = scm_cons2 (SCM_IM_LETREC, rvars, scm_cons (inits, body));
1260 SCM_SETCAR (xorig, letrec);
1261 SCM_SETCDR (xorig, SCM_EOL);
1262 }
1263 else
1264 {
1265 SCM_ASSYNT (SCM_CONSP (x), scm_s_body, what);
1266 SCM_SETCAR (xorig, SCM_CAR (x));
1267 SCM_SETCDR (xorig, SCM_CDR (x));
1268 }
1269
1270 return xorig;
1271 }
1272
1273 SCM
1274 scm_macroexp (SCM x, SCM env)
1275 {
1276 SCM res, proc, orig_sym;
1277
1278 /* Don't bother to produce error messages here. We get them when we
1279 eventually execute the code for real. */
1280
1281 macro_tail:
1282 orig_sym = SCM_CAR (x);
1283 if (!SCM_SYMBOLP (orig_sym))
1284 return x;
1285
1286 #ifdef USE_THREADS
1287 {
1288 SCM *proc_ptr = scm_lookupcar1 (x, env, 0);
1289 if (proc_ptr == NULL)
1290 {
1291 /* We have lost the race. */
1292 goto macro_tail;
1293 }
1294 proc = *proc_ptr;
1295 }
1296 #else
1297 proc = *scm_lookupcar (x, env, 0);
1298 #endif
1299
1300 /* Only handle memoizing macros. `Acros' and `macros' are really
1301 special forms and should not be evaluated here. */
1302
1303 if (!SCM_MACROP (proc) || SCM_MACRO_TYPE (proc) != 2)
1304 return x;
1305
1306 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of lookupcar */
1307 res = scm_call_2 (SCM_MACRO_CODE (proc), x, env);
1308
1309 if (scm_ilength (res) <= 0)
1310 res = scm_list_2 (SCM_IM_BEGIN, res);
1311
1312 SCM_DEFER_INTS;
1313 SCM_SETCAR (x, SCM_CAR (res));
1314 SCM_SETCDR (x, SCM_CDR (res));
1315 SCM_ALLOW_INTS;
1316
1317 goto macro_tail;
1318 }
1319
1320 /* scm_unmemocopy takes a memoized expression together with its
1321 * environment and rewrites it to its original form. Thus, it is the
1322 * inversion of the rewrite rules above. The procedure is not
1323 * optimized for speed. It's used in scm_iprin1 when printing the
1324 * code of a closure, in scm_procedure_source, in display_frame when
1325 * generating the source for a stackframe in a backtrace, and in
1326 * display_expression.
1327 *
1328 * Unmemoizing is not a realiable process. You can not in general
1329 * expect to get the original source back.
1330 *
1331 * However, GOOPS currently relies on this for method compilation.
1332 * This ought to change.
1333 */
1334
1335 #define SCM_BIT8(x) (127 & SCM_UNPACK (x))
1336
1337 static SCM
1338 build_binding_list (SCM names, SCM inits)
1339 {
1340 SCM bindings = SCM_EOL;
1341 while (!SCM_NULLP (names))
1342 {
1343 SCM binding = scm_list_2 (SCM_CAR (names), SCM_CAR (inits));
1344 bindings = scm_cons (binding, bindings);
1345 names = SCM_CDR (names);
1346 inits = SCM_CDR (inits);
1347 }
1348 return bindings;
1349 }
1350
1351 static SCM
1352 unmemocopy (SCM x, SCM env)
1353 {
1354 SCM ls, z;
1355 #ifdef DEBUG_EXTENSIONS
1356 SCM p;
1357 #endif
1358 if (!SCM_CONSP (x))
1359 return x;
1360 #ifdef DEBUG_EXTENSIONS
1361 p = scm_whash_lookup (scm_source_whash, x);
1362 #endif
1363 switch (SCM_ITAG7 (SCM_CAR (x)))
1364 {
1365 case SCM_BIT8(SCM_IM_AND):
1366 ls = z = scm_cons (scm_sym_and, SCM_UNSPECIFIED);
1367 break;
1368 case SCM_BIT8(SCM_IM_BEGIN):
1369 ls = z = scm_cons (scm_sym_begin, SCM_UNSPECIFIED);
1370 break;
1371 case SCM_BIT8(SCM_IM_CASE):
1372 ls = z = scm_cons (scm_sym_case, SCM_UNSPECIFIED);
1373 break;
1374 case SCM_BIT8(SCM_IM_COND):
1375 ls = z = scm_cons (scm_sym_cond, SCM_UNSPECIFIED);
1376 break;
1377 case SCM_BIT8 (SCM_IM_DO):
1378 {
1379 /* format: (#@do (nk nk-1 ...) (i1 ... ik) (test) (body) s1 ... sk),
1380 * where nx is the name of a local variable, ix is an initializer for
1381 * the local variable, test is the test clause of the do loop, body is
1382 * the body of the do loop and sx are the step clauses for the local
1383 * variables. */
1384 SCM names, inits, test, memoized_body, steps, bindings;
1385
1386 x = SCM_CDR (x);
1387 names = SCM_CAR (x);
1388 x = SCM_CDR (x);
1389 inits = scm_reverse (unmemocopy (SCM_CAR (x), env));
1390 env = EXTEND_ENV (names, SCM_EOL, env);
1391 x = SCM_CDR (x);
1392 test = unmemocopy (SCM_CAR (x), env);
1393 x = SCM_CDR (x);
1394 memoized_body = SCM_CAR (x);
1395 x = SCM_CDR (x);
1396 steps = scm_reverse (unmemocopy (x, env));
1397
1398 /* build transformed binding list */
1399 bindings = SCM_EOL;
1400 while (!SCM_NULLP (names))
1401 {
1402 SCM name = SCM_CAR (names);
1403 SCM init = SCM_CAR (inits);
1404 SCM step = SCM_CAR (steps);
1405 step = SCM_EQ_P (step, name) ? SCM_EOL : scm_list_1 (step);
1406
1407 bindings = scm_cons (scm_cons2 (name, init, step), bindings);
1408
1409 names = SCM_CDR (names);
1410 inits = SCM_CDR (inits);
1411 steps = SCM_CDR (steps);
1412 }
1413 z = scm_cons (test, SCM_UNSPECIFIED);
1414 ls = scm_cons2 (scm_sym_do, bindings, z);
1415
1416 x = scm_cons (SCM_BOOL_F, memoized_body);
1417 break;
1418 }
1419 case SCM_BIT8(SCM_IM_IF):
1420 ls = z = scm_cons (scm_sym_if, SCM_UNSPECIFIED);
1421 break;
1422 case SCM_BIT8 (SCM_IM_LET):
1423 {
1424 /* format: (#@let (nk nk-1 ...) (i1 ... ik) b1 ...),
1425 * where nx is the name of a local variable, ix is an initializer for
1426 * the local variable and by are the body clauses. */
1427 SCM names, inits, bindings;
1428
1429 x = SCM_CDR (x);
1430 names = SCM_CAR (x);
1431 x = SCM_CDR (x);
1432 inits = scm_reverse (unmemocopy (SCM_CAR (x), env));
1433 env = EXTEND_ENV (names, SCM_EOL, env);
1434
1435 bindings = build_binding_list (names, inits);
1436 z = scm_cons (bindings, SCM_UNSPECIFIED);
1437 ls = scm_cons (scm_sym_let, z);
1438 break;
1439 }
1440 case SCM_BIT8 (SCM_IM_LETREC):
1441 {
1442 /* format: (#@letrec (nk nk-1 ...) (i1 ... ik) b1 ...),
1443 * where nx is the name of a local variable, ix is an initializer for
1444 * the local variable and by are the body clauses. */
1445 SCM names, inits, bindings;
1446
1447 x = SCM_CDR (x);
1448 names = SCM_CAR (x);
1449 env = EXTEND_ENV (names, SCM_EOL, env);
1450 x = SCM_CDR (x);
1451 inits = scm_reverse (unmemocopy (SCM_CAR (x), env));
1452
1453 bindings = build_binding_list (names, inits);
1454 z = scm_cons (bindings, SCM_UNSPECIFIED);
1455 ls = scm_cons (scm_sym_letrec, z);
1456 break;
1457 }
1458 case SCM_BIT8(SCM_IM_LETSTAR):
1459 {
1460 SCM b, y;
1461 x = SCM_CDR (x);
1462 b = SCM_CAR (x);
1463 y = SCM_EOL;
1464 if SCM_IMP (b)
1465 {
1466 env = EXTEND_ENV (SCM_EOL, SCM_EOL, env);
1467 goto letstar;
1468 }
1469 y = z = scm_acons (SCM_CAR (b),
1470 unmemocar (
1471 scm_cons (unmemocopy (SCM_CADR (b), env), SCM_EOL), env),
1472 SCM_UNSPECIFIED);
1473 env = EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
1474 b = SCM_CDDR (b);
1475 if (SCM_IMP (b))
1476 {
1477 SCM_SETCDR (y, SCM_EOL);
1478 ls = scm_cons (scm_sym_let, z = scm_cons (y, SCM_UNSPECIFIED));
1479 break;
1480 }
1481 do
1482 {
1483 SCM_SETCDR (z, scm_acons (SCM_CAR (b),
1484 unmemocar (
1485 scm_list_1 (unmemocopy (SCM_CADR (b), env)), env),
1486 SCM_UNSPECIFIED));
1487 z = SCM_CDR (z);
1488 env = EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
1489 b = SCM_CDDR (b);
1490 }
1491 while (SCM_NIMP (b));
1492 SCM_SETCDR (z, SCM_EOL);
1493 letstar:
1494 ls = scm_cons (scm_sym_letstar, z = scm_cons (y, SCM_UNSPECIFIED));
1495 break;
1496 }
1497 case SCM_BIT8(SCM_IM_OR):
1498 ls = z = scm_cons (scm_sym_or, SCM_UNSPECIFIED);
1499 break;
1500 case SCM_BIT8(SCM_IM_LAMBDA):
1501 x = SCM_CDR (x);
1502 z = scm_cons (SCM_CAR (x), SCM_UNSPECIFIED);
1503 ls = scm_cons (scm_sym_lambda, z);
1504 env = EXTEND_ENV (SCM_CAR (x), SCM_EOL, env);
1505 break;
1506 case SCM_BIT8(SCM_IM_QUOTE):
1507 ls = z = scm_cons (scm_sym_quote, SCM_UNSPECIFIED);
1508 break;
1509 case SCM_BIT8(SCM_IM_SET_X):
1510 ls = z = scm_cons (scm_sym_set_x, SCM_UNSPECIFIED);
1511 break;
1512 case SCM_BIT8(SCM_IM_DEFINE):
1513 {
1514 SCM n;
1515 x = SCM_CDR (x);
1516 n = SCM_CAR (x);
1517 z = scm_cons (n, SCM_UNSPECIFIED);
1518 ls = scm_cons (scm_sym_define, z);
1519 if (!SCM_NULLP (env))
1520 SCM_SETCAR (SCM_CAR (env), scm_cons (n, SCM_CAAR (env)));
1521 break;
1522 }
1523 case SCM_BIT8(SCM_MAKISYM (0)):
1524 z = SCM_CAR (x);
1525 if (!SCM_ISYMP (z))
1526 goto unmemo;
1527 switch (SCM_ISYMNUM (z))
1528 {
1529 case (SCM_ISYMNUM (SCM_IM_APPLY)):
1530 ls = z = scm_cons (scm_sym_atapply, SCM_UNSPECIFIED);
1531 goto loop;
1532 case (SCM_ISYMNUM (SCM_IM_CONT)):
1533 ls = z = scm_cons (scm_sym_atcall_cc, SCM_UNSPECIFIED);
1534 goto loop;
1535 case (SCM_ISYMNUM (SCM_IM_DELAY)):
1536 ls = z = scm_cons (scm_sym_delay, SCM_UNSPECIFIED);
1537 x = SCM_CDR (x);
1538 goto loop;
1539 case (SCM_ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
1540 ls = z = scm_cons (scm_sym_at_call_with_values, SCM_UNSPECIFIED);
1541 goto loop;
1542 default:
1543 /* appease the Sun compiler god: */ ;
1544 }
1545 unmemo:
1546 default:
1547 ls = z = unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
1548 SCM_UNSPECIFIED),
1549 env);
1550 }
1551 loop:
1552 x = SCM_CDR (x);
1553 while (SCM_CONSP (x))
1554 {
1555 SCM form = SCM_CAR (x);
1556 if (!SCM_ISYMP (form))
1557 {
1558 SCM copy = scm_cons (unmemocopy (form, env), SCM_UNSPECIFIED);
1559 SCM_SETCDR (z, unmemocar (copy, env));
1560 z = SCM_CDR (z);
1561 }
1562 x = SCM_CDR (x);
1563 }
1564 SCM_SETCDR (z, x);
1565 #ifdef DEBUG_EXTENSIONS
1566 if (!SCM_FALSEP (p))
1567 scm_whash_insert (scm_source_whash, ls, p);
1568 #endif
1569 return ls;
1570 }
1571
1572
1573 SCM
1574 scm_unmemocopy (SCM x, SCM env)
1575 {
1576 if (!SCM_NULLP (env))
1577 /* Make a copy of the lowest frame to protect it from
1578 modifications by SCM_IM_DEFINE */
1579 return unmemocopy (x, scm_cons (SCM_CAR (env), SCM_CDR (env)));
1580 else
1581 return unmemocopy (x, env);
1582 }
1583
1584 #ifndef SCM_RECKLESS
1585
1586 int
1587 scm_badargsp (SCM formals, SCM args)
1588 {
1589 while (SCM_NIMP (formals))
1590 {
1591 if (!SCM_CONSP (formals))
1592 return 0;
1593 if (SCM_IMP(args))
1594 return 1;
1595 formals = SCM_CDR (formals);
1596 args = SCM_CDR (args);
1597 }
1598 return !SCM_NULLP (args) ? 1 : 0;
1599 }
1600
1601 #endif
1602
1603 static int
1604 scm_badformalsp (SCM closure, int n)
1605 {
1606 SCM formals = SCM_CLOSURE_FORMALS (closure);
1607 while (!SCM_NULLP (formals))
1608 {
1609 if (!SCM_CONSP (formals))
1610 return 0;
1611 if (n == 0)
1612 return 1;
1613 --n;
1614 formals = SCM_CDR (formals);
1615 }
1616 return n;
1617 }
1618
1619 \f
1620 SCM
1621 scm_eval_args (SCM l, SCM env, SCM proc)
1622 {
1623 SCM results = SCM_EOL, *lloc = &results, res;
1624 while (SCM_CONSP (l))
1625 {
1626 res = EVALCAR (l, env);
1627
1628 *lloc = scm_list_1 (res);
1629 lloc = SCM_CDRLOC (*lloc);
1630 l = SCM_CDR (l);
1631 }
1632 #ifdef SCM_CAUTIOUS
1633 if (!SCM_NULLP (l))
1634 scm_wrong_num_args (proc);
1635 #endif
1636 return results;
1637 }
1638
1639 SCM
1640 scm_eval_body (SCM code, SCM env)
1641 {
1642 SCM next;
1643 again:
1644 next = SCM_CDR (code);
1645 while (!SCM_NULLP (next))
1646 {
1647 if (SCM_IMP (SCM_CAR (code)))
1648 {
1649 if (SCM_ISYMP (SCM_CAR (code)))
1650 {
1651 code = scm_m_expand_body (code, env);
1652 goto again;
1653 }
1654 }
1655 else
1656 SCM_XEVAL (SCM_CAR (code), env);
1657 code = next;
1658 next = SCM_CDR (code);
1659 }
1660 return SCM_XEVALCAR (code, env);
1661 }
1662
1663
1664 #endif /* !DEVAL */
1665
1666
1667 /* SECTION: This code is specific for the debugging support. One
1668 * branch is read when DEVAL isn't defined, the other when DEVAL is
1669 * defined.
1670 */
1671
1672 #ifndef DEVAL
1673
1674 #define SCM_APPLY scm_apply
1675 #define PREP_APPLY(proc, args)
1676 #define ENTER_APPLY
1677 #define RETURN(x) do { return x; } while (0)
1678 #ifdef STACK_CHECKING
1679 #ifndef NO_CEVAL_STACK_CHECKING
1680 #define EVAL_STACK_CHECKING
1681 #endif
1682 #endif
1683
1684 #else /* !DEVAL */
1685
1686 #undef SCM_CEVAL
1687 #define SCM_CEVAL scm_deval /* Substitute all uses of scm_ceval */
1688 #undef SCM_APPLY
1689 #define SCM_APPLY scm_dapply
1690 #undef PREP_APPLY
1691 #define PREP_APPLY(p, l) \
1692 { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
1693 #undef ENTER_APPLY
1694 #define ENTER_APPLY \
1695 do { \
1696 SCM_SET_ARGSREADY (debug);\
1697 if (CHECK_APPLY && SCM_TRAPS_P)\
1698 if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && PROCTRACEP (proc)))\
1699 {\
1700 SCM tmp, tail = SCM_BOOL(SCM_TRACED_FRAME_P (debug)); \
1701 SCM_SET_TRACED_FRAME (debug); \
1702 SCM_TRAPS_P = 0;\
1703 if (SCM_CHEAPTRAPS_P)\
1704 {\
1705 tmp = scm_make_debugobj (&debug);\
1706 scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
1707 }\
1708 else\
1709 {\
1710 int first;\
1711 tmp = scm_make_continuation (&first);\
1712 if (first)\
1713 scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
1714 }\
1715 SCM_TRAPS_P = 1;\
1716 }\
1717 } while (0)
1718 #undef RETURN
1719 #define RETURN(e) do { proc = (e); goto exit; } while (0)
1720 #ifdef STACK_CHECKING
1721 #ifndef EVAL_STACK_CHECKING
1722 #define EVAL_STACK_CHECKING
1723 #endif
1724 #endif
1725
1726 /* scm_ceval_ptr points to the currently selected evaluator.
1727 * *fixme*: Although efficiency is important here, this state variable
1728 * should probably not be a global. It should be related to the
1729 * current repl.
1730 */
1731
1732
1733 SCM (*scm_ceval_ptr) (SCM x, SCM env);
1734
1735 /* scm_last_debug_frame contains a pointer to the last debugging
1736 * information stack frame. It is accessed very often from the
1737 * debugging evaluator, so it should probably not be indirectly
1738 * addressed. Better to save and restore it from the current root at
1739 * any stack swaps.
1740 */
1741
1742 #ifndef USE_THREADS
1743 scm_t_debug_frame *scm_last_debug_frame;
1744 #endif
1745
1746 /* scm_debug_eframe_size is the number of slots available for pseudo
1747 * stack frames at each real stack frame.
1748 */
1749
1750 long scm_debug_eframe_size;
1751
1752 int scm_debug_mode, scm_check_entry_p, scm_check_apply_p, scm_check_exit_p;
1753
1754 long scm_eval_stack;
1755
1756 scm_t_option scm_eval_opts[] = {
1757 { SCM_OPTION_INTEGER, "stack", 22000, "Size of thread stacks (in machine words)." }
1758 };
1759
1760 scm_t_option scm_debug_opts[] = {
1761 { SCM_OPTION_BOOLEAN, "cheap", 1,
1762 "*Flyweight representation of the stack at traps." },
1763 { SCM_OPTION_BOOLEAN, "breakpoints", 0, "*Check for breakpoints." },
1764 { SCM_OPTION_BOOLEAN, "trace", 0, "*Trace mode." },
1765 { SCM_OPTION_BOOLEAN, "procnames", 1,
1766 "Record procedure names at definition." },
1767 { SCM_OPTION_BOOLEAN, "backwards", 0,
1768 "Display backtrace in anti-chronological order." },
1769 { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
1770 { SCM_OPTION_INTEGER, "indent", 10, "Maximal indentation in backtrace." },
1771 { SCM_OPTION_INTEGER, "frames", 3,
1772 "Maximum number of tail-recursive frames in backtrace." },
1773 { SCM_OPTION_INTEGER, "maxdepth", 1000,
1774 "Maximal number of stored backtrace frames." },
1775 { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
1776 { SCM_OPTION_BOOLEAN, "backtrace", 0, "Show backtrace on error." },
1777 { SCM_OPTION_BOOLEAN, "debug", 0, "Use the debugging evaluator." },
1778 { SCM_OPTION_INTEGER, "stack", 20000, "Stack size limit (measured in words; 0 = no check)." },
1779 { SCM_OPTION_SCM, "show-file-name", (unsigned long)SCM_BOOL_T, "Show file names and line numbers in backtraces when not `#f'. A value of `base' displays only base names, while `#t' displays full names."}
1780 };
1781
1782 scm_t_option scm_evaluator_trap_table[] = {
1783 { SCM_OPTION_BOOLEAN, "traps", 0, "Enable evaluator traps." },
1784 { SCM_OPTION_BOOLEAN, "enter-frame", 0, "Trap when eval enters new frame." },
1785 { SCM_OPTION_BOOLEAN, "apply-frame", 0, "Trap when entering apply." },
1786 { SCM_OPTION_BOOLEAN, "exit-frame", 0, "Trap when exiting eval or apply." },
1787 { SCM_OPTION_SCM, "enter-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for enter-frame traps." },
1788 { SCM_OPTION_SCM, "apply-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for apply-frame traps." },
1789 { SCM_OPTION_SCM, "exit-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for exit-frame traps." }
1790 };
1791
1792 SCM_DEFINE (scm_eval_options_interface, "eval-options-interface", 0, 1, 0,
1793 (SCM setting),
1794 "Option interface for the evaluation options. Instead of using\n"
1795 "this procedure directly, use the procedures @code{eval-enable},\n"
1796 "@code{eval-disable}, @code{eval-set!} and @var{eval-options}.")
1797 #define FUNC_NAME s_scm_eval_options_interface
1798 {
1799 SCM ans;
1800 SCM_DEFER_INTS;
1801 ans = scm_options (setting,
1802 scm_eval_opts,
1803 SCM_N_EVAL_OPTIONS,
1804 FUNC_NAME);
1805 scm_eval_stack = SCM_EVAL_STACK * sizeof (void *);
1806 SCM_ALLOW_INTS;
1807 return ans;
1808 }
1809 #undef FUNC_NAME
1810
1811 SCM_DEFINE (scm_evaluator_traps, "evaluator-traps-interface", 0, 1, 0,
1812 (SCM setting),
1813 "Option interface for the evaluator trap options.")
1814 #define FUNC_NAME s_scm_evaluator_traps
1815 {
1816 SCM ans;
1817 SCM_DEFER_INTS;
1818 ans = scm_options (setting,
1819 scm_evaluator_trap_table,
1820 SCM_N_EVALUATOR_TRAPS,
1821 FUNC_NAME);
1822 SCM_RESET_DEBUG_MODE;
1823 SCM_ALLOW_INTS;
1824 return ans;
1825 }
1826 #undef FUNC_NAME
1827
1828 static SCM
1829 deval_args (SCM l, SCM env, SCM proc, SCM *lloc)
1830 {
1831 SCM *results = lloc, res;
1832 while (SCM_CONSP (l))
1833 {
1834 res = EVALCAR (l, env);
1835
1836 *lloc = scm_list_1 (res);
1837 lloc = SCM_CDRLOC (*lloc);
1838 l = SCM_CDR (l);
1839 }
1840 #ifdef SCM_CAUTIOUS
1841 if (!SCM_NULLP (l))
1842 scm_wrong_num_args (proc);
1843 #endif
1844 return *results;
1845 }
1846
1847 #endif /* !DEVAL */
1848
1849
1850 /* SECTION: This code is compiled twice.
1851 */
1852
1853
1854 /* Update the toplevel environment frame ENV so that it refers to the
1855 * current module. */
1856 #define UPDATE_TOPLEVEL_ENV(env) \
1857 do { \
1858 SCM p = scm_current_module_lookup_closure (); \
1859 if (p != SCM_CAR(env)) \
1860 env = scm_top_level_env (p); \
1861 } while (0)
1862
1863 #ifndef DEVAL
1864 #define CHECK_EQVISH(A,B) (SCM_EQ_P ((A), (B)) || (!SCM_FALSEP (scm_eqv_p ((A), (B)))))
1865 #endif /* DEVAL */
1866
1867 /* This is the evaluator. Like any real monster, it has three heads:
1868 *
1869 * scm_ceval is the non-debugging evaluator, scm_deval is the debugging
1870 * version. Both are implemented using a common code base, using the
1871 * following mechanism: SCM_CEVAL is a macro, which is either defined to
1872 * scm_ceval or scm_deval. Thus, there is no function SCM_CEVAL, but the code
1873 * for SCM_CEVAL actually compiles to either scm_ceval or scm_deval. When
1874 * SCM_CEVAL is defined to scm_ceval, it is known that the macro DEVAL is not
1875 * defined. When SCM_CEVAL is defined to scm_deval, then the macro DEVAL is
1876 * known to be defined. Thus, in SCM_CEVAL parts for the debugging evaluator
1877 * are enclosed within #ifdef DEVAL ... #endif.
1878 *
1879 * All three (scm_ceval, scm_deval and their common implementation SCM_CEVAL)
1880 * take two input parameters, x and env: x is a single expression to be
1881 * evalutated. env is the environment in which bindings are searched.
1882 *
1883 * x is known to be a cell (i. e. a pair or any other non-immediate). Since x
1884 * is a single expression, it is necessarily in a tail position. If x is just
1885 * a call to another function like in the expression (foo exp1 exp2 ...), the
1886 * realization of that call therefore _must_not_ increase stack usage (the
1887 * evaluation of exp1, exp2 etc., however, may do so). This is realized by
1888 * making extensive use of 'goto' statements within the evaluator: The gotos
1889 * replace recursive calls to SCM_CEVAL, thus re-using the same stack frame
1890 * that SCM_CEVAL was already using. If, however, x represents some form that
1891 * requires to evaluate a sequence of expressions like (begin exp1 exp2 ...),
1892 * then recursive calls to SCM_CEVAL are performed for all but the last
1893 * expression of that sequence. */
1894
1895 #if 0
1896 SCM
1897 scm_ceval (SCM x, SCM env)
1898 {}
1899 #endif
1900
1901 #if 0
1902 SCM
1903 scm_deval (SCM x, SCM env)
1904 {}
1905 #endif
1906
1907 SCM
1908 SCM_CEVAL (SCM x, SCM env)
1909 {
1910 union
1911 {
1912 SCM *lloc;
1913 SCM arg1;
1914 } t;
1915 SCM proc, arg2, orig_sym;
1916 #ifdef DEVAL
1917 scm_t_debug_frame debug;
1918 scm_t_debug_info *debug_info_end;
1919 debug.prev = scm_last_debug_frame;
1920 debug.status = scm_debug_eframe_size;
1921 /*
1922 * The debug.vect contains twice as much scm_t_debug_info frames as the
1923 * user has specified with (debug-set! frames <n>).
1924 *
1925 * Even frames are eval frames, odd frames are apply frames.
1926 */
1927 debug.vect = (scm_t_debug_info *) alloca (scm_debug_eframe_size
1928 * sizeof (scm_t_debug_info));
1929 debug.info = debug.vect;
1930 debug_info_end = debug.vect + scm_debug_eframe_size;
1931 scm_last_debug_frame = &debug;
1932 #endif
1933 #ifdef EVAL_STACK_CHECKING
1934 if (scm_stack_checking_enabled_p
1935 && SCM_STACK_OVERFLOW_P ((SCM_STACKITEM *) &proc))
1936 {
1937 #ifdef DEVAL
1938 debug.info->e.exp = x;
1939 debug.info->e.env = env;
1940 #endif
1941 scm_report_stack_overflow ();
1942 }
1943 #endif
1944 #ifdef DEVAL
1945 goto start;
1946 #endif
1947 loopnoap:
1948 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
1949 loop:
1950 #ifdef DEVAL
1951 SCM_CLEAR_ARGSREADY (debug);
1952 if (SCM_OVERFLOWP (debug))
1953 --debug.info;
1954 /*
1955 * In theory, this should be the only place where it is necessary to
1956 * check for space in debug.vect since both eval frames and
1957 * available space are even.
1958 *
1959 * For this to be the case, however, it is necessary that primitive
1960 * special forms which jump back to `loop', `begin' or some similar
1961 * label call PREP_APPLY. A convenient way to do this is to jump to
1962 * `loopnoap' or `cdrxnoap'.
1963 */
1964 else if (++debug.info >= debug_info_end)
1965 {
1966 SCM_SET_OVERFLOW (debug);
1967 debug.info -= 2;
1968 }
1969 start:
1970 debug.info->e.exp = x;
1971 debug.info->e.env = env;
1972 if (CHECK_ENTRY && SCM_TRAPS_P)
1973 if (SCM_ENTER_FRAME_P || (SCM_BREAKPOINTS_P && SRCBRKP (x)))
1974 {
1975 SCM tail = SCM_BOOL(SCM_TAILRECP (debug));
1976 SCM_SET_TAILREC (debug);
1977 if (SCM_CHEAPTRAPS_P)
1978 t.arg1 = scm_make_debugobj (&debug);
1979 else
1980 {
1981 int first;
1982 SCM val = scm_make_continuation (&first);
1983
1984 if (first)
1985 t.arg1 = val;
1986 else
1987 {
1988 x = val;
1989 if (SCM_IMP (x))
1990 RETURN (x);
1991 else
1992 /* This gives the possibility for the debugger to
1993 modify the source expression before evaluation. */
1994 goto dispatch;
1995 }
1996 }
1997 SCM_TRAPS_P = 0;
1998 scm_call_4 (SCM_ENTER_FRAME_HDLR,
1999 scm_sym_enter_frame,
2000 t.arg1,
2001 tail,
2002 scm_unmemocopy (x, env));
2003 SCM_TRAPS_P = 1;
2004 }
2005 #endif
2006 #if defined (USE_THREADS) || defined (DEVAL)
2007 dispatch:
2008 #endif
2009 SCM_TICK;
2010 switch (SCM_TYP7 (x))
2011 {
2012 case scm_tc7_symbol:
2013 /* Only happens when called at top level. */
2014 x = scm_cons (x, SCM_UNDEFINED);
2015 RETURN (*scm_lookupcar (x, env, 1));
2016
2017 case SCM_BIT8(SCM_IM_AND):
2018 x = SCM_CDR (x);
2019 while (!SCM_NULLP (SCM_CDR (x)))
2020 {
2021 if (SCM_FALSEP (EVALCAR (x, env)))
2022 RETURN (SCM_BOOL_F);
2023 else
2024 x = SCM_CDR (x);
2025 }
2026 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2027 goto carloop;
2028
2029 case SCM_BIT8(SCM_IM_BEGIN):
2030 if (SCM_NULLP (SCM_CDR (x)))
2031 RETURN (SCM_UNSPECIFIED);
2032
2033 /* (currently unused)
2034 cdrxnoap: */
2035 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2036 /* (currently unused)
2037 cdrxbegin: */
2038 x = SCM_CDR (x);
2039
2040 begin:
2041 /* If we are on toplevel with a lookup closure, we need to sync
2042 with the current module. */
2043 if (SCM_CONSP (env) && !SCM_CONSP (SCM_CAR (env)))
2044 {
2045 UPDATE_TOPLEVEL_ENV (env);
2046 while (!SCM_NULLP (SCM_CDR (x)))
2047 {
2048 EVALCAR (x, env);
2049 UPDATE_TOPLEVEL_ENV (env);
2050 x = SCM_CDR (x);
2051 }
2052 goto carloop;
2053 }
2054 else
2055 goto nontoplevel_begin;
2056
2057 nontoplevel_cdrxnoap:
2058 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2059 nontoplevel_cdrxbegin:
2060 x = SCM_CDR (x);
2061 nontoplevel_begin:
2062 while (!SCM_NULLP (SCM_CDR (x)))
2063 {
2064 if (SCM_IMP (SCM_CAR (x)))
2065 {
2066 if (SCM_ISYMP (SCM_CAR (x)))
2067 {
2068 x = scm_m_expand_body (x, env);
2069 goto nontoplevel_begin;
2070 }
2071 else
2072 SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (x));
2073 }
2074 else
2075 SCM_CEVAL (SCM_CAR (x), env);
2076 x = SCM_CDR (x);
2077 }
2078
2079 carloop: /* scm_eval car of last form in list */
2080 if (SCM_IMP (SCM_CAR (x)))
2081 {
2082 x = SCM_CAR (x);
2083 RETURN (SCM_EVALIM (x, env));
2084 }
2085
2086 if (SCM_SYMBOLP (SCM_CAR (x)))
2087 RETURN (*scm_lookupcar (x, env, 1));
2088
2089 x = SCM_CAR (x);
2090 goto loop; /* tail recurse */
2091
2092
2093 case SCM_BIT8(SCM_IM_CASE):
2094 x = SCM_CDR (x);
2095 t.arg1 = EVALCAR (x, env);
2096 while (SCM_NIMP (x = SCM_CDR (x)))
2097 {
2098 proc = SCM_CAR (x);
2099 if (SCM_EQ_P (scm_sym_else, SCM_CAR (proc)))
2100 {
2101 x = SCM_CDR (proc);
2102 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2103 goto begin;
2104 }
2105 proc = SCM_CAR (proc);
2106 while (SCM_NIMP (proc))
2107 {
2108 if (CHECK_EQVISH (SCM_CAR (proc), t.arg1))
2109 {
2110 x = SCM_CDAR (x);
2111 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2112 goto begin;
2113 }
2114 proc = SCM_CDR (proc);
2115 }
2116 }
2117 RETURN (SCM_UNSPECIFIED);
2118
2119
2120 case SCM_BIT8 (SCM_IM_COND):
2121 x = SCM_CDR (x);
2122 while (!SCM_NULLP (x))
2123 {
2124 proc = SCM_CAR (x);
2125 if (SCM_EQ_P (SCM_CAR (proc), scm_sym_else))
2126 {
2127 x = SCM_CDR (proc);
2128 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2129 goto begin;
2130 }
2131 t.arg1 = EVALCAR (proc, env);
2132 if (!SCM_FALSEP (t.arg1))
2133 {
2134 x = SCM_CDR (proc);
2135 if (SCM_NULLP (x))
2136 RETURN (t.arg1);
2137 if (!SCM_EQ_P (scm_sym_arrow, SCM_CAR (x)))
2138 {
2139 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2140 goto begin;
2141 }
2142 proc = SCM_CDR (x);
2143 proc = EVALCAR (proc, env);
2144 SCM_ASRTGO (SCM_NIMP (proc), badfun);
2145 PREP_APPLY (proc, scm_list_1 (t.arg1));
2146 ENTER_APPLY;
2147 if (SCM_CLOSUREP(proc) && scm_badformalsp (proc, 1))
2148 goto umwrongnumargs;
2149 goto evap1;
2150 }
2151 x = SCM_CDR (x);
2152 }
2153 RETURN (SCM_UNSPECIFIED);
2154
2155
2156 case SCM_BIT8(SCM_IM_DO):
2157 x = SCM_CDR (x);
2158 proc = SCM_CADR (x); /* inits */
2159 t.arg1 = SCM_EOL; /* values */
2160 while (SCM_NIMP (proc))
2161 {
2162 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
2163 proc = SCM_CDR (proc);
2164 }
2165 env = EXTEND_ENV (SCM_CAR (x), t.arg1, env);
2166 x = SCM_CDDR (x);
2167 while (proc = SCM_CAR (x), SCM_FALSEP (EVALCAR (proc, env)))
2168 {
2169 for (proc = SCM_CADR (x); SCM_NIMP (proc); proc = SCM_CDR (proc))
2170 {
2171 t.arg1 = SCM_CAR (proc); /* body */
2172 SIDEVAL (t.arg1, env);
2173 }
2174 for (t.arg1 = SCM_EOL, proc = SCM_CDDR (x);
2175 SCM_NIMP (proc);
2176 proc = SCM_CDR (proc))
2177 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1); /* steps */
2178 env = EXTEND_ENV (SCM_CAAR (env), t.arg1, SCM_CDR (env));
2179 }
2180 x = SCM_CDR (proc);
2181 if (SCM_NULLP (x))
2182 RETURN (SCM_UNSPECIFIED);
2183 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2184 goto nontoplevel_begin;
2185
2186
2187 case SCM_BIT8(SCM_IM_IF):
2188 x = SCM_CDR (x);
2189 if (!SCM_FALSEP (EVALCAR (x, env)))
2190 x = SCM_CDR (x);
2191 else if (SCM_IMP (x = SCM_CDDR (x)))
2192 RETURN (SCM_UNSPECIFIED);
2193 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2194 goto carloop;
2195
2196
2197 case SCM_BIT8(SCM_IM_LET):
2198 x = SCM_CDR (x);
2199 proc = SCM_CADR (x);
2200 t.arg1 = SCM_EOL;
2201 do
2202 {
2203 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
2204 }
2205 while (SCM_NIMP (proc = SCM_CDR (proc)));
2206 env = EXTEND_ENV (SCM_CAR (x), t.arg1, env);
2207 x = SCM_CDR (x);
2208 goto nontoplevel_cdrxnoap;
2209
2210
2211 case SCM_BIT8(SCM_IM_LETREC):
2212 x = SCM_CDR (x);
2213 env = EXTEND_ENV (SCM_CAR (x), scm_undefineds, env);
2214 x = SCM_CDR (x);
2215 proc = SCM_CAR (x);
2216 t.arg1 = SCM_EOL;
2217 do
2218 {
2219 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
2220 }
2221 while (SCM_NIMP (proc = SCM_CDR (proc)));
2222 SCM_SETCDR (SCM_CAR (env), t.arg1);
2223 goto nontoplevel_cdrxnoap;
2224
2225
2226 case SCM_BIT8(SCM_IM_LETSTAR):
2227 x = SCM_CDR (x);
2228 {
2229 SCM bindings = SCM_CAR (x);
2230 if (SCM_NULLP (bindings))
2231 env = EXTEND_ENV (SCM_EOL, SCM_EOL, env);
2232 else
2233 {
2234 do
2235 {
2236 SCM name = SCM_CAR (bindings);
2237 SCM init = SCM_CDR (bindings);
2238 env = EXTEND_ENV (name, EVALCAR (init, env), env);
2239 bindings = SCM_CDR (init);
2240 }
2241 while (!SCM_NULLP (bindings));
2242 }
2243 }
2244 goto nontoplevel_cdrxnoap;
2245
2246
2247 case SCM_BIT8(SCM_IM_OR):
2248 x = SCM_CDR (x);
2249 while (!SCM_NULLP (SCM_CDR (x)))
2250 {
2251 SCM val = EVALCAR (x, env);
2252 if (!SCM_FALSEP (val))
2253 RETURN (val);
2254 else
2255 x = SCM_CDR (x);
2256 }
2257 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2258 goto carloop;
2259
2260
2261 case SCM_BIT8(SCM_IM_LAMBDA):
2262 RETURN (scm_closure (SCM_CDR (x), env));
2263
2264
2265 case SCM_BIT8(SCM_IM_QUOTE):
2266 RETURN (SCM_CADR (x));
2267
2268
2269 case SCM_BIT8(SCM_IM_SET_X):
2270 x = SCM_CDR (x);
2271 proc = SCM_CAR (x);
2272 switch (SCM_ITAG3 (proc))
2273 {
2274 case scm_tc3_cons:
2275 if (SCM_VARIABLEP (proc))
2276 t.lloc = SCM_VARIABLE_LOC (proc);
2277 else
2278 t.lloc = scm_lookupcar (x, env, 1);
2279 break;
2280 #ifdef MEMOIZE_LOCALS
2281 case scm_tc3_imm24:
2282 t.lloc = scm_ilookup (proc, env);
2283 break;
2284 #endif
2285 }
2286 x = SCM_CDR (x);
2287 *t.lloc = EVALCAR (x, env);
2288 #ifdef SICP
2289 RETURN (*t.lloc);
2290 #else
2291 RETURN (SCM_UNSPECIFIED);
2292 #endif
2293
2294
2295 case SCM_BIT8(SCM_IM_DEFINE): /* only for internal defines */
2296 scm_misc_error (NULL, "Bad define placement", SCM_EOL);
2297
2298 /* new syntactic forms go here. */
2299 case SCM_BIT8(SCM_MAKISYM (0)):
2300 proc = SCM_CAR (x);
2301 SCM_ASRTGO (SCM_ISYMP (proc), badfun);
2302 switch (SCM_ISYMNUM (proc))
2303 {
2304 case (SCM_ISYMNUM (SCM_IM_APPLY)):
2305 proc = SCM_CDR (x);
2306 proc = EVALCAR (proc, env);
2307 SCM_ASRTGO (SCM_NIMP (proc), badfun);
2308 if (SCM_CLOSUREP (proc))
2309 {
2310 SCM argl, tl;
2311 PREP_APPLY (proc, SCM_EOL);
2312 t.arg1 = SCM_CDDR (x);
2313 t.arg1 = EVALCAR (t.arg1, env);
2314 apply_closure:
2315 /* Go here to tail-call a closure. PROC is the closure
2316 and T.ARG1 is the list of arguments. Do not forget to
2317 call PREP_APPLY. */
2318 #ifdef DEVAL
2319 debug.info->a.args = t.arg1;
2320 #endif
2321 #ifndef SCM_RECKLESS
2322 if (scm_badargsp (SCM_CLOSURE_FORMALS (proc), t.arg1))
2323 goto wrongnumargs;
2324 #endif
2325 ENTER_APPLY;
2326 /* Copy argument list */
2327 if (SCM_IMP (t.arg1))
2328 argl = t.arg1;
2329 else
2330 {
2331 argl = tl = scm_cons (SCM_CAR (t.arg1), SCM_UNSPECIFIED);
2332 while (SCM_NIMP (t.arg1 = SCM_CDR (t.arg1))
2333 && SCM_CONSP (t.arg1))
2334 {
2335 SCM_SETCDR (tl, scm_cons (SCM_CAR (t.arg1),
2336 SCM_UNSPECIFIED));
2337 tl = SCM_CDR (tl);
2338 }
2339 SCM_SETCDR (tl, t.arg1);
2340 }
2341
2342 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), argl, SCM_ENV (proc));
2343 x = SCM_CLOSURE_BODY (proc);
2344 goto nontoplevel_begin;
2345 }
2346 proc = scm_f_apply;
2347 goto evapply;
2348
2349 case (SCM_ISYMNUM (SCM_IM_CONT)):
2350 {
2351 int first;
2352 SCM val = scm_make_continuation (&first);
2353
2354 if (first)
2355 t.arg1 = val;
2356 else
2357 RETURN (val);
2358 }
2359 proc = SCM_CDR (x);
2360 proc = scm_eval_car (proc, env);
2361 SCM_ASRTGO (SCM_NIMP (proc), badfun);
2362 PREP_APPLY (proc, scm_list_1 (t.arg1));
2363 ENTER_APPLY;
2364 if (SCM_CLOSUREP(proc) && scm_badformalsp (proc, 1))
2365 goto umwrongnumargs;
2366 goto evap1;
2367
2368 case (SCM_ISYMNUM (SCM_IM_DELAY)):
2369 RETURN (scm_makprom (scm_closure (SCM_CDR (x), env)));
2370
2371 case (SCM_ISYMNUM (SCM_IM_DISPATCH)):
2372 proc = SCM_CADR (x); /* unevaluated operands */
2373 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2374 if (SCM_IMP (proc))
2375 arg2 = *scm_ilookup (proc, env);
2376 else if (!SCM_CONSP (proc))
2377 {
2378 if (SCM_VARIABLEP (proc))
2379 arg2 = SCM_VARIABLE_REF (proc);
2380 else
2381 arg2 = *scm_lookupcar (SCM_CDR (x), env, 1);
2382 }
2383 else
2384 {
2385 arg2 = scm_list_1 (EVALCAR (proc, env));
2386 t.lloc = SCM_CDRLOC (arg2);
2387 while (SCM_NIMP (proc = SCM_CDR (proc)))
2388 {
2389 *t.lloc = scm_list_1 (EVALCAR (proc, env));
2390 t.lloc = SCM_CDRLOC (*t.lloc);
2391 }
2392 }
2393
2394 type_dispatch:
2395 /* The type dispatch code is duplicated here
2396 * (c.f. objects.c:scm_mcache_compute_cmethod) since that
2397 * cuts down execution time for type dispatch to 50%.
2398 */
2399 {
2400 long i, n, end, mask;
2401 SCM z = SCM_CDDR (x);
2402 n = SCM_INUM (SCM_CAR (z)); /* maximum number of specializers */
2403 proc = SCM_CADR (z);
2404
2405 if (SCM_NIMP (proc))
2406 {
2407 /* Prepare for linear search */
2408 mask = -1;
2409 i = 0;
2410 end = SCM_VECTOR_LENGTH (proc);
2411 }
2412 else
2413 {
2414 /* Compute a hash value */
2415 long hashset = SCM_INUM (proc);
2416 long j = n;
2417 z = SCM_CDDR (z);
2418 mask = SCM_INUM (SCM_CAR (z));
2419 proc = SCM_CADR (z);
2420 i = 0;
2421 t.arg1 = arg2;
2422 if (SCM_NIMP (t.arg1))
2423 do
2424 {
2425 i += SCM_STRUCT_DATA (scm_class_of (SCM_CAR (t.arg1)))
2426 [scm_si_hashsets + hashset];
2427 t.arg1 = SCM_CDR (t.arg1);
2428 }
2429 while (j-- && SCM_NIMP (t.arg1));
2430 i &= mask;
2431 end = i;
2432 }
2433
2434 /* Search for match */
2435 do
2436 {
2437 long j = n;
2438 z = SCM_VELTS (proc)[i];
2439 t.arg1 = arg2; /* list of arguments */
2440 if (SCM_NIMP (t.arg1))
2441 do
2442 {
2443 /* More arguments than specifiers => CLASS != ENV */
2444 if (! SCM_EQ_P (scm_class_of (SCM_CAR (t.arg1)), SCM_CAR (z)))
2445 goto next_method;
2446 t.arg1 = SCM_CDR (t.arg1);
2447 z = SCM_CDR (z);
2448 }
2449 while (j-- && SCM_NIMP (t.arg1));
2450 /* Fewer arguments than specifiers => CAR != ENV */
2451 if (!(SCM_IMP (SCM_CAR (z)) || SCM_CONSP (SCM_CAR (z))))
2452 goto next_method;
2453 apply_cmethod:
2454 env = EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (z)),
2455 arg2,
2456 SCM_CMETHOD_ENV (z));
2457 x = SCM_CMETHOD_CODE (z);
2458 goto nontoplevel_cdrxbegin;
2459 next_method:
2460 i = (i + 1) & mask;
2461 } while (i != end);
2462
2463 z = scm_memoize_method (x, arg2);
2464 goto apply_cmethod;
2465 }
2466
2467 case (SCM_ISYMNUM (SCM_IM_SLOT_REF)):
2468 x = SCM_CDR (x);
2469 t.arg1 = EVALCAR (x, env);
2470 RETURN (SCM_PACK (SCM_STRUCT_DATA (t.arg1) [SCM_INUM (SCM_CADR (x))]));
2471
2472 case (SCM_ISYMNUM (SCM_IM_SLOT_SET_X)):
2473 x = SCM_CDR (x);
2474 t.arg1 = EVALCAR (x, env);
2475 x = SCM_CDR (x);
2476 proc = SCM_CDR (x);
2477 SCM_STRUCT_DATA (t.arg1) [SCM_INUM (SCM_CAR (x))]
2478 = SCM_UNPACK (EVALCAR (proc, env));
2479 RETURN (SCM_UNSPECIFIED);
2480
2481 case (SCM_ISYMNUM (SCM_IM_NIL_COND)):
2482 proc = SCM_CDR (x);
2483 while (SCM_NIMP (x = SCM_CDR (proc)))
2484 {
2485 if (!(SCM_FALSEP (t.arg1 = EVALCAR (proc, env))
2486 || SCM_EQ_P (t.arg1, scm_lisp_nil)))
2487 {
2488 if (SCM_EQ_P (SCM_CAR (x), SCM_UNSPECIFIED))
2489 RETURN (t.arg1);
2490 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2491 goto carloop;
2492 }
2493 proc = SCM_CDR (x);
2494 }
2495 x = proc;
2496 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2497 goto carloop;
2498
2499 case (SCM_ISYMNUM (SCM_IM_NIL_IFY)):
2500 x = SCM_CDR (x);
2501 RETURN ((SCM_FALSEP (proc = EVALCAR (x, env)) || SCM_NULLP (proc))
2502 ? scm_lisp_nil
2503 : proc);
2504
2505 case (SCM_ISYMNUM (SCM_IM_T_IFY)):
2506 x = SCM_CDR (x);
2507 RETURN (!SCM_FALSEP (EVALCAR (x, env)) ? scm_lisp_t : scm_lisp_nil);
2508
2509 case (SCM_ISYMNUM (SCM_IM_0_COND)):
2510 proc = SCM_CDR (x);
2511 while (SCM_NIMP (x = SCM_CDR (proc)))
2512 {
2513 if (!(SCM_FALSEP (t.arg1 = EVALCAR (proc, env))
2514 || SCM_EQ_P (t.arg1, SCM_INUM0)))
2515 {
2516 if (SCM_EQ_P (SCM_CAR (x), SCM_UNSPECIFIED))
2517 RETURN (t.arg1);
2518 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2519 goto carloop;
2520 }
2521 proc = SCM_CDR (x);
2522 }
2523 x = proc;
2524 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2525 goto carloop;
2526
2527 case (SCM_ISYMNUM (SCM_IM_0_IFY)):
2528 x = SCM_CDR (x);
2529 RETURN (SCM_FALSEP (proc = EVALCAR (x, env))
2530 ? SCM_INUM0
2531 : proc);
2532
2533 case (SCM_ISYMNUM (SCM_IM_1_IFY)):
2534 x = SCM_CDR (x);
2535 RETURN (!SCM_FALSEP (EVALCAR (x, env))
2536 ? SCM_MAKINUM (1)
2537 : SCM_INUM0);
2538
2539 case (SCM_ISYMNUM (SCM_IM_BIND)):
2540 {
2541 SCM vars, exps, vals;
2542
2543 x = SCM_CDR (x);
2544 vars = SCM_CAAR (x);
2545 exps = SCM_CDAR (x);
2546
2547 vals = SCM_EOL;
2548
2549 while (SCM_NIMP (exps))
2550 {
2551 vals = scm_cons (EVALCAR (exps, env), vals);
2552 exps = SCM_CDR (exps);
2553 }
2554
2555 scm_swap_bindings (vars, vals);
2556 scm_dynwinds = scm_acons (vars, vals, scm_dynwinds);
2557
2558 arg2 = x = SCM_CDR (x);
2559 while (!SCM_NULLP (arg2 = SCM_CDR (arg2)))
2560 {
2561 SIDEVAL (SCM_CAR (x), env);
2562 x = arg2;
2563 }
2564 proc = EVALCAR (x, env);
2565
2566 scm_dynwinds = SCM_CDR (scm_dynwinds);
2567 scm_swap_bindings (vars, vals);
2568
2569 RETURN (proc);
2570 }
2571
2572 case (SCM_ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
2573 {
2574 proc = SCM_CDR (x);
2575 x = EVALCAR (proc, env);
2576 proc = SCM_CDR (proc);
2577 proc = EVALCAR (proc, env);
2578 t.arg1 = SCM_APPLY (x, SCM_EOL, SCM_EOL);
2579 if (SCM_VALUESP (t.arg1))
2580 t.arg1 = scm_struct_ref (t.arg1, SCM_INUM0);
2581 else
2582 t.arg1 = scm_list_1 (t.arg1);
2583 if (SCM_CLOSUREP (proc))
2584 {
2585 PREP_APPLY (proc, t.arg1);
2586 goto apply_closure;
2587 }
2588 return SCM_APPLY (proc, t.arg1, SCM_EOL);
2589 }
2590
2591 default:
2592 goto badfun;
2593 }
2594
2595 default:
2596 proc = x;
2597 badfun:
2598 scm_misc_error (NULL, "Wrong type to apply: ~S", scm_list_1 (proc));
2599 case scm_tc7_vector:
2600 case scm_tc7_wvect:
2601 #ifdef HAVE_ARRAYS
2602 case scm_tc7_bvect:
2603 case scm_tc7_byvect:
2604 case scm_tc7_svect:
2605 case scm_tc7_ivect:
2606 case scm_tc7_uvect:
2607 case scm_tc7_fvect:
2608 case scm_tc7_dvect:
2609 case scm_tc7_cvect:
2610 #ifdef HAVE_LONG_LONGS
2611 case scm_tc7_llvect:
2612 #endif
2613 #endif
2614 case scm_tc7_string:
2615 case scm_tc7_smob:
2616 case scm_tcs_closures:
2617 case scm_tc7_cclo:
2618 case scm_tc7_pws:
2619 case scm_tcs_subrs:
2620 case scm_tcs_struct:
2621 RETURN (x);
2622
2623 case scm_tc7_variable:
2624 RETURN (SCM_VARIABLE_REF(x));
2625
2626 #ifdef MEMOIZE_LOCALS
2627 case SCM_BIT8(SCM_ILOC00):
2628 proc = *scm_ilookup (SCM_CAR (x), env);
2629 SCM_ASRTGO (SCM_NIMP (proc), badfun);
2630 #ifndef SCM_RECKLESS
2631 #ifdef SCM_CAUTIOUS
2632 goto checkargs;
2633 #endif
2634 #endif
2635 break;
2636 #endif /* ifdef MEMOIZE_LOCALS */
2637
2638 case scm_tcs_cons_nimcar:
2639 orig_sym = SCM_CAR (x);
2640 if (SCM_SYMBOLP (orig_sym))
2641 {
2642 #ifdef USE_THREADS
2643 t.lloc = scm_lookupcar1 (x, env, 1);
2644 if (t.lloc == NULL)
2645 {
2646 /* we have lost the race, start again. */
2647 goto dispatch;
2648 }
2649 proc = *t.lloc;
2650 #else
2651 proc = *scm_lookupcar (x, env, 1);
2652 #endif
2653
2654 if (SCM_IMP (proc))
2655 {
2656 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
2657 lookupcar */
2658 goto badfun;
2659 }
2660 if (SCM_MACROP (proc))
2661 {
2662 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
2663 lookupcar */
2664 handle_a_macro:
2665 #ifdef DEVAL
2666 /* Set a flag during macro expansion so that macro
2667 application frames can be deleted from the backtrace. */
2668 SCM_SET_MACROEXP (debug);
2669 #endif
2670 t.arg1 = SCM_APPLY (SCM_MACRO_CODE (proc), x,
2671 scm_cons (env, scm_listofnull));
2672
2673 #ifdef DEVAL
2674 SCM_CLEAR_MACROEXP (debug);
2675 #endif
2676 switch (SCM_MACRO_TYPE (proc))
2677 {
2678 case 2:
2679 if (scm_ilength (t.arg1) <= 0)
2680 t.arg1 = scm_list_2 (SCM_IM_BEGIN, t.arg1);
2681 #ifdef DEVAL
2682 if (!SCM_CLOSUREP (SCM_MACRO_CODE (proc)))
2683 {
2684 SCM_DEFER_INTS;
2685 SCM_SETCAR (x, SCM_CAR (t.arg1));
2686 SCM_SETCDR (x, SCM_CDR (t.arg1));
2687 SCM_ALLOW_INTS;
2688 goto dispatch;
2689 }
2690 /* Prevent memoizing of debug info expression. */
2691 debug.info->e.exp = scm_cons_source (debug.info->e.exp,
2692 SCM_CAR (x),
2693 SCM_CDR (x));
2694 #endif
2695 SCM_DEFER_INTS;
2696 SCM_SETCAR (x, SCM_CAR (t.arg1));
2697 SCM_SETCDR (x, SCM_CDR (t.arg1));
2698 SCM_ALLOW_INTS;
2699 goto loopnoap;
2700 case 1:
2701 if (SCM_NIMP (x = t.arg1))
2702 goto loopnoap;
2703 case 0:
2704 RETURN (t.arg1);
2705 }
2706 }
2707 }
2708 else
2709 proc = SCM_CEVAL (SCM_CAR (x), env);
2710 SCM_ASRTGO (!SCM_IMP (proc), badfun);
2711 #ifndef SCM_RECKLESS
2712 #ifdef SCM_CAUTIOUS
2713 checkargs:
2714 #endif
2715 if (SCM_CLOSUREP (proc))
2716 {
2717 arg2 = SCM_CLOSURE_FORMALS (proc);
2718 t.arg1 = SCM_CDR (x);
2719 while (!SCM_NULLP (arg2))
2720 {
2721 if (!SCM_CONSP (arg2))
2722 goto evapply;
2723 if (SCM_IMP (t.arg1))
2724 goto umwrongnumargs;
2725 arg2 = SCM_CDR (arg2);
2726 t.arg1 = SCM_CDR (t.arg1);
2727 }
2728 if (!SCM_NULLP (t.arg1))
2729 goto umwrongnumargs;
2730 }
2731 else if (SCM_MACROP (proc))
2732 goto handle_a_macro;
2733 #endif
2734 }
2735
2736
2737 evapply:
2738 PREP_APPLY (proc, SCM_EOL);
2739 if (SCM_NULLP (SCM_CDR (x))) {
2740 ENTER_APPLY;
2741 evap0:
2742 switch (SCM_TYP7 (proc))
2743 { /* no arguments given */
2744 case scm_tc7_subr_0:
2745 RETURN (SCM_SUBRF (proc) ());
2746 case scm_tc7_subr_1o:
2747 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
2748 case scm_tc7_lsubr:
2749 RETURN (SCM_SUBRF (proc) (SCM_EOL));
2750 case scm_tc7_rpsubr:
2751 RETURN (SCM_BOOL_T);
2752 case scm_tc7_asubr:
2753 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
2754 case scm_tc7_smob:
2755 if (!SCM_SMOB_APPLICABLE_P (proc))
2756 goto badfun;
2757 RETURN (SCM_SMOB_APPLY_0 (proc));
2758 case scm_tc7_cclo:
2759 t.arg1 = proc;
2760 proc = SCM_CCLO_SUBR (proc);
2761 #ifdef DEVAL
2762 debug.info->a.proc = proc;
2763 debug.info->a.args = scm_list_1 (t.arg1);
2764 #endif
2765 goto evap1;
2766 case scm_tc7_pws:
2767 proc = SCM_PROCEDURE (proc);
2768 #ifdef DEVAL
2769 debug.info->a.proc = proc;
2770 #endif
2771 if (!SCM_CLOSUREP (proc))
2772 goto evap0;
2773 if (scm_badformalsp (proc, 0))
2774 goto umwrongnumargs;
2775 case scm_tcs_closures:
2776 x = SCM_CLOSURE_BODY (proc);
2777 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), SCM_EOL, SCM_ENV (proc));
2778 goto nontoplevel_begin;
2779 case scm_tcs_struct:
2780 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
2781 {
2782 x = SCM_ENTITY_PROCEDURE (proc);
2783 arg2 = SCM_EOL;
2784 goto type_dispatch;
2785 }
2786 else if (!SCM_I_OPERATORP (proc))
2787 goto badfun;
2788 else
2789 {
2790 t.arg1 = proc;
2791 proc = (SCM_I_ENTITYP (proc)
2792 ? SCM_ENTITY_PROCEDURE (proc)
2793 : SCM_OPERATOR_PROCEDURE (proc));
2794 #ifdef DEVAL
2795 debug.info->a.proc = proc;
2796 debug.info->a.args = scm_list_1 (t.arg1);
2797 #endif
2798 if (SCM_NIMP (proc))
2799 goto evap1;
2800 else
2801 goto badfun;
2802 }
2803 case scm_tc7_subr_1:
2804 case scm_tc7_subr_2:
2805 case scm_tc7_subr_2o:
2806 case scm_tc7_cxr:
2807 case scm_tc7_subr_3:
2808 case scm_tc7_lsubr_2:
2809 umwrongnumargs:
2810 unmemocar (x, env);
2811 wrongnumargs:
2812 scm_wrong_num_args (proc);
2813 default:
2814 /* handle macros here */
2815 goto badfun;
2816 }
2817 }
2818
2819 /* must handle macros by here */
2820 x = SCM_CDR (x);
2821 #ifdef SCM_CAUTIOUS
2822 if (SCM_IMP (x))
2823 goto wrongnumargs;
2824 else if (SCM_CONSP (x))
2825 {
2826 if (SCM_IMP (SCM_CAR (x)))
2827 t.arg1 = SCM_EVALIM (SCM_CAR (x), env);
2828 else
2829 t.arg1 = EVALCELLCAR (x, env);
2830 }
2831 else
2832 goto wrongnumargs;
2833 #else
2834 t.arg1 = EVALCAR (x, env);
2835 #endif
2836 #ifdef DEVAL
2837 debug.info->a.args = scm_list_1 (t.arg1);
2838 #endif
2839 x = SCM_CDR (x);
2840 if (SCM_NULLP (x))
2841 {
2842 ENTER_APPLY;
2843 evap1:
2844 switch (SCM_TYP7 (proc))
2845 { /* have one argument in t.arg1 */
2846 case scm_tc7_subr_2o:
2847 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
2848 case scm_tc7_subr_1:
2849 case scm_tc7_subr_1o:
2850 RETURN (SCM_SUBRF (proc) (t.arg1));
2851 case scm_tc7_cxr:
2852 if (SCM_SUBRF (proc))
2853 {
2854 if (SCM_INUMP (t.arg1))
2855 {
2856 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (t.arg1))));
2857 }
2858 else if (SCM_REALP (t.arg1))
2859 {
2860 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (t.arg1))));
2861 }
2862 #ifdef SCM_BIGDIG
2863 else if (SCM_BIGP (t.arg1))
2864 {
2865 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (t.arg1))));
2866 }
2867 #endif
2868 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), t.arg1,
2869 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
2870 }
2871 proc = SCM_SNAME (proc);
2872 {
2873 char *chrs = SCM_SYMBOL_CHARS (proc) + SCM_SYMBOL_LENGTH (proc) - 1;
2874 while ('c' != *--chrs)
2875 {
2876 SCM_ASSERT (SCM_CONSP (t.arg1),
2877 t.arg1, SCM_ARG1, SCM_SYMBOL_CHARS (proc));
2878 t.arg1 = ('a' == *chrs) ? SCM_CAR (t.arg1) : SCM_CDR (t.arg1);
2879 }
2880 RETURN (t.arg1);
2881 }
2882 case scm_tc7_rpsubr:
2883 RETURN (SCM_BOOL_T);
2884 case scm_tc7_asubr:
2885 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
2886 case scm_tc7_lsubr:
2887 #ifdef DEVAL
2888 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
2889 #else
2890 RETURN (SCM_SUBRF (proc) (scm_list_1 (t.arg1)));
2891 #endif
2892 case scm_tc7_smob:
2893 if (!SCM_SMOB_APPLICABLE_P (proc))
2894 goto badfun;
2895 RETURN (SCM_SMOB_APPLY_1 (proc, t.arg1));
2896 case scm_tc7_cclo:
2897 arg2 = t.arg1;
2898 t.arg1 = proc;
2899 proc = SCM_CCLO_SUBR (proc);
2900 #ifdef DEVAL
2901 debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
2902 debug.info->a.proc = proc;
2903 #endif
2904 goto evap2;
2905 case scm_tc7_pws:
2906 proc = SCM_PROCEDURE (proc);
2907 #ifdef DEVAL
2908 debug.info->a.proc = proc;
2909 #endif
2910 if (!SCM_CLOSUREP (proc))
2911 goto evap1;
2912 if (scm_badformalsp (proc, 1))
2913 goto umwrongnumargs;
2914 case scm_tcs_closures:
2915 /* clos1: */
2916 x = SCM_CLOSURE_BODY (proc);
2917 #ifdef DEVAL
2918 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), debug.info->a.args, SCM_ENV (proc));
2919 #else
2920 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), scm_list_1 (t.arg1), SCM_ENV (proc));
2921 #endif
2922 goto nontoplevel_begin;
2923 case scm_tcs_struct:
2924 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
2925 {
2926 x = SCM_ENTITY_PROCEDURE (proc);
2927 #ifdef DEVAL
2928 arg2 = debug.info->a.args;
2929 #else
2930 arg2 = scm_list_1 (t.arg1);
2931 #endif
2932 goto type_dispatch;
2933 }
2934 else if (!SCM_I_OPERATORP (proc))
2935 goto badfun;
2936 else
2937 {
2938 arg2 = t.arg1;
2939 t.arg1 = proc;
2940 proc = (SCM_I_ENTITYP (proc)
2941 ? SCM_ENTITY_PROCEDURE (proc)
2942 : SCM_OPERATOR_PROCEDURE (proc));
2943 #ifdef DEVAL
2944 debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
2945 debug.info->a.proc = proc;
2946 #endif
2947 if (SCM_NIMP (proc))
2948 goto evap2;
2949 else
2950 goto badfun;
2951 }
2952 case scm_tc7_subr_2:
2953 case scm_tc7_subr_0:
2954 case scm_tc7_subr_3:
2955 case scm_tc7_lsubr_2:
2956 goto wrongnumargs;
2957 default:
2958 goto badfun;
2959 }
2960 }
2961 #ifdef SCM_CAUTIOUS
2962 if (SCM_IMP (x))
2963 goto wrongnumargs;
2964 else if (SCM_CONSP (x))
2965 {
2966 if (SCM_IMP (SCM_CAR (x)))
2967 arg2 = SCM_EVALIM (SCM_CAR (x), env);
2968 else
2969 arg2 = EVALCELLCAR (x, env);
2970 }
2971 else
2972 goto wrongnumargs;
2973 #else
2974 arg2 = EVALCAR (x, env);
2975 #endif
2976 { /* have two or more arguments */
2977 #ifdef DEVAL
2978 debug.info->a.args = scm_list_2 (t.arg1, arg2);
2979 #endif
2980 x = SCM_CDR (x);
2981 if (SCM_NULLP (x)) {
2982 ENTER_APPLY;
2983 evap2:
2984 switch (SCM_TYP7 (proc))
2985 { /* have two arguments */
2986 case scm_tc7_subr_2:
2987 case scm_tc7_subr_2o:
2988 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
2989 case scm_tc7_lsubr:
2990 #ifdef DEVAL
2991 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
2992 #else
2993 RETURN (SCM_SUBRF (proc) (scm_list_2 (t.arg1, arg2)));
2994 #endif
2995 case scm_tc7_lsubr_2:
2996 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_EOL));
2997 case scm_tc7_rpsubr:
2998 case scm_tc7_asubr:
2999 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
3000 case scm_tc7_smob:
3001 if (!SCM_SMOB_APPLICABLE_P (proc))
3002 goto badfun;
3003 RETURN (SCM_SMOB_APPLY_2 (proc, t.arg1, arg2));
3004 cclon:
3005 case scm_tc7_cclo:
3006 #ifdef DEVAL
3007 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
3008 scm_cons (proc, debug.info->a.args),
3009 SCM_EOL));
3010 #else
3011 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
3012 scm_cons2 (proc, t.arg1,
3013 scm_cons (arg2,
3014 scm_eval_args (x,
3015 env,
3016 proc))),
3017 SCM_EOL));
3018 #endif
3019 case scm_tcs_struct:
3020 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3021 {
3022 x = SCM_ENTITY_PROCEDURE (proc);
3023 #ifdef DEVAL
3024 arg2 = debug.info->a.args;
3025 #else
3026 arg2 = scm_list_2 (t.arg1, arg2);
3027 #endif
3028 goto type_dispatch;
3029 }
3030 else if (!SCM_I_OPERATORP (proc))
3031 goto badfun;
3032 else
3033 {
3034 operatorn:
3035 #ifdef DEVAL
3036 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
3037 ? SCM_ENTITY_PROCEDURE (proc)
3038 : SCM_OPERATOR_PROCEDURE (proc),
3039 scm_cons (proc, debug.info->a.args),
3040 SCM_EOL));
3041 #else
3042 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
3043 ? SCM_ENTITY_PROCEDURE (proc)
3044 : SCM_OPERATOR_PROCEDURE (proc),
3045 scm_cons2 (proc, t.arg1,
3046 scm_cons (arg2,
3047 scm_eval_args (x,
3048 env,
3049 proc))),
3050 SCM_EOL));
3051 #endif
3052 }
3053 case scm_tc7_subr_0:
3054 case scm_tc7_cxr:
3055 case scm_tc7_subr_1o:
3056 case scm_tc7_subr_1:
3057 case scm_tc7_subr_3:
3058 goto wrongnumargs;
3059 default:
3060 goto badfun;
3061 case scm_tc7_pws:
3062 proc = SCM_PROCEDURE (proc);
3063 #ifdef DEVAL
3064 debug.info->a.proc = proc;
3065 #endif
3066 if (!SCM_CLOSUREP (proc))
3067 goto evap2;
3068 if (scm_badformalsp (proc, 2))
3069 goto umwrongnumargs;
3070 case scm_tcs_closures:
3071 /* clos2: */
3072 #ifdef DEVAL
3073 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3074 debug.info->a.args,
3075 SCM_ENV (proc));
3076 #else
3077 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3078 scm_list_2 (t.arg1, arg2), SCM_ENV (proc));
3079 #endif
3080 x = SCM_CLOSURE_BODY (proc);
3081 goto nontoplevel_begin;
3082 }
3083 }
3084 #ifdef SCM_CAUTIOUS
3085 if (SCM_IMP (x) || !SCM_CONSP (x))
3086 goto wrongnumargs;
3087 #endif
3088 #ifdef DEVAL
3089 debug.info->a.args = scm_cons2 (t.arg1, arg2,
3090 deval_args (x, env, proc, SCM_CDRLOC (SCM_CDR (debug.info->a.args))));
3091 #endif
3092 ENTER_APPLY;
3093 evap3:
3094 switch (SCM_TYP7 (proc))
3095 { /* have 3 or more arguments */
3096 #ifdef DEVAL
3097 case scm_tc7_subr_3:
3098 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
3099 RETURN (SCM_SUBRF (proc) (t.arg1, arg2,
3100 SCM_CADDR (debug.info->a.args)));
3101 case scm_tc7_asubr:
3102 #ifdef BUILTIN_RPASUBR
3103 t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
3104 arg2 = SCM_CDDR (debug.info->a.args);
3105 do
3106 {
3107 t.arg1 = SCM_SUBRF(proc)(t.arg1, SCM_CAR (arg2));
3108 arg2 = SCM_CDR (arg2);
3109 }
3110 while (SCM_NIMP (arg2));
3111 RETURN (t.arg1);
3112 #endif /* BUILTIN_RPASUBR */
3113 case scm_tc7_rpsubr:
3114 #ifdef BUILTIN_RPASUBR
3115 if (SCM_FALSEP (SCM_SUBRF (proc) (t.arg1, arg2)))
3116 RETURN (SCM_BOOL_F);
3117 t.arg1 = SCM_CDDR (debug.info->a.args);
3118 do
3119 {
3120 if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, SCM_CAR (t.arg1))))
3121 RETURN (SCM_BOOL_F);
3122 arg2 = SCM_CAR (t.arg1);
3123 t.arg1 = SCM_CDR (t.arg1);
3124 }
3125 while (SCM_NIMP (t.arg1));
3126 RETURN (SCM_BOOL_T);
3127 #else /* BUILTIN_RPASUBR */
3128 RETURN (SCM_APPLY (proc, t.arg1,
3129 scm_acons (arg2,
3130 SCM_CDDR (debug.info->a.args),
3131 SCM_EOL)));
3132 #endif /* BUILTIN_RPASUBR */
3133 case scm_tc7_lsubr_2:
3134 RETURN (SCM_SUBRF (proc) (t.arg1, arg2,
3135 SCM_CDDR (debug.info->a.args)));
3136 case scm_tc7_lsubr:
3137 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
3138 case scm_tc7_smob:
3139 if (!SCM_SMOB_APPLICABLE_P (proc))
3140 goto badfun;
3141 RETURN (SCM_SMOB_APPLY_3 (proc, t.arg1, arg2,
3142 SCM_CDDR (debug.info->a.args)));
3143 case scm_tc7_cclo:
3144 goto cclon;
3145 case scm_tc7_pws:
3146 proc = SCM_PROCEDURE (proc);
3147 debug.info->a.proc = proc;
3148 if (!SCM_CLOSUREP (proc))
3149 goto evap3;
3150 if (scm_badargsp (SCM_CLOSURE_FORMALS (proc), debug.info->a.args))
3151 goto umwrongnumargs;
3152 case scm_tcs_closures:
3153 SCM_SET_ARGSREADY (debug);
3154 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3155 debug.info->a.args,
3156 SCM_ENV (proc));
3157 x = SCM_CLOSURE_BODY (proc);
3158 goto nontoplevel_begin;
3159 #else /* DEVAL */
3160 case scm_tc7_subr_3:
3161 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
3162 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, EVALCAR (x, env)));
3163 case scm_tc7_asubr:
3164 #ifdef BUILTIN_RPASUBR
3165 t.arg1 = SCM_SUBRF (proc) (t.arg1, arg2);
3166 do
3167 {
3168 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
3169 x = SCM_CDR(x);
3170 }
3171 while (SCM_NIMP (x));
3172 RETURN (t.arg1);
3173 #endif /* BUILTIN_RPASUBR */
3174 case scm_tc7_rpsubr:
3175 #ifdef BUILTIN_RPASUBR
3176 if (SCM_FALSEP (SCM_SUBRF (proc) (t.arg1, arg2)))
3177 RETURN (SCM_BOOL_F);
3178 do
3179 {
3180 t.arg1 = EVALCAR (x, env);
3181 if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, t.arg1)))
3182 RETURN (SCM_BOOL_F);
3183 arg2 = t.arg1;
3184 x = SCM_CDR (x);
3185 }
3186 while (SCM_NIMP (x));
3187 RETURN (SCM_BOOL_T);
3188 #else /* BUILTIN_RPASUBR */
3189 RETURN (SCM_APPLY (proc, t.arg1,
3190 scm_acons (arg2,
3191 scm_eval_args (x, env, proc),
3192 SCM_EOL)));
3193 #endif /* BUILTIN_RPASUBR */
3194 case scm_tc7_lsubr_2:
3195 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, scm_eval_args (x, env, proc)));
3196 case scm_tc7_lsubr:
3197 RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1,
3198 arg2,
3199 scm_eval_args (x, env, proc))));
3200 case scm_tc7_smob:
3201 if (!SCM_SMOB_APPLICABLE_P (proc))
3202 goto badfun;
3203 RETURN (SCM_SMOB_APPLY_3 (proc, t.arg1, arg2,
3204 scm_eval_args (x, env, proc)));
3205 case scm_tc7_cclo:
3206 goto cclon;
3207 case scm_tc7_pws:
3208 proc = SCM_PROCEDURE (proc);
3209 if (!SCM_CLOSUREP (proc))
3210 goto evap3;
3211 {
3212 SCM formals = SCM_CLOSURE_FORMALS (proc);
3213 if (SCM_NULLP (formals)
3214 || (SCM_CONSP (formals)
3215 && (SCM_NULLP (SCM_CDR (formals))
3216 || (SCM_CONSP (SCM_CDR (formals))
3217 && scm_badargsp (SCM_CDDR (formals), x)))))
3218 goto umwrongnumargs;
3219 }
3220 case scm_tcs_closures:
3221 #ifdef DEVAL
3222 SCM_SET_ARGSREADY (debug);
3223 #endif
3224 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3225 scm_cons2 (t.arg1,
3226 arg2,
3227 scm_eval_args (x, env, proc)),
3228 SCM_ENV (proc));
3229 x = SCM_CLOSURE_BODY (proc);
3230 goto nontoplevel_begin;
3231 #endif /* DEVAL */
3232 case scm_tcs_struct:
3233 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3234 {
3235 #ifdef DEVAL
3236 arg2 = debug.info->a.args;
3237 #else
3238 arg2 = scm_cons2 (t.arg1, arg2, scm_eval_args (x, env, proc));
3239 #endif
3240 x = SCM_ENTITY_PROCEDURE (proc);
3241 goto type_dispatch;
3242 }
3243 else if (!SCM_I_OPERATORP (proc))
3244 goto badfun;
3245 else
3246 goto operatorn;
3247 case scm_tc7_subr_2:
3248 case scm_tc7_subr_1o:
3249 case scm_tc7_subr_2o:
3250 case scm_tc7_subr_0:
3251 case scm_tc7_cxr:
3252 case scm_tc7_subr_1:
3253 goto wrongnumargs;
3254 default:
3255 goto badfun;
3256 }
3257 }
3258 #ifdef DEVAL
3259 exit:
3260 if (CHECK_EXIT && SCM_TRAPS_P)
3261 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
3262 {
3263 SCM_CLEAR_TRACED_FRAME (debug);
3264 if (SCM_CHEAPTRAPS_P)
3265 t.arg1 = scm_make_debugobj (&debug);
3266 else
3267 {
3268 int first;
3269 SCM val = scm_make_continuation (&first);
3270
3271 if (first)
3272 t.arg1 = val;
3273 else
3274 {
3275 proc = val;
3276 goto ret;
3277 }
3278 }
3279 SCM_TRAPS_P = 0;
3280 scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, t.arg1, proc);
3281 SCM_TRAPS_P = 1;
3282 }
3283 ret:
3284 scm_last_debug_frame = debug.prev;
3285 return proc;
3286 #endif
3287 }
3288
3289
3290 /* SECTION: This code is compiled once.
3291 */
3292
3293 #ifndef DEVAL
3294
3295 \f
3296 /* Simple procedure calls
3297 */
3298
3299 SCM
3300 scm_call_0 (SCM proc)
3301 {
3302 return scm_apply (proc, SCM_EOL, SCM_EOL);
3303 }
3304
3305 SCM
3306 scm_call_1 (SCM proc, SCM arg1)
3307 {
3308 return scm_apply (proc, arg1, scm_listofnull);
3309 }
3310
3311 SCM
3312 scm_call_2 (SCM proc, SCM arg1, SCM arg2)
3313 {
3314 return scm_apply (proc, arg1, scm_cons (arg2, scm_listofnull));
3315 }
3316
3317 SCM
3318 scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
3319 {
3320 return scm_apply (proc, arg1, scm_cons2 (arg2, arg3, scm_listofnull));
3321 }
3322
3323 SCM
3324 scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
3325 {
3326 return scm_apply (proc, arg1, scm_cons2 (arg2, arg3,
3327 scm_cons (arg4, scm_listofnull)));
3328 }
3329
3330 /* Simple procedure applies
3331 */
3332
3333 SCM
3334 scm_apply_0 (SCM proc, SCM args)
3335 {
3336 return scm_apply (proc, args, SCM_EOL);
3337 }
3338
3339 SCM
3340 scm_apply_1 (SCM proc, SCM arg1, SCM args)
3341 {
3342 return scm_apply (proc, scm_cons (arg1, args), SCM_EOL);
3343 }
3344
3345 SCM
3346 scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
3347 {
3348 return scm_apply (proc, scm_cons2 (arg1, arg2, args), SCM_EOL);
3349 }
3350
3351 SCM
3352 scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
3353 {
3354 return scm_apply (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)),
3355 SCM_EOL);
3356 }
3357
3358 /* This code processes the arguments to apply:
3359
3360 (apply PROC ARG1 ... ARGS)
3361
3362 Given a list (ARG1 ... ARGS), this function conses the ARG1
3363 ... arguments onto the front of ARGS, and returns the resulting
3364 list. Note that ARGS is a list; thus, the argument to this
3365 function is a list whose last element is a list.
3366
3367 Apply calls this function, and applies PROC to the elements of the
3368 result. apply:nconc2last takes care of building the list of
3369 arguments, given (ARG1 ... ARGS).
3370
3371 Rather than do new consing, apply:nconc2last destroys its argument.
3372 On that topic, this code came into my care with the following
3373 beautifully cryptic comment on that topic: "This will only screw
3374 you if you do (scm_apply scm_apply '( ... ))" If you know what
3375 they're referring to, send me a patch to this comment. */
3376
3377 SCM_DEFINE (scm_nconc2last, "apply:nconc2last", 1, 0, 0,
3378 (SCM lst),
3379 "Given a list (@var{arg1} @dots{} @var{args}), this function\n"
3380 "conses the @var{arg1} @dots{} arguments onto the front of\n"
3381 "@var{args}, and returns the resulting list. Note that\n"
3382 "@var{args} is a list; thus, the argument to this function is\n"
3383 "a list whose last element is a list.\n"
3384 "Note: Rather than do new consing, @code{apply:nconc2last}\n"
3385 "destroys its argument, so use with care.")
3386 #define FUNC_NAME s_scm_nconc2last
3387 {
3388 SCM *lloc;
3389 SCM_VALIDATE_NONEMPTYLIST (1,lst);
3390 lloc = &lst;
3391 while (!SCM_NULLP (SCM_CDR (*lloc)))
3392 lloc = SCM_CDRLOC (*lloc);
3393 SCM_ASSERT (scm_ilength (SCM_CAR (*lloc)) >= 0, lst, SCM_ARG1, FUNC_NAME);
3394 *lloc = SCM_CAR (*lloc);
3395 return lst;
3396 }
3397 #undef FUNC_NAME
3398
3399 #endif /* !DEVAL */
3400
3401
3402 /* SECTION: When DEVAL is defined this code yields scm_dapply.
3403 * It is compiled twice.
3404 */
3405
3406 #if 0
3407 SCM
3408 scm_apply (SCM proc, SCM arg1, SCM args)
3409 {}
3410 #endif
3411
3412 #if 0
3413 SCM
3414 scm_dapply (SCM proc, SCM arg1, SCM args)
3415 { /* empty */ }
3416 #endif
3417
3418
3419 /* Apply a function to a list of arguments.
3420
3421 This function is exported to the Scheme level as taking two
3422 required arguments and a tail argument, as if it were:
3423 (lambda (proc arg1 . args) ...)
3424 Thus, if you just have a list of arguments to pass to a procedure,
3425 pass the list as ARG1, and '() for ARGS. If you have some fixed
3426 args, pass the first as ARG1, then cons any remaining fixed args
3427 onto the front of your argument list, and pass that as ARGS. */
3428
3429 SCM
3430 SCM_APPLY (SCM proc, SCM arg1, SCM args)
3431 {
3432 #ifdef DEBUG_EXTENSIONS
3433 #ifdef DEVAL
3434 scm_t_debug_frame debug;
3435 scm_t_debug_info debug_vect_body;
3436 debug.prev = scm_last_debug_frame;
3437 debug.status = SCM_APPLYFRAME;
3438 debug.vect = &debug_vect_body;
3439 debug.vect[0].a.proc = proc;
3440 debug.vect[0].a.args = SCM_EOL;
3441 scm_last_debug_frame = &debug;
3442 #else
3443 if (SCM_DEBUGGINGP)
3444 return scm_dapply (proc, arg1, args);
3445 #endif
3446 #endif
3447
3448 SCM_ASRTGO (SCM_NIMP (proc), badproc);
3449
3450 /* If ARGS is the empty list, then we're calling apply with only two
3451 arguments --- ARG1 is the list of arguments for PROC. Whatever
3452 the case, futz with things so that ARG1 is the first argument to
3453 give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
3454 rest.
3455
3456 Setting the debug apply frame args this way is pretty messy.
3457 Perhaps we should store arg1 and args directly in the frame as
3458 received, and let scm_frame_arguments unpack them, because that's
3459 a relatively rare operation. This works for now; if the Guile
3460 developer archives are still around, see Mikael's post of
3461 11-Apr-97. */
3462 if (SCM_NULLP (args))
3463 {
3464 if (SCM_NULLP (arg1))
3465 {
3466 arg1 = SCM_UNDEFINED;
3467 #ifdef DEVAL
3468 debug.vect[0].a.args = SCM_EOL;
3469 #endif
3470 }
3471 else
3472 {
3473 #ifdef DEVAL
3474 debug.vect[0].a.args = arg1;
3475 #endif
3476 args = SCM_CDR (arg1);
3477 arg1 = SCM_CAR (arg1);
3478 }
3479 }
3480 else
3481 {
3482 args = scm_nconc2last (args);
3483 #ifdef DEVAL
3484 debug.vect[0].a.args = scm_cons (arg1, args);
3485 #endif
3486 }
3487 #ifdef DEVAL
3488 if (SCM_ENTER_FRAME_P && SCM_TRAPS_P)
3489 {
3490 SCM tmp;
3491 if (SCM_CHEAPTRAPS_P)
3492 tmp = scm_make_debugobj (&debug);
3493 else
3494 {
3495 int first;
3496
3497 tmp = scm_make_continuation (&first);
3498 if (!first)
3499 goto entap;
3500 }
3501 SCM_TRAPS_P = 0;
3502 scm_call_2 (SCM_ENTER_FRAME_HDLR, scm_sym_enter_frame, tmp);
3503 SCM_TRAPS_P = 1;
3504 }
3505 entap:
3506 ENTER_APPLY;
3507 #endif
3508 tail:
3509 switch (SCM_TYP7 (proc))
3510 {
3511 case scm_tc7_subr_2o:
3512 args = SCM_NULLP (args) ? SCM_UNDEFINED : SCM_CAR (args);
3513 RETURN (SCM_SUBRF (proc) (arg1, args));
3514 case scm_tc7_subr_2:
3515 SCM_ASRTGO (!SCM_NULLP (args) && SCM_NULLP (SCM_CDR (args)),
3516 wrongnumargs);
3517 args = SCM_CAR (args);
3518 RETURN (SCM_SUBRF (proc) (arg1, args));
3519 case scm_tc7_subr_0:
3520 SCM_ASRTGO (SCM_UNBNDP (arg1), wrongnumargs);
3521 RETURN (SCM_SUBRF (proc) ());
3522 case scm_tc7_subr_1:
3523 SCM_ASRTGO (!SCM_UNBNDP (arg1), wrongnumargs);
3524 case scm_tc7_subr_1o:
3525 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
3526 RETURN (SCM_SUBRF (proc) (arg1));
3527 case scm_tc7_cxr:
3528 SCM_ASRTGO (!SCM_UNBNDP (arg1) && SCM_NULLP (args), wrongnumargs);
3529 if (SCM_SUBRF (proc))
3530 {
3531 if (SCM_INUMP (arg1))
3532 {
3533 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1))));
3534 }
3535 else if (SCM_REALP (arg1))
3536 {
3537 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
3538 }
3539 #ifdef SCM_BIGDIG
3540 else if (SCM_BIGP (arg1))
3541 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
3542 #endif
3543 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
3544 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
3545 }
3546 proc = SCM_SNAME (proc);
3547 {
3548 char *chrs = SCM_SYMBOL_CHARS (proc) + SCM_SYMBOL_LENGTH (proc) - 1;
3549 while ('c' != *--chrs)
3550 {
3551 SCM_ASSERT (SCM_CONSP (arg1),
3552 arg1, SCM_ARG1, SCM_SYMBOL_CHARS (proc));
3553 arg1 = ('a' == *chrs) ? SCM_CAR (arg1) : SCM_CDR (arg1);
3554 }
3555 RETURN (arg1);
3556 }
3557 case scm_tc7_subr_3:
3558 SCM_ASRTGO (!SCM_NULLP (args)
3559 && !SCM_NULLP (SCM_CDR (args))
3560 && SCM_NULLP (SCM_CDDR (args)),
3561 wrongnumargs);
3562 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CADR (args)));
3563 case scm_tc7_lsubr:
3564 #ifdef DEVAL
3565 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args));
3566 #else
3567 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)));
3568 #endif
3569 case scm_tc7_lsubr_2:
3570 SCM_ASRTGO (SCM_CONSP (args), wrongnumargs);
3571 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)));
3572 case scm_tc7_asubr:
3573 if (SCM_NULLP (args))
3574 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
3575 while (SCM_NIMP (args))
3576 {
3577 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
3578 arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
3579 args = SCM_CDR (args);
3580 }
3581 RETURN (arg1);
3582 case scm_tc7_rpsubr:
3583 if (SCM_NULLP (args))
3584 RETURN (SCM_BOOL_T);
3585 while (SCM_NIMP (args))
3586 {
3587 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
3588 if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
3589 RETURN (SCM_BOOL_F);
3590 arg1 = SCM_CAR (args);
3591 args = SCM_CDR (args);
3592 }
3593 RETURN (SCM_BOOL_T);
3594 case scm_tcs_closures:
3595 #ifdef DEVAL
3596 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
3597 #else
3598 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
3599 #endif
3600 #ifndef SCM_RECKLESS
3601 if (scm_badargsp (SCM_CLOSURE_FORMALS (proc), arg1))
3602 goto wrongnumargs;
3603 #endif
3604
3605 /* Copy argument list */
3606 if (SCM_IMP (arg1))
3607 args = arg1;
3608 else
3609 {
3610 SCM tl = args = scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED);
3611 while (arg1 = SCM_CDR (arg1), SCM_CONSP (arg1))
3612 {
3613 SCM_SETCDR (tl, scm_cons (SCM_CAR (arg1),
3614 SCM_UNSPECIFIED));
3615 tl = SCM_CDR (tl);
3616 }
3617 SCM_SETCDR (tl, arg1);
3618 }
3619
3620 args = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), args, SCM_ENV (proc));
3621 proc = SCM_CLOSURE_BODY (proc);
3622 again:
3623 arg1 = proc;
3624 while (!SCM_NULLP (arg1 = SCM_CDR (arg1)))
3625 {
3626 if (SCM_IMP (SCM_CAR (proc)))
3627 {
3628 if (SCM_ISYMP (SCM_CAR (proc)))
3629 {
3630 proc = scm_m_expand_body (proc, args);
3631 goto again;
3632 }
3633 else
3634 SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (proc));
3635 }
3636 else
3637 SCM_CEVAL (SCM_CAR (proc), args);
3638 proc = arg1;
3639 }
3640 RETURN (EVALCAR (proc, args));
3641 case scm_tc7_smob:
3642 if (!SCM_SMOB_APPLICABLE_P (proc))
3643 goto badproc;
3644 if (SCM_UNBNDP (arg1))
3645 RETURN (SCM_SMOB_APPLY_0 (proc));
3646 else if (SCM_NULLP (args))
3647 RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
3648 else if (SCM_NULLP (SCM_CDR (args)))
3649 RETURN (SCM_SMOB_APPLY_2 (proc, arg1, SCM_CAR (args)));
3650 else
3651 RETURN (SCM_SMOB_APPLY_3 (proc, arg1, SCM_CAR (args), SCM_CDR (args)));
3652 case scm_tc7_cclo:
3653 #ifdef DEVAL
3654 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
3655 arg1 = proc;
3656 proc = SCM_CCLO_SUBR (proc);
3657 debug.vect[0].a.proc = proc;
3658 debug.vect[0].a.args = scm_cons (arg1, args);
3659 #else
3660 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
3661 arg1 = proc;
3662 proc = SCM_CCLO_SUBR (proc);
3663 #endif
3664 goto tail;
3665 case scm_tc7_pws:
3666 proc = SCM_PROCEDURE (proc);
3667 #ifdef DEVAL
3668 debug.vect[0].a.proc = proc;
3669 #endif
3670 goto tail;
3671 case scm_tcs_struct:
3672 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3673 {
3674 #ifdef DEVAL
3675 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
3676 #else
3677 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
3678 #endif
3679 RETURN (scm_apply_generic (proc, args));
3680 }
3681 else if (!SCM_I_OPERATORP (proc))
3682 goto badproc;
3683 else
3684 {
3685 #ifdef DEVAL
3686 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
3687 #else
3688 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
3689 #endif
3690 arg1 = proc;
3691 proc = (SCM_I_ENTITYP (proc)
3692 ? SCM_ENTITY_PROCEDURE (proc)
3693 : SCM_OPERATOR_PROCEDURE (proc));
3694 #ifdef DEVAL
3695 debug.vect[0].a.proc = proc;
3696 debug.vect[0].a.args = scm_cons (arg1, args);
3697 #endif
3698 if (SCM_NIMP (proc))
3699 goto tail;
3700 else
3701 goto badproc;
3702 }
3703 wrongnumargs:
3704 scm_wrong_num_args (proc);
3705 default:
3706 badproc:
3707 scm_wrong_type_arg ("apply", SCM_ARG1, proc);
3708 RETURN (arg1);
3709 }
3710 #ifdef DEVAL
3711 exit:
3712 if (CHECK_EXIT && SCM_TRAPS_P)
3713 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
3714 {
3715 SCM_CLEAR_TRACED_FRAME (debug);
3716 if (SCM_CHEAPTRAPS_P)
3717 arg1 = scm_make_debugobj (&debug);
3718 else
3719 {
3720 int first;
3721 SCM val = scm_make_continuation (&first);
3722
3723 if (first)
3724 arg1 = val;
3725 else
3726 {
3727 proc = val;
3728 goto ret;
3729 }
3730 }
3731 SCM_TRAPS_P = 0;
3732 scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
3733 SCM_TRAPS_P = 1;
3734 }
3735 ret:
3736 scm_last_debug_frame = debug.prev;
3737 return proc;
3738 #endif
3739 }
3740
3741
3742 /* SECTION: The rest of this file is only read once.
3743 */
3744
3745 #ifndef DEVAL
3746
3747 /* Typechecking for multi-argument MAP and FOR-EACH.
3748
3749 Verify that each element of the vector ARGV, except for the first,
3750 is a proper list whose length is LEN. Attribute errors to WHO,
3751 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
3752 static inline void
3753 check_map_args (SCM argv,
3754 long len,
3755 SCM gf,
3756 SCM proc,
3757 SCM args,
3758 const char *who)
3759 {
3760 SCM *ve = SCM_VELTS (argv);
3761 long i;
3762
3763 for (i = SCM_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
3764 {
3765 long elt_len = scm_ilength (ve[i]);
3766
3767 if (elt_len < 0)
3768 {
3769 if (gf)
3770 scm_apply_generic (gf, scm_cons (proc, args));
3771 else
3772 scm_wrong_type_arg (who, i + 2, ve[i]);
3773 }
3774
3775 if (elt_len != len)
3776 scm_out_of_range (who, ve[i]);
3777 }
3778
3779 scm_remember_upto_here_1 (argv);
3780 }
3781
3782
3783 SCM_GPROC (s_map, "map", 2, 0, 1, scm_map, g_map);
3784
3785 /* Note: Currently, scm_map applies PROC to the argument list(s)
3786 sequentially, starting with the first element(s). This is used in
3787 evalext.c where the Scheme procedure `map-in-order', which guarantees
3788 sequential behaviour, is implemented using scm_map. If the
3789 behaviour changes, we need to update `map-in-order'.
3790 */
3791
3792 SCM
3793 scm_map (SCM proc, SCM arg1, SCM args)
3794 #define FUNC_NAME s_map
3795 {
3796 long i, len;
3797 SCM res = SCM_EOL;
3798 SCM *pres = &res;
3799 SCM *ve = &args; /* Keep args from being optimized away. */
3800
3801 len = scm_ilength (arg1);
3802 SCM_GASSERTn (len >= 0,
3803 g_map, scm_cons2 (proc, arg1, args), SCM_ARG2, s_map);
3804 SCM_VALIDATE_REST_ARGUMENT (args);
3805 if (SCM_NULLP (args))
3806 {
3807 while (SCM_NIMP (arg1))
3808 {
3809 *pres = scm_list_1 (scm_apply (proc, SCM_CAR (arg1), scm_listofnull));
3810 pres = SCM_CDRLOC (*pres);
3811 arg1 = SCM_CDR (arg1);
3812 }
3813 return res;
3814 }
3815 args = scm_vector (arg1 = scm_cons (arg1, args));
3816 ve = SCM_VELTS (args);
3817 #ifndef SCM_RECKLESS
3818 check_map_args (args, len, g_map, proc, arg1, s_map);
3819 #endif
3820 while (1)
3821 {
3822 arg1 = SCM_EOL;
3823 for (i = SCM_VECTOR_LENGTH (args) - 1; i >= 0; i--)
3824 {
3825 if (SCM_IMP (ve[i]))
3826 return res;
3827 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
3828 ve[i] = SCM_CDR (ve[i]);
3829 }
3830 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
3831 pres = SCM_CDRLOC (*pres);
3832 }
3833 }
3834 #undef FUNC_NAME
3835
3836
3837 SCM_GPROC (s_for_each, "for-each", 2, 0, 1, scm_for_each, g_for_each);
3838
3839 SCM
3840 scm_for_each (SCM proc, SCM arg1, SCM args)
3841 #define FUNC_NAME s_for_each
3842 {
3843 SCM *ve = &args; /* Keep args from being optimized away. */
3844 long i, len;
3845 len = scm_ilength (arg1);
3846 SCM_GASSERTn (len >= 0, g_for_each, scm_cons2 (proc, arg1, args),
3847 SCM_ARG2, s_for_each);
3848 SCM_VALIDATE_REST_ARGUMENT (args);
3849 if SCM_NULLP (args)
3850 {
3851 while SCM_NIMP (arg1)
3852 {
3853 scm_apply (proc, SCM_CAR (arg1), scm_listofnull);
3854 arg1 = SCM_CDR (arg1);
3855 }
3856 return SCM_UNSPECIFIED;
3857 }
3858 args = scm_vector (arg1 = scm_cons (arg1, args));
3859 ve = SCM_VELTS (args);
3860 #ifndef SCM_RECKLESS
3861 check_map_args (args, len, g_for_each, proc, arg1, s_for_each);
3862 #endif
3863 while (1)
3864 {
3865 arg1 = SCM_EOL;
3866 for (i = SCM_VECTOR_LENGTH (args) - 1; i >= 0; i--)
3867 {
3868 if SCM_IMP
3869 (ve[i]) return SCM_UNSPECIFIED;
3870 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
3871 ve[i] = SCM_CDR (ve[i]);
3872 }
3873 scm_apply (proc, arg1, SCM_EOL);
3874 }
3875 }
3876 #undef FUNC_NAME
3877
3878
3879 SCM
3880 scm_closure (SCM code, SCM env)
3881 {
3882 SCM z;
3883 SCM closcar = scm_cons (code, SCM_EOL);
3884 z = scm_alloc_cell (SCM_UNPACK (closcar) + scm_tc3_closure,
3885 (scm_t_bits) env);
3886 scm_remember_upto_here (closcar);
3887 return z;
3888 }
3889
3890
3891 scm_t_bits scm_tc16_promise;
3892
3893 SCM
3894 scm_makprom (SCM code)
3895 {
3896 SCM_RETURN_NEWSMOB (scm_tc16_promise, SCM_UNPACK (code));
3897 }
3898
3899
3900
3901 static int
3902 promise_print (SCM exp, SCM port, scm_print_state *pstate)
3903 {
3904 int writingp = SCM_WRITINGP (pstate);
3905 scm_puts ("#<promise ", port);
3906 SCM_SET_WRITINGP (pstate, 1);
3907 scm_iprin1 (SCM_CELL_OBJECT_1 (exp), port, pstate);
3908 SCM_SET_WRITINGP (pstate, writingp);
3909 scm_putc ('>', port);
3910 return !0;
3911 }
3912
3913
3914 SCM_DEFINE (scm_force, "force", 1, 0, 0,
3915 (SCM x),
3916 "If the promise @var{x} has not been computed yet, compute and\n"
3917 "return @var{x}, otherwise just return the previously computed\n"
3918 "value.")
3919 #define FUNC_NAME s_scm_force
3920 {
3921 SCM_VALIDATE_SMOB (1, x, promise);
3922 if (!((1L << 16) & SCM_CELL_WORD_0 (x)))
3923 {
3924 SCM ans = scm_call_0 (SCM_CELL_OBJECT_1 (x));
3925 if (!((1L << 16) & SCM_CELL_WORD_0 (x)))
3926 {
3927 SCM_DEFER_INTS;
3928 SCM_SET_CELL_OBJECT_1 (x, ans);
3929 SCM_SET_CELL_WORD_0 (x, SCM_CELL_WORD_0 (x) | (1L << 16));
3930 SCM_ALLOW_INTS;
3931 }
3932 }
3933 return SCM_CELL_OBJECT_1 (x);
3934 }
3935 #undef FUNC_NAME
3936
3937
3938 SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
3939 (SCM obj),
3940 "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
3941 "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
3942 #define FUNC_NAME s_scm_promise_p
3943 {
3944 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
3945 }
3946 #undef FUNC_NAME
3947
3948
3949 SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
3950 (SCM xorig, SCM x, SCM y),
3951 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
3952 "Any source properties associated with @var{xorig} are also associated\n"
3953 "with the new pair.")
3954 #define FUNC_NAME s_scm_cons_source
3955 {
3956 SCM p, z;
3957 z = scm_cons (x, y);
3958 /* Copy source properties possibly associated with xorig. */
3959 p = scm_whash_lookup (scm_source_whash, xorig);
3960 if (!SCM_IMP (p))
3961 scm_whash_insert (scm_source_whash, z, p);
3962 return z;
3963 }
3964 #undef FUNC_NAME
3965
3966
3967 SCM_DEFINE (scm_copy_tree, "copy-tree", 1, 0, 0,
3968 (SCM obj),
3969 "Recursively copy the data tree that is bound to @var{obj}, and return a\n"
3970 "pointer to the new data structure. @code{copy-tree} recurses down the\n"
3971 "contents of both pairs and vectors (since both cons cells and vector\n"
3972 "cells may point to arbitrary objects), and stops recursing when it hits\n"
3973 "any other object.")
3974 #define FUNC_NAME s_scm_copy_tree
3975 {
3976 SCM ans, tl;
3977 if (SCM_IMP (obj))
3978 return obj;
3979 if (SCM_VECTORP (obj))
3980 {
3981 unsigned long i = SCM_VECTOR_LENGTH (obj);
3982 ans = scm_c_make_vector (i, SCM_UNSPECIFIED);
3983 while (i--)
3984 SCM_VELTS (ans)[i] = scm_copy_tree (SCM_VELTS (obj)[i]);
3985 return ans;
3986 }
3987 if (!SCM_CONSP (obj))
3988 return obj;
3989 ans = tl = scm_cons_source (obj,
3990 scm_copy_tree (SCM_CAR (obj)),
3991 SCM_UNSPECIFIED);
3992 while (obj = SCM_CDR (obj), SCM_CONSP (obj))
3993 {
3994 SCM_SETCDR (tl, scm_cons (scm_copy_tree (SCM_CAR (obj)),
3995 SCM_UNSPECIFIED));
3996 tl = SCM_CDR (tl);
3997 }
3998 SCM_SETCDR (tl, obj);
3999 return ans;
4000 }
4001 #undef FUNC_NAME
4002
4003
4004 /* We have three levels of EVAL here:
4005
4006 - scm_i_eval (exp, env)
4007
4008 evaluates EXP in environment ENV. ENV is a lexical environment
4009 structure as used by the actual tree code evaluator. When ENV is
4010 a top-level environment, then changes to the current module are
4011 tracked by updating ENV so that it continues to be in sync with
4012 the current module.
4013
4014 - scm_primitive_eval (exp)
4015
4016 evaluates EXP in the top-level environment as determined by the
4017 current module. This is done by constructing a suitable
4018 environment and calling scm_i_eval. Thus, changes to the
4019 top-level module are tracked normally.
4020
4021 - scm_eval (exp, mod)
4022
4023 evaluates EXP while MOD is the current module. This is done by
4024 setting the current module to MOD, invoking scm_primitive_eval on
4025 EXP, and then restoring the current module to the value it had
4026 previously. That is, while EXP is evaluated, changes to the
4027 current module are tracked, but these changes do not persist when
4028 scm_eval returns.
4029
4030 For each level of evals, there are two variants, distinguished by a
4031 _x suffix: the ordinary variant does not modify EXP while the _x
4032 variant can destructively modify EXP into something completely
4033 unintelligible. A Scheme data structure passed as EXP to one of the
4034 _x variants should not ever be used again for anything. So when in
4035 doubt, use the ordinary variant.
4036
4037 */
4038
4039 SCM
4040 scm_i_eval_x (SCM exp, SCM env)
4041 {
4042 return SCM_XEVAL (exp, env);
4043 }
4044
4045 SCM
4046 scm_i_eval (SCM exp, SCM env)
4047 {
4048 exp = scm_copy_tree (exp);
4049 return SCM_XEVAL (exp, env);
4050 }
4051
4052 SCM
4053 scm_primitive_eval_x (SCM exp)
4054 {
4055 SCM env;
4056 SCM transformer = scm_current_module_transformer ();
4057 if (SCM_NIMP (transformer))
4058 exp = scm_call_1 (transformer, exp);
4059 env = scm_top_level_env (scm_current_module_lookup_closure ());
4060 return scm_i_eval_x (exp, env);
4061 }
4062
4063 SCM_DEFINE (scm_primitive_eval, "primitive-eval", 1, 0, 0,
4064 (SCM exp),
4065 "Evaluate @var{exp} in the top-level environment specified by\n"
4066 "the current module.")
4067 #define FUNC_NAME s_scm_primitive_eval
4068 {
4069 SCM env;
4070 SCM transformer = scm_current_module_transformer ();
4071 if (SCM_NIMP (transformer))
4072 exp = scm_call_1 (transformer, exp);
4073 env = scm_top_level_env (scm_current_module_lookup_closure ());
4074 return scm_i_eval (exp, env);
4075 }
4076 #undef FUNC_NAME
4077
4078 /* Eval does not take the second arg optionally. This is intentional
4079 * in order to be R5RS compatible, and to prepare for the new module
4080 * system, where we would like to make the choice of evaluation
4081 * environment explicit. */
4082
4083 static void
4084 change_environment (void *data)
4085 {
4086 SCM pair = SCM_PACK (data);
4087 SCM new_module = SCM_CAR (pair);
4088 SCM old_module = scm_current_module ();
4089 SCM_SETCDR (pair, old_module);
4090 scm_set_current_module (new_module);
4091 }
4092
4093
4094 static void
4095 restore_environment (void *data)
4096 {
4097 SCM pair = SCM_PACK (data);
4098 SCM old_module = SCM_CDR (pair);
4099 SCM new_module = scm_current_module ();
4100 SCM_SETCAR (pair, new_module);
4101 scm_set_current_module (old_module);
4102 }
4103
4104 static SCM
4105 inner_eval_x (void *data)
4106 {
4107 return scm_primitive_eval_x (SCM_PACK(data));
4108 }
4109
4110 SCM
4111 scm_eval_x (SCM exp, SCM module)
4112 #define FUNC_NAME "eval!"
4113 {
4114 SCM_VALIDATE_MODULE (2, module);
4115
4116 return scm_internal_dynamic_wind
4117 (change_environment, inner_eval_x, restore_environment,
4118 (void *) SCM_UNPACK (exp),
4119 (void *) SCM_UNPACK (scm_cons (module, SCM_BOOL_F)));
4120 }
4121 #undef FUNC_NAME
4122
4123 static SCM
4124 inner_eval (void *data)
4125 {
4126 return scm_primitive_eval (SCM_PACK(data));
4127 }
4128
4129 SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
4130 (SCM exp, SCM module),
4131 "Evaluate @var{exp}, a list representing a Scheme expression,\n"
4132 "in the top-level environment specified by @var{module}.\n"
4133 "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
4134 "@var{module} is made the current module. The current module\n"
4135 "is reset to its previous value when @var{eval} returns.")
4136 #define FUNC_NAME s_scm_eval
4137 {
4138 SCM_VALIDATE_MODULE (2, module);
4139
4140 return scm_internal_dynamic_wind
4141 (change_environment, inner_eval, restore_environment,
4142 (void *) SCM_UNPACK (exp),
4143 (void *) SCM_UNPACK (scm_cons (module, SCM_BOOL_F)));
4144 }
4145 #undef FUNC_NAME
4146
4147
4148 /* At this point, scm_deval and scm_dapply are generated.
4149 */
4150
4151 #ifdef DEBUG_EXTENSIONS
4152 # define DEVAL
4153 # include "eval.c"
4154 #endif
4155
4156
4157
4158 void
4159 scm_init_eval ()
4160 {
4161 scm_init_opts (scm_evaluator_traps,
4162 scm_evaluator_trap_table,
4163 SCM_N_EVALUATOR_TRAPS);
4164 scm_init_opts (scm_eval_options_interface,
4165 scm_eval_opts,
4166 SCM_N_EVAL_OPTIONS);
4167
4168 scm_tc16_promise = scm_make_smob_type ("promise", 0);
4169 scm_set_smob_mark (scm_tc16_promise, scm_markcdr);
4170 scm_set_smob_print (scm_tc16_promise, promise_print);
4171
4172 /* Dirk:Fixme:: make scm_undefineds local to eval.c: it's only used here. */
4173 scm_undefineds = scm_list_1 (SCM_UNDEFINED);
4174 SCM_SETCDR (scm_undefineds, scm_undefineds);
4175 scm_listofnull = scm_list_1 (SCM_EOL);
4176
4177 scm_f_apply = scm_c_define_subr ("apply", scm_tc7_lsubr_2, scm_apply);
4178
4179 /* acros */
4180 /* end of acros */
4181
4182 #ifndef SCM_MAGIC_SNARFER
4183 #include "libguile/eval.x"
4184 #endif
4185
4186 scm_c_define ("nil", scm_lisp_nil);
4187 scm_c_define ("t", scm_lisp_t);
4188
4189 scm_add_feature ("delay");
4190 }
4191
4192 #endif /* !DEVAL */
4193
4194 /*
4195 Local Variables:
4196 c-file-style: "gnu"
4197 End:
4198 */