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