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