Retire inclusion guard macro SCM_MAGIC_SNARFER.
[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 union
1894 {
1895 SCM *lloc;
1896 SCM arg1;
1897 } t;
1898 SCM proc, arg2, orig_sym;
1899 #ifdef DEVAL
1900 scm_t_debug_frame debug;
1901 scm_t_debug_info *debug_info_end;
1902 debug.prev = scm_last_debug_frame;
1903 debug.status = 0;
1904 /*
1905 * The debug.vect contains twice as much scm_t_debug_info frames as the
1906 * user has specified with (debug-set! frames <n>).
1907 *
1908 * Even frames are eval frames, odd frames are apply frames.
1909 */
1910 debug.vect = (scm_t_debug_info *) alloca (scm_debug_eframe_size
1911 * sizeof (scm_t_debug_info));
1912 debug.info = debug.vect;
1913 debug_info_end = debug.vect + scm_debug_eframe_size;
1914 scm_last_debug_frame = &debug;
1915 #endif
1916 #ifdef EVAL_STACK_CHECKING
1917 if (scm_stack_checking_enabled_p
1918 && SCM_STACK_OVERFLOW_P ((SCM_STACKITEM *) &proc))
1919 {
1920 #ifdef DEVAL
1921 debug.info->e.exp = x;
1922 debug.info->e.env = env;
1923 #endif
1924 scm_report_stack_overflow ();
1925 }
1926 #endif
1927
1928 #ifdef DEVAL
1929 goto start;
1930 #endif
1931
1932 loopnoap:
1933 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
1934 loop:
1935 #ifdef DEVAL
1936 SCM_CLEAR_ARGSREADY (debug);
1937 if (SCM_OVERFLOWP (debug))
1938 --debug.info;
1939 /*
1940 * In theory, this should be the only place where it is necessary to
1941 * check for space in debug.vect since both eval frames and
1942 * available space are even.
1943 *
1944 * For this to be the case, however, it is necessary that primitive
1945 * special forms which jump back to `loop', `begin' or some similar
1946 * label call PREP_APPLY. A convenient way to do this is to jump to
1947 * `loopnoap' or `cdrxnoap'.
1948 */
1949 else if (++debug.info >= debug_info_end)
1950 {
1951 SCM_SET_OVERFLOW (debug);
1952 debug.info -= 2;
1953 }
1954
1955 start:
1956 debug.info->e.exp = x;
1957 debug.info->e.env = env;
1958 if (CHECK_ENTRY && SCM_TRAPS_P)
1959 if (SCM_ENTER_FRAME_P || (SCM_BREAKPOINTS_P && SRCBRKP (x)))
1960 {
1961 SCM tail = SCM_BOOL(SCM_TAILRECP (debug));
1962 SCM_SET_TAILREC (debug);
1963 if (SCM_CHEAPTRAPS_P)
1964 t.arg1 = scm_make_debugobj (&debug);
1965 else
1966 {
1967 int first;
1968 SCM val = scm_make_continuation (&first);
1969
1970 if (first)
1971 t.arg1 = val;
1972 else
1973 {
1974 x = val;
1975 if (SCM_IMP (x))
1976 RETURN (x);
1977 else
1978 /* This gives the possibility for the debugger to
1979 modify the source expression before evaluation. */
1980 goto dispatch;
1981 }
1982 }
1983 SCM_TRAPS_P = 0;
1984 scm_call_4 (SCM_ENTER_FRAME_HDLR,
1985 scm_sym_enter_frame,
1986 t.arg1,
1987 tail,
1988 scm_unmemocopy (x, env));
1989 SCM_TRAPS_P = 1;
1990 }
1991 #endif
1992 #if defined (USE_THREADS) || defined (DEVAL)
1993 dispatch:
1994 #endif
1995 SCM_TICK;
1996 switch (SCM_TYP7 (x))
1997 {
1998 case scm_tc7_symbol:
1999 /* Only happens when called at top level. */
2000 x = scm_cons (x, SCM_UNDEFINED);
2001 RETURN (*scm_lookupcar (x, env, 1));
2002
2003 case SCM_BIT8 (SCM_IM_AND):
2004 x = SCM_CDR (x);
2005 while (!SCM_NULLP (SCM_CDR (x)))
2006 {
2007 SCM test_result = EVALCAR (x, env);
2008 if (SCM_FALSEP (test_result) || SCM_NILP (test_result))
2009 RETURN (SCM_BOOL_F);
2010 else
2011 x = SCM_CDR (x);
2012 }
2013 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2014 goto carloop;
2015
2016 case SCM_BIT8 (SCM_IM_BEGIN):
2017 if (SCM_NULLP (SCM_CDR (x)))
2018 RETURN (SCM_UNSPECIFIED);
2019
2020 /* (currently unused)
2021 cdrxnoap: */
2022 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2023 /* (currently unused)
2024 cdrxbegin: */
2025 x = SCM_CDR (x);
2026
2027 begin:
2028 /* If we are on toplevel with a lookup closure, we need to sync
2029 with the current module. */
2030 if (SCM_CONSP (env) && !SCM_CONSP (SCM_CAR (env)))
2031 {
2032 UPDATE_TOPLEVEL_ENV (env);
2033 while (!SCM_NULLP (SCM_CDR (x)))
2034 {
2035 EVALCAR (x, env);
2036 UPDATE_TOPLEVEL_ENV (env);
2037 x = SCM_CDR (x);
2038 }
2039 goto carloop;
2040 }
2041 else
2042 goto nontoplevel_begin;
2043
2044 nontoplevel_cdrxnoap:
2045 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2046 x = SCM_CDR (x);
2047 nontoplevel_begin:
2048 while (!SCM_NULLP (SCM_CDR (x)))
2049 {
2050 SCM form = SCM_CAR (x);
2051 if (SCM_IMP (form))
2052 {
2053 if (SCM_ISYMP (form))
2054 {
2055 x = scm_m_expand_body (x, env);
2056 goto nontoplevel_begin;
2057 }
2058 else
2059 SCM_VALIDATE_NON_EMPTY_COMBINATION (form);
2060 }
2061 else
2062 SCM_CEVAL (form, env);
2063 x = SCM_CDR (x);
2064 }
2065
2066 carloop:
2067 {
2068 /* scm_eval last form in list */
2069 SCM last_form = SCM_CAR (x);
2070
2071 if (SCM_CONSP (last_form))
2072 {
2073 /* This is by far the most frequent case. */
2074 x = last_form;
2075 goto loop; /* tail recurse */
2076 }
2077 else if (SCM_IMP (last_form))
2078 RETURN (SCM_EVALIM (last_form, env));
2079 else if (SCM_VARIABLEP (last_form))
2080 RETURN (SCM_VARIABLE_REF (last_form));
2081 else if (SCM_SYMBOLP (last_form))
2082 RETURN (*scm_lookupcar (x, env, 1));
2083 else
2084 RETURN (last_form);
2085 }
2086
2087
2088 case SCM_BIT8 (SCM_IM_CASE):
2089 x = SCM_CDR (x);
2090 {
2091 SCM key = EVALCAR (x, env);
2092 x = SCM_CDR (x);
2093 while (!SCM_NULLP (x))
2094 {
2095 SCM clause = SCM_CAR (x);
2096 SCM labels = SCM_CAR (clause);
2097 if (SCM_EQ_P (labels, scm_sym_else))
2098 {
2099 x = SCM_CDR (clause);
2100 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2101 goto begin;
2102 }
2103 while (!SCM_NULLP (labels))
2104 {
2105 SCM label = SCM_CAR (labels);
2106 if (SCM_EQ_P (label, key) || !SCM_FALSEP (scm_eqv_p (label, key)))
2107 {
2108 x = SCM_CDR (clause);
2109 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2110 goto begin;
2111 }
2112 labels = SCM_CDR (labels);
2113 }
2114 x = SCM_CDR (x);
2115 }
2116 }
2117 RETURN (SCM_UNSPECIFIED);
2118
2119
2120 case SCM_BIT8 (SCM_IM_COND):
2121 x = SCM_CDR (x);
2122 while (!SCM_NULLP (x))
2123 {
2124 SCM clause = SCM_CAR (x);
2125 if (SCM_EQ_P (SCM_CAR (clause), scm_sym_else))
2126 {
2127 x = SCM_CDR (clause);
2128 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2129 goto begin;
2130 }
2131 else
2132 {
2133 t.arg1 = EVALCAR (clause, env);
2134 if (!SCM_FALSEP (t.arg1) && !SCM_NILP (t.arg1))
2135 {
2136 x = SCM_CDR (clause);
2137 if (SCM_NULLP (x))
2138 RETURN (t.arg1);
2139 else if (!SCM_EQ_P (SCM_CAR (x), scm_sym_arrow))
2140 {
2141 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2142 goto begin;
2143 }
2144 else
2145 {
2146 proc = SCM_CDR (x);
2147 proc = EVALCAR (proc, env);
2148 SCM_ASRTGO (!SCM_IMP (proc), badfun);
2149 PREP_APPLY (proc, scm_list_1 (t.arg1));
2150 ENTER_APPLY;
2151 if (SCM_CLOSUREP(proc) && scm_badformalsp (proc, 1))
2152 goto umwrongnumargs;
2153 else
2154 goto evap1;
2155 }
2156 }
2157 x = SCM_CDR (x);
2158 }
2159 }
2160 RETURN (SCM_UNSPECIFIED);
2161
2162
2163 case SCM_BIT8 (SCM_IM_DO):
2164 x = SCM_CDR (x);
2165 {
2166 /* Compute the initialization values and the initial environment. */
2167 SCM init_forms = SCM_CADR (x);
2168 SCM init_values = SCM_EOL;
2169 while (!SCM_NULLP (init_forms))
2170 {
2171 init_values = scm_cons (EVALCAR (init_forms, env), init_values);
2172 init_forms = SCM_CDR (init_forms);
2173 }
2174 env = EXTEND_ENV (SCM_CAR (x), init_values, env);
2175 }
2176 x = SCM_CDDR (x);
2177 {
2178 SCM test_form = SCM_CAR (x);
2179 SCM body_forms = SCM_CADR (x);
2180 SCM step_forms = SCM_CDDR (x);
2181
2182 SCM test_result = EVALCAR (test_form, env);
2183
2184 while (SCM_FALSEP (test_result) || SCM_NILP (test_result))
2185 {
2186 {
2187 /* Evaluate body forms. */
2188 SCM temp_forms;
2189 for (temp_forms = body_forms;
2190 !SCM_NULLP (temp_forms);
2191 temp_forms = SCM_CDR (temp_forms))
2192 {
2193 SCM form = SCM_CAR (temp_forms);
2194 /* Dirk:FIXME: We only need to eval forms, that may have a
2195 * side effect here. This is only true for forms that start
2196 * with a pair. All others are just constants. However,
2197 * since in the common case there is no constant expression
2198 * in a body of a do form, we just check for immediates here
2199 * and have SCM_CEVAL take care of other cases. In the long
2200 * run it would make sense to get rid of this test and have
2201 * the macro transformer of 'do' eliminate all forms that
2202 * have no sideeffect. */
2203 if (!SCM_IMP (form))
2204 SCM_CEVAL (form, env);
2205 }
2206 }
2207
2208 {
2209 /* Evaluate the step expressions. */
2210 SCM temp_forms;
2211 SCM step_values = SCM_EOL;
2212 for (temp_forms = step_forms;
2213 !SCM_NULLP (temp_forms);
2214 temp_forms = SCM_CDR (temp_forms))
2215 {
2216 SCM value = EVALCAR (temp_forms, env);
2217 step_values = scm_cons (value, step_values);
2218 }
2219 env = EXTEND_ENV (SCM_CAAR (env), step_values, SCM_CDR (env));
2220 }
2221
2222 test_result = EVALCAR (test_form, env);
2223 }
2224 }
2225 x = SCM_CDAR (x);
2226 if (SCM_NULLP (x))
2227 RETURN (SCM_UNSPECIFIED);
2228 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2229 goto nontoplevel_begin;
2230
2231
2232 case SCM_BIT8 (SCM_IM_IF):
2233 x = SCM_CDR (x);
2234 {
2235 SCM test_result = EVALCAR (x, env);
2236 if (!SCM_FALSEP (test_result) && !SCM_NILP (test_result))
2237 x = SCM_CDR (x);
2238 else
2239 {
2240 x = SCM_CDDR (x);
2241 if (SCM_NULLP (x))
2242 RETURN (SCM_UNSPECIFIED);
2243 }
2244 }
2245 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2246 goto carloop;
2247
2248
2249 case SCM_BIT8 (SCM_IM_LET):
2250 x = SCM_CDR (x);
2251 {
2252 SCM init_forms = SCM_CADR (x);
2253 SCM init_values = SCM_EOL;
2254 do
2255 {
2256 init_values = scm_cons (EVALCAR (init_forms, env), init_values);
2257 init_forms = SCM_CDR (init_forms);
2258 }
2259 while (!SCM_NULLP (init_forms));
2260 env = EXTEND_ENV (SCM_CAR (x), init_values, env);
2261 }
2262 x = SCM_CDR (x);
2263 goto nontoplevel_cdrxnoap;
2264
2265
2266 case SCM_BIT8 (SCM_IM_LETREC):
2267 x = SCM_CDR (x);
2268 env = EXTEND_ENV (SCM_CAR (x), scm_undefineds, env);
2269 x = SCM_CDR (x);
2270 {
2271 SCM init_forms = SCM_CAR (x);
2272 SCM init_values = SCM_EOL;
2273 do
2274 {
2275 init_values = scm_cons (EVALCAR (init_forms, env), init_values);
2276 init_forms = SCM_CDR (init_forms);
2277 }
2278 while (!SCM_NULLP (init_forms));
2279 SCM_SETCDR (SCM_CAR (env), init_values);
2280 }
2281 goto nontoplevel_cdrxnoap;
2282
2283
2284 case SCM_BIT8 (SCM_IM_LETSTAR):
2285 x = SCM_CDR (x);
2286 {
2287 SCM bindings = SCM_CAR (x);
2288 if (SCM_NULLP (bindings))
2289 env = EXTEND_ENV (SCM_EOL, SCM_EOL, env);
2290 else
2291 {
2292 do
2293 {
2294 SCM name = SCM_CAR (bindings);
2295 SCM init = SCM_CDR (bindings);
2296 env = EXTEND_ENV (name, EVALCAR (init, env), env);
2297 bindings = SCM_CDR (init);
2298 }
2299 while (!SCM_NULLP (bindings));
2300 }
2301 }
2302 goto nontoplevel_cdrxnoap;
2303
2304
2305 case SCM_BIT8 (SCM_IM_OR):
2306 x = SCM_CDR (x);
2307 while (!SCM_NULLP (SCM_CDR (x)))
2308 {
2309 SCM val = EVALCAR (x, env);
2310 if (!SCM_FALSEP (val) && !SCM_NILP (val))
2311 RETURN (val);
2312 else
2313 x = SCM_CDR (x);
2314 }
2315 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2316 goto carloop;
2317
2318
2319 case SCM_BIT8 (SCM_IM_LAMBDA):
2320 RETURN (scm_closure (SCM_CDR (x), env));
2321
2322
2323 case SCM_BIT8 (SCM_IM_QUOTE):
2324 RETURN (SCM_CADR (x));
2325
2326
2327 case SCM_BIT8 (SCM_IM_SET_X):
2328 x = SCM_CDR (x);
2329 {
2330 SCM *location;
2331 SCM variable = SCM_CAR (x);
2332 if (SCM_VARIABLEP (variable))
2333 location = SCM_VARIABLE_LOC (variable);
2334 #ifdef MEMOIZE_LOCALS
2335 else if (SCM_ILOCP (variable))
2336 location = scm_ilookup (variable, env);
2337 #endif
2338 else /* (SCM_SYMBOLP (variable)) is known to be true */
2339 location = scm_lookupcar (x, env, 1);
2340 x = SCM_CDR (x);
2341 *location = EVALCAR (x, env);
2342 }
2343 RETURN (SCM_UNSPECIFIED);
2344
2345
2346 case SCM_BIT8(SCM_IM_DEFINE): /* only for internal defines */
2347 scm_misc_error (NULL, "Bad define placement", SCM_EOL);
2348
2349
2350 /* new syntactic forms go here. */
2351 case SCM_BIT8 (SCM_MAKISYM (0)):
2352 proc = SCM_CAR (x);
2353 SCM_ASRTGO (SCM_ISYMP (proc), badfun);
2354 switch (SCM_ISYMNUM (proc))
2355 {
2356
2357
2358 case (SCM_ISYMNUM (SCM_IM_APPLY)):
2359 proc = SCM_CDR (x);
2360 proc = EVALCAR (proc, env);
2361 SCM_ASRTGO (!SCM_IMP (proc), badfun);
2362 if (SCM_CLOSUREP (proc))
2363 {
2364 PREP_APPLY (proc, SCM_EOL);
2365 t.arg1 = SCM_CDDR (x);
2366 t.arg1 = EVALCAR (t.arg1, env);
2367 apply_closure:
2368 /* Go here to tail-call a closure. PROC is the closure
2369 and T.ARG1 is the list of arguments. Do not forget to
2370 call PREP_APPLY. */
2371 {
2372 SCM formals = SCM_CLOSURE_FORMALS (proc);
2373 #ifdef DEVAL
2374 debug.info->a.args = t.arg1;
2375 #endif
2376 #ifndef SCM_RECKLESS
2377 if (scm_badargsp (formals, t.arg1))
2378 goto wrongnumargs;
2379 #endif
2380 ENTER_APPLY;
2381 /* Copy argument list */
2382 if (SCM_NULL_OR_NIL_P (t.arg1))
2383 env = EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
2384 else
2385 {
2386 SCM args = scm_list_1 (SCM_CAR (t.arg1));
2387 SCM tail = args;
2388 t.arg1 = SCM_CDR (t.arg1);
2389 while (!SCM_NULL_OR_NIL_P (t.arg1))
2390 {
2391 SCM new_tail = scm_list_1 (SCM_CAR (t.arg1));
2392 SCM_SETCDR (tail, new_tail);
2393 tail = new_tail;
2394 t.arg1 = SCM_CDR (t.arg1);
2395 }
2396 env = EXTEND_ENV (formals, args, SCM_ENV (proc));
2397 }
2398
2399 x = SCM_CLOSURE_BODY (proc);
2400 goto nontoplevel_begin;
2401 }
2402 }
2403 else
2404 {
2405 proc = scm_f_apply;
2406 goto evapply;
2407 }
2408
2409
2410 case (SCM_ISYMNUM (SCM_IM_CONT)):
2411 {
2412 int first;
2413 SCM val = scm_make_continuation (&first);
2414
2415 if (first)
2416 t.arg1 = val;
2417 else
2418 RETURN (val);
2419 }
2420 proc = SCM_CDR (x);
2421 proc = scm_eval_car (proc, env);
2422 SCM_ASRTGO (SCM_NIMP (proc), badfun);
2423 PREP_APPLY (proc, scm_list_1 (t.arg1));
2424 ENTER_APPLY;
2425 if (SCM_CLOSUREP(proc) && scm_badformalsp (proc, 1))
2426 goto umwrongnumargs;
2427 goto evap1;
2428
2429 case (SCM_ISYMNUM (SCM_IM_DELAY)):
2430 RETURN (scm_makprom (scm_closure (SCM_CDR (x), env)));
2431
2432 case (SCM_ISYMNUM (SCM_IM_DISPATCH)):
2433 {
2434 /* If not done yet, evaluate the operand forms. The result is a
2435 * list of arguments stored in t.arg1, which is used to perform the
2436 * function dispatch. */
2437 SCM operand_forms = SCM_CADR (x);
2438 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2439 if (SCM_ILOCP (operand_forms))
2440 t.arg1 = *scm_ilookup (operand_forms, env);
2441 else if (SCM_VARIABLEP (operand_forms))
2442 t.arg1 = SCM_VARIABLE_REF (operand_forms);
2443 else if (!SCM_CONSP (operand_forms))
2444 t.arg1 = *scm_lookupcar (SCM_CDR (x), env, 1);
2445 else
2446 {
2447 SCM tail = t.arg1 = scm_list_1 (EVALCAR (operand_forms, env));
2448 operand_forms = SCM_CDR (operand_forms);
2449 while (!SCM_NULLP (operand_forms))
2450 {
2451 SCM new_tail = scm_list_1 (EVALCAR (operand_forms, env));
2452 SCM_SETCDR (tail, new_tail);
2453 tail = new_tail;
2454 operand_forms = SCM_CDR (operand_forms);
2455 }
2456 }
2457 }
2458
2459 /* The type dispatch code is duplicated below
2460 * (c.f. objects.c:scm_mcache_compute_cmethod) since that
2461 * cuts down execution time for type dispatch to 50%. */
2462 type_dispatch: /* inputs: x, t.arg1 */
2463 /* Type dispatch means to determine from the types of the function
2464 * arguments (i. e. the 'signature' of the call), which method from
2465 * a generic function is to be called. This process of selecting
2466 * the right method takes some time. To speed it up, guile uses
2467 * caching: Together with the macro call to dispatch the signatures
2468 * of some previous calls to that generic function from the same
2469 * place are stored (in the code!) in a cache that we call the
2470 * 'method cache'. This is done since it is likely, that
2471 * consecutive calls to dispatch from that position in the code will
2472 * have the same signature. Thus, the type dispatch works as
2473 * follows: First, determine a hash value from the signature of the
2474 * actual arguments. Second, use this hash value as an index to
2475 * find that same signature in the method cache stored at this
2476 * position in the code. If found, you have also found the
2477 * corresponding method that belongs to that signature. If the
2478 * signature is not found in the method cache, you have to perform a
2479 * full search over all signatures stored with the generic
2480 * function. */
2481 {
2482 unsigned long int specializers;
2483 unsigned long int hash_value;
2484 unsigned long int cache_end_pos;
2485 unsigned long int mask;
2486 SCM method_cache;
2487
2488 {
2489 SCM z = SCM_CDDR (x);
2490 SCM tmp = SCM_CADR (z);
2491 specializers = SCM_INUM (SCM_CAR (z));
2492
2493 /* Compute a hash value for searching the method cache. There
2494 * are two variants for computing the hash value, a (rather)
2495 * complicated one, and a simple one. For the complicated one
2496 * explained below, tmp holds a number that is used in the
2497 * computation. */
2498 if (SCM_INUMP (tmp))
2499 {
2500 /* Use the signature of the actual arguments to determine
2501 * the hash value. This is done as follows: Each class has
2502 * an array of random numbers, that are determined when the
2503 * class is created. The integer 'hashset' is an index into
2504 * that array of random numbers. Now, from all classes that
2505 * are part of the signature of the actual arguments, the
2506 * random numbers at index 'hashset' are taken and summed
2507 * up, giving the hash value. The value of 'hashset' is
2508 * stored at the call to dispatch. This allows to have
2509 * different 'formulas' for calculating the hash value at
2510 * different places where dispatch is called. This allows
2511 * to optimize the hash formula at every individual place
2512 * where dispatch is called, such that hopefully the hash
2513 * value that is computed will directly point to the right
2514 * method in the method cache. */
2515 unsigned long int hashset = SCM_INUM (tmp);
2516 unsigned long int counter = specializers + 1;
2517 SCM tmp_arg = t.arg1;
2518 hash_value = 0;
2519 while (!SCM_NULLP (tmp_arg) && counter != 0)
2520 {
2521 SCM class = scm_class_of (SCM_CAR (tmp_arg));
2522 hash_value += SCM_INSTANCE_HASH (class, hashset);
2523 tmp_arg = SCM_CDR (tmp_arg);
2524 counter--;
2525 }
2526 z = SCM_CDDR (z);
2527 method_cache = SCM_CADR (z);
2528 mask = SCM_INUM (SCM_CAR (z));
2529 hash_value &= mask;
2530 cache_end_pos = hash_value;
2531 }
2532 else
2533 {
2534 /* This method of determining the hash value is much
2535 * simpler: Set the hash value to zero and just perform a
2536 * linear search through the method cache. */
2537 method_cache = tmp;
2538 mask = (unsigned long int) ((long) -1);
2539 hash_value = 0;
2540 cache_end_pos = SCM_VECTOR_LENGTH (method_cache);
2541 }
2542 }
2543
2544 {
2545 /* Search the method cache for a method with a matching
2546 * signature. Start the search at position 'hash_value'. The
2547 * hashing implementation uses linear probing for conflict
2548 * resolution, that is, if the signature in question is not
2549 * found at the starting index in the hash table, the next table
2550 * entry is tried, and so on, until in the worst case the whole
2551 * cache has been searched, but still the signature has not been
2552 * found. */
2553 SCM z;
2554 do
2555 {
2556 SCM args = t.arg1; /* list of arguments */
2557 z = SCM_VELTS (method_cache)[hash_value];
2558 while (!SCM_NULLP (args))
2559 {
2560 /* More arguments than specifiers => CLASS != ENV */
2561 SCM class_of_arg = scm_class_of (SCM_CAR (args));
2562 if (!SCM_EQ_P (class_of_arg, SCM_CAR (z)))
2563 goto next_method;
2564 args = SCM_CDR (args);
2565 z = SCM_CDR (z);
2566 }
2567 /* Fewer arguments than specifiers => CAR != ENV */
2568 if (SCM_NULLP (SCM_CAR (z)) || SCM_CONSP (SCM_CAR (z)))
2569 goto apply_cmethod;
2570 next_method:
2571 hash_value = (hash_value + 1) & mask;
2572 } while (hash_value != cache_end_pos);
2573
2574 /* No appropriate method was found in the cache. */
2575 z = scm_memoize_method (x, t.arg1);
2576
2577 apply_cmethod: /* inputs: z, t.arg1 */
2578 {
2579 SCM formals = SCM_CMETHOD_FORMALS (z);
2580 env = EXTEND_ENV (formals, t.arg1, SCM_CMETHOD_ENV (z));
2581 x = SCM_CMETHOD_BODY (z);
2582 goto nontoplevel_begin;
2583 }
2584 }
2585 }
2586
2587
2588 case (SCM_ISYMNUM (SCM_IM_SLOT_REF)):
2589 x = SCM_CDR (x);
2590 {
2591 SCM instance = EVALCAR (x, env);
2592 unsigned long int slot = SCM_INUM (SCM_CADR (x));
2593 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
2594 }
2595
2596
2597 case (SCM_ISYMNUM (SCM_IM_SLOT_SET_X)):
2598 x = SCM_CDR (x);
2599 {
2600 SCM instance = EVALCAR (x, env);
2601 unsigned long int slot = SCM_INUM (SCM_CADR (x));
2602 SCM value = EVALCAR (SCM_CDDR (x), env);
2603 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (value);
2604 RETURN (SCM_UNSPECIFIED);
2605 }
2606
2607
2608 #ifdef SCM_ENABLE_ELISP
2609
2610 case (SCM_ISYMNUM (SCM_IM_NIL_COND)):
2611 {
2612 SCM test_form = SCM_CDR (x);
2613 x = SCM_CDR (test_form);
2614 while (!SCM_NULL_OR_NIL_P (x))
2615 {
2616 SCM test_result = EVALCAR (test_form, env);
2617 if (!(SCM_FALSEP (test_result)
2618 || SCM_NULL_OR_NIL_P (test_result)))
2619 {
2620 if (SCM_EQ_P (SCM_CAR (x), SCM_UNSPECIFIED))
2621 RETURN (test_result);
2622 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2623 goto carloop;
2624 }
2625 else
2626 {
2627 test_form = SCM_CDR (x);
2628 x = SCM_CDR (test_form);
2629 }
2630 }
2631 x = test_form;
2632 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2633 goto carloop;
2634 }
2635
2636 #endif /* SCM_ENABLE_ELISP */
2637
2638 case (SCM_ISYMNUM (SCM_IM_BIND)):
2639 {
2640 SCM vars, exps, vals;
2641
2642 x = SCM_CDR (x);
2643 vars = SCM_CAAR (x);
2644 exps = SCM_CDAR (x);
2645
2646 vals = SCM_EOL;
2647
2648 while (SCM_NIMP (exps))
2649 {
2650 vals = scm_cons (EVALCAR (exps, env), vals);
2651 exps = SCM_CDR (exps);
2652 }
2653
2654 scm_swap_bindings (vars, vals);
2655 scm_dynwinds = scm_acons (vars, vals, scm_dynwinds);
2656
2657 /* Ignore all but the last evaluation result. */
2658 for (x = SCM_CDR (x); !SCM_NULLP (SCM_CDR (x)); x = SCM_CDR (x))
2659 {
2660 if (SCM_CONSP (SCM_CAR (x)))
2661 SCM_CEVAL (SCM_CAR (x), env);
2662 }
2663 proc = EVALCAR (x, env);
2664
2665 scm_dynwinds = SCM_CDR (scm_dynwinds);
2666 scm_swap_bindings (vars, vals);
2667
2668 RETURN (proc);
2669 }
2670
2671
2672 case (SCM_ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
2673 {
2674 proc = SCM_CDR (x);
2675 x = EVALCAR (proc, env);
2676 proc = SCM_CDR (proc);
2677 proc = EVALCAR (proc, env);
2678 t.arg1 = SCM_APPLY (x, SCM_EOL, SCM_EOL);
2679 if (SCM_VALUESP (t.arg1))
2680 t.arg1 = scm_struct_ref (t.arg1, SCM_INUM0);
2681 else
2682 t.arg1 = scm_list_1 (t.arg1);
2683 if (SCM_CLOSUREP (proc))
2684 {
2685 PREP_APPLY (proc, t.arg1);
2686 goto apply_closure;
2687 }
2688 return SCM_APPLY (proc, t.arg1, SCM_EOL);
2689 }
2690
2691
2692 default:
2693 goto badfun;
2694 }
2695
2696 default:
2697 proc = x;
2698 badfun:
2699 scm_misc_error (NULL, "Wrong type to apply: ~S", scm_list_1 (proc));
2700 case scm_tc7_vector:
2701 case scm_tc7_wvect:
2702 #ifdef HAVE_ARRAYS
2703 case scm_tc7_bvect:
2704 case scm_tc7_byvect:
2705 case scm_tc7_svect:
2706 case scm_tc7_ivect:
2707 case scm_tc7_uvect:
2708 case scm_tc7_fvect:
2709 case scm_tc7_dvect:
2710 case scm_tc7_cvect:
2711 #ifdef HAVE_LONG_LONGS
2712 case scm_tc7_llvect:
2713 #endif
2714 #endif
2715 case scm_tc7_string:
2716 case scm_tc7_smob:
2717 case scm_tcs_closures:
2718 case scm_tc7_cclo:
2719 case scm_tc7_pws:
2720 case scm_tcs_subrs:
2721 case scm_tcs_struct:
2722 RETURN (x);
2723
2724 case scm_tc7_variable:
2725 RETURN (SCM_VARIABLE_REF(x));
2726
2727 #ifdef MEMOIZE_LOCALS
2728 case SCM_BIT8(SCM_ILOC00):
2729 proc = *scm_ilookup (SCM_CAR (x), env);
2730 SCM_ASRTGO (SCM_NIMP (proc), badfun);
2731 #ifndef SCM_RECKLESS
2732 #ifdef SCM_CAUTIOUS
2733 goto checkargs;
2734 #endif
2735 #endif
2736 break;
2737 #endif /* ifdef MEMOIZE_LOCALS */
2738
2739 case scm_tcs_cons_nimcar:
2740 orig_sym = SCM_CAR (x);
2741 if (SCM_SYMBOLP (orig_sym))
2742 {
2743 #ifdef USE_THREADS
2744 {
2745 SCM *location = scm_lookupcar1 (x, env, 1);
2746 if (location == NULL)
2747 {
2748 /* we have lost the race, start again. */
2749 goto dispatch;
2750 }
2751 proc = *location;
2752 }
2753 #else
2754 proc = *scm_lookupcar (x, env, 1);
2755 #endif
2756
2757 if (SCM_IMP (proc))
2758 {
2759 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
2760 lookupcar */
2761 goto badfun;
2762 }
2763 if (SCM_MACROP (proc))
2764 {
2765 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
2766 lookupcar */
2767 handle_a_macro:
2768 #ifdef DEVAL
2769 /* Set a flag during macro expansion so that macro
2770 application frames can be deleted from the backtrace. */
2771 SCM_SET_MACROEXP (debug);
2772 #endif
2773 t.arg1 = SCM_APPLY (SCM_MACRO_CODE (proc), x,
2774 scm_cons (env, scm_listofnull));
2775
2776 #ifdef DEVAL
2777 SCM_CLEAR_MACROEXP (debug);
2778 #endif
2779 switch (SCM_MACRO_TYPE (proc))
2780 {
2781 case 2:
2782 if (scm_ilength (t.arg1) <= 0)
2783 t.arg1 = scm_list_2 (SCM_IM_BEGIN, t.arg1);
2784 #ifdef DEVAL
2785 if (!SCM_CLOSUREP (SCM_MACRO_CODE (proc)))
2786 {
2787 SCM_DEFER_INTS;
2788 SCM_SETCAR (x, SCM_CAR (t.arg1));
2789 SCM_SETCDR (x, SCM_CDR (t.arg1));
2790 SCM_ALLOW_INTS;
2791 goto dispatch;
2792 }
2793 /* Prevent memoizing of debug info expression. */
2794 debug.info->e.exp = scm_cons_source (debug.info->e.exp,
2795 SCM_CAR (x),
2796 SCM_CDR (x));
2797 #endif
2798 SCM_DEFER_INTS;
2799 SCM_SETCAR (x, SCM_CAR (t.arg1));
2800 SCM_SETCDR (x, SCM_CDR (t.arg1));
2801 SCM_ALLOW_INTS;
2802 goto loopnoap;
2803 case 1:
2804 if (SCM_NIMP (x = t.arg1))
2805 goto loopnoap;
2806 case 0:
2807 RETURN (t.arg1);
2808 }
2809 }
2810 }
2811 else
2812 proc = SCM_CEVAL (SCM_CAR (x), env);
2813 SCM_ASRTGO (!SCM_IMP (proc), badfun);
2814 #ifndef SCM_RECKLESS
2815 #ifdef SCM_CAUTIOUS
2816 checkargs:
2817 #endif
2818 if (SCM_CLOSUREP (proc))
2819 {
2820 arg2 = SCM_CLOSURE_FORMALS (proc);
2821 t.arg1 = SCM_CDR (x);
2822 while (!SCM_NULLP (arg2))
2823 {
2824 if (!SCM_CONSP (arg2))
2825 goto evapply;
2826 if (SCM_IMP (t.arg1))
2827 goto umwrongnumargs;
2828 arg2 = SCM_CDR (arg2);
2829 t.arg1 = SCM_CDR (t.arg1);
2830 }
2831 if (!SCM_NULLP (t.arg1))
2832 goto umwrongnumargs;
2833 }
2834 else if (SCM_MACROP (proc))
2835 goto handle_a_macro;
2836 #endif
2837 }
2838
2839
2840 evapply:
2841 PREP_APPLY (proc, SCM_EOL);
2842 if (SCM_NULLP (SCM_CDR (x))) {
2843 ENTER_APPLY;
2844 evap0:
2845 switch (SCM_TYP7 (proc))
2846 { /* no arguments given */
2847 case scm_tc7_subr_0:
2848 RETURN (SCM_SUBRF (proc) ());
2849 case scm_tc7_subr_1o:
2850 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
2851 case scm_tc7_lsubr:
2852 RETURN (SCM_SUBRF (proc) (SCM_EOL));
2853 case scm_tc7_rpsubr:
2854 RETURN (SCM_BOOL_T);
2855 case scm_tc7_asubr:
2856 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
2857 case scm_tc7_smob:
2858 if (!SCM_SMOB_APPLICABLE_P (proc))
2859 goto badfun;
2860 RETURN (SCM_SMOB_APPLY_0 (proc));
2861 case scm_tc7_cclo:
2862 t.arg1 = proc;
2863 proc = SCM_CCLO_SUBR (proc);
2864 #ifdef DEVAL
2865 debug.info->a.proc = proc;
2866 debug.info->a.args = scm_list_1 (t.arg1);
2867 #endif
2868 goto evap1;
2869 case scm_tc7_pws:
2870 proc = SCM_PROCEDURE (proc);
2871 #ifdef DEVAL
2872 debug.info->a.proc = proc;
2873 #endif
2874 if (!SCM_CLOSUREP (proc))
2875 goto evap0;
2876 if (scm_badformalsp (proc, 0))
2877 goto umwrongnumargs;
2878 case scm_tcs_closures:
2879 x = SCM_CLOSURE_BODY (proc);
2880 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), SCM_EOL, SCM_ENV (proc));
2881 goto nontoplevel_begin;
2882 case scm_tcs_struct:
2883 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
2884 {
2885 x = SCM_ENTITY_PROCEDURE (proc);
2886 t.arg1 = SCM_EOL;
2887 goto type_dispatch;
2888 }
2889 else if (!SCM_I_OPERATORP (proc))
2890 goto badfun;
2891 else
2892 {
2893 t.arg1 = proc;
2894 proc = (SCM_I_ENTITYP (proc)
2895 ? SCM_ENTITY_PROCEDURE (proc)
2896 : SCM_OPERATOR_PROCEDURE (proc));
2897 #ifdef DEVAL
2898 debug.info->a.proc = proc;
2899 debug.info->a.args = scm_list_1 (t.arg1);
2900 #endif
2901 if (SCM_NIMP (proc))
2902 goto evap1;
2903 else
2904 goto badfun;
2905 }
2906 case scm_tc7_subr_1:
2907 case scm_tc7_subr_2:
2908 case scm_tc7_subr_2o:
2909 case scm_tc7_cxr:
2910 case scm_tc7_subr_3:
2911 case scm_tc7_lsubr_2:
2912 umwrongnumargs:
2913 unmemocar (x, env);
2914 wrongnumargs:
2915 scm_wrong_num_args (proc);
2916 default:
2917 /* handle macros here */
2918 goto badfun;
2919 }
2920 }
2921
2922 /* must handle macros by here */
2923 x = SCM_CDR (x);
2924 #ifdef SCM_CAUTIOUS
2925 if (SCM_IMP (x))
2926 goto wrongnumargs;
2927 else if (SCM_CONSP (x))
2928 {
2929 if (SCM_IMP (SCM_CAR (x)))
2930 t.arg1 = SCM_EVALIM (SCM_CAR (x), env);
2931 else
2932 t.arg1 = EVALCELLCAR (x, env);
2933 }
2934 else
2935 goto wrongnumargs;
2936 #else
2937 t.arg1 = EVALCAR (x, env);
2938 #endif
2939 #ifdef DEVAL
2940 debug.info->a.args = scm_list_1 (t.arg1);
2941 #endif
2942 x = SCM_CDR (x);
2943 if (SCM_NULLP (x))
2944 {
2945 ENTER_APPLY;
2946 evap1:
2947 switch (SCM_TYP7 (proc))
2948 { /* have one argument in t.arg1 */
2949 case scm_tc7_subr_2o:
2950 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
2951 case scm_tc7_subr_1:
2952 case scm_tc7_subr_1o:
2953 RETURN (SCM_SUBRF (proc) (t.arg1));
2954 case scm_tc7_cxr:
2955 if (SCM_SUBRF (proc))
2956 {
2957 if (SCM_INUMP (t.arg1))
2958 {
2959 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (t.arg1))));
2960 }
2961 else if (SCM_REALP (t.arg1))
2962 {
2963 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (t.arg1))));
2964 }
2965 #ifdef SCM_BIGDIG
2966 else if (SCM_BIGP (t.arg1))
2967 {
2968 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (t.arg1))));
2969 }
2970 #endif
2971 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), t.arg1,
2972 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
2973 }
2974 proc = SCM_SNAME (proc);
2975 {
2976 char *chrs = SCM_SYMBOL_CHARS (proc) + SCM_SYMBOL_LENGTH (proc) - 1;
2977 while ('c' != *--chrs)
2978 {
2979 SCM_ASSERT (SCM_CONSP (t.arg1),
2980 t.arg1, SCM_ARG1, SCM_SYMBOL_CHARS (proc));
2981 t.arg1 = ('a' == *chrs) ? SCM_CAR (t.arg1) : SCM_CDR (t.arg1);
2982 }
2983 RETURN (t.arg1);
2984 }
2985 case scm_tc7_rpsubr:
2986 RETURN (SCM_BOOL_T);
2987 case scm_tc7_asubr:
2988 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
2989 case scm_tc7_lsubr:
2990 #ifdef DEVAL
2991 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
2992 #else
2993 RETURN (SCM_SUBRF (proc) (scm_list_1 (t.arg1)));
2994 #endif
2995 case scm_tc7_smob:
2996 if (!SCM_SMOB_APPLICABLE_P (proc))
2997 goto badfun;
2998 RETURN (SCM_SMOB_APPLY_1 (proc, t.arg1));
2999 case scm_tc7_cclo:
3000 arg2 = t.arg1;
3001 t.arg1 = proc;
3002 proc = SCM_CCLO_SUBR (proc);
3003 #ifdef DEVAL
3004 debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
3005 debug.info->a.proc = proc;
3006 #endif
3007 goto evap2;
3008 case scm_tc7_pws:
3009 proc = SCM_PROCEDURE (proc);
3010 #ifdef DEVAL
3011 debug.info->a.proc = proc;
3012 #endif
3013 if (!SCM_CLOSUREP (proc))
3014 goto evap1;
3015 if (scm_badformalsp (proc, 1))
3016 goto umwrongnumargs;
3017 case scm_tcs_closures:
3018 /* clos1: */
3019 x = SCM_CLOSURE_BODY (proc);
3020 #ifdef DEVAL
3021 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), debug.info->a.args, SCM_ENV (proc));
3022 #else
3023 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), scm_list_1 (t.arg1), SCM_ENV (proc));
3024 #endif
3025 goto nontoplevel_begin;
3026 case scm_tcs_struct:
3027 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3028 {
3029 x = SCM_ENTITY_PROCEDURE (proc);
3030 #ifdef DEVAL
3031 t.arg1 = debug.info->a.args;
3032 #else
3033 t.arg1 = scm_list_1 (t.arg1);
3034 #endif
3035 goto type_dispatch;
3036 }
3037 else if (!SCM_I_OPERATORP (proc))
3038 goto badfun;
3039 else
3040 {
3041 arg2 = t.arg1;
3042 t.arg1 = proc;
3043 proc = (SCM_I_ENTITYP (proc)
3044 ? SCM_ENTITY_PROCEDURE (proc)
3045 : SCM_OPERATOR_PROCEDURE (proc));
3046 #ifdef DEVAL
3047 debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
3048 debug.info->a.proc = proc;
3049 #endif
3050 if (SCM_NIMP (proc))
3051 goto evap2;
3052 else
3053 goto badfun;
3054 }
3055 case scm_tc7_subr_2:
3056 case scm_tc7_subr_0:
3057 case scm_tc7_subr_3:
3058 case scm_tc7_lsubr_2:
3059 goto wrongnumargs;
3060 default:
3061 goto badfun;
3062 }
3063 }
3064 #ifdef SCM_CAUTIOUS
3065 if (SCM_IMP (x))
3066 goto wrongnumargs;
3067 else if (SCM_CONSP (x))
3068 {
3069 if (SCM_IMP (SCM_CAR (x)))
3070 arg2 = SCM_EVALIM (SCM_CAR (x), env);
3071 else
3072 arg2 = EVALCELLCAR (x, env);
3073 }
3074 else
3075 goto wrongnumargs;
3076 #else
3077 arg2 = EVALCAR (x, env);
3078 #endif
3079 { /* have two or more arguments */
3080 #ifdef DEVAL
3081 debug.info->a.args = scm_list_2 (t.arg1, arg2);
3082 #endif
3083 x = SCM_CDR (x);
3084 if (SCM_NULLP (x)) {
3085 ENTER_APPLY;
3086 evap2:
3087 switch (SCM_TYP7 (proc))
3088 { /* have two arguments */
3089 case scm_tc7_subr_2:
3090 case scm_tc7_subr_2o:
3091 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
3092 case scm_tc7_lsubr:
3093 #ifdef DEVAL
3094 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
3095 #else
3096 RETURN (SCM_SUBRF (proc) (scm_list_2 (t.arg1, arg2)));
3097 #endif
3098 case scm_tc7_lsubr_2:
3099 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_EOL));
3100 case scm_tc7_rpsubr:
3101 case scm_tc7_asubr:
3102 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
3103 case scm_tc7_smob:
3104 if (!SCM_SMOB_APPLICABLE_P (proc))
3105 goto badfun;
3106 RETURN (SCM_SMOB_APPLY_2 (proc, t.arg1, arg2));
3107 cclon:
3108 case scm_tc7_cclo:
3109 #ifdef DEVAL
3110 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
3111 scm_cons (proc, debug.info->a.args),
3112 SCM_EOL));
3113 #else
3114 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
3115 scm_cons2 (proc, t.arg1,
3116 scm_cons (arg2,
3117 scm_eval_args (x,
3118 env,
3119 proc))),
3120 SCM_EOL));
3121 #endif
3122 case scm_tcs_struct:
3123 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3124 {
3125 x = SCM_ENTITY_PROCEDURE (proc);
3126 #ifdef DEVAL
3127 t.arg1 = debug.info->a.args;
3128 #else
3129 t.arg1 = scm_list_2 (t.arg1, arg2);
3130 #endif
3131 goto type_dispatch;
3132 }
3133 else if (!SCM_I_OPERATORP (proc))
3134 goto badfun;
3135 else
3136 {
3137 operatorn:
3138 #ifdef DEVAL
3139 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
3140 ? SCM_ENTITY_PROCEDURE (proc)
3141 : SCM_OPERATOR_PROCEDURE (proc),
3142 scm_cons (proc, debug.info->a.args),
3143 SCM_EOL));
3144 #else
3145 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
3146 ? SCM_ENTITY_PROCEDURE (proc)
3147 : SCM_OPERATOR_PROCEDURE (proc),
3148 scm_cons2 (proc, t.arg1,
3149 scm_cons (arg2,
3150 scm_eval_args (x,
3151 env,
3152 proc))),
3153 SCM_EOL));
3154 #endif
3155 }
3156 case scm_tc7_subr_0:
3157 case scm_tc7_cxr:
3158 case scm_tc7_subr_1o:
3159 case scm_tc7_subr_1:
3160 case scm_tc7_subr_3:
3161 goto wrongnumargs;
3162 default:
3163 goto badfun;
3164 case scm_tc7_pws:
3165 proc = SCM_PROCEDURE (proc);
3166 #ifdef DEVAL
3167 debug.info->a.proc = proc;
3168 #endif
3169 if (!SCM_CLOSUREP (proc))
3170 goto evap2;
3171 if (scm_badformalsp (proc, 2))
3172 goto umwrongnumargs;
3173 case scm_tcs_closures:
3174 /* clos2: */
3175 #ifdef DEVAL
3176 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3177 debug.info->a.args,
3178 SCM_ENV (proc));
3179 #else
3180 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3181 scm_list_2 (t.arg1, arg2), SCM_ENV (proc));
3182 #endif
3183 x = SCM_CLOSURE_BODY (proc);
3184 goto nontoplevel_begin;
3185 }
3186 }
3187 #ifdef SCM_CAUTIOUS
3188 if (SCM_IMP (x) || !SCM_CONSP (x))
3189 goto wrongnumargs;
3190 #endif
3191 #ifdef DEVAL
3192 debug.info->a.args = scm_cons2 (t.arg1, arg2,
3193 deval_args (x, env, proc, SCM_CDRLOC (SCM_CDR (debug.info->a.args))));
3194 #endif
3195 ENTER_APPLY;
3196 evap3:
3197 switch (SCM_TYP7 (proc))
3198 { /* have 3 or more arguments */
3199 #ifdef DEVAL
3200 case scm_tc7_subr_3:
3201 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
3202 RETURN (SCM_SUBRF (proc) (t.arg1, arg2,
3203 SCM_CADDR (debug.info->a.args)));
3204 case scm_tc7_asubr:
3205 #ifdef BUILTIN_RPASUBR
3206 t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
3207 arg2 = SCM_CDDR (debug.info->a.args);
3208 do
3209 {
3210 t.arg1 = SCM_SUBRF(proc)(t.arg1, SCM_CAR (arg2));
3211 arg2 = SCM_CDR (arg2);
3212 }
3213 while (SCM_NIMP (arg2));
3214 RETURN (t.arg1);
3215 #endif /* BUILTIN_RPASUBR */
3216 case scm_tc7_rpsubr:
3217 #ifdef BUILTIN_RPASUBR
3218 if (SCM_FALSEP (SCM_SUBRF (proc) (t.arg1, arg2)))
3219 RETURN (SCM_BOOL_F);
3220 t.arg1 = SCM_CDDR (debug.info->a.args);
3221 do
3222 {
3223 if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, SCM_CAR (t.arg1))))
3224 RETURN (SCM_BOOL_F);
3225 arg2 = SCM_CAR (t.arg1);
3226 t.arg1 = SCM_CDR (t.arg1);
3227 }
3228 while (SCM_NIMP (t.arg1));
3229 RETURN (SCM_BOOL_T);
3230 #else /* BUILTIN_RPASUBR */
3231 RETURN (SCM_APPLY (proc, t.arg1,
3232 scm_acons (arg2,
3233 SCM_CDDR (debug.info->a.args),
3234 SCM_EOL)));
3235 #endif /* BUILTIN_RPASUBR */
3236 case scm_tc7_lsubr_2:
3237 RETURN (SCM_SUBRF (proc) (t.arg1, arg2,
3238 SCM_CDDR (debug.info->a.args)));
3239 case scm_tc7_lsubr:
3240 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
3241 case scm_tc7_smob:
3242 if (!SCM_SMOB_APPLICABLE_P (proc))
3243 goto badfun;
3244 RETURN (SCM_SMOB_APPLY_3 (proc, t.arg1, arg2,
3245 SCM_CDDR (debug.info->a.args)));
3246 case scm_tc7_cclo:
3247 goto cclon;
3248 case scm_tc7_pws:
3249 proc = SCM_PROCEDURE (proc);
3250 debug.info->a.proc = proc;
3251 if (!SCM_CLOSUREP (proc))
3252 goto evap3;
3253 if (scm_badargsp (SCM_CLOSURE_FORMALS (proc), debug.info->a.args))
3254 goto umwrongnumargs;
3255 case scm_tcs_closures:
3256 SCM_SET_ARGSREADY (debug);
3257 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3258 debug.info->a.args,
3259 SCM_ENV (proc));
3260 x = SCM_CLOSURE_BODY (proc);
3261 goto nontoplevel_begin;
3262 #else /* DEVAL */
3263 case scm_tc7_subr_3:
3264 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
3265 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, EVALCAR (x, env)));
3266 case scm_tc7_asubr:
3267 #ifdef BUILTIN_RPASUBR
3268 t.arg1 = SCM_SUBRF (proc) (t.arg1, arg2);
3269 do
3270 {
3271 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
3272 x = SCM_CDR(x);
3273 }
3274 while (SCM_NIMP (x));
3275 RETURN (t.arg1);
3276 #endif /* BUILTIN_RPASUBR */
3277 case scm_tc7_rpsubr:
3278 #ifdef BUILTIN_RPASUBR
3279 if (SCM_FALSEP (SCM_SUBRF (proc) (t.arg1, arg2)))
3280 RETURN (SCM_BOOL_F);
3281 do
3282 {
3283 t.arg1 = EVALCAR (x, env);
3284 if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, t.arg1)))
3285 RETURN (SCM_BOOL_F);
3286 arg2 = t.arg1;
3287 x = SCM_CDR (x);
3288 }
3289 while (SCM_NIMP (x));
3290 RETURN (SCM_BOOL_T);
3291 #else /* BUILTIN_RPASUBR */
3292 RETURN (SCM_APPLY (proc, t.arg1,
3293 scm_acons (arg2,
3294 scm_eval_args (x, env, proc),
3295 SCM_EOL)));
3296 #endif /* BUILTIN_RPASUBR */
3297 case scm_tc7_lsubr_2:
3298 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, scm_eval_args (x, env, proc)));
3299 case scm_tc7_lsubr:
3300 RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1,
3301 arg2,
3302 scm_eval_args (x, env, proc))));
3303 case scm_tc7_smob:
3304 if (!SCM_SMOB_APPLICABLE_P (proc))
3305 goto badfun;
3306 RETURN (SCM_SMOB_APPLY_3 (proc, t.arg1, arg2,
3307 scm_eval_args (x, env, proc)));
3308 case scm_tc7_cclo:
3309 goto cclon;
3310 case scm_tc7_pws:
3311 proc = SCM_PROCEDURE (proc);
3312 if (!SCM_CLOSUREP (proc))
3313 goto evap3;
3314 {
3315 SCM formals = SCM_CLOSURE_FORMALS (proc);
3316 if (SCM_NULLP (formals)
3317 || (SCM_CONSP (formals)
3318 && (SCM_NULLP (SCM_CDR (formals))
3319 || (SCM_CONSP (SCM_CDR (formals))
3320 && scm_badargsp (SCM_CDDR (formals), x)))))
3321 goto umwrongnumargs;
3322 }
3323 case scm_tcs_closures:
3324 #ifdef DEVAL
3325 SCM_SET_ARGSREADY (debug);
3326 #endif
3327 env = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
3328 scm_cons2 (t.arg1,
3329 arg2,
3330 scm_eval_args (x, env, proc)),
3331 SCM_ENV (proc));
3332 x = SCM_CLOSURE_BODY (proc);
3333 goto nontoplevel_begin;
3334 #endif /* DEVAL */
3335 case scm_tcs_struct:
3336 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3337 {
3338 #ifdef DEVAL
3339 t.arg1 = debug.info->a.args;
3340 #else
3341 t.arg1 = scm_cons2 (t.arg1, arg2, scm_eval_args (x, env, proc));
3342 #endif
3343 x = SCM_ENTITY_PROCEDURE (proc);
3344 goto type_dispatch;
3345 }
3346 else if (!SCM_I_OPERATORP (proc))
3347 goto badfun;
3348 else
3349 goto operatorn;
3350 case scm_tc7_subr_2:
3351 case scm_tc7_subr_1o:
3352 case scm_tc7_subr_2o:
3353 case scm_tc7_subr_0:
3354 case scm_tc7_cxr:
3355 case scm_tc7_subr_1:
3356 goto wrongnumargs;
3357 default:
3358 goto badfun;
3359 }
3360 }
3361 #ifdef DEVAL
3362 exit:
3363 if (CHECK_EXIT && SCM_TRAPS_P)
3364 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
3365 {
3366 SCM_CLEAR_TRACED_FRAME (debug);
3367 if (SCM_CHEAPTRAPS_P)
3368 t.arg1 = scm_make_debugobj (&debug);
3369 else
3370 {
3371 int first;
3372 SCM val = scm_make_continuation (&first);
3373
3374 if (first)
3375 t.arg1 = val;
3376 else
3377 {
3378 proc = val;
3379 goto ret;
3380 }
3381 }
3382 SCM_TRAPS_P = 0;
3383 scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, t.arg1, proc);
3384 SCM_TRAPS_P = 1;
3385 }
3386 ret:
3387 scm_last_debug_frame = debug.prev;
3388 return proc;
3389 #endif
3390 }
3391
3392
3393 /* SECTION: This code is compiled once.
3394 */
3395
3396 #ifndef DEVAL
3397
3398 \f
3399 /* Simple procedure calls
3400 */
3401
3402 SCM
3403 scm_call_0 (SCM proc)
3404 {
3405 return scm_apply (proc, SCM_EOL, SCM_EOL);
3406 }
3407
3408 SCM
3409 scm_call_1 (SCM proc, SCM arg1)
3410 {
3411 return scm_apply (proc, arg1, scm_listofnull);
3412 }
3413
3414 SCM
3415 scm_call_2 (SCM proc, SCM arg1, SCM arg2)
3416 {
3417 return scm_apply (proc, arg1, scm_cons (arg2, scm_listofnull));
3418 }
3419
3420 SCM
3421 scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
3422 {
3423 return scm_apply (proc, arg1, scm_cons2 (arg2, arg3, scm_listofnull));
3424 }
3425
3426 SCM
3427 scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
3428 {
3429 return scm_apply (proc, arg1, scm_cons2 (arg2, arg3,
3430 scm_cons (arg4, scm_listofnull)));
3431 }
3432
3433 /* Simple procedure applies
3434 */
3435
3436 SCM
3437 scm_apply_0 (SCM proc, SCM args)
3438 {
3439 return scm_apply (proc, args, SCM_EOL);
3440 }
3441
3442 SCM
3443 scm_apply_1 (SCM proc, SCM arg1, SCM args)
3444 {
3445 return scm_apply (proc, scm_cons (arg1, args), SCM_EOL);
3446 }
3447
3448 SCM
3449 scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
3450 {
3451 return scm_apply (proc, scm_cons2 (arg1, arg2, args), SCM_EOL);
3452 }
3453
3454 SCM
3455 scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
3456 {
3457 return scm_apply (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)),
3458 SCM_EOL);
3459 }
3460
3461 /* This code processes the arguments to apply:
3462
3463 (apply PROC ARG1 ... ARGS)
3464
3465 Given a list (ARG1 ... ARGS), this function conses the ARG1
3466 ... arguments onto the front of ARGS, and returns the resulting
3467 list. Note that ARGS is a list; thus, the argument to this
3468 function is a list whose last element is a list.
3469
3470 Apply calls this function, and applies PROC to the elements of the
3471 result. apply:nconc2last takes care of building the list of
3472 arguments, given (ARG1 ... ARGS).
3473
3474 Rather than do new consing, apply:nconc2last destroys its argument.
3475 On that topic, this code came into my care with the following
3476 beautifully cryptic comment on that topic: "This will only screw
3477 you if you do (scm_apply scm_apply '( ... ))" If you know what
3478 they're referring to, send me a patch to this comment. */
3479
3480 SCM_DEFINE (scm_nconc2last, "apply:nconc2last", 1, 0, 0,
3481 (SCM lst),
3482 "Given a list (@var{arg1} @dots{} @var{args}), this function\n"
3483 "conses the @var{arg1} @dots{} arguments onto the front of\n"
3484 "@var{args}, and returns the resulting list. Note that\n"
3485 "@var{args} is a list; thus, the argument to this function is\n"
3486 "a list whose last element is a list.\n"
3487 "Note: Rather than do new consing, @code{apply:nconc2last}\n"
3488 "destroys its argument, so use with care.")
3489 #define FUNC_NAME s_scm_nconc2last
3490 {
3491 SCM *lloc;
3492 SCM_VALIDATE_NONEMPTYLIST (1,lst);
3493 lloc = &lst;
3494 while (!SCM_NULLP (SCM_CDR (*lloc))) /* Perhaps should be
3495 SCM_NULL_OR_NIL_P, but not
3496 needed in 99.99% of cases,
3497 and it could seriously hurt
3498 performance. - Neil */
3499 lloc = SCM_CDRLOC (*lloc);
3500 SCM_ASSERT (scm_ilength (SCM_CAR (*lloc)) >= 0, lst, SCM_ARG1, FUNC_NAME);
3501 *lloc = SCM_CAR (*lloc);
3502 return lst;
3503 }
3504 #undef FUNC_NAME
3505
3506 #endif /* !DEVAL */
3507
3508
3509 /* SECTION: When DEVAL is defined this code yields scm_dapply.
3510 * It is compiled twice.
3511 */
3512
3513 #if 0
3514 SCM
3515 scm_apply (SCM proc, SCM arg1, SCM args)
3516 {}
3517 #endif
3518
3519 #if 0
3520 SCM
3521 scm_dapply (SCM proc, SCM arg1, SCM args)
3522 { /* empty */ }
3523 #endif
3524
3525
3526 /* Apply a function to a list of arguments.
3527
3528 This function is exported to the Scheme level as taking two
3529 required arguments and a tail argument, as if it were:
3530 (lambda (proc arg1 . args) ...)
3531 Thus, if you just have a list of arguments to pass to a procedure,
3532 pass the list as ARG1, and '() for ARGS. If you have some fixed
3533 args, pass the first as ARG1, then cons any remaining fixed args
3534 onto the front of your argument list, and pass that as ARGS. */
3535
3536 SCM
3537 SCM_APPLY (SCM proc, SCM arg1, SCM args)
3538 {
3539 #ifdef DEBUG_EXTENSIONS
3540 #ifdef DEVAL
3541 scm_t_debug_frame debug;
3542 scm_t_debug_info debug_vect_body;
3543 debug.prev = scm_last_debug_frame;
3544 debug.status = SCM_APPLYFRAME;
3545 debug.vect = &debug_vect_body;
3546 debug.vect[0].a.proc = proc;
3547 debug.vect[0].a.args = SCM_EOL;
3548 scm_last_debug_frame = &debug;
3549 #else
3550 if (SCM_DEBUGGINGP)
3551 return scm_dapply (proc, arg1, args);
3552 #endif
3553 #endif
3554
3555 SCM_ASRTGO (SCM_NIMP (proc), badproc);
3556
3557 /* If ARGS is the empty list, then we're calling apply with only two
3558 arguments --- ARG1 is the list of arguments for PROC. Whatever
3559 the case, futz with things so that ARG1 is the first argument to
3560 give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
3561 rest.
3562
3563 Setting the debug apply frame args this way is pretty messy.
3564 Perhaps we should store arg1 and args directly in the frame as
3565 received, and let scm_frame_arguments unpack them, because that's
3566 a relatively rare operation. This works for now; if the Guile
3567 developer archives are still around, see Mikael's post of
3568 11-Apr-97. */
3569 if (SCM_NULLP (args))
3570 {
3571 if (SCM_NULLP (arg1))
3572 {
3573 arg1 = SCM_UNDEFINED;
3574 #ifdef DEVAL
3575 debug.vect[0].a.args = SCM_EOL;
3576 #endif
3577 }
3578 else
3579 {
3580 #ifdef DEVAL
3581 debug.vect[0].a.args = arg1;
3582 #endif
3583 args = SCM_CDR (arg1);
3584 arg1 = SCM_CAR (arg1);
3585 }
3586 }
3587 else
3588 {
3589 args = scm_nconc2last (args);
3590 #ifdef DEVAL
3591 debug.vect[0].a.args = scm_cons (arg1, args);
3592 #endif
3593 }
3594 #ifdef DEVAL
3595 if (SCM_ENTER_FRAME_P && SCM_TRAPS_P)
3596 {
3597 SCM tmp;
3598 if (SCM_CHEAPTRAPS_P)
3599 tmp = scm_make_debugobj (&debug);
3600 else
3601 {
3602 int first;
3603
3604 tmp = scm_make_continuation (&first);
3605 if (!first)
3606 goto entap;
3607 }
3608 SCM_TRAPS_P = 0;
3609 scm_call_2 (SCM_ENTER_FRAME_HDLR, scm_sym_enter_frame, tmp);
3610 SCM_TRAPS_P = 1;
3611 }
3612 entap:
3613 ENTER_APPLY;
3614 #endif
3615 tail:
3616 switch (SCM_TYP7 (proc))
3617 {
3618 case scm_tc7_subr_2o:
3619 args = SCM_NULLP (args) ? SCM_UNDEFINED : SCM_CAR (args);
3620 RETURN (SCM_SUBRF (proc) (arg1, args));
3621 case scm_tc7_subr_2:
3622 SCM_ASRTGO (!SCM_NULLP (args) && SCM_NULLP (SCM_CDR (args)),
3623 wrongnumargs);
3624 args = SCM_CAR (args);
3625 RETURN (SCM_SUBRF (proc) (arg1, args));
3626 case scm_tc7_subr_0:
3627 SCM_ASRTGO (SCM_UNBNDP (arg1), wrongnumargs);
3628 RETURN (SCM_SUBRF (proc) ());
3629 case scm_tc7_subr_1:
3630 SCM_ASRTGO (!SCM_UNBNDP (arg1), wrongnumargs);
3631 case scm_tc7_subr_1o:
3632 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
3633 RETURN (SCM_SUBRF (proc) (arg1));
3634 case scm_tc7_cxr:
3635 SCM_ASRTGO (!SCM_UNBNDP (arg1) && SCM_NULLP (args), wrongnumargs);
3636 if (SCM_SUBRF (proc))
3637 {
3638 if (SCM_INUMP (arg1))
3639 {
3640 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1))));
3641 }
3642 else if (SCM_REALP (arg1))
3643 {
3644 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
3645 }
3646 #ifdef SCM_BIGDIG
3647 else if (SCM_BIGP (arg1))
3648 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
3649 #endif
3650 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
3651 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
3652 }
3653 proc = SCM_SNAME (proc);
3654 {
3655 char *chrs = SCM_SYMBOL_CHARS (proc) + SCM_SYMBOL_LENGTH (proc) - 1;
3656 while ('c' != *--chrs)
3657 {
3658 SCM_ASSERT (SCM_CONSP (arg1),
3659 arg1, SCM_ARG1, SCM_SYMBOL_CHARS (proc));
3660 arg1 = ('a' == *chrs) ? SCM_CAR (arg1) : SCM_CDR (arg1);
3661 }
3662 RETURN (arg1);
3663 }
3664 case scm_tc7_subr_3:
3665 SCM_ASRTGO (!SCM_NULLP (args)
3666 && !SCM_NULLP (SCM_CDR (args))
3667 && SCM_NULLP (SCM_CDDR (args)),
3668 wrongnumargs);
3669 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CADR (args)));
3670 case scm_tc7_lsubr:
3671 #ifdef DEVAL
3672 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args));
3673 #else
3674 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)));
3675 #endif
3676 case scm_tc7_lsubr_2:
3677 SCM_ASRTGO (SCM_CONSP (args), wrongnumargs);
3678 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)));
3679 case scm_tc7_asubr:
3680 if (SCM_NULLP (args))
3681 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
3682 while (SCM_NIMP (args))
3683 {
3684 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
3685 arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
3686 args = SCM_CDR (args);
3687 }
3688 RETURN (arg1);
3689 case scm_tc7_rpsubr:
3690 if (SCM_NULLP (args))
3691 RETURN (SCM_BOOL_T);
3692 while (SCM_NIMP (args))
3693 {
3694 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
3695 if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
3696 RETURN (SCM_BOOL_F);
3697 arg1 = SCM_CAR (args);
3698 args = SCM_CDR (args);
3699 }
3700 RETURN (SCM_BOOL_T);
3701 case scm_tcs_closures:
3702 #ifdef DEVAL
3703 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
3704 #else
3705 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
3706 #endif
3707 #ifndef SCM_RECKLESS
3708 if (scm_badargsp (SCM_CLOSURE_FORMALS (proc), arg1))
3709 goto wrongnumargs;
3710 #endif
3711
3712 /* Copy argument list */
3713 if (SCM_IMP (arg1))
3714 args = arg1;
3715 else
3716 {
3717 SCM tl = args = scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED);
3718 while (arg1 = SCM_CDR (arg1), SCM_CONSP (arg1))
3719 {
3720 SCM_SETCDR (tl, scm_cons (SCM_CAR (arg1),
3721 SCM_UNSPECIFIED));
3722 tl = SCM_CDR (tl);
3723 }
3724 SCM_SETCDR (tl, arg1);
3725 }
3726
3727 args = EXTEND_ENV (SCM_CLOSURE_FORMALS (proc), args, SCM_ENV (proc));
3728 proc = SCM_CLOSURE_BODY (proc);
3729 again:
3730 arg1 = proc;
3731 while (!SCM_NULLP (arg1 = SCM_CDR (arg1)))
3732 {
3733 if (SCM_IMP (SCM_CAR (proc)))
3734 {
3735 if (SCM_ISYMP (SCM_CAR (proc)))
3736 {
3737 proc = scm_m_expand_body (proc, args);
3738 goto again;
3739 }
3740 else
3741 SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (proc));
3742 }
3743 else
3744 SCM_CEVAL (SCM_CAR (proc), args);
3745 proc = arg1;
3746 }
3747 RETURN (EVALCAR (proc, args));
3748 case scm_tc7_smob:
3749 if (!SCM_SMOB_APPLICABLE_P (proc))
3750 goto badproc;
3751 if (SCM_UNBNDP (arg1))
3752 RETURN (SCM_SMOB_APPLY_0 (proc));
3753 else if (SCM_NULLP (args))
3754 RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
3755 else if (SCM_NULLP (SCM_CDR (args)))
3756 RETURN (SCM_SMOB_APPLY_2 (proc, arg1, SCM_CAR (args)));
3757 else
3758 RETURN (SCM_SMOB_APPLY_3 (proc, arg1, SCM_CAR (args), SCM_CDR (args)));
3759 case scm_tc7_cclo:
3760 #ifdef DEVAL
3761 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
3762 arg1 = proc;
3763 proc = SCM_CCLO_SUBR (proc);
3764 debug.vect[0].a.proc = proc;
3765 debug.vect[0].a.args = scm_cons (arg1, args);
3766 #else
3767 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
3768 arg1 = proc;
3769 proc = SCM_CCLO_SUBR (proc);
3770 #endif
3771 goto tail;
3772 case scm_tc7_pws:
3773 proc = SCM_PROCEDURE (proc);
3774 #ifdef DEVAL
3775 debug.vect[0].a.proc = proc;
3776 #endif
3777 goto tail;
3778 case scm_tcs_struct:
3779 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3780 {
3781 #ifdef DEVAL
3782 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
3783 #else
3784 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
3785 #endif
3786 RETURN (scm_apply_generic (proc, args));
3787 }
3788 else if (!SCM_I_OPERATORP (proc))
3789 goto badproc;
3790 else
3791 {
3792 #ifdef DEVAL
3793 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
3794 #else
3795 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
3796 #endif
3797 arg1 = proc;
3798 proc = (SCM_I_ENTITYP (proc)
3799 ? SCM_ENTITY_PROCEDURE (proc)
3800 : SCM_OPERATOR_PROCEDURE (proc));
3801 #ifdef DEVAL
3802 debug.vect[0].a.proc = proc;
3803 debug.vect[0].a.args = scm_cons (arg1, args);
3804 #endif
3805 if (SCM_NIMP (proc))
3806 goto tail;
3807 else
3808 goto badproc;
3809 }
3810 wrongnumargs:
3811 scm_wrong_num_args (proc);
3812 default:
3813 badproc:
3814 scm_wrong_type_arg ("apply", SCM_ARG1, proc);
3815 RETURN (arg1);
3816 }
3817 #ifdef DEVAL
3818 exit:
3819 if (CHECK_EXIT && SCM_TRAPS_P)
3820 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
3821 {
3822 SCM_CLEAR_TRACED_FRAME (debug);
3823 if (SCM_CHEAPTRAPS_P)
3824 arg1 = scm_make_debugobj (&debug);
3825 else
3826 {
3827 int first;
3828 SCM val = scm_make_continuation (&first);
3829
3830 if (first)
3831 arg1 = val;
3832 else
3833 {
3834 proc = val;
3835 goto ret;
3836 }
3837 }
3838 SCM_TRAPS_P = 0;
3839 scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
3840 SCM_TRAPS_P = 1;
3841 }
3842 ret:
3843 scm_last_debug_frame = debug.prev;
3844 return proc;
3845 #endif
3846 }
3847
3848
3849 /* SECTION: The rest of this file is only read once.
3850 */
3851
3852 #ifndef DEVAL
3853
3854 /* Typechecking for multi-argument MAP and FOR-EACH.
3855
3856 Verify that each element of the vector ARGV, except for the first,
3857 is a proper list whose length is LEN. Attribute errors to WHO,
3858 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
3859 static inline void
3860 check_map_args (SCM argv,
3861 long len,
3862 SCM gf,
3863 SCM proc,
3864 SCM args,
3865 const char *who)
3866 {
3867 SCM *ve = SCM_VELTS (argv);
3868 long i;
3869
3870 for (i = SCM_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
3871 {
3872 long elt_len = scm_ilength (ve[i]);
3873
3874 if (elt_len < 0)
3875 {
3876 if (gf)
3877 scm_apply_generic (gf, scm_cons (proc, args));
3878 else
3879 scm_wrong_type_arg (who, i + 2, ve[i]);
3880 }
3881
3882 if (elt_len != len)
3883 scm_out_of_range (who, ve[i]);
3884 }
3885
3886 scm_remember_upto_here_1 (argv);
3887 }
3888
3889
3890 SCM_GPROC (s_map, "map", 2, 0, 1, scm_map, g_map);
3891
3892 /* Note: Currently, scm_map applies PROC to the argument list(s)
3893 sequentially, starting with the first element(s). This is used in
3894 evalext.c where the Scheme procedure `map-in-order', which guarantees
3895 sequential behaviour, is implemented using scm_map. If the
3896 behaviour changes, we need to update `map-in-order'.
3897 */
3898
3899 SCM
3900 scm_map (SCM proc, SCM arg1, SCM args)
3901 #define FUNC_NAME s_map
3902 {
3903 long i, len;
3904 SCM res = SCM_EOL;
3905 SCM *pres = &res;
3906 SCM *ve = &args; /* Keep args from being optimized away. */
3907
3908 len = scm_ilength (arg1);
3909 SCM_GASSERTn (len >= 0,
3910 g_map, scm_cons2 (proc, arg1, args), SCM_ARG2, s_map);
3911 SCM_VALIDATE_REST_ARGUMENT (args);
3912 if (SCM_NULLP (args))
3913 {
3914 while (SCM_NIMP (arg1))
3915 {
3916 *pres = scm_list_1 (scm_apply (proc, SCM_CAR (arg1), scm_listofnull));
3917 pres = SCM_CDRLOC (*pres);
3918 arg1 = SCM_CDR (arg1);
3919 }
3920 return res;
3921 }
3922 args = scm_vector (arg1 = scm_cons (arg1, args));
3923 ve = SCM_VELTS (args);
3924 #ifndef SCM_RECKLESS
3925 check_map_args (args, len, g_map, proc, arg1, s_map);
3926 #endif
3927 while (1)
3928 {
3929 arg1 = SCM_EOL;
3930 for (i = SCM_VECTOR_LENGTH (args) - 1; i >= 0; i--)
3931 {
3932 if (SCM_IMP (ve[i]))
3933 return res;
3934 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
3935 ve[i] = SCM_CDR (ve[i]);
3936 }
3937 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
3938 pres = SCM_CDRLOC (*pres);
3939 }
3940 }
3941 #undef FUNC_NAME
3942
3943
3944 SCM_GPROC (s_for_each, "for-each", 2, 0, 1, scm_for_each, g_for_each);
3945
3946 SCM
3947 scm_for_each (SCM proc, SCM arg1, SCM args)
3948 #define FUNC_NAME s_for_each
3949 {
3950 SCM *ve = &args; /* Keep args from being optimized away. */
3951 long i, len;
3952 len = scm_ilength (arg1);
3953 SCM_GASSERTn (len >= 0, g_for_each, scm_cons2 (proc, arg1, args),
3954 SCM_ARG2, s_for_each);
3955 SCM_VALIDATE_REST_ARGUMENT (args);
3956 if (SCM_NULLP (args))
3957 {
3958 while (SCM_NIMP (arg1))
3959 {
3960 scm_apply (proc, SCM_CAR (arg1), scm_listofnull);
3961 arg1 = SCM_CDR (arg1);
3962 }
3963 return SCM_UNSPECIFIED;
3964 }
3965 args = scm_vector (arg1 = scm_cons (arg1, args));
3966 ve = SCM_VELTS (args);
3967 #ifndef SCM_RECKLESS
3968 check_map_args (args, len, g_for_each, proc, arg1, s_for_each);
3969 #endif
3970 while (1)
3971 {
3972 arg1 = SCM_EOL;
3973 for (i = SCM_VECTOR_LENGTH (args) - 1; i >= 0; i--)
3974 {
3975 if (SCM_IMP (ve[i]))
3976 return SCM_UNSPECIFIED;
3977 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
3978 ve[i] = SCM_CDR (ve[i]);
3979 }
3980 scm_apply (proc, arg1, SCM_EOL);
3981 }
3982 }
3983 #undef FUNC_NAME
3984
3985
3986 SCM
3987 scm_closure (SCM code, SCM env)
3988 {
3989 SCM z;
3990 SCM closcar = scm_cons (code, SCM_EOL);
3991 z = scm_cell (SCM_UNPACK (closcar) + scm_tc3_closure, (scm_t_bits) env);
3992 scm_remember_upto_here (closcar);
3993 return z;
3994 }
3995
3996
3997 scm_t_bits scm_tc16_promise;
3998
3999 SCM
4000 scm_makprom (SCM code)
4001 {
4002 SCM_RETURN_NEWSMOB (scm_tc16_promise, SCM_UNPACK (code));
4003 }
4004
4005
4006
4007 static int
4008 promise_print (SCM exp, SCM port, scm_print_state *pstate)
4009 {
4010 int writingp = SCM_WRITINGP (pstate);
4011 scm_puts ("#<promise ", port);
4012 SCM_SET_WRITINGP (pstate, 1);
4013 scm_iprin1 (SCM_CELL_OBJECT_1 (exp), port, pstate);
4014 SCM_SET_WRITINGP (pstate, writingp);
4015 scm_putc ('>', port);
4016 return !0;
4017 }
4018
4019
4020 SCM_DEFINE (scm_force, "force", 1, 0, 0,
4021 (SCM x),
4022 "If the promise @var{x} has not been computed yet, compute and\n"
4023 "return @var{x}, otherwise just return the previously computed\n"
4024 "value.")
4025 #define FUNC_NAME s_scm_force
4026 {
4027 SCM_VALIDATE_SMOB (1, x, promise);
4028 if (!((1L << 16) & SCM_CELL_WORD_0 (x)))
4029 {
4030 SCM ans = scm_call_0 (SCM_CELL_OBJECT_1 (x));
4031 if (!((1L << 16) & SCM_CELL_WORD_0 (x)))
4032 {
4033 SCM_DEFER_INTS;
4034 SCM_SET_CELL_OBJECT_1 (x, ans);
4035 SCM_SET_CELL_WORD_0 (x, SCM_CELL_WORD_0 (x) | (1L << 16));
4036 SCM_ALLOW_INTS;
4037 }
4038 }
4039 return SCM_CELL_OBJECT_1 (x);
4040 }
4041 #undef FUNC_NAME
4042
4043
4044 SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
4045 (SCM obj),
4046 "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
4047 "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
4048 #define FUNC_NAME s_scm_promise_p
4049 {
4050 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
4051 }
4052 #undef FUNC_NAME
4053
4054
4055 SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
4056 (SCM xorig, SCM x, SCM y),
4057 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
4058 "Any source properties associated with @var{xorig} are also associated\n"
4059 "with the new pair.")
4060 #define FUNC_NAME s_scm_cons_source
4061 {
4062 SCM p, z;
4063 z = scm_cons (x, y);
4064 /* Copy source properties possibly associated with xorig. */
4065 p = scm_whash_lookup (scm_source_whash, xorig);
4066 if (!SCM_IMP (p))
4067 scm_whash_insert (scm_source_whash, z, p);
4068 return z;
4069 }
4070 #undef FUNC_NAME
4071
4072
4073 SCM_DEFINE (scm_copy_tree, "copy-tree", 1, 0, 0,
4074 (SCM obj),
4075 "Recursively copy the data tree that is bound to @var{obj}, and return a\n"
4076 "pointer to the new data structure. @code{copy-tree} recurses down the\n"
4077 "contents of both pairs and vectors (since both cons cells and vector\n"
4078 "cells may point to arbitrary objects), and stops recursing when it hits\n"
4079 "any other object.")
4080 #define FUNC_NAME s_scm_copy_tree
4081 {
4082 SCM ans, tl;
4083 if (SCM_IMP (obj))
4084 return obj;
4085 if (SCM_VECTORP (obj))
4086 {
4087 unsigned long i = SCM_VECTOR_LENGTH (obj);
4088 ans = scm_c_make_vector (i, SCM_UNSPECIFIED);
4089 while (i--)
4090 SCM_VELTS (ans)[i] = scm_copy_tree (SCM_VELTS (obj)[i]);
4091 return ans;
4092 }
4093 if (!SCM_CONSP (obj))
4094 return obj;
4095 ans = tl = scm_cons_source (obj,
4096 scm_copy_tree (SCM_CAR (obj)),
4097 SCM_UNSPECIFIED);
4098 while (obj = SCM_CDR (obj), SCM_CONSP (obj))
4099 {
4100 SCM_SETCDR (tl, scm_cons (scm_copy_tree (SCM_CAR (obj)),
4101 SCM_UNSPECIFIED));
4102 tl = SCM_CDR (tl);
4103 }
4104 SCM_SETCDR (tl, obj);
4105 return ans;
4106 }
4107 #undef FUNC_NAME
4108
4109
4110 /* We have three levels of EVAL here:
4111
4112 - scm_i_eval (exp, env)
4113
4114 evaluates EXP in environment ENV. ENV is a lexical environment
4115 structure as used by the actual tree code evaluator. When ENV is
4116 a top-level environment, then changes to the current module are
4117 tracked by updating ENV so that it continues to be in sync with
4118 the current module.
4119
4120 - scm_primitive_eval (exp)
4121
4122 evaluates EXP in the top-level environment as determined by the
4123 current module. This is done by constructing a suitable
4124 environment and calling scm_i_eval. Thus, changes to the
4125 top-level module are tracked normally.
4126
4127 - scm_eval (exp, mod)
4128
4129 evaluates EXP while MOD is the current module. This is done by
4130 setting the current module to MOD, invoking scm_primitive_eval on
4131 EXP, and then restoring the current module to the value it had
4132 previously. That is, while EXP is evaluated, changes to the
4133 current module are tracked, but these changes do not persist when
4134 scm_eval returns.
4135
4136 For each level of evals, there are two variants, distinguished by a
4137 _x suffix: the ordinary variant does not modify EXP while the _x
4138 variant can destructively modify EXP into something completely
4139 unintelligible. A Scheme data structure passed as EXP to one of the
4140 _x variants should not ever be used again for anything. So when in
4141 doubt, use the ordinary variant.
4142
4143 */
4144
4145 SCM
4146 scm_i_eval_x (SCM exp, SCM env)
4147 {
4148 return SCM_XEVAL (exp, env);
4149 }
4150
4151 SCM
4152 scm_i_eval (SCM exp, SCM env)
4153 {
4154 exp = scm_copy_tree (exp);
4155 return SCM_XEVAL (exp, env);
4156 }
4157
4158 SCM
4159 scm_primitive_eval_x (SCM exp)
4160 {
4161 SCM env;
4162 SCM transformer = scm_current_module_transformer ();
4163 if (SCM_NIMP (transformer))
4164 exp = scm_call_1 (transformer, exp);
4165 env = scm_top_level_env (scm_current_module_lookup_closure ());
4166 return scm_i_eval_x (exp, env);
4167 }
4168
4169 SCM_DEFINE (scm_primitive_eval, "primitive-eval", 1, 0, 0,
4170 (SCM exp),
4171 "Evaluate @var{exp} in the top-level environment specified by\n"
4172 "the current module.")
4173 #define FUNC_NAME s_scm_primitive_eval
4174 {
4175 SCM env;
4176 SCM transformer = scm_current_module_transformer ();
4177 if (SCM_NIMP (transformer))
4178 exp = scm_call_1 (transformer, exp);
4179 env = scm_top_level_env (scm_current_module_lookup_closure ());
4180 return scm_i_eval (exp, env);
4181 }
4182 #undef FUNC_NAME
4183
4184 /* Eval does not take the second arg optionally. This is intentional
4185 * in order to be R5RS compatible, and to prepare for the new module
4186 * system, where we would like to make the choice of evaluation
4187 * environment explicit. */
4188
4189 static void
4190 change_environment (void *data)
4191 {
4192 SCM pair = SCM_PACK (data);
4193 SCM new_module = SCM_CAR (pair);
4194 SCM old_module = scm_current_module ();
4195 SCM_SETCDR (pair, old_module);
4196 scm_set_current_module (new_module);
4197 }
4198
4199
4200 static void
4201 restore_environment (void *data)
4202 {
4203 SCM pair = SCM_PACK (data);
4204 SCM old_module = SCM_CDR (pair);
4205 SCM new_module = scm_current_module ();
4206 SCM_SETCAR (pair, new_module);
4207 scm_set_current_module (old_module);
4208 }
4209
4210 static SCM
4211 inner_eval_x (void *data)
4212 {
4213 return scm_primitive_eval_x (SCM_PACK(data));
4214 }
4215
4216 SCM
4217 scm_eval_x (SCM exp, SCM module)
4218 #define FUNC_NAME "eval!"
4219 {
4220 SCM_VALIDATE_MODULE (2, module);
4221
4222 return scm_internal_dynamic_wind
4223 (change_environment, inner_eval_x, restore_environment,
4224 (void *) SCM_UNPACK (exp),
4225 (void *) SCM_UNPACK (scm_cons (module, SCM_BOOL_F)));
4226 }
4227 #undef FUNC_NAME
4228
4229 static SCM
4230 inner_eval (void *data)
4231 {
4232 return scm_primitive_eval (SCM_PACK(data));
4233 }
4234
4235 SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
4236 (SCM exp, SCM module),
4237 "Evaluate @var{exp}, a list representing a Scheme expression,\n"
4238 "in the top-level environment specified by @var{module}.\n"
4239 "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
4240 "@var{module} is made the current module. The current module\n"
4241 "is reset to its previous value when @var{eval} returns.")
4242 #define FUNC_NAME s_scm_eval
4243 {
4244 SCM_VALIDATE_MODULE (2, module);
4245
4246 return scm_internal_dynamic_wind
4247 (change_environment, inner_eval, restore_environment,
4248 (void *) SCM_UNPACK (exp),
4249 (void *) SCM_UNPACK (scm_cons (module, SCM_BOOL_F)));
4250 }
4251 #undef FUNC_NAME
4252
4253
4254 /* At this point, scm_deval and scm_dapply are generated.
4255 */
4256
4257 #ifdef DEBUG_EXTENSIONS
4258 # define DEVAL
4259 # include "eval.c"
4260 #endif
4261
4262
4263
4264 void
4265 scm_init_eval ()
4266 {
4267 scm_init_opts (scm_evaluator_traps,
4268 scm_evaluator_trap_table,
4269 SCM_N_EVALUATOR_TRAPS);
4270 scm_init_opts (scm_eval_options_interface,
4271 scm_eval_opts,
4272 SCM_N_EVAL_OPTIONS);
4273
4274 scm_tc16_promise = scm_make_smob_type ("promise", 0);
4275 scm_set_smob_mark (scm_tc16_promise, scm_markcdr);
4276 scm_set_smob_print (scm_tc16_promise, promise_print);
4277
4278 /* Dirk:Fixme:: make scm_undefineds local to eval.c: it's only used here. */
4279 scm_undefineds = scm_list_1 (SCM_UNDEFINED);
4280 SCM_SETCDR (scm_undefineds, scm_undefineds);
4281 scm_listofnull = scm_list_1 (SCM_EOL);
4282
4283 scm_f_apply = scm_c_define_subr ("apply", scm_tc7_lsubr_2, scm_apply);
4284
4285 /* acros */
4286 /* end of acros */
4287
4288 #include "libguile/eval.x"
4289
4290 scm_add_feature ("delay");
4291 }
4292
4293 #endif /* !DEVAL */
4294
4295 /*
4296 Local Variables:
4297 c-file-style: "gnu"
4298 End:
4299 */