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