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