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