* eval.c (CEVAL): Don't distinguish between short and long
[bpt/guile.git] / libguile / eval.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
2 * Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 \f
20
21 /* This file is read twice in order to produce debugging versions of ceval and
22 * scm_apply. These functions, deval and scm_dapply, are produced when we
23 * define the preprocessor macro DEVAL. The file is divided into sections
24 * which are treated differently with respect to DEVAL. The heads of these
25 * sections are marked with the string "SECTION:". */
26
27 /* SECTION: This code is compiled once.
28 */
29
30 #if HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include "libguile/__scm.h"
35
36 #ifndef DEVAL
37
38 /* AIX requires this to be the first thing in the file. The #pragma
39 directive is indented so pre-ANSI compilers will ignore it, rather
40 than choke on it. */
41 #ifndef __GNUC__
42 # if HAVE_ALLOCA_H
43 # include <alloca.h>
44 # else
45 # ifdef _AIX
46 # pragma alloca
47 # else
48 # ifndef alloca /* predefined by HP cc +Olibcalls */
49 char *alloca ();
50 # endif
51 # endif
52 # endif
53 #endif
54
55 #include "libguile/_scm.h"
56 #include "libguile/alist.h"
57 #include "libguile/async.h"
58 #include "libguile/continuations.h"
59 #include "libguile/debug.h"
60 #include "libguile/deprecation.h"
61 #include "libguile/dynwind.h"
62 #include "libguile/eq.h"
63 #include "libguile/feature.h"
64 #include "libguile/fluids.h"
65 #include "libguile/futures.h"
66 #include "libguile/goops.h"
67 #include "libguile/hash.h"
68 #include "libguile/hashtab.h"
69 #include "libguile/lang.h"
70 #include "libguile/list.h"
71 #include "libguile/macros.h"
72 #include "libguile/modules.h"
73 #include "libguile/objects.h"
74 #include "libguile/ports.h"
75 #include "libguile/procprop.h"
76 #include "libguile/root.h"
77 #include "libguile/smob.h"
78 #include "libguile/srcprop.h"
79 #include "libguile/stackchk.h"
80 #include "libguile/strings.h"
81 #include "libguile/throw.h"
82 #include "libguile/validate.h"
83 #include "libguile/values.h"
84 #include "libguile/vectors.h"
85
86 #include "libguile/eval.h"
87
88 \f
89
90 static SCM canonicalize_define (SCM expr);
91
92 \f
93
94 /* {Syntax Errors}
95 *
96 * This section defines the message strings for the syntax errors that can be
97 * detected during memoization and the functions and macros that shall be
98 * called by the memoizer code to signal syntax errors. */
99
100
101 /* Syntax errors that can be detected during memoization: */
102
103 /* Circular or improper lists do not form valid scheme expressions. If a
104 * circular list or an improper list is detected in a place where a scheme
105 * expression is expected, a 'Bad expression' error is signalled. */
106 static const char s_bad_expression[] = "Bad expression";
107
108 /* If a form is detected that holds a different number of expressions than are
109 * required in that context, a 'Missing or extra expression' error is
110 * signalled. */
111 static const char s_expression[] = "Missing or extra expression in";
112
113 /* If a form is detected that holds less expressions than are required in that
114 * context, a 'Missing expression' error is signalled. */
115 static const char s_missing_expression[] = "Missing expression in";
116
117 /* If a form is detected that holds more expressions than are allowed in that
118 * context, an 'Extra expression' error is signalled. */
119 static const char s_extra_expression[] = "Extra expression in";
120
121 /* The empty combination '()' is not allowed as an expression in scheme. If
122 * it is detected in a place where an expression is expected, an 'Illegal
123 * empty combination' error is signalled. Note: If you encounter this error
124 * message, it is very likely that you intended to denote the empty list. To
125 * do so, you need to quote the empty list like (quote ()) or '(). */
126 static const char s_empty_combination[] = "Illegal empty combination";
127
128 /* A body may hold an arbitrary number of internal defines, followed by a
129 * non-empty sequence of expressions. If a body with an empty sequence of
130 * expressions is detected, a 'Missing body expression' error is signalled.
131 */
132 static const char s_missing_body_expression[] = "Missing body expression in";
133
134 /* A body may hold an arbitrary number of internal defines, followed by a
135 * non-empty sequence of expressions. Each the definitions and the
136 * expressions may be grouped arbitraryly with begin, but it is not allowed to
137 * mix definitions and expressions. If a define form in a body mixes
138 * definitions and expressions, a 'Mixed definitions and expressions' error is
139 * signalled. */
140 static const char s_mixed_body_forms[] = "Mixed definitions and expressions in";
141 /* Definitions are only allowed on the top level and at the start of a body.
142 * If a definition is detected anywhere else, a 'Bad define placement' error
143 * is signalled. */
144 static const char s_bad_define[] = "Bad define placement";
145
146 /* Case or cond expressions must have at least one clause. If a case or cond
147 * expression without any clauses is detected, a 'Missing clauses' error is
148 * signalled. */
149 static const char s_missing_clauses[] = "Missing clauses";
150
151 /* If there is an 'else' clause in a case or a cond statement, it must be the
152 * last clause. If after the 'else' case clause further clauses are detected,
153 * a 'Misplaced else clause' error is signalled. */
154 static const char s_misplaced_else_clause[] = "Misplaced else clause";
155
156 /* If a case clause is detected that is not in the format
157 * (<label(s)> <expression1> <expression2> ...)
158 * a 'Bad case clause' error is signalled. */
159 static const char s_bad_case_clause[] = "Bad case clause";
160
161 /* If a case clause is detected where the <label(s)> element is neither a
162 * proper list nor (in case of the last clause) the syntactic keyword 'else',
163 * a 'Bad case labels' error is signalled. Note: If you encounter this error
164 * for an else-clause which seems to be syntactically correct, check if 'else'
165 * is really a syntactic keyword in that context. If 'else' is bound in the
166 * local or global environment, it is not considered a syntactic keyword, but
167 * will be treated as any other variable. */
168 static const char s_bad_case_labels[] = "Bad case labels";
169
170 /* In a case statement all labels have to be distinct. If in a case statement
171 * a label occurs more than once, a 'Duplicate case label' error is
172 * signalled. */
173 static const char s_duplicate_case_label[] = "Duplicate case label";
174
175 /* If a cond clause is detected that is not in one of the formats
176 * (<test> <expression1> ...) or (else <expression1> <expression2> ...)
177 * a 'Bad cond clause' error is signalled. */
178 static const char s_bad_cond_clause[] = "Bad cond clause";
179
180 /* If a cond clause is detected that uses the alternate '=>' form, but does
181 * not hold a recipient element for the test result, a 'Missing recipient'
182 * error is signalled. */
183 static const char s_missing_recipient[] = "Missing recipient in";
184
185 /* If in a position where a variable name is required some other object is
186 * detected, a 'Bad variable' error is signalled. */
187 static const char s_bad_variable[] = "Bad variable";
188
189 /* Bindings for forms like 'let' and 'do' have to be given in a proper,
190 * possibly empty list. If any other object is detected in a place where a
191 * list of bindings was required, a 'Bad bindings' error is signalled. */
192 static const char s_bad_bindings[] = "Bad bindings";
193
194 /* Depending on the syntactic context, a binding has to be in the format
195 * (<variable> <expression>) or (<variable> <expression1> <expression2>).
196 * If anything else is detected in a place where a binding was expected, a
197 * 'Bad binding' error is signalled. */
198 static const char s_bad_binding[] = "Bad binding";
199
200 /* Some syntactic forms don't allow variable names to appear more than once in
201 * a list of bindings. If such a situation is nevertheless detected, a
202 * 'Duplicate binding' error is signalled. */
203 static const char s_duplicate_binding[] = "Duplicate binding";
204
205 /* If the exit form of a 'do' expression is not in the format
206 * (<test> <expression> ...)
207 * a 'Bad exit clause' error is signalled. */
208 static const char s_bad_exit_clause[] = "Bad exit clause";
209
210 /* The formal function arguments of a lambda expression have to be either a
211 * single symbol or a non-cyclic list. For anything else a 'Bad formals'
212 * error is signalled. */
213 static const char s_bad_formals[] = "Bad formals";
214
215 /* If in a lambda expression something else than a symbol is detected at a
216 * place where a formal function argument is required, a 'Bad formal' error is
217 * signalled. */
218 static const char s_bad_formal[] = "Bad formal";
219
220 /* If in the arguments list of a lambda expression an argument name occurs
221 * more than once, a 'Duplicate formal' error is signalled. */
222 static const char s_duplicate_formal[] = "Duplicate formal";
223
224 /* If the evaluation of an unquote-splicing expression gives something else
225 * than a proper list, a 'Non-list result for unquote-splicing' error is
226 * signalled. */
227 static const char s_splicing[] = "Non-list result for unquote-splicing";
228
229 /* If something else than an exact integer is detected as the argument for
230 * @slot-ref and @slot-set!, a 'Bad slot number' error is signalled. */
231 static const char s_bad_slot_number[] = "Bad slot number";
232
233
234 /* Signal a syntax error. We distinguish between the form that caused the
235 * error and the enclosing expression. The error message will print out as
236 * shown in the following pattern. The file name and line number are only
237 * given when they can be determined from the erroneous form or from the
238 * enclosing expression.
239 *
240 * <filename>: In procedure memoization:
241 * <filename>: In file <name>, line <nr>: <error-message> in <expression>. */
242
243 SCM_SYMBOL (syntax_error_key, "syntax-error");
244
245 /* The prototype is needed to indicate that the function does not return. */
246 static void
247 syntax_error (const char* const, const SCM, const SCM) SCM_NORETURN;
248
249 static void
250 syntax_error (const char* const msg, const SCM form, const SCM expr)
251 {
252 const SCM msg_string = scm_makfrom0str (msg);
253 SCM filename = SCM_BOOL_F;
254 SCM linenr = SCM_BOOL_F;
255 const char *format;
256 SCM args;
257
258 if (SCM_CONSP (form))
259 {
260 filename = scm_source_property (form, scm_sym_filename);
261 linenr = scm_source_property (form, scm_sym_line);
262 }
263
264 if (SCM_FALSEP (filename) && SCM_FALSEP (linenr) && SCM_CONSP (expr))
265 {
266 filename = scm_source_property (expr, scm_sym_filename);
267 linenr = scm_source_property (expr, scm_sym_line);
268 }
269
270 if (!SCM_UNBNDP (expr))
271 {
272 if (!SCM_FALSEP (filename))
273 {
274 format = "In file ~S, line ~S: ~A ~S in expression ~S.";
275 args = scm_list_5 (filename, linenr, msg_string, form, expr);
276 }
277 else if (!SCM_FALSEP (linenr))
278 {
279 format = "In line ~S: ~A ~S in expression ~S.";
280 args = scm_list_4 (linenr, msg_string, form, expr);
281 }
282 else
283 {
284 format = "~A ~S in expression ~S.";
285 args = scm_list_3 (msg_string, form, expr);
286 }
287 }
288 else
289 {
290 if (!SCM_FALSEP (filename))
291 {
292 format = "In file ~S, line ~S: ~A ~S.";
293 args = scm_list_4 (filename, linenr, msg_string, form);
294 }
295 else if (!SCM_FALSEP (linenr))
296 {
297 format = "In line ~S: ~A ~S.";
298 args = scm_list_3 (linenr, msg_string, form);
299 }
300 else
301 {
302 format = "~A ~S.";
303 args = scm_list_2 (msg_string, form);
304 }
305 }
306
307 scm_error (syntax_error_key, "memoization", format, args, SCM_BOOL_F);
308 }
309
310
311 /* Shortcut macros to simplify syntax error handling. */
312 #define ASSERT_SYNTAX(cond, message, form) \
313 { if (!(cond)) syntax_error (message, form, SCM_UNDEFINED); }
314 #define ASSERT_SYNTAX_2(cond, message, form, expr) \
315 { if (!(cond)) syntax_error (message, form, expr); }
316
317 \f
318
319 /* {Ilocs}
320 *
321 * Ilocs are memoized references to variables in local environment frames.
322 * They are represented as three values: The relative offset of the
323 * environment frame, the number of the binding within that frame, and a
324 * boolean value indicating whether the binding is the last binding in the
325 * frame.
326 */
327 #define SCM_ILOC00 SCM_MAKE_ITAG8(0L, scm_tc8_iloc)
328 #define SCM_IDINC (0x00100000L)
329 #define SCM_IDSTMSK (-SCM_IDINC)
330 #define SCM_MAKE_ILOC(frame_nr, binding_nr, last_p) \
331 SCM_PACK ( \
332 ((frame_nr) << 8) \
333 + ((binding_nr) << 20) \
334 + ((last_p) ? SCM_ICDR : 0) \
335 + scm_tc8_iloc )
336
337 #if (SCM_DEBUG_DEBUGGING_SUPPORT == 1)
338
339 SCM scm_dbg_make_iloc (SCM frame, SCM binding, SCM cdrp);
340 SCM_DEFINE (scm_dbg_make_iloc, "dbg-make-iloc", 3, 0, 0,
341 (SCM frame, SCM binding, SCM cdrp),
342 "Return a new iloc with frame offset @var{frame}, binding\n"
343 "offset @var{binding} and the cdr flag @var{cdrp}.")
344 #define FUNC_NAME s_scm_dbg_make_iloc
345 {
346 SCM_VALIDATE_INUM (1, frame);
347 SCM_VALIDATE_INUM (2, binding);
348 return SCM_MAKE_ILOC (SCM_INUM (frame),
349 SCM_INUM (binding),
350 !SCM_FALSEP (cdrp));
351 }
352 #undef FUNC_NAME
353
354 SCM scm_dbg_iloc_p (SCM obj);
355 SCM_DEFINE (scm_dbg_iloc_p, "dbg-iloc?", 1, 0, 0,
356 (SCM obj),
357 "Return @code{#t} if @var{obj} is an iloc.")
358 #define FUNC_NAME s_scm_dbg_iloc_p
359 {
360 return SCM_BOOL (SCM_ILOCP (obj));
361 }
362 #undef FUNC_NAME
363
364 #endif
365
366 \f
367
368 /* The function lookup_symbol is used during memoization: Lookup the symbol
369 * in the environment. If there is no binding for the symbol, SCM_UNDEFINED
370 * is returned. If the symbol is a syntactic keyword, the macro object to
371 * which the symbol is bound is returned. If the symbol is a global variable,
372 * the variable object to which the symbol is bound is returned. Finally, if
373 * the symbol is a local variable the corresponding iloc object is returned.
374 */
375
376 /* A helper function for lookup_symbol: Try to find the symbol in the top
377 * level environment frame. The function returns SCM_UNDEFINED if the symbol
378 * is unbound, it returns a macro object if the symbol is a syntactic keyword
379 * and it returns a variable object if the symbol is a global variable. */
380 static SCM
381 lookup_global_symbol (const SCM symbol, const SCM top_level)
382 {
383 const SCM variable = scm_sym2var (symbol, top_level, SCM_BOOL_F);
384 if (SCM_FALSEP (variable))
385 {
386 return SCM_UNDEFINED;
387 }
388 else
389 {
390 const SCM value = SCM_VARIABLE_REF (variable);
391 if (SCM_MACROP (value))
392 return value;
393 else
394 return variable;
395 }
396 }
397
398 static SCM
399 lookup_symbol (const SCM symbol, const SCM env)
400 {
401 SCM frame_idx;
402 unsigned int frame_nr;
403
404 for (frame_idx = env, frame_nr = 0;
405 !SCM_NULLP (frame_idx);
406 frame_idx = SCM_CDR (frame_idx), ++frame_nr)
407 {
408 const SCM frame = SCM_CAR (frame_idx);
409 if (SCM_CONSP (frame))
410 {
411 /* frame holds a local environment frame */
412 SCM symbol_idx;
413 unsigned int symbol_nr;
414
415 for (symbol_idx = SCM_CAR (frame), symbol_nr = 0;
416 SCM_CONSP (symbol_idx);
417 symbol_idx = SCM_CDR (symbol_idx), ++symbol_nr)
418 {
419 if (SCM_EQ_P (SCM_CAR (symbol_idx), symbol))
420 /* found the symbol, therefore return the iloc */
421 return SCM_MAKE_ILOC (frame_nr, symbol_nr, 0);
422 }
423 if (SCM_EQ_P (symbol_idx, symbol))
424 /* found the symbol as the last element of the current frame */
425 return SCM_MAKE_ILOC (frame_nr, symbol_nr, 1);
426 }
427 else
428 {
429 /* no more local environment frames */
430 return lookup_global_symbol (symbol, frame);
431 }
432 }
433
434 return lookup_global_symbol (symbol, SCM_BOOL_F);
435 }
436
437
438 /* Return true if the symbol is - from the point of view of a macro
439 * transformer - a literal in the sense specified in chapter "pattern
440 * language" of R5RS. In the code below, however, we don't match the
441 * definition of R5RS exactly: It returns true if the identifier has no
442 * binding or if it is a syntactic keyword. */
443 static int
444 literal_p (const SCM symbol, const SCM env)
445 {
446 const SCM value = lookup_symbol (symbol, env);
447 if (SCM_UNBNDP (value) || SCM_MACROP (value))
448 return 1;
449 else
450 return 0;
451 }
452
453
454 /* Return true if the expression is self-quoting in the memoized code. Thus,
455 * some other objects (like e. g. vectors) are reported as self-quoting, which
456 * according to R5RS would need to be quoted. */
457 static int
458 is_self_quoting_p (const SCM expr)
459 {
460 if (SCM_CONSP (expr))
461 return 0;
462 else if (SCM_SYMBOLP (expr))
463 return 0;
464 else if (SCM_NULLP (expr))
465 return 0;
466 else return 1;
467 }
468
469 \f
470
471 /* Lookup a given local variable in an environment. The local variable is
472 * given as an iloc, that is a triple <frame, binding, last?>, where frame
473 * indicates the relative number of the environment frame (counting upwards
474 * from the innermost environment frame), binding indicates the number of the
475 * binding within the frame, and last? (which is extracted from the iloc using
476 * the macro SCM_ICDRP) indicates whether the binding forms the binding at the
477 * very end of the improper list of bindings. */
478 SCM *
479 scm_ilookup (SCM iloc, SCM env)
480 {
481 unsigned int frame_nr = SCM_IFRAME (iloc);
482 unsigned int binding_nr = SCM_IDIST (iloc);
483 SCM frames = env;
484 SCM bindings;
485
486 for (; 0 != frame_nr; --frame_nr)
487 frames = SCM_CDR (frames);
488
489 bindings = SCM_CAR (frames);
490 for (; 0 != binding_nr; --binding_nr)
491 bindings = SCM_CDR (bindings);
492
493 if (SCM_ICDRP (iloc))
494 return SCM_CDRLOC (bindings);
495 return SCM_CARLOC (SCM_CDR (bindings));
496 }
497
498
499 SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
500
501 static void error_unbound_variable (SCM symbol) SCM_NORETURN;
502 static void
503 error_unbound_variable (SCM symbol)
504 {
505 scm_error (scm_unbound_variable_key, NULL,
506 "Unbound variable: ~S",
507 scm_list_1 (symbol), SCM_BOOL_F);
508 }
509
510
511 /* The Lookup Car Race
512 - by Eva Luator
513
514 Memoization of variables and special forms is done while executing
515 the code for the first time. As long as there is only one thread
516 everything is fine, but as soon as two threads execute the same
517 code concurrently `for the first time' they can come into conflict.
518
519 This memoization includes rewriting variable references into more
520 efficient forms and expanding macros. Furthermore, macro expansion
521 includes `compiling' special forms like `let', `cond', etc. into
522 tree-code instructions.
523
524 There shouldn't normally be a problem with memoizing local and
525 global variable references (into ilocs and variables), because all
526 threads will mutate the code in *exactly* the same way and (if I
527 read the C code correctly) it is not possible to observe a half-way
528 mutated cons cell. The lookup procedure can handle this
529 transparently without any critical sections.
530
531 It is different with macro expansion, because macro expansion
532 happens outside of the lookup procedure and can't be
533 undone. Therefore the lookup procedure can't cope with it. It has
534 to indicate failure when it detects a lost race and hope that the
535 caller can handle it. Luckily, it turns out that this is the case.
536
537 An example to illustrate this: Suppose that the following form will
538 be memoized concurrently by two threads
539
540 (let ((x 12)) x)
541
542 Let's first examine the lookup of X in the body. The first thread
543 decides that it has to find the symbol "x" in the environment and
544 starts to scan it. Then the other thread takes over and actually
545 overtakes the first. It looks up "x" and substitutes an
546 appropriate iloc for it. Now the first thread continues and
547 completes its lookup. It comes to exactly the same conclusions as
548 the second one and could - without much ado - just overwrite the
549 iloc with the same iloc.
550
551 But let's see what will happen when the race occurs while looking
552 up the symbol "let" at the start of the form. It could happen that
553 the second thread interrupts the lookup of the first thread and not
554 only substitutes a variable for it but goes right ahead and
555 replaces it with the compiled form (#@let* (x 12) x). Now, when
556 the first thread completes its lookup, it would replace the #@let*
557 with a variable containing the "let" binding, effectively reverting
558 the form to (let (x 12) x). This is wrong. It has to detect that
559 it has lost the race and the evaluator has to reconsider the
560 changed form completely.
561
562 This race condition could be resolved with some kind of traffic
563 light (like mutexes) around scm_lookupcar, but I think that it is
564 best to avoid them in this case. They would serialize memoization
565 completely and because lookup involves calling arbitrary Scheme
566 code (via the lookup-thunk), threads could be blocked for an
567 arbitrary amount of time or even deadlock. But with the current
568 solution a lot of unnecessary work is potentially done. */
569
570 /* SCM_LOOKUPCAR1 is what SCM_LOOKUPCAR used to be but is allowed to
571 return NULL to indicate a failed lookup due to some race conditions
572 between threads. This only happens when VLOC is the first cell of
573 a special form that will eventually be memoized (like `let', etc.)
574 In that case the whole lookup is bogus and the caller has to
575 reconsider the complete special form.
576
577 SCM_LOOKUPCAR is still there, of course. It just calls
578 SCM_LOOKUPCAR1 and aborts on receiving NULL. So SCM_LOOKUPCAR
579 should only be called when it is known that VLOC is not the first
580 pair of a special form. Otherwise, use SCM_LOOKUPCAR1 and check
581 for NULL. I think I've found the only places where this
582 applies. */
583
584 static SCM *
585 scm_lookupcar1 (SCM vloc, SCM genv, int check)
586 {
587 SCM env = genv;
588 register SCM *al, fl, var = SCM_CAR (vloc);
589 register SCM iloc = SCM_ILOC00;
590 for (; SCM_NIMP (env); env = SCM_CDR (env))
591 {
592 if (!SCM_CONSP (SCM_CAR (env)))
593 break;
594 al = SCM_CARLOC (env);
595 for (fl = SCM_CAR (*al); SCM_NIMP (fl); fl = SCM_CDR (fl))
596 {
597 if (!SCM_CONSP (fl))
598 {
599 if (SCM_EQ_P (fl, var))
600 {
601 if (! SCM_EQ_P (SCM_CAR (vloc), var))
602 goto race;
603 SCM_SET_CELL_WORD_0 (vloc, SCM_UNPACK (iloc) + SCM_ICDR);
604 return SCM_CDRLOC (*al);
605 }
606 else
607 break;
608 }
609 al = SCM_CDRLOC (*al);
610 if (SCM_EQ_P (SCM_CAR (fl), var))
611 {
612 if (SCM_UNBNDP (SCM_CAR (*al)))
613 {
614 env = SCM_EOL;
615 goto errout;
616 }
617 if (!SCM_EQ_P (SCM_CAR (vloc), var))
618 goto race;
619 SCM_SETCAR (vloc, iloc);
620 return SCM_CARLOC (*al);
621 }
622 iloc = SCM_PACK (SCM_UNPACK (iloc) + SCM_IDINC);
623 }
624 iloc = SCM_PACK ((~SCM_IDSTMSK) & (SCM_UNPACK(iloc) + SCM_IFRINC));
625 }
626 {
627 SCM top_thunk, real_var;
628 if (SCM_NIMP (env))
629 {
630 top_thunk = SCM_CAR (env); /* env now refers to a
631 top level env thunk */
632 env = SCM_CDR (env);
633 }
634 else
635 top_thunk = SCM_BOOL_F;
636 real_var = scm_sym2var (var, top_thunk, SCM_BOOL_F);
637 if (SCM_FALSEP (real_var))
638 goto errout;
639
640 if (!SCM_NULLP (env) || SCM_UNBNDP (SCM_VARIABLE_REF (real_var)))
641 {
642 errout:
643 if (check)
644 {
645 if (SCM_NULLP (env))
646 error_unbound_variable (var);
647 else
648 scm_misc_error (NULL, "Damaged environment: ~S",
649 scm_list_1 (var));
650 }
651 else
652 {
653 /* A variable could not be found, but we shall
654 not throw an error. */
655 static SCM undef_object = SCM_UNDEFINED;
656 return &undef_object;
657 }
658 }
659
660 if (!SCM_EQ_P (SCM_CAR (vloc), var))
661 {
662 /* Some other thread has changed the very cell we are working
663 on. In effect, it must have done our job or messed it up
664 completely. */
665 race:
666 var = SCM_CAR (vloc);
667 if (SCM_VARIABLEP (var))
668 return SCM_VARIABLE_LOC (var);
669 if (SCM_ILOCP (var))
670 return scm_ilookup (var, genv);
671 /* We can't cope with anything else than variables and ilocs. When
672 a special form has been memoized (i.e. `let' into `#@let') we
673 return NULL and expect the calling function to do the right
674 thing. For the evaluator, this means going back and redoing
675 the dispatch on the car of the form. */
676 return NULL;
677 }
678
679 SCM_SETCAR (vloc, real_var);
680 return SCM_VARIABLE_LOC (real_var);
681 }
682 }
683
684 SCM *
685 scm_lookupcar (SCM vloc, SCM genv, int check)
686 {
687 SCM *loc = scm_lookupcar1 (vloc, genv, check);
688 if (loc == NULL)
689 abort ();
690 return loc;
691 }
692
693 \f
694
695 /* Rewrite the body (which is given as the list of expressions forming the
696 * body) into its internal form. The internal form of a body (<expr> ...) is
697 * just the body itself, but prefixed with an ISYM that denotes to what kind
698 * of outer construct this body belongs: (<ISYM> <expr> ...). A lambda body
699 * starts with SCM_IM_LAMBDA, for example, a body of a let starts with
700 * SCM_IM_LET, etc.
701 *
702 * It is assumed that the calling expression has already made sure that the
703 * body is a proper list. */
704 static SCM
705 m_body (SCM op, SCM exprs)
706 {
707 /* Don't add another ISYM if one is present already. */
708 if (SCM_ISYMP (SCM_CAR (exprs)))
709 return exprs;
710 else
711 return scm_cons (op, exprs);
712 }
713
714
715 /* The function m_expand_body memoizes a proper list of expressions
716 * forming a body. This function takes care of dealing with internal
717 * defines and transforming them into an equivalent letrec expression.
718 * The list of expressions is rewritten in place. */
719
720 /* This is a helper function for m_expand_body. It helps to figure out whether
721 * an expression denotes a syntactic keyword. */
722 static SCM
723 try_macro_lookup (const SCM expr, const SCM env)
724 {
725 if (SCM_SYMBOLP (expr))
726 {
727 const SCM value = lookup_symbol (expr, env);
728 return value;
729 }
730 else
731 {
732 return SCM_UNDEFINED;
733 }
734 }
735
736 /* This is a helper function for m_expand_body. It expands user macros,
737 * because for the correct translation of a body we need to know whether they
738 * expand to a definition. */
739 static SCM
740 expand_user_macros (SCM expr, const SCM env)
741 {
742 while (SCM_CONSP (expr))
743 {
744 const SCM car_expr = SCM_CAR (expr);
745 const SCM new_car = expand_user_macros (car_expr, env);
746 const SCM value = try_macro_lookup (new_car, env);
747
748 if (SCM_MACROP (value) && SCM_MACRO_TYPE (value) == 2)
749 {
750 /* User macros transform code into code. */
751 expr = scm_call_2 (SCM_MACRO_CODE (value), expr, env);
752 /* We need to reiterate on the transformed code. */
753 }
754 else
755 {
756 /* No user macro: return. */
757 SCM_SETCAR (expr, new_car);
758 return expr;
759 }
760 }
761
762 return expr;
763 }
764
765 /* This is a helper function for m_expand_body. It determines if a given form
766 * represents an application of a given built-in macro. The built-in macro to
767 * check for is identified by its syntactic keyword. The form is an
768 * application of the given macro if looking up the car of the form in the
769 * given environment actually returns the built-in macro. */
770 static int
771 is_system_macro_p (const SCM syntactic_keyword, const SCM form, const SCM env)
772 {
773 if (SCM_CONSP (form))
774 {
775 const SCM car_form = SCM_CAR (form);
776 const SCM value = try_macro_lookup (car_form, env);
777 if (SCM_BUILTIN_MACRO_P (value))
778 {
779 const SCM macro_name = scm_macro_name (value);
780 return SCM_EQ_P (macro_name, syntactic_keyword);
781 }
782 }
783
784 return 0;
785 }
786
787 static void
788 m_expand_body (const SCM forms, const SCM env)
789 {
790 /* The first body form can be skipped since it is known to be the ISYM that
791 * was prepended to the body by m_body. */
792 SCM cdr_forms = SCM_CDR (forms);
793 SCM form_idx = cdr_forms;
794 SCM definitions = SCM_EOL;
795 SCM sequence = SCM_EOL;
796
797 /* According to R5RS, the list of body forms consists of two parts: a number
798 * (maybe zero) of definitions, followed by a non-empty sequence of
799 * expressions. Each the definitions and the expressions may be grouped
800 * arbitrarily with begin, but it is not allowed to mix definitions and
801 * expressions. The task of the following loop therefore is to split the
802 * list of body forms into the list of definitions and the sequence of
803 * expressions. */
804 while (!SCM_NULLP (form_idx))
805 {
806 const SCM form = SCM_CAR (form_idx);
807 const SCM new_form = expand_user_macros (form, env);
808 if (is_system_macro_p (scm_sym_define, new_form, env))
809 {
810 definitions = scm_cons (new_form, definitions);
811 form_idx = SCM_CDR (form_idx);
812 }
813 else if (is_system_macro_p (scm_sym_begin, new_form, env))
814 {
815 /* We have encountered a group of forms. This has to be either a
816 * (possibly empty) group of (possibly further grouped) definitions,
817 * or a non-empty group of (possibly further grouped)
818 * expressions. */
819 const SCM grouped_forms = SCM_CDR (new_form);
820 unsigned int found_definition = 0;
821 unsigned int found_expression = 0;
822 SCM grouped_form_idx = grouped_forms;
823 while (!found_expression && !SCM_NULLP (grouped_form_idx))
824 {
825 const SCM inner_form = SCM_CAR (grouped_form_idx);
826 const SCM new_inner_form = expand_user_macros (inner_form, env);
827 if (is_system_macro_p (scm_sym_define, new_inner_form, env))
828 {
829 found_definition = 1;
830 definitions = scm_cons (new_inner_form, definitions);
831 grouped_form_idx = SCM_CDR (grouped_form_idx);
832 }
833 else if (is_system_macro_p (scm_sym_begin, new_inner_form, env))
834 {
835 const SCM inner_group = SCM_CDR (new_inner_form);
836 grouped_form_idx
837 = scm_append (scm_list_2 (inner_group,
838 SCM_CDR (grouped_form_idx)));
839 }
840 else
841 {
842 /* The group marks the start of the expressions of the body.
843 * We have to make sure that within the same group we have
844 * not encountered a definition before. */
845 ASSERT_SYNTAX (!found_definition, s_mixed_body_forms, form);
846 found_expression = 1;
847 grouped_form_idx = SCM_EOL;
848 }
849 }
850
851 /* We have finished processing the group. If we have not yet
852 * encountered an expression we continue processing the forms of the
853 * body to collect further definition forms. Otherwise, the group
854 * marks the start of the sequence of expressions of the body. */
855 if (!found_expression)
856 {
857 form_idx = SCM_CDR (form_idx);
858 }
859 else
860 {
861 sequence = form_idx;
862 form_idx = SCM_EOL;
863 }
864 }
865 else
866 {
867 /* We have detected a form which is no definition. This marks the
868 * start of the sequence of expressions of the body. */
869 sequence = form_idx;
870 form_idx = SCM_EOL;
871 }
872 }
873
874 /* FIXME: forms does not hold information about the file location. */
875 ASSERT_SYNTAX (SCM_CONSP (sequence), s_missing_body_expression, cdr_forms);
876
877 if (!SCM_NULLP (definitions))
878 {
879 SCM definition_idx;
880 SCM letrec_tail;
881 SCM letrec_expression;
882 SCM new_letrec_expression;
883
884 SCM bindings = SCM_EOL;
885 for (definition_idx = definitions;
886 !SCM_NULLP (definition_idx);
887 definition_idx = SCM_CDR (definition_idx))
888 {
889 const SCM definition = SCM_CAR (definition_idx);
890 const SCM canonical_definition = canonicalize_define (definition);
891 const SCM binding = SCM_CDR (canonical_definition);
892 bindings = scm_cons (binding, bindings);
893 };
894
895 letrec_tail = scm_cons (bindings, sequence);
896 /* FIXME: forms does not hold information about the file location. */
897 letrec_expression = scm_cons_source (forms, scm_sym_letrec, letrec_tail);
898 new_letrec_expression = scm_m_letrec (letrec_expression, env);
899 SCM_SETCAR (forms, new_letrec_expression);
900 SCM_SETCDR (forms, SCM_EOL);
901 }
902 else
903 {
904 SCM_SETCAR (forms, SCM_CAR (sequence));
905 SCM_SETCDR (forms, SCM_CDR (sequence));
906 }
907 }
908
909
910 /* Start of the memoizers for the standard R5RS builtin macros. */
911
912
913 SCM_SYNTAX (s_and, "and", scm_i_makbimacro, scm_m_and);
914 SCM_GLOBAL_SYMBOL (scm_sym_and, s_and);
915
916 SCM
917 scm_m_and (SCM expr, SCM env SCM_UNUSED)
918 {
919 const SCM cdr_expr = SCM_CDR (expr);
920 const long length = scm_ilength (cdr_expr);
921
922 ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
923
924 if (length == 0)
925 {
926 /* Special case: (and) is replaced by #t. */
927 return SCM_BOOL_T;
928 }
929 else
930 {
931 SCM_SETCAR (expr, SCM_IM_AND);
932 return expr;
933 }
934 }
935
936
937 SCM_SYNTAX (s_begin, "begin", scm_i_makbimacro, scm_m_begin);
938 SCM_GLOBAL_SYMBOL (scm_sym_begin, s_begin);
939
940 SCM
941 scm_m_begin (SCM expr, SCM env SCM_UNUSED)
942 {
943 const SCM cdr_expr = SCM_CDR (expr);
944 /* Dirk:FIXME:: An empty begin clause is not generally allowed by R5RS.
945 * That means, there should be a distinction between uses of begin where an
946 * empty clause is OK and where it is not. */
947 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
948
949 SCM_SETCAR (expr, SCM_IM_BEGIN);
950 return expr;
951 }
952
953
954 SCM_SYNTAX (s_case, "case", scm_i_makbimacro, scm_m_case);
955 SCM_GLOBAL_SYMBOL (scm_sym_case, s_case);
956 SCM_GLOBAL_SYMBOL (scm_sym_else, "else");
957
958 SCM
959 scm_m_case (SCM expr, SCM env)
960 {
961 SCM clauses;
962 SCM all_labels = SCM_EOL;
963
964 /* Check, whether 'else is a literal, i. e. not bound to a value. */
965 const int else_literal_p = literal_p (scm_sym_else, env);
966
967 const SCM cdr_expr = SCM_CDR (expr);
968 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
969 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_clauses, expr);
970
971 clauses = SCM_CDR (cdr_expr);
972 while (!SCM_NULLP (clauses))
973 {
974 SCM labels;
975
976 const SCM clause = SCM_CAR (clauses);
977 ASSERT_SYNTAX_2 (scm_ilength (clause) >= 2,
978 s_bad_case_clause, clause, expr);
979
980 labels = SCM_CAR (clause);
981 if (SCM_CONSP (labels))
982 {
983 ASSERT_SYNTAX_2 (scm_ilength (labels) >= 0,
984 s_bad_case_labels, labels, expr);
985 all_labels = scm_append_x (scm_list_2 (labels, all_labels));
986 }
987 else if (SCM_NULLP (labels))
988 {
989 /* The list of labels is empty. According to R5RS this is allowed.
990 * It means that the sequence of expressions will never be executed.
991 * Therefore, as an optimization, we could remove the whole
992 * clause. */
993 }
994 else
995 {
996 ASSERT_SYNTAX_2 (SCM_EQ_P (labels, scm_sym_else) && else_literal_p,
997 s_bad_case_labels, labels, expr);
998 ASSERT_SYNTAX_2 (SCM_NULLP (SCM_CDR (clauses)),
999 s_misplaced_else_clause, clause, expr);
1000 }
1001
1002 /* build the new clause */
1003 if (SCM_EQ_P (labels, scm_sym_else))
1004 SCM_SETCAR (clause, SCM_IM_ELSE);
1005
1006 clauses = SCM_CDR (clauses);
1007 }
1008
1009 /* Check whether all case labels are distinct. */
1010 for (; !SCM_NULLP (all_labels); all_labels = SCM_CDR (all_labels))
1011 {
1012 const SCM label = SCM_CAR (all_labels);
1013 ASSERT_SYNTAX_2 (SCM_FALSEP (scm_c_memq (label, SCM_CDR (all_labels))),
1014 s_duplicate_case_label, label, expr);
1015 }
1016
1017 SCM_SETCAR (expr, SCM_IM_CASE);
1018 return expr;
1019 }
1020
1021
1022 SCM_SYNTAX (s_cond, "cond", scm_i_makbimacro, scm_m_cond);
1023 SCM_GLOBAL_SYMBOL (scm_sym_cond, s_cond);
1024 SCM_GLOBAL_SYMBOL (scm_sym_arrow, "=>");
1025
1026 SCM
1027 scm_m_cond (SCM expr, SCM env)
1028 {
1029 /* Check, whether 'else or '=> is a literal, i. e. not bound to a value. */
1030 const int else_literal_p = literal_p (scm_sym_else, env);
1031 const int arrow_literal_p = literal_p (scm_sym_arrow, env);
1032
1033 const SCM clauses = SCM_CDR (expr);
1034 SCM clause_idx;
1035
1036 ASSERT_SYNTAX (scm_ilength (clauses) >= 0, s_bad_expression, expr);
1037 ASSERT_SYNTAX (scm_ilength (clauses) >= 1, s_missing_clauses, expr);
1038
1039 for (clause_idx = clauses;
1040 !SCM_NULLP (clause_idx);
1041 clause_idx = SCM_CDR (clause_idx))
1042 {
1043 SCM test;
1044
1045 const SCM clause = SCM_CAR (clause_idx);
1046 const long length = scm_ilength (clause);
1047 ASSERT_SYNTAX_2 (length >= 1, s_bad_cond_clause, clause, expr);
1048
1049 test = SCM_CAR (clause);
1050 if (SCM_EQ_P (test, scm_sym_else) && else_literal_p)
1051 {
1052 const int last_clause_p = SCM_NULLP (SCM_CDR (clause_idx));
1053 ASSERT_SYNTAX_2 (length >= 2,
1054 s_bad_cond_clause, clause, expr);
1055 ASSERT_SYNTAX_2 (last_clause_p,
1056 s_misplaced_else_clause, clause, expr);
1057 SCM_SETCAR (clause, SCM_IM_ELSE);
1058 }
1059 else if (length >= 2
1060 && SCM_EQ_P (SCM_CADR (clause), scm_sym_arrow)
1061 && arrow_literal_p)
1062 {
1063 ASSERT_SYNTAX_2 (length > 2, s_missing_recipient, clause, expr);
1064 ASSERT_SYNTAX_2 (length == 3, s_extra_expression, clause, expr);
1065 SCM_SETCAR (SCM_CDR (clause), SCM_IM_ARROW);
1066 }
1067 }
1068
1069 SCM_SETCAR (expr, SCM_IM_COND);
1070 return expr;
1071 }
1072
1073
1074 SCM_SYNTAX (s_define, "define", scm_i_makbimacro, scm_m_define);
1075 SCM_GLOBAL_SYMBOL (scm_sym_define, s_define);
1076
1077 /* Guile provides an extension to R5RS' define syntax to represent function
1078 * currying in a compact way. With this extension, it is allowed to write
1079 * (define <nested-variable> <body>), where <nested-variable> has of one of
1080 * the forms (<nested-variable> <formals>), (<nested-variable> . <formal>),
1081 * (<variable> <formals>) or (<variable> . <formal>). As in R5RS, <formals>
1082 * should be either a sequence of zero or more variables, or a sequence of one
1083 * or more variables followed by a space-delimited period and another
1084 * variable. Each level of argument nesting wraps the <body> within another
1085 * lambda expression. For example, the following forms are allowed, each one
1086 * followed by an equivalent, more explicit implementation.
1087 * Example 1:
1088 * (define ((a b . c) . d) <body>) is equivalent to
1089 * (define a (lambda (b . c) (lambda d <body>)))
1090 * Example 2:
1091 * (define (((a) b) c . d) <body>) is equivalent to
1092 * (define a (lambda () (lambda (b) (lambda (c . d) <body>))))
1093 */
1094 /* Dirk:FIXME:: We should provide an implementation for 'define' in the R5RS
1095 * module that does not implement this extension. */
1096 static SCM
1097 canonicalize_define (const SCM expr)
1098 {
1099 SCM body;
1100 SCM variable;
1101
1102 const SCM cdr_expr = SCM_CDR (expr);
1103 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1104 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
1105
1106 body = SCM_CDR (cdr_expr);
1107 variable = SCM_CAR (cdr_expr);
1108 while (SCM_CONSP (variable))
1109 {
1110 /* This while loop realizes function currying by variable nesting.
1111 * Variable is known to be a nested-variable. In every iteration of the
1112 * loop another level of lambda expression is created, starting with the
1113 * innermost one. Note that we don't check for duplicate formals here:
1114 * This will be done by the memoizer of the lambda expression. */
1115 const SCM formals = SCM_CDR (variable);
1116 const SCM tail = scm_cons (formals, body);
1117
1118 /* Add source properties to each new lambda expression: */
1119 const SCM lambda = scm_cons_source (variable, scm_sym_lambda, tail);
1120
1121 body = scm_list_1 (lambda);
1122 variable = SCM_CAR (variable);
1123 }
1124 ASSERT_SYNTAX_2 (SCM_SYMBOLP (variable), s_bad_variable, variable, expr);
1125 ASSERT_SYNTAX (scm_ilength (body) == 1, s_expression, expr);
1126
1127 SCM_SETCAR (cdr_expr, variable);
1128 SCM_SETCDR (cdr_expr, body);
1129 return expr;
1130 }
1131
1132 SCM
1133 scm_m_define (SCM expr, SCM env)
1134 {
1135 ASSERT_SYNTAX (SCM_TOP_LEVEL (env), s_bad_define, expr);
1136
1137 {
1138 const SCM canonical_definition = canonicalize_define (expr);
1139 const SCM cdr_canonical_definition = SCM_CDR (canonical_definition);
1140 const SCM variable = SCM_CAR (cdr_canonical_definition);
1141 const SCM body = SCM_CDR (cdr_canonical_definition);
1142 const SCM value = scm_eval_car (body, env);
1143
1144 SCM var;
1145 if (SCM_REC_PROCNAMES_P)
1146 {
1147 SCM tmp = value;
1148 while (SCM_MACROP (tmp))
1149 tmp = SCM_MACRO_CODE (tmp);
1150 if (SCM_CLOSUREP (tmp)
1151 /* Only the first definition determines the name. */
1152 && SCM_FALSEP (scm_procedure_property (tmp, scm_sym_name)))
1153 scm_set_procedure_property_x (tmp, scm_sym_name, variable);
1154 }
1155
1156 var = scm_sym2var (variable, scm_env_top_level (env), SCM_BOOL_T);
1157 SCM_VARIABLE_SET (var, value);
1158
1159 return SCM_UNSPECIFIED;
1160 }
1161 }
1162
1163
1164 /* This is a helper function for forms (<keyword> <expression>) that are
1165 * transformed into (#@<keyword> '() <memoized_expression>) in order to allow
1166 * for easy creation of a thunk (i. e. a closure without arguments) using the
1167 * ('() <memoized_expression>) tail of the memoized form. */
1168 static SCM
1169 memoize_as_thunk_prototype (const SCM expr, const SCM env SCM_UNUSED)
1170 {
1171 const SCM cdr_expr = SCM_CDR (expr);
1172 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1173 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
1174
1175 SCM_SETCDR (expr, scm_cons (SCM_EOL, cdr_expr));
1176
1177 return expr;
1178 }
1179
1180
1181 SCM_SYNTAX (s_delay, "delay", scm_i_makbimacro, scm_m_delay);
1182 SCM_GLOBAL_SYMBOL (scm_sym_delay, s_delay);
1183
1184 /* Promises are implemented as closures with an empty parameter list. Thus,
1185 * (delay <expression>) is transformed into (#@delay '() <expression>), where
1186 * the empty list represents the empty parameter list. This representation
1187 * allows for easy creation of the closure during evaluation. */
1188 SCM
1189 scm_m_delay (SCM expr, SCM env)
1190 {
1191 const SCM new_expr = memoize_as_thunk_prototype (expr, env);
1192 SCM_SETCAR (new_expr, SCM_IM_DELAY);
1193 return new_expr;
1194 }
1195
1196
1197 SCM_SYNTAX(s_do, "do", scm_i_makbimacro, scm_m_do);
1198 SCM_GLOBAL_SYMBOL(scm_sym_do, s_do);
1199
1200 /* DO gets the most radically altered syntax. The order of the vars is
1201 * reversed here. During the evaluation this allows for simple consing of the
1202 * results of the inits and steps:
1203
1204 (do ((<var1> <init1> <step1>)
1205 (<var2> <init2>)
1206 ... )
1207 (<test> <return>)
1208 <body>)
1209
1210 ;; becomes
1211
1212 (#@do (<init1> <init2> ... <initn>)
1213 (varn ... var2 var1)
1214 (<test> <return>)
1215 (<body>)
1216 <step1> <step2> ... <stepn>) ;; missing steps replaced by var
1217 */
1218 SCM
1219 scm_m_do (SCM expr, SCM env SCM_UNUSED)
1220 {
1221 SCM variables = SCM_EOL;
1222 SCM init_forms = SCM_EOL;
1223 SCM step_forms = SCM_EOL;
1224 SCM binding_idx;
1225 SCM cddr_expr;
1226 SCM exit_clause;
1227 SCM commands;
1228 SCM tail;
1229
1230 const SCM cdr_expr = SCM_CDR (expr);
1231 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1232 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
1233
1234 /* Collect variables, init and step forms. */
1235 binding_idx = SCM_CAR (cdr_expr);
1236 ASSERT_SYNTAX_2 (scm_ilength (binding_idx) >= 0,
1237 s_bad_bindings, binding_idx, expr);
1238 for (; !SCM_NULLP (binding_idx); binding_idx = SCM_CDR (binding_idx))
1239 {
1240 const SCM binding = SCM_CAR (binding_idx);
1241 const long length = scm_ilength (binding);
1242 ASSERT_SYNTAX_2 (length == 2 || length == 3,
1243 s_bad_binding, binding, expr);
1244
1245 {
1246 const SCM name = SCM_CAR (binding);
1247 const SCM init = SCM_CADR (binding);
1248 const SCM step = (length == 2) ? name : SCM_CADDR (binding);
1249 ASSERT_SYNTAX_2 (SCM_SYMBOLP (name), s_bad_variable, name, expr);
1250 ASSERT_SYNTAX_2 (SCM_FALSEP (scm_c_memq (name, variables)),
1251 s_duplicate_binding, name, expr);
1252
1253 variables = scm_cons (name, variables);
1254 init_forms = scm_cons (init, init_forms);
1255 step_forms = scm_cons (step, step_forms);
1256 }
1257 }
1258 init_forms = scm_reverse_x (init_forms, SCM_UNDEFINED);
1259 step_forms = scm_reverse_x (step_forms, SCM_UNDEFINED);
1260
1261 /* Memoize the test form and the exit sequence. */
1262 cddr_expr = SCM_CDR (cdr_expr);
1263 exit_clause = SCM_CAR (cddr_expr);
1264 ASSERT_SYNTAX_2 (scm_ilength (exit_clause) >= 1,
1265 s_bad_exit_clause, exit_clause, expr);
1266
1267 commands = SCM_CDR (cddr_expr);
1268 tail = scm_cons2 (exit_clause, commands, step_forms);
1269 tail = scm_cons2 (init_forms, variables, tail);
1270 SCM_SETCAR (expr, SCM_IM_DO);
1271 SCM_SETCDR (expr, tail);
1272 return expr;
1273 }
1274
1275
1276 SCM_SYNTAX (s_if, "if", scm_i_makbimacro, scm_m_if);
1277 SCM_GLOBAL_SYMBOL (scm_sym_if, s_if);
1278
1279 SCM
1280 scm_m_if (SCM expr, SCM env SCM_UNUSED)
1281 {
1282 const SCM cdr_expr = SCM_CDR (expr);
1283 const long length = scm_ilength (cdr_expr);
1284 ASSERT_SYNTAX (length == 2 || length == 3, s_expression, expr);
1285 SCM_SETCAR (expr, SCM_IM_IF);
1286 return expr;
1287 }
1288
1289
1290 SCM_SYNTAX (s_lambda, "lambda", scm_i_makbimacro, scm_m_lambda);
1291 SCM_GLOBAL_SYMBOL (scm_sym_lambda, s_lambda);
1292
1293 /* A helper function for memoize_lambda to support checking for duplicate
1294 * formal arguments: Return true if OBJ is `eq?' to one of the elements of
1295 * LIST or to the cdr of the last cons. Therefore, LIST may have any of the
1296 * forms that a formal argument can have:
1297 * <rest>, (<arg1> ...), (<arg1> ... . <rest>) */
1298 static int
1299 c_improper_memq (SCM obj, SCM list)
1300 {
1301 for (; SCM_CONSP (list); list = SCM_CDR (list))
1302 {
1303 if (SCM_EQ_P (SCM_CAR (list), obj))
1304 return 1;
1305 }
1306 return SCM_EQ_P (list, obj);
1307 }
1308
1309 SCM
1310 scm_m_lambda (SCM expr, SCM env SCM_UNUSED)
1311 {
1312 SCM formals;
1313 SCM formals_idx;
1314 SCM cddr_expr;
1315 int documentation;
1316 SCM body;
1317 SCM new_body;
1318
1319 const SCM cdr_expr = SCM_CDR (expr);
1320 const long length = scm_ilength (cdr_expr);
1321 ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
1322 ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
1323
1324 /* Before iterating the list of formal arguments, make sure the formals
1325 * actually are given as either a symbol or a non-cyclic list. */
1326 formals = SCM_CAR (cdr_expr);
1327 if (SCM_CONSP (formals))
1328 {
1329 /* Dirk:FIXME:: We should check for a cyclic list of formals, and if
1330 * detected, report a 'Bad formals' error. */
1331 }
1332 else
1333 {
1334 ASSERT_SYNTAX_2 (SCM_SYMBOLP (formals) || SCM_NULLP (formals),
1335 s_bad_formals, formals, expr);
1336 }
1337
1338 /* Now iterate the list of formal arguments to check if all formals are
1339 * symbols, and that there are no duplicates. */
1340 formals_idx = formals;
1341 while (SCM_CONSP (formals_idx))
1342 {
1343 const SCM formal = SCM_CAR (formals_idx);
1344 const SCM next_idx = SCM_CDR (formals_idx);
1345 ASSERT_SYNTAX_2 (SCM_SYMBOLP (formal), s_bad_formal, formal, expr);
1346 ASSERT_SYNTAX_2 (!c_improper_memq (formal, next_idx),
1347 s_duplicate_formal, formal, expr);
1348 formals_idx = next_idx;
1349 }
1350 ASSERT_SYNTAX_2 (SCM_NULLP (formals_idx) || SCM_SYMBOLP (formals_idx),
1351 s_bad_formal, formals_idx, expr);
1352
1353 /* Memoize the body. Keep a potential documentation string. */
1354 /* Dirk:FIXME:: We should probably extract the documentation string to
1355 * some external database. Otherwise it will slow down execution, since
1356 * the documentation string will have to be skipped with every execution
1357 * of the closure. */
1358 cddr_expr = SCM_CDR (cdr_expr);
1359 documentation = (length >= 3 && SCM_STRINGP (SCM_CAR (cddr_expr)));
1360 body = documentation ? SCM_CDR (cddr_expr) : cddr_expr;
1361 new_body = m_body (SCM_IM_LAMBDA, body);
1362
1363 SCM_SETCAR (expr, SCM_IM_LAMBDA);
1364 if (documentation)
1365 SCM_SETCDR (cddr_expr, new_body);
1366 else
1367 SCM_SETCDR (cdr_expr, new_body);
1368 return expr;
1369 }
1370
1371
1372 /* Check if the format of the bindings is ((<symbol> <init-form>) ...). */
1373 static void
1374 check_bindings (const SCM bindings, const SCM expr)
1375 {
1376 SCM binding_idx;
1377
1378 ASSERT_SYNTAX_2 (scm_ilength (bindings) >= 0,
1379 s_bad_bindings, bindings, expr);
1380
1381 binding_idx = bindings;
1382 for (; !SCM_NULLP (binding_idx); binding_idx = SCM_CDR (binding_idx))
1383 {
1384 SCM name; /* const */
1385
1386 const SCM binding = SCM_CAR (binding_idx);
1387 ASSERT_SYNTAX_2 (scm_ilength (binding) == 2,
1388 s_bad_binding, binding, expr);
1389
1390 name = SCM_CAR (binding);
1391 ASSERT_SYNTAX_2 (SCM_SYMBOLP (name), s_bad_variable, name, expr);
1392 }
1393 }
1394
1395
1396 /* The bindings, which must have the format ((v1 i1) (v2 i2) ... (vn in)), are
1397 * transformed to the lists (vn ... v2 v1) and (i1 i2 ... in). That is, the
1398 * variables are returned in a list with their order reversed, and the init
1399 * forms are returned in a list in the same order as they are given in the
1400 * bindings. If a duplicate variable name is detected, an error is
1401 * signalled. */
1402 static void
1403 transform_bindings (
1404 const SCM bindings, const SCM expr,
1405 SCM *const rvarptr, SCM *const initptr )
1406 {
1407 SCM rvariables = SCM_EOL;
1408 SCM rinits = SCM_EOL;
1409 SCM binding_idx = bindings;
1410 for (; !SCM_NULLP (binding_idx); binding_idx = SCM_CDR (binding_idx))
1411 {
1412 const SCM binding = SCM_CAR (binding_idx);
1413 const SCM cdr_binding = SCM_CDR (binding);
1414 const SCM name = SCM_CAR (binding);
1415 ASSERT_SYNTAX_2 (SCM_FALSEP (scm_c_memq (name, rvariables)),
1416 s_duplicate_binding, name, expr);
1417 rvariables = scm_cons (name, rvariables);
1418 rinits = scm_cons (SCM_CAR (cdr_binding), rinits);
1419 }
1420 *rvarptr = rvariables;
1421 *initptr = scm_reverse_x (rinits, SCM_UNDEFINED);
1422 }
1423
1424
1425 SCM_SYNTAX(s_let, "let", scm_i_makbimacro, scm_m_let);
1426 SCM_GLOBAL_SYMBOL(scm_sym_let, s_let);
1427
1428 /* This function is a helper function for memoize_let. It transforms
1429 * (let name ((var init) ...) body ...) into
1430 * ((letrec ((name (lambda (var ...) body ...))) name) init ...)
1431 * and memoizes the expression. It is assumed that the caller has checked
1432 * that name is a symbol and that there are bindings and a body. */
1433 static SCM
1434 memoize_named_let (const SCM expr, const SCM env SCM_UNUSED)
1435 {
1436 SCM rvariables;
1437 SCM variables;
1438 SCM inits;
1439
1440 const SCM cdr_expr = SCM_CDR (expr);
1441 const SCM name = SCM_CAR (cdr_expr);
1442 const SCM cddr_expr = SCM_CDR (cdr_expr);
1443 const SCM bindings = SCM_CAR (cddr_expr);
1444 check_bindings (bindings, expr);
1445
1446 transform_bindings (bindings, expr, &rvariables, &inits);
1447 variables = scm_reverse_x (rvariables, SCM_UNDEFINED);
1448
1449 {
1450 const SCM let_body = SCM_CDR (cddr_expr);
1451 const SCM lambda_body = m_body (SCM_IM_LET, let_body);
1452 const SCM lambda_tail = scm_cons (variables, lambda_body);
1453 const SCM lambda_form = scm_cons_source (expr, scm_sym_lambda, lambda_tail);
1454
1455 const SCM rvar = scm_list_1 (name);
1456 const SCM init = scm_list_1 (lambda_form);
1457 const SCM body = m_body (SCM_IM_LET, scm_list_1 (name));
1458 const SCM letrec_tail = scm_cons (rvar, scm_cons (init, body));
1459 const SCM letrec_form = scm_cons_source (expr, SCM_IM_LETREC, letrec_tail);
1460 return scm_cons_source (expr, letrec_form, inits);
1461 }
1462 }
1463
1464 /* (let ((v1 i1) (v2 i2) ...) body) with variables v1 .. vn and initializers
1465 * i1 .. in is transformed to (#@let (vn ... v2 v1) (i1 i2 ...) body). */
1466 SCM
1467 scm_m_let (SCM expr, SCM env)
1468 {
1469 SCM bindings;
1470
1471 const SCM cdr_expr = SCM_CDR (expr);
1472 const long length = scm_ilength (cdr_expr);
1473 ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
1474 ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
1475
1476 bindings = SCM_CAR (cdr_expr);
1477 if (SCM_SYMBOLP (bindings))
1478 {
1479 ASSERT_SYNTAX (length >= 3, s_missing_expression, expr);
1480 return memoize_named_let (expr, env);
1481 }
1482
1483 check_bindings (bindings, expr);
1484 if (SCM_NULLP (bindings) || SCM_NULLP (SCM_CDR (bindings)))
1485 {
1486 /* Special case: no bindings or single binding => let* is faster. */
1487 const SCM body = m_body (SCM_IM_LET, SCM_CDR (cdr_expr));
1488 return scm_m_letstar (scm_cons2 (SCM_CAR (expr), bindings, body), env);
1489 }
1490 else
1491 {
1492 /* plain let */
1493 SCM rvariables;
1494 SCM inits;
1495 transform_bindings (bindings, expr, &rvariables, &inits);
1496
1497 {
1498 const SCM new_body = m_body (SCM_IM_LET, SCM_CDR (cdr_expr));
1499 const SCM new_tail = scm_cons2 (rvariables, inits, new_body);
1500 SCM_SETCAR (expr, SCM_IM_LET);
1501 SCM_SETCDR (expr, new_tail);
1502 return expr;
1503 }
1504 }
1505 }
1506
1507
1508 SCM_SYNTAX (s_letstar, "let*", scm_i_makbimacro, scm_m_letstar);
1509 SCM_GLOBAL_SYMBOL (scm_sym_letstar, s_letstar);
1510
1511 /* (let* ((v1 i1) (v2 i2) ...) body) with variables v1 .. vn and initializers
1512 * i1 .. in is transformed into the form (#@let* (v1 i1 v2 i2 ...) body). */
1513 SCM
1514 scm_m_letstar (SCM expr, SCM env SCM_UNUSED)
1515 {
1516 SCM binding_idx;
1517 SCM new_body;
1518
1519 const SCM cdr_expr = SCM_CDR (expr);
1520 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1521 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
1522
1523 binding_idx = SCM_CAR (cdr_expr);
1524 check_bindings (binding_idx, expr);
1525
1526 /* Transform ((v1 i1) (v2 i2) ...) into (v1 i1 v2 i2 ...). The
1527 * transformation is done in place. At the beginning of one iteration of
1528 * the loop the variable binding_idx holds the form
1529 * P1:( (vn . P2:(in . ())) . P3:( (vn+1 in+1) ... ) ),
1530 * where P1, P2 and P3 indicate the pairs, that are relevant for the
1531 * transformation. P1 and P2 are modified in the loop, P3 remains
1532 * untouched. After the execution of the loop, P1 will hold
1533 * P1:( vn . P2:(in . P3:( (vn+1 in+1) ... )) )
1534 * and binding_idx will hold P3. */
1535 while (!SCM_NULLP (binding_idx))
1536 {
1537 const SCM cdr_binding_idx = SCM_CDR (binding_idx); /* remember P3 */
1538 const SCM binding = SCM_CAR (binding_idx);
1539 const SCM name = SCM_CAR (binding);
1540 const SCM cdr_binding = SCM_CDR (binding);
1541
1542 SCM_SETCDR (cdr_binding, cdr_binding_idx); /* update P2 */
1543 SCM_SETCAR (binding_idx, name); /* update P1 */
1544 SCM_SETCDR (binding_idx, cdr_binding); /* update P1 */
1545
1546 binding_idx = cdr_binding_idx; /* continue with P3 */
1547 }
1548
1549 new_body = m_body (SCM_IM_LETSTAR, SCM_CDR (cdr_expr));
1550 SCM_SETCAR (expr, SCM_IM_LETSTAR);
1551 /* the bindings have been changed in place */
1552 SCM_SETCDR (cdr_expr, new_body);
1553 return expr;
1554 }
1555
1556
1557 SCM_SYNTAX(s_letrec, "letrec", scm_i_makbimacro, scm_m_letrec);
1558 SCM_GLOBAL_SYMBOL(scm_sym_letrec, s_letrec);
1559
1560 SCM
1561 scm_m_letrec (SCM expr, SCM env)
1562 {
1563 SCM bindings;
1564
1565 const SCM cdr_expr = SCM_CDR (expr);
1566 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1567 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
1568
1569 bindings = SCM_CAR (cdr_expr);
1570 if (SCM_NULLP (bindings))
1571 {
1572 /* no bindings, let* is executed faster */
1573 SCM body = m_body (SCM_IM_LETREC, SCM_CDR (cdr_expr));
1574 return scm_m_letstar (scm_cons2 (SCM_CAR (expr), SCM_EOL, body), env);
1575 }
1576 else
1577 {
1578 SCM rvariables;
1579 SCM inits;
1580 SCM new_body;
1581
1582 check_bindings (bindings, expr);
1583 transform_bindings (bindings, expr, &rvariables, &inits);
1584 new_body = m_body (SCM_IM_LETREC, SCM_CDR (cdr_expr));
1585 return scm_cons2 (SCM_IM_LETREC, rvariables, scm_cons (inits, new_body));
1586 }
1587 }
1588
1589
1590 SCM_SYNTAX (s_or, "or", scm_i_makbimacro, scm_m_or);
1591 SCM_GLOBAL_SYMBOL (scm_sym_or, s_or);
1592
1593 SCM
1594 scm_m_or (SCM expr, SCM env SCM_UNUSED)
1595 {
1596 const SCM cdr_expr = SCM_CDR (expr);
1597 const long length = scm_ilength (cdr_expr);
1598
1599 ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
1600
1601 if (length == 0)
1602 {
1603 /* Special case: (or) is replaced by #f. */
1604 return SCM_BOOL_F;
1605 }
1606 else
1607 {
1608 SCM_SETCAR (expr, SCM_IM_OR);
1609 return expr;
1610 }
1611 }
1612
1613
1614 SCM_SYNTAX (s_quasiquote, "quasiquote", scm_makacro, scm_m_quasiquote);
1615 SCM_GLOBAL_SYMBOL (scm_sym_quasiquote, s_quasiquote);
1616 SCM_GLOBAL_SYMBOL (scm_sym_unquote, "unquote");
1617 SCM_GLOBAL_SYMBOL (scm_sym_uq_splicing, "unquote-splicing");
1618
1619 /* Internal function to handle a quasiquotation: 'form' is the parameter in
1620 * the call (quasiquotation form), 'env' is the environment where unquoted
1621 * expressions will be evaluated, and 'depth' is the current quasiquotation
1622 * nesting level and is known to be greater than zero. */
1623 static SCM
1624 iqq (SCM form, SCM env, unsigned long int depth)
1625 {
1626 if (SCM_CONSP (form))
1627 {
1628 const SCM tmp = SCM_CAR (form);
1629 if (SCM_EQ_P (tmp, scm_sym_quasiquote))
1630 {
1631 const SCM args = SCM_CDR (form);
1632 ASSERT_SYNTAX (scm_ilength (args) == 1, s_expression, form);
1633 return scm_list_2 (tmp, iqq (SCM_CAR (args), env, depth + 1));
1634 }
1635 else if (SCM_EQ_P (tmp, scm_sym_unquote))
1636 {
1637 const SCM args = SCM_CDR (form);
1638 ASSERT_SYNTAX (scm_ilength (args) == 1, s_expression, form);
1639 if (depth - 1 == 0)
1640 return scm_eval_car (args, env);
1641 else
1642 return scm_list_2 (tmp, iqq (SCM_CAR (args), env, depth - 1));
1643 }
1644 else if (SCM_CONSP (tmp)
1645 && SCM_EQ_P (SCM_CAR (tmp), scm_sym_uq_splicing))
1646 {
1647 const SCM args = SCM_CDR (tmp);
1648 ASSERT_SYNTAX (scm_ilength (args) == 1, s_expression, form);
1649 if (depth - 1 == 0)
1650 {
1651 const SCM list = scm_eval_car (args, env);
1652 const SCM rest = SCM_CDR (form);
1653 ASSERT_SYNTAX_2 (scm_ilength (list) >= 0,
1654 s_splicing, list, form);
1655 return scm_append (scm_list_2 (list, iqq (rest, env, depth)));
1656 }
1657 else
1658 return scm_cons (iqq (SCM_CAR (form), env, depth - 1),
1659 iqq (SCM_CDR (form), env, depth));
1660 }
1661 else
1662 return scm_cons (iqq (SCM_CAR (form), env, depth),
1663 iqq (SCM_CDR (form), env, depth));
1664 }
1665 else if (SCM_VECTORP (form))
1666 {
1667 size_t i = SCM_VECTOR_LENGTH (form);
1668 SCM const *const data = SCM_VELTS (form);
1669 SCM tmp = SCM_EOL;
1670 while (i != 0)
1671 tmp = scm_cons (data[--i], tmp);
1672 scm_remember_upto_here_1 (form);
1673 return scm_vector (iqq (tmp, env, depth));
1674 }
1675 else
1676 return form;
1677 }
1678
1679 SCM
1680 scm_m_quasiquote (SCM expr, SCM env)
1681 {
1682 const SCM cdr_expr = SCM_CDR (expr);
1683 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1684 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
1685 return iqq (SCM_CAR (cdr_expr), env, 1);
1686 }
1687
1688
1689 SCM_SYNTAX (s_quote, "quote", scm_i_makbimacro, scm_m_quote);
1690 SCM_GLOBAL_SYMBOL (scm_sym_quote, s_quote);
1691
1692 SCM
1693 scm_m_quote (SCM expr, SCM env SCM_UNUSED)
1694 {
1695 SCM quotee;
1696
1697 const SCM cdr_expr = SCM_CDR (expr);
1698 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1699 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
1700 quotee = SCM_CAR (cdr_expr);
1701 if (is_self_quoting_p (quotee))
1702 return quotee;
1703 SCM_SETCAR (expr, SCM_IM_QUOTE);
1704 return expr;
1705 }
1706
1707
1708 /* Will go into the RnRS module when Guile is factorized.
1709 SCM_SYNTAX (s_set_x, "set!", scm_i_makbimacro, scm_m_set_x); */
1710 static const char s_set_x[] = "set!";
1711 SCM_GLOBAL_SYMBOL (scm_sym_set_x, s_set_x);
1712
1713 SCM
1714 scm_m_set_x (SCM expr, SCM env SCM_UNUSED)
1715 {
1716 SCM variable;
1717
1718 const SCM cdr_expr = SCM_CDR (expr);
1719 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1720 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
1721 variable = SCM_CAR (cdr_expr);
1722 ASSERT_SYNTAX_2 (SCM_SYMBOLP (variable) || SCM_VARIABLEP (variable),
1723 s_bad_variable, variable, expr);
1724
1725 SCM_SETCAR (expr, SCM_IM_SET_X);
1726 return expr;
1727 }
1728
1729
1730 /* Start of the memoizers for non-R5RS builtin macros. */
1731
1732
1733 SCM_SYNTAX (s_atapply, "@apply", scm_i_makbimacro, scm_m_apply);
1734 SCM_GLOBAL_SYMBOL (scm_sym_atapply, s_atapply);
1735 SCM_GLOBAL_SYMBOL (scm_sym_apply, s_atapply + 1);
1736
1737 SCM
1738 scm_m_apply (SCM expr, SCM env SCM_UNUSED)
1739 {
1740 const SCM cdr_expr = SCM_CDR (expr);
1741 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1742 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_missing_expression, expr);
1743
1744 SCM_SETCAR (expr, SCM_IM_APPLY);
1745 return expr;
1746 }
1747
1748
1749 SCM_SYNTAX (s_atbind, "@bind", scm_i_makbimacro, scm_m_atbind);
1750
1751 /* FIXME: The following explanation should go into the documentation: */
1752 /* (@bind ((var init) ...) body ...) will assign the values of the `init's to
1753 * the global variables named by `var's (symbols, not evaluated), creating
1754 * them if they don't exist, executes body, and then restores the previous
1755 * values of the `var's. Additionally, whenever control leaves body, the
1756 * values of the `var's are saved and restored when control returns. It is an
1757 * error when a symbol appears more than once among the `var's. All `init's
1758 * are evaluated before any `var' is set.
1759 *
1760 * Think of this as `let' for dynamic scope.
1761 */
1762
1763 /* (@bind ((var1 exp1) ... (varn expn)) body ...) is memoized into
1764 * (#@bind ((varn ... var1) . (exp1 ... expn)) body ...).
1765 *
1766 * FIXME - also implement `@bind*'.
1767 */
1768 SCM
1769 scm_m_atbind (SCM expr, SCM env)
1770 {
1771 SCM bindings;
1772 SCM rvariables;
1773 SCM inits;
1774 SCM variable_idx;
1775
1776 const SCM top_level = scm_env_top_level (env);
1777
1778 const SCM cdr_expr = SCM_CDR (expr);
1779 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1780 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
1781 bindings = SCM_CAR (cdr_expr);
1782 check_bindings (bindings, expr);
1783 transform_bindings (bindings, expr, &rvariables, &inits);
1784
1785 for (variable_idx = rvariables;
1786 !SCM_NULLP (variable_idx);
1787 variable_idx = SCM_CDR (variable_idx))
1788 {
1789 /* The first call to scm_sym2var will look beyond the current module,
1790 * while the second call wont. */
1791 const SCM variable = SCM_CAR (variable_idx);
1792 SCM new_variable = scm_sym2var (variable, top_level, SCM_BOOL_F);
1793 if (SCM_FALSEP (new_variable))
1794 new_variable = scm_sym2var (variable, top_level, SCM_BOOL_T);
1795 SCM_SETCAR (variable_idx, new_variable);
1796 }
1797
1798 SCM_SETCAR (expr, SCM_IM_BIND);
1799 SCM_SETCAR (cdr_expr, scm_cons (rvariables, inits));
1800 return expr;
1801 }
1802
1803
1804 SCM_SYNTAX(s_atcall_cc, "@call-with-current-continuation", scm_i_makbimacro, scm_m_cont);
1805 SCM_GLOBAL_SYMBOL(scm_sym_atcall_cc, s_atcall_cc);
1806
1807 SCM
1808 scm_m_cont (SCM expr, SCM env SCM_UNUSED)
1809 {
1810 const SCM cdr_expr = SCM_CDR (expr);
1811 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1812 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
1813
1814 SCM_SETCAR (expr, SCM_IM_CONT);
1815 return expr;
1816 }
1817
1818
1819 SCM_SYNTAX (s_at_call_with_values, "@call-with-values", scm_i_makbimacro, scm_m_at_call_with_values);
1820 SCM_GLOBAL_SYMBOL(scm_sym_at_call_with_values, s_at_call_with_values);
1821
1822 SCM
1823 scm_m_at_call_with_values (SCM expr, SCM env SCM_UNUSED)
1824 {
1825 const SCM cdr_expr = SCM_CDR (expr);
1826 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1827 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
1828
1829 SCM_SETCAR (expr, SCM_IM_CALL_WITH_VALUES);
1830 return expr;
1831 }
1832
1833
1834 SCM_SYNTAX (s_future, "future", scm_i_makbimacro, scm_m_future);
1835 SCM_GLOBAL_SYMBOL (scm_sym_future, s_future);
1836
1837 /* Like promises, futures are implemented as closures with an empty
1838 * parameter list. Thus, (future <expression>) is transformed into
1839 * (#@future '() <expression>), where the empty list represents the
1840 * empty parameter list. This representation allows for easy creation
1841 * of the closure during evaluation. */
1842 SCM
1843 scm_m_future (SCM expr, SCM env)
1844 {
1845 const SCM new_expr = memoize_as_thunk_prototype (expr, env);
1846 SCM_SETCAR (new_expr, SCM_IM_FUTURE);
1847 return new_expr;
1848 }
1849
1850
1851 SCM_SYNTAX (s_gset_x, "set!", scm_i_makbimacro, scm_m_generalized_set_x);
1852 SCM_SYMBOL (scm_sym_setter, "setter");
1853
1854 SCM
1855 scm_m_generalized_set_x (SCM expr, SCM env)
1856 {
1857 SCM target, exp_target;
1858
1859 const SCM cdr_expr = SCM_CDR (expr);
1860 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1861 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
1862
1863 target = SCM_CAR (cdr_expr);
1864 if (!SCM_CONSP (target))
1865 {
1866 /* R5RS usage */
1867 return scm_m_set_x (expr, env);
1868 }
1869 else
1870 {
1871 /* (set! (foo bar ...) baz) becomes ((setter foo) bar ... baz) */
1872 /* Macroexpanding the target might return things of the form
1873 (begin <atom>). In that case, <atom> must be a symbol or a
1874 variable and we memoize to (set! <atom> ...).
1875 */
1876 exp_target = scm_macroexp (target, env);
1877 if (SCM_EQ_P (SCM_CAR (exp_target), SCM_IM_BEGIN)
1878 && !SCM_NULLP (SCM_CDR (exp_target))
1879 && SCM_NULLP (SCM_CDDR (exp_target)))
1880 {
1881 exp_target= SCM_CADR (exp_target);
1882 ASSERT_SYNTAX_2 (SCM_SYMBOLP (exp_target)
1883 || SCM_VARIABLEP (exp_target),
1884 s_bad_variable, exp_target, expr);
1885 return scm_cons (SCM_IM_SET_X, scm_cons (exp_target,
1886 SCM_CDR (cdr_expr)));
1887 }
1888 else
1889 {
1890 const SCM setter_proc_tail = scm_list_1 (SCM_CAR (target));
1891 const SCM setter_proc = scm_cons_source (expr, scm_sym_setter,
1892 setter_proc_tail);
1893
1894 const SCM cddr_expr = SCM_CDR (cdr_expr);
1895 const SCM setter_args = scm_append_x (scm_list_2 (SCM_CDR (target),
1896 cddr_expr));
1897
1898 SCM_SETCAR (expr, setter_proc);
1899 SCM_SETCDR (expr, setter_args);
1900 return expr;
1901 }
1902 }
1903 }
1904
1905
1906 /* @slot-ref is bound privately in the (oop goops) module from goops.c. As
1907 * soon as the module system allows us to more freely create bindings in
1908 * arbitrary modules during the startup phase, the code from goops.c should be
1909 * moved here. */
1910 SCM
1911 scm_m_atslot_ref (SCM expr, SCM env SCM_UNUSED)
1912 {
1913 SCM slot_nr;
1914
1915 const SCM cdr_expr = SCM_CDR (expr);
1916 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1917 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
1918 slot_nr = SCM_CADR (cdr_expr);
1919 ASSERT_SYNTAX_2 (SCM_INUMP (slot_nr), s_bad_slot_number, slot_nr, expr);
1920
1921 SCM_SETCAR (expr, SCM_IM_SLOT_REF);
1922 return expr;
1923 }
1924
1925
1926 /* @slot-set! is bound privately in the (oop goops) module from goops.c. As
1927 * soon as the module system allows us to more freely create bindings in
1928 * arbitrary modules during the startup phase, the code from goops.c should be
1929 * moved here. */
1930 SCM
1931 scm_m_atslot_set_x (SCM expr, SCM env SCM_UNUSED)
1932 {
1933 SCM slot_nr;
1934
1935 const SCM cdr_expr = SCM_CDR (expr);
1936 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1937 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 3, s_expression, expr);
1938 slot_nr = SCM_CADR (cdr_expr);
1939 ASSERT_SYNTAX_2 (SCM_INUMP (slot_nr), s_bad_slot_number, slot_nr, expr);
1940
1941 SCM_SETCAR (expr, SCM_IM_SLOT_SET_X);
1942 return expr;
1943 }
1944
1945
1946 #if SCM_ENABLE_ELISP
1947
1948 static const char s_defun[] = "Symbol's function definition is void";
1949
1950 SCM_SYNTAX (s_nil_cond, "nil-cond", scm_i_makbimacro, scm_m_nil_cond);
1951
1952 /* nil-cond expressions have the form
1953 * (nil-cond COND VAL COND VAL ... ELSEVAL) */
1954 SCM
1955 scm_m_nil_cond (SCM expr, SCM env SCM_UNUSED)
1956 {
1957 const long length = scm_ilength (SCM_CDR (expr));
1958 ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
1959 ASSERT_SYNTAX (length >= 1 && (length % 2) == 1, s_expression, expr);
1960
1961 SCM_SETCAR (expr, SCM_IM_NIL_COND);
1962 return expr;
1963 }
1964
1965
1966 SCM_SYNTAX (s_atfop, "@fop", scm_i_makbimacro, scm_m_atfop);
1967
1968 /* The @fop-macro handles procedure and macro applications for elisp. The
1969 * input expression must have the form
1970 * (@fop <var> (transformer-macro <expr> ...))
1971 * where <var> must be a symbol. The expression is transformed into the
1972 * memoized form of either
1973 * (apply <un-aliased var> (transformer-macro <expr> ...))
1974 * if the value of var (across all aliasing) is not a macro, or
1975 * (<un-aliased var> <expr> ...)
1976 * if var is a macro. */
1977 SCM
1978 scm_m_atfop (SCM expr, SCM env SCM_UNUSED)
1979 {
1980 SCM location;
1981 SCM symbol;
1982
1983 const SCM cdr_expr = SCM_CDR (expr);
1984 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
1985 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 1, s_missing_expression, expr);
1986
1987 symbol = SCM_CAR (cdr_expr);
1988 ASSERT_SYNTAX_2 (SCM_SYMBOLP (symbol), s_bad_variable, symbol, expr);
1989
1990 location = scm_symbol_fref (symbol);
1991 ASSERT_SYNTAX_2 (SCM_VARIABLEP (location), s_defun, symbol, expr);
1992
1993 /* The elisp function `defalias' allows to define aliases for symbols. To
1994 * look up such definitions, the chain of symbol definitions has to be
1995 * followed up to the terminal symbol. */
1996 while (SCM_SYMBOLP (SCM_VARIABLE_REF (location)))
1997 {
1998 const SCM alias = SCM_VARIABLE_REF (location);
1999 location = scm_symbol_fref (alias);
2000 ASSERT_SYNTAX_2 (SCM_VARIABLEP (location), s_defun, symbol, expr);
2001 }
2002
2003 /* Memoize the value location belonging to the terminal symbol. */
2004 SCM_SETCAR (cdr_expr, location);
2005
2006 if (!SCM_MACROP (SCM_VARIABLE_REF (location)))
2007 {
2008 /* Since the location does not contain a macro, the form is a procedure
2009 * application. Replace `@fop' by `@apply' and transform the expression
2010 * including the `transformer-macro'. */
2011 SCM_SETCAR (expr, SCM_IM_APPLY);
2012 return expr;
2013 }
2014 else
2015 {
2016 /* Since the location contains a macro, the arguments should not be
2017 * transformed, so the `transformer-macro' is cut out. The resulting
2018 * expression starts with the memoized variable, that is at the cdr of
2019 * the input expression. */
2020 SCM_SETCDR (cdr_expr, SCM_CDADR (cdr_expr));
2021 return cdr_expr;
2022 }
2023 }
2024
2025 #endif /* SCM_ENABLE_ELISP */
2026
2027
2028 #if (SCM_ENABLE_DEPRECATED == 1)
2029
2030 /* Deprecated in guile 1.7.0 on 2003-11-09. */
2031 SCM
2032 scm_m_expand_body (SCM exprs, SCM env)
2033 {
2034 scm_c_issue_deprecation_warning
2035 ("`scm_m_expand_body' is deprecated.");
2036 m_expand_body (exprs, env);
2037 return exprs;
2038 }
2039
2040
2041 SCM_SYNTAX (s_undefine, "undefine", scm_makacro, scm_m_undefine);
2042
2043 SCM
2044 scm_m_undefine (SCM expr, SCM env)
2045 {
2046 SCM variable;
2047 SCM location;
2048
2049 const SCM cdr_expr = SCM_CDR (expr);
2050 ASSERT_SYNTAX (SCM_TOP_LEVEL (env), "Bad undefine placement in", expr);
2051 ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
2052 ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
2053
2054 variable = SCM_CAR (cdr_expr);
2055 ASSERT_SYNTAX_2 (SCM_SYMBOLP (variable), s_bad_variable, variable, expr);
2056 location = scm_sym2var (variable, scm_env_top_level (env), SCM_BOOL_F);
2057 ASSERT_SYNTAX_2 (!SCM_FALSEP (location)
2058 && !SCM_UNBNDP (SCM_VARIABLE_REF (location)),
2059 "variable already unbound ", variable, expr);
2060 SCM_VARIABLE_SET (location, SCM_UNDEFINED);
2061 return SCM_UNSPECIFIED;
2062 }
2063
2064
2065 SCM
2066 scm_macroexp (SCM x, SCM env)
2067 {
2068 SCM res, proc, orig_sym;
2069
2070 /* Don't bother to produce error messages here. We get them when we
2071 eventually execute the code for real. */
2072
2073 macro_tail:
2074 orig_sym = SCM_CAR (x);
2075 if (!SCM_SYMBOLP (orig_sym))
2076 return x;
2077
2078 {
2079 SCM *proc_ptr = scm_lookupcar1 (x, env, 0);
2080 if (proc_ptr == NULL)
2081 {
2082 /* We have lost the race. */
2083 goto macro_tail;
2084 }
2085 proc = *proc_ptr;
2086 }
2087
2088 /* Only handle memoizing macros. `Acros' and `macros' are really
2089 special forms and should not be evaluated here. */
2090
2091 if (!SCM_MACROP (proc)
2092 || (SCM_MACRO_TYPE (proc) != 2 && !SCM_BUILTIN_MACRO_P (proc)))
2093 return x;
2094
2095 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of lookupcar */
2096 res = scm_call_2 (SCM_MACRO_CODE (proc), x, env);
2097
2098 if (scm_ilength (res) <= 0)
2099 res = scm_list_2 (SCM_IM_BEGIN, res);
2100
2101 SCM_DEFER_INTS;
2102 SCM_SETCAR (x, SCM_CAR (res));
2103 SCM_SETCDR (x, SCM_CDR (res));
2104 SCM_ALLOW_INTS;
2105
2106 goto macro_tail;
2107 }
2108
2109 #endif
2110
2111 /*****************************************************************************/
2112 /*****************************************************************************/
2113 /* The definitions for unmemoization start here. */
2114 /*****************************************************************************/
2115 /*****************************************************************************/
2116
2117 #define SCM_BIT7(x) (127 & SCM_UNPACK (x))
2118
2119 SCM_SYMBOL (sym_three_question_marks, "???");
2120
2121
2122 /* scm_unmemocopy takes a memoized expression together with its
2123 * environment and rewrites it to its original form. Thus, it is the
2124 * inversion of the rewrite rules above. The procedure is not
2125 * optimized for speed. It's used in scm_iprin1 when printing the
2126 * code of a closure, in scm_procedure_source, in display_frame when
2127 * generating the source for a stackframe in a backtrace, and in
2128 * display_expression.
2129 *
2130 * Unmemoizing is not a reliable process. You cannot in general
2131 * expect to get the original source back.
2132 *
2133 * However, GOOPS currently relies on this for method compilation.
2134 * This ought to change.
2135 */
2136
2137 static SCM
2138 build_binding_list (SCM rnames, SCM rinits)
2139 {
2140 SCM bindings = SCM_EOL;
2141 while (!SCM_NULLP (rnames))
2142 {
2143 SCM binding = scm_list_2 (SCM_CAR (rnames), SCM_CAR (rinits));
2144 bindings = scm_cons (binding, bindings);
2145 rnames = SCM_CDR (rnames);
2146 rinits = SCM_CDR (rinits);
2147 }
2148 return bindings;
2149 }
2150
2151
2152 static SCM
2153 unmemocar (SCM form, SCM env)
2154 {
2155 if (!SCM_CONSP (form))
2156 return form;
2157 else
2158 {
2159 SCM c = SCM_CAR (form);
2160 if (SCM_VARIABLEP (c))
2161 {
2162 SCM sym = scm_module_reverse_lookup (scm_env_module (env), c);
2163 if (SCM_FALSEP (sym))
2164 sym = sym_three_question_marks;
2165 SCM_SETCAR (form, sym);
2166 }
2167 else if (SCM_ILOCP (c))
2168 {
2169 unsigned long int ir;
2170
2171 for (ir = SCM_IFRAME (c); ir != 0; --ir)
2172 env = SCM_CDR (env);
2173 env = SCM_CAAR (env);
2174 for (ir = SCM_IDIST (c); ir != 0; --ir)
2175 env = SCM_CDR (env);
2176
2177 SCM_SETCAR (form, SCM_ICDRP (c) ? env : SCM_CAR (env));
2178 }
2179 return form;
2180 }
2181 }
2182
2183
2184 SCM
2185 scm_unmemocopy (SCM x, SCM env)
2186 {
2187 SCM ls, z;
2188 SCM p;
2189
2190 if (SCM_VECTORP (x))
2191 {
2192 return scm_list_2 (scm_sym_quote, x);
2193 }
2194 else if (!SCM_CONSP (x))
2195 return x;
2196
2197 p = scm_whash_lookup (scm_source_whash, x);
2198 if (SCM_ISYMP (SCM_CAR (x)))
2199 {
2200 switch (SCM_ISYMNUM (SCM_CAR (x)))
2201 {
2202 case (SCM_ISYMNUM (SCM_IM_AND)):
2203 ls = z = scm_cons (scm_sym_and, SCM_UNSPECIFIED);
2204 break;
2205 case (SCM_ISYMNUM (SCM_IM_BEGIN)):
2206 ls = z = scm_cons (scm_sym_begin, SCM_UNSPECIFIED);
2207 break;
2208 case (SCM_ISYMNUM (SCM_IM_CASE)):
2209 ls = z = scm_cons (scm_sym_case, SCM_UNSPECIFIED);
2210 break;
2211 case (SCM_ISYMNUM (SCM_IM_COND)):
2212 ls = z = scm_cons (scm_sym_cond, SCM_UNSPECIFIED);
2213 break;
2214 case (SCM_ISYMNUM (SCM_IM_DO)):
2215 {
2216 /* format: (#@do (i1 ... ik) (nk ... n1) (test) (body) s1 ... sk),
2217 * where ix is an initializer for a local variable, nx is the name
2218 * of the local variable, test is the test clause of the do loop,
2219 * body is the body of the do loop and sx are the step clauses for
2220 * the local variables. */
2221 SCM names, inits, test, memoized_body, steps, bindings;
2222
2223 x = SCM_CDR (x);
2224 inits = scm_reverse (scm_unmemocopy (SCM_CAR (x), env));
2225 x = SCM_CDR (x);
2226 names = SCM_CAR (x);
2227 env = SCM_EXTEND_ENV (names, SCM_EOL, env);
2228 x = SCM_CDR (x);
2229 test = scm_unmemocopy (SCM_CAR (x), env);
2230 x = SCM_CDR (x);
2231 memoized_body = SCM_CAR (x);
2232 x = SCM_CDR (x);
2233 steps = scm_reverse (scm_unmemocopy (x, env));
2234
2235 /* build transformed binding list */
2236 bindings = SCM_EOL;
2237 while (!SCM_NULLP (names))
2238 {
2239 SCM name = SCM_CAR (names);
2240 SCM init = SCM_CAR (inits);
2241 SCM step = SCM_CAR (steps);
2242 step = SCM_EQ_P (step, name) ? SCM_EOL : scm_list_1 (step);
2243
2244 bindings = scm_cons (scm_cons2 (name, init, step), bindings);
2245
2246 names = SCM_CDR (names);
2247 inits = SCM_CDR (inits);
2248 steps = SCM_CDR (steps);
2249 }
2250 z = scm_cons (test, SCM_UNSPECIFIED);
2251 ls = scm_cons2 (scm_sym_do, bindings, z);
2252
2253 x = scm_cons (SCM_BOOL_F, memoized_body);
2254 break;
2255 }
2256 case (SCM_ISYMNUM (SCM_IM_IF)):
2257 ls = z = scm_cons (scm_sym_if, SCM_UNSPECIFIED);
2258 break;
2259 case (SCM_ISYMNUM (SCM_IM_LET)):
2260 {
2261 /* format: (#@let (nk nk-1 ...) (i1 ... ik) b1 ...),
2262 * where nx is the name of a local variable, ix is an initializer
2263 * for the local variable and by are the body clauses. */
2264 SCM rnames, rinits, bindings;
2265
2266 x = SCM_CDR (x);
2267 rnames = SCM_CAR (x);
2268 x = SCM_CDR (x);
2269 rinits = scm_reverse (scm_unmemocopy (SCM_CAR (x), env));
2270 env = SCM_EXTEND_ENV (rnames, SCM_EOL, env);
2271
2272 bindings = build_binding_list (rnames, rinits);
2273 z = scm_cons (bindings, SCM_UNSPECIFIED);
2274 ls = scm_cons (scm_sym_let, z);
2275 break;
2276 }
2277 case (SCM_ISYMNUM (SCM_IM_LETREC)):
2278 {
2279 /* format: (#@letrec (vn ... v2 v1) (i1 i2 ... in) b1 ...),
2280 * where vx is the name of a local variable, ix is an initializer
2281 * for the local variable and by are the body clauses. */
2282 SCM rnames, rinits, bindings;
2283
2284 x = SCM_CDR (x);
2285 rnames = SCM_CAR (x);
2286 env = SCM_EXTEND_ENV (rnames, SCM_EOL, env);
2287 x = SCM_CDR (x);
2288 rinits = scm_reverse (scm_unmemocopy (SCM_CAR (x), env));
2289
2290 bindings = build_binding_list (rnames, rinits);
2291 z = scm_cons (bindings, SCM_UNSPECIFIED);
2292 ls = scm_cons (scm_sym_letrec, z);
2293 break;
2294 }
2295 case (SCM_ISYMNUM (SCM_IM_LETSTAR)):
2296 {
2297 SCM b, y;
2298 x = SCM_CDR (x);
2299 b = SCM_CAR (x);
2300 y = SCM_EOL;
2301 if (SCM_NULLP (b))
2302 {
2303 env = SCM_EXTEND_ENV (SCM_EOL, SCM_EOL, env);
2304 }
2305 else
2306 {
2307 SCM copy = scm_unmemocopy (SCM_CADR (b), env);
2308 SCM initializer = unmemocar (scm_list_1 (copy), env);
2309 y = z = scm_acons (SCM_CAR (b), initializer, SCM_UNSPECIFIED);
2310 env = SCM_EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
2311 b = SCM_CDDR (b);
2312 if (SCM_NULLP (b))
2313 {
2314 SCM_SETCDR (y, SCM_EOL);
2315 z = scm_cons (y, SCM_UNSPECIFIED);
2316 ls = scm_cons (scm_sym_let, z);
2317 break;
2318 }
2319 do
2320 {
2321 copy = scm_unmemocopy (SCM_CADR (b), env);
2322 initializer = unmemocar (scm_list_1 (copy), env);
2323 SCM_SETCDR (z, scm_acons (SCM_CAR (b),
2324 copy,
2325 SCM_UNSPECIFIED));
2326 z = SCM_CDR (z);
2327 env = SCM_EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
2328 b = SCM_CDDR (b);
2329 }
2330 while (!SCM_NULLP (b));
2331 SCM_SETCDR (z, SCM_EOL);
2332 }
2333 z = scm_cons (y, SCM_UNSPECIFIED);
2334 ls = scm_cons (scm_sym_letstar, z);
2335 break;
2336 }
2337 case (SCM_ISYMNUM (SCM_IM_OR)):
2338 ls = z = scm_cons (scm_sym_or, SCM_UNSPECIFIED);
2339 break;
2340 case (SCM_ISYMNUM (SCM_IM_LAMBDA)):
2341 x = SCM_CDR (x);
2342 z = scm_cons (SCM_CAR (x), SCM_UNSPECIFIED);
2343 ls = scm_cons (scm_sym_lambda, z);
2344 env = SCM_EXTEND_ENV (SCM_CAR (x), SCM_EOL, env);
2345 break;
2346 case (SCM_ISYMNUM (SCM_IM_QUOTE)):
2347 ls = z = scm_cons (scm_sym_quote, SCM_UNSPECIFIED);
2348 break;
2349 case (SCM_ISYMNUM (SCM_IM_SET_X)):
2350 ls = z = scm_cons (scm_sym_set_x, SCM_UNSPECIFIED);
2351 break;
2352 case (SCM_ISYMNUM (SCM_IM_APPLY)):
2353 ls = z = scm_cons (scm_sym_atapply, SCM_UNSPECIFIED);
2354 break;
2355 case (SCM_ISYMNUM (SCM_IM_CONT)):
2356 ls = z = scm_cons (scm_sym_atcall_cc, SCM_UNSPECIFIED);
2357 break;
2358 case (SCM_ISYMNUM (SCM_IM_DELAY)):
2359 ls = z = scm_cons (scm_sym_delay, SCM_UNSPECIFIED);
2360 x = SCM_CDR (x);
2361 break;
2362 case (SCM_ISYMNUM (SCM_IM_FUTURE)):
2363 ls = z = scm_cons (scm_sym_future, SCM_UNSPECIFIED);
2364 x = SCM_CDR (x);
2365 break;
2366 case (SCM_ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
2367 ls = z = scm_cons (scm_sym_at_call_with_values, SCM_UNSPECIFIED);
2368 break;
2369 case (SCM_ISYMNUM (SCM_IM_ELSE)):
2370 ls = z = scm_cons (scm_sym_else, SCM_UNSPECIFIED);
2371 break;
2372 default:
2373 ls = z = unmemocar (scm_cons (scm_unmemocopy (SCM_CAR (x), env),
2374 SCM_UNSPECIFIED),
2375 env);
2376 }
2377 }
2378 else
2379 {
2380 ls = z = unmemocar (scm_cons (scm_unmemocopy (SCM_CAR (x), env),
2381 SCM_UNSPECIFIED),
2382 env);
2383 }
2384
2385 x = SCM_CDR (x);
2386 while (SCM_CONSP (x))
2387 {
2388 SCM form = SCM_CAR (x);
2389 if (!SCM_ISYMP (form))
2390 {
2391 SCM copy = scm_cons (scm_unmemocopy (form, env), SCM_UNSPECIFIED);
2392 SCM_SETCDR (z, unmemocar (copy, env));
2393 z = SCM_CDR (z);
2394 }
2395 else if (SCM_EQ_P (form, SCM_IM_ARROW))
2396 {
2397 SCM_SETCDR (z, scm_cons (scm_sym_arrow, SCM_UNSPECIFIED));
2398 z = SCM_CDR (z);
2399 }
2400 x = SCM_CDR (x);
2401 }
2402 SCM_SETCDR (z, x);
2403 if (!SCM_FALSEP (p))
2404 scm_whash_insert (scm_source_whash, ls, p);
2405 return ls;
2406 }
2407
2408
2409 #if (SCM_ENABLE_DEPRECATED == 1)
2410
2411 SCM
2412 scm_unmemocar (SCM form, SCM env)
2413 {
2414 return unmemocar (form, env);
2415 }
2416
2417 #endif
2418
2419 /*****************************************************************************/
2420 /*****************************************************************************/
2421 /* The definitions for execution start here. */
2422 /*****************************************************************************/
2423 /*****************************************************************************/
2424
2425 SCM_GLOBAL_SYMBOL (scm_sym_enter_frame, "enter-frame");
2426 SCM_GLOBAL_SYMBOL (scm_sym_apply_frame, "apply-frame");
2427 SCM_GLOBAL_SYMBOL (scm_sym_exit_frame, "exit-frame");
2428 SCM_GLOBAL_SYMBOL (scm_sym_trace, "trace");
2429
2430 /* A function object to implement "apply" for non-closure functions. */
2431 static SCM f_apply;
2432 /* An endless list consisting of #<undefined> objects: */
2433 static SCM undefineds;
2434
2435
2436 int
2437 scm_badargsp (SCM formals, SCM args)
2438 {
2439 while (!SCM_NULLP (formals))
2440 {
2441 if (!SCM_CONSP (formals))
2442 return 0;
2443 if (SCM_NULLP (args))
2444 return 1;
2445 formals = SCM_CDR (formals);
2446 args = SCM_CDR (args);
2447 }
2448 return !SCM_NULLP (args) ? 1 : 0;
2449 }
2450
2451 \f
2452
2453 /* The evaluator contains a plethora of EVAL symbols. This is an attempt at
2454 * explanation.
2455 *
2456 * The following macros should be used in code which is read twice (where the
2457 * choice of evaluator is hard soldered):
2458 *
2459 * CEVAL is the symbol used within one evaluator to call itself.
2460 * Originally, it is defined to ceval, but is redefined to deval during the
2461 * second pass.
2462 *
2463 * SCM_EVALIM is used when it is known that the expression is an
2464 * immediate. (This macro never calls an evaluator.)
2465 *
2466 * EVAL evaluates an expression that is expected to have its symbols already
2467 * memoized. Expressions that are not of the form '(<form> <form> ...)' are
2468 * evaluated inline without calling an evaluator.
2469 *
2470 * EVALCAR evaluates the car of an expression 'X:(Y:<form> <form> ...)',
2471 * potentially replacing a symbol at the position Y:<form> by its memoized
2472 * variable. If Y:<form> is not of the form '(<form> <form> ...)', the
2473 * evaluation is performed inline without calling an evaluator.
2474 *
2475 * The following macros should be used in code which is read once
2476 * (where the choice of evaluator is dynamic):
2477 *
2478 * SCM_XEVAL corresponds to EVAL, but uses ceval *or* deval depending on the
2479 * debugging mode.
2480 *
2481 * SCM_XEVALCAR corresponds to EVALCAR, but uses ceval *or* deval depending
2482 * on the debugging mode.
2483 *
2484 * The main motivation for keeping this plethora is efficiency
2485 * together with maintainability (=> locality of code).
2486 */
2487
2488 static SCM ceval (SCM x, SCM env);
2489 static SCM deval (SCM x, SCM env);
2490 #define CEVAL ceval
2491
2492
2493 #define SCM_EVALIM2(x) \
2494 ((SCM_EQ_P ((x), SCM_EOL) \
2495 ? syntax_error (s_empty_combination, (x), SCM_UNDEFINED), 0 \
2496 : 0), \
2497 (x))
2498
2499 #define SCM_EVALIM(x, env) (SCM_ILOCP (x) \
2500 ? *scm_ilookup ((x), (env)) \
2501 : SCM_EVALIM2(x))
2502
2503 #define SCM_XEVAL(x, env) \
2504 (SCM_IMP (x) \
2505 ? SCM_EVALIM2 (x) \
2506 : (SCM_VARIABLEP (x) \
2507 ? SCM_VARIABLE_REF (x) \
2508 : (SCM_CONSP (x) \
2509 ? (scm_debug_mode_p \
2510 ? deval ((x), (env)) \
2511 : ceval ((x), (env))) \
2512 : (x))))
2513
2514 #define SCM_XEVALCAR(x, env) \
2515 (SCM_IMP (SCM_CAR (x)) \
2516 ? SCM_EVALIM (SCM_CAR (x), (env)) \
2517 : (SCM_VARIABLEP (SCM_CAR (x)) \
2518 ? SCM_VARIABLE_REF (SCM_CAR (x)) \
2519 : (SCM_CONSP (SCM_CAR (x)) \
2520 ? (scm_debug_mode_p \
2521 ? deval (SCM_CAR (x), (env)) \
2522 : ceval (SCM_CAR (x), (env))) \
2523 : (!SCM_SYMBOLP (SCM_CAR (x)) \
2524 ? SCM_CAR (x) \
2525 : *scm_lookupcar ((x), (env), 1)))))
2526
2527 #define EVAL(x, env) \
2528 (SCM_IMP (x) \
2529 ? SCM_EVALIM ((x), (env)) \
2530 : (SCM_VARIABLEP (x) \
2531 ? SCM_VARIABLE_REF (x) \
2532 : (SCM_CONSP (x) \
2533 ? CEVAL ((x), (env)) \
2534 : (x))))
2535
2536 #define EVALCAR(x, env) \
2537 (SCM_IMP (SCM_CAR (x)) \
2538 ? SCM_EVALIM (SCM_CAR (x), (env)) \
2539 : (SCM_VARIABLEP (SCM_CAR (x)) \
2540 ? SCM_VARIABLE_REF (SCM_CAR (x)) \
2541 : (SCM_CONSP (SCM_CAR (x)) \
2542 ? CEVAL (SCM_CAR (x), (env)) \
2543 : (!SCM_SYMBOLP (SCM_CAR (x)) \
2544 ? SCM_CAR (x) \
2545 : *scm_lookupcar ((x), (env), 1)))))
2546
2547 SCM_REC_MUTEX (source_mutex);
2548
2549
2550 SCM
2551 scm_eval_car (SCM pair, SCM env)
2552 {
2553 return SCM_XEVALCAR (pair, env);
2554 }
2555
2556
2557 SCM
2558 scm_eval_args (SCM l, SCM env, SCM proc)
2559 {
2560 SCM results = SCM_EOL, *lloc = &results, res;
2561 while (SCM_CONSP (l))
2562 {
2563 res = EVALCAR (l, env);
2564
2565 *lloc = scm_list_1 (res);
2566 lloc = SCM_CDRLOC (*lloc);
2567 l = SCM_CDR (l);
2568 }
2569 if (!SCM_NULLP (l))
2570 scm_wrong_num_args (proc);
2571 return results;
2572 }
2573
2574
2575 SCM
2576 scm_eval_body (SCM code, SCM env)
2577 {
2578 SCM next;
2579
2580 again:
2581 next = SCM_CDR (code);
2582 while (!SCM_NULLP (next))
2583 {
2584 if (SCM_IMP (SCM_CAR (code)))
2585 {
2586 if (SCM_ISYMP (SCM_CAR (code)))
2587 {
2588 scm_rec_mutex_lock (&source_mutex);
2589 /* check for race condition */
2590 if (SCM_ISYMP (SCM_CAR (code)))
2591 m_expand_body (code, env);
2592 scm_rec_mutex_unlock (&source_mutex);
2593 goto again;
2594 }
2595 }
2596 else
2597 SCM_XEVAL (SCM_CAR (code), env);
2598 code = next;
2599 next = SCM_CDR (code);
2600 }
2601 return SCM_XEVALCAR (code, env);
2602 }
2603
2604 #endif /* !DEVAL */
2605
2606
2607 /* SECTION: This code is specific for the debugging support. One
2608 * branch is read when DEVAL isn't defined, the other when DEVAL is
2609 * defined.
2610 */
2611
2612 #ifndef DEVAL
2613
2614 #define SCM_APPLY scm_apply
2615 #define PREP_APPLY(proc, args)
2616 #define ENTER_APPLY
2617 #define RETURN(x) do { return x; } while (0)
2618 #ifdef STACK_CHECKING
2619 #ifndef NO_CEVAL_STACK_CHECKING
2620 #define EVAL_STACK_CHECKING
2621 #endif
2622 #endif
2623
2624 #else /* !DEVAL */
2625
2626 #undef CEVAL
2627 #define CEVAL deval /* Substitute all uses of ceval */
2628
2629 #undef SCM_APPLY
2630 #define SCM_APPLY scm_dapply
2631
2632 #undef PREP_APPLY
2633 #define PREP_APPLY(p, l) \
2634 { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
2635
2636 #undef ENTER_APPLY
2637 #define ENTER_APPLY \
2638 do { \
2639 SCM_SET_ARGSREADY (debug);\
2640 if (scm_check_apply_p && SCM_TRAPS_P)\
2641 if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && PROCTRACEP (proc)))\
2642 {\
2643 SCM tmp, tail = SCM_BOOL(SCM_TRACED_FRAME_P (debug)); \
2644 SCM_SET_TRACED_FRAME (debug); \
2645 SCM_TRAPS_P = 0;\
2646 if (SCM_CHEAPTRAPS_P)\
2647 {\
2648 tmp = scm_make_debugobj (&debug);\
2649 scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
2650 }\
2651 else\
2652 {\
2653 int first;\
2654 tmp = scm_make_continuation (&first);\
2655 if (first)\
2656 scm_call_3 (SCM_APPLY_FRAME_HDLR, scm_sym_apply_frame, tmp, tail);\
2657 }\
2658 SCM_TRAPS_P = 1;\
2659 }\
2660 } while (0)
2661
2662 #undef RETURN
2663 #define RETURN(e) do { proc = (e); goto exit; } while (0)
2664
2665 #ifdef STACK_CHECKING
2666 #ifndef EVAL_STACK_CHECKING
2667 #define EVAL_STACK_CHECKING
2668 #endif
2669 #endif
2670
2671
2672 /* scm_last_debug_frame contains a pointer to the last debugging information
2673 * stack frame. It is accessed very often from the debugging evaluator, so it
2674 * should probably not be indirectly addressed. Better to save and restore it
2675 * from the current root at any stack swaps.
2676 */
2677
2678 /* scm_debug_eframe_size is the number of slots available for pseudo
2679 * stack frames at each real stack frame.
2680 */
2681
2682 long scm_debug_eframe_size;
2683
2684 int scm_debug_mode_p;
2685 int scm_check_entry_p;
2686 int scm_check_apply_p;
2687 int scm_check_exit_p;
2688
2689 long scm_eval_stack;
2690
2691 scm_t_option scm_eval_opts[] = {
2692 { SCM_OPTION_INTEGER, "stack", 22000, "Size of thread stacks (in machine words)." }
2693 };
2694
2695 scm_t_option scm_debug_opts[] = {
2696 { SCM_OPTION_BOOLEAN, "cheap", 1,
2697 "*Flyweight representation of the stack at traps." },
2698 { SCM_OPTION_BOOLEAN, "breakpoints", 0, "*Check for breakpoints." },
2699 { SCM_OPTION_BOOLEAN, "trace", 0, "*Trace mode." },
2700 { SCM_OPTION_BOOLEAN, "procnames", 1,
2701 "Record procedure names at definition." },
2702 { SCM_OPTION_BOOLEAN, "backwards", 0,
2703 "Display backtrace in anti-chronological order." },
2704 { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
2705 { SCM_OPTION_INTEGER, "indent", 10, "Maximal indentation in backtrace." },
2706 { SCM_OPTION_INTEGER, "frames", 3,
2707 "Maximum number of tail-recursive frames in backtrace." },
2708 { SCM_OPTION_INTEGER, "maxdepth", 1000,
2709 "Maximal number of stored backtrace frames." },
2710 { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
2711 { SCM_OPTION_BOOLEAN, "backtrace", 0, "Show backtrace on error." },
2712 { SCM_OPTION_BOOLEAN, "debug", 0, "Use the debugging evaluator." },
2713 { SCM_OPTION_INTEGER, "stack", 20000, "Stack size limit (measured in words; 0 = no check)." },
2714 { 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."}
2715 };
2716
2717 scm_t_option scm_evaluator_trap_table[] = {
2718 { SCM_OPTION_BOOLEAN, "traps", 0, "Enable evaluator traps." },
2719 { SCM_OPTION_BOOLEAN, "enter-frame", 0, "Trap when eval enters new frame." },
2720 { SCM_OPTION_BOOLEAN, "apply-frame", 0, "Trap when entering apply." },
2721 { SCM_OPTION_BOOLEAN, "exit-frame", 0, "Trap when exiting eval or apply." },
2722 { SCM_OPTION_SCM, "enter-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for enter-frame traps." },
2723 { SCM_OPTION_SCM, "apply-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for apply-frame traps." },
2724 { SCM_OPTION_SCM, "exit-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for exit-frame traps." }
2725 };
2726
2727 SCM_DEFINE (scm_eval_options_interface, "eval-options-interface", 0, 1, 0,
2728 (SCM setting),
2729 "Option interface for the evaluation options. Instead of using\n"
2730 "this procedure directly, use the procedures @code{eval-enable},\n"
2731 "@code{eval-disable}, @code{eval-set!} and @code{eval-options}.")
2732 #define FUNC_NAME s_scm_eval_options_interface
2733 {
2734 SCM ans;
2735 SCM_DEFER_INTS;
2736 ans = scm_options (setting,
2737 scm_eval_opts,
2738 SCM_N_EVAL_OPTIONS,
2739 FUNC_NAME);
2740 scm_eval_stack = SCM_EVAL_STACK * sizeof (void *);
2741 SCM_ALLOW_INTS;
2742 return ans;
2743 }
2744 #undef FUNC_NAME
2745
2746
2747 SCM_DEFINE (scm_evaluator_traps, "evaluator-traps-interface", 0, 1, 0,
2748 (SCM setting),
2749 "Option interface for the evaluator trap options.")
2750 #define FUNC_NAME s_scm_evaluator_traps
2751 {
2752 SCM ans;
2753 SCM_DEFER_INTS;
2754 ans = scm_options (setting,
2755 scm_evaluator_trap_table,
2756 SCM_N_EVALUATOR_TRAPS,
2757 FUNC_NAME);
2758 SCM_RESET_DEBUG_MODE;
2759 SCM_ALLOW_INTS;
2760 return ans;
2761 }
2762 #undef FUNC_NAME
2763
2764
2765 static SCM
2766 deval_args (SCM l, SCM env, SCM proc, SCM *lloc)
2767 {
2768 SCM *results = lloc, res;
2769 while (SCM_CONSP (l))
2770 {
2771 res = EVALCAR (l, env);
2772
2773 *lloc = scm_list_1 (res);
2774 lloc = SCM_CDRLOC (*lloc);
2775 l = SCM_CDR (l);
2776 }
2777 if (!SCM_NULLP (l))
2778 scm_wrong_num_args (proc);
2779 return *results;
2780 }
2781
2782 #endif /* !DEVAL */
2783
2784
2785 /* SECTION: This code is compiled twice.
2786 */
2787
2788
2789 /* Update the toplevel environment frame ENV so that it refers to the
2790 * current module. */
2791 #define UPDATE_TOPLEVEL_ENV(env) \
2792 do { \
2793 SCM p = scm_current_module_lookup_closure (); \
2794 if (p != SCM_CAR (env)) \
2795 env = scm_top_level_env (p); \
2796 } while (0)
2797
2798
2799 #define SCM_VALIDATE_NON_EMPTY_COMBINATION(x) \
2800 ASSERT_SYNTAX (!SCM_EQ_P ((x), SCM_EOL), s_empty_combination, x)
2801
2802
2803 /* This is the evaluator. Like any real monster, it has three heads:
2804 *
2805 * ceval is the non-debugging evaluator, deval is the debugging version. Both
2806 * are implemented using a common code base, using the following mechanism:
2807 * CEVAL is a macro, which is either defined to ceval or deval. Thus, there
2808 * is no function CEVAL, but the code for CEVAL actually compiles to either
2809 * ceval or deval. When CEVAL is defined to ceval, it is known that the macro
2810 * DEVAL is not defined. When CEVAL is defined to deval, then the macro DEVAL
2811 * is known to be defined. Thus, in CEVAL parts for the debugging evaluator
2812 * are enclosed within #ifdef DEVAL ... #endif.
2813 *
2814 * All three (ceval, deval and their common implementation CEVAL) take two
2815 * input parameters, x and env: x is a single expression to be evalutated.
2816 * env is the environment in which bindings are searched.
2817 *
2818 * x is known to be a pair. Since x is a single expression, it is necessarily
2819 * in a tail position. If x is just a call to another function like in the
2820 * expression (foo exp1 exp2 ...), the realization of that call therefore
2821 * _must_not_ increase stack usage (the evaluation of exp1, exp2 etc.,
2822 * however, may do so). This is realized by making extensive use of 'goto'
2823 * statements within the evaluator: The gotos replace recursive calls to
2824 * CEVAL, thus re-using the same stack frame that CEVAL was already using.
2825 * If, however, x represents some form that requires to evaluate a sequence of
2826 * expressions like (begin exp1 exp2 ...), then recursive calls to CEVAL are
2827 * performed for all but the last expression of that sequence. */
2828
2829 static SCM
2830 CEVAL (SCM x, SCM env)
2831 {
2832 SCM proc, arg1;
2833 #ifdef DEVAL
2834 scm_t_debug_frame debug;
2835 scm_t_debug_info *debug_info_end;
2836 debug.prev = scm_last_debug_frame;
2837 debug.status = 0;
2838 /*
2839 * The debug.vect contains twice as much scm_t_debug_info frames as the
2840 * user has specified with (debug-set! frames <n>).
2841 *
2842 * Even frames are eval frames, odd frames are apply frames.
2843 */
2844 debug.vect = (scm_t_debug_info *) alloca (scm_debug_eframe_size
2845 * sizeof (scm_t_debug_info));
2846 debug.info = debug.vect;
2847 debug_info_end = debug.vect + scm_debug_eframe_size;
2848 scm_last_debug_frame = &debug;
2849 #endif
2850 #ifdef EVAL_STACK_CHECKING
2851 if (scm_stack_checking_enabled_p && SCM_STACK_OVERFLOW_P (&proc))
2852 {
2853 #ifdef DEVAL
2854 debug.info->e.exp = x;
2855 debug.info->e.env = env;
2856 #endif
2857 scm_report_stack_overflow ();
2858 }
2859 #endif
2860
2861 #ifdef DEVAL
2862 goto start;
2863 #endif
2864
2865 loop:
2866 #ifdef DEVAL
2867 SCM_CLEAR_ARGSREADY (debug);
2868 if (SCM_OVERFLOWP (debug))
2869 --debug.info;
2870 /*
2871 * In theory, this should be the only place where it is necessary to
2872 * check for space in debug.vect since both eval frames and
2873 * available space are even.
2874 *
2875 * For this to be the case, however, it is necessary that primitive
2876 * special forms which jump back to `loop', `begin' or some similar
2877 * label call PREP_APPLY.
2878 */
2879 else if (++debug.info >= debug_info_end)
2880 {
2881 SCM_SET_OVERFLOW (debug);
2882 debug.info -= 2;
2883 }
2884
2885 start:
2886 debug.info->e.exp = x;
2887 debug.info->e.env = env;
2888 if (scm_check_entry_p && SCM_TRAPS_P)
2889 {
2890 if (SCM_ENTER_FRAME_P
2891 || (SCM_BREAKPOINTS_P && scm_c_source_property_breakpoint_p (x)))
2892 {
2893 SCM stackrep;
2894 SCM tail = SCM_BOOL (SCM_TAILRECP (debug));
2895 SCM_SET_TAILREC (debug);
2896 if (SCM_CHEAPTRAPS_P)
2897 stackrep = scm_make_debugobj (&debug);
2898 else
2899 {
2900 int first;
2901 SCM val = scm_make_continuation (&first);
2902
2903 if (first)
2904 stackrep = val;
2905 else
2906 {
2907 x = val;
2908 if (SCM_IMP (x))
2909 RETURN (x);
2910 else
2911 /* This gives the possibility for the debugger to
2912 modify the source expression before evaluation. */
2913 goto dispatch;
2914 }
2915 }
2916 SCM_TRAPS_P = 0;
2917 scm_call_4 (SCM_ENTER_FRAME_HDLR,
2918 scm_sym_enter_frame,
2919 stackrep,
2920 tail,
2921 scm_unmemocopy (x, env));
2922 SCM_TRAPS_P = 1;
2923 }
2924 }
2925 #endif
2926 dispatch:
2927 SCM_TICK;
2928 if (SCM_ISYMP (SCM_CAR (x)))
2929 {
2930 switch (SCM_ISYMNUM (SCM_CAR (x)))
2931 {
2932 case (SCM_ISYMNUM (SCM_IM_AND)):
2933 x = SCM_CDR (x);
2934 while (!SCM_NULLP (SCM_CDR (x)))
2935 {
2936 SCM test_result = EVALCAR (x, env);
2937 if (SCM_FALSEP (test_result) || SCM_NILP (test_result))
2938 RETURN (SCM_BOOL_F);
2939 else
2940 x = SCM_CDR (x);
2941 }
2942 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2943 goto carloop;
2944
2945 case (SCM_ISYMNUM (SCM_IM_BEGIN)):
2946 x = SCM_CDR (x);
2947 if (SCM_NULLP (x))
2948 RETURN (SCM_UNSPECIFIED);
2949
2950 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
2951
2952 begin:
2953 /* If we are on toplevel with a lookup closure, we need to sync
2954 with the current module. */
2955 if (SCM_CONSP (env) && !SCM_CONSP (SCM_CAR (env)))
2956 {
2957 UPDATE_TOPLEVEL_ENV (env);
2958 while (!SCM_NULLP (SCM_CDR (x)))
2959 {
2960 EVALCAR (x, env);
2961 UPDATE_TOPLEVEL_ENV (env);
2962 x = SCM_CDR (x);
2963 }
2964 goto carloop;
2965 }
2966 else
2967 goto nontoplevel_begin;
2968
2969 nontoplevel_begin:
2970 while (!SCM_NULLP (SCM_CDR (x)))
2971 {
2972 SCM form = SCM_CAR (x);
2973 if (SCM_IMP (form))
2974 {
2975 if (SCM_ISYMP (form))
2976 {
2977 scm_rec_mutex_lock (&source_mutex);
2978 /* check for race condition */
2979 if (SCM_ISYMP (SCM_CAR (x)))
2980 m_expand_body (x, env);
2981 scm_rec_mutex_unlock (&source_mutex);
2982 goto nontoplevel_begin;
2983 }
2984 else
2985 SCM_VALIDATE_NON_EMPTY_COMBINATION (form);
2986 }
2987 else
2988 EVAL (form, env);
2989 x = SCM_CDR (x);
2990 }
2991
2992 carloop:
2993 {
2994 /* scm_eval last form in list */
2995 SCM last_form = SCM_CAR (x);
2996
2997 if (SCM_CONSP (last_form))
2998 {
2999 /* This is by far the most frequent case. */
3000 x = last_form;
3001 goto loop; /* tail recurse */
3002 }
3003 else if (SCM_IMP (last_form))
3004 RETURN (SCM_EVALIM (last_form, env));
3005 else if (SCM_VARIABLEP (last_form))
3006 RETURN (SCM_VARIABLE_REF (last_form));
3007 else if (SCM_SYMBOLP (last_form))
3008 RETURN (*scm_lookupcar (x, env, 1));
3009 else
3010 RETURN (last_form);
3011 }
3012
3013
3014 case (SCM_ISYMNUM (SCM_IM_CASE)):
3015 x = SCM_CDR (x);
3016 {
3017 SCM key = EVALCAR (x, env);
3018 x = SCM_CDR (x);
3019 while (!SCM_NULLP (x))
3020 {
3021 SCM clause = SCM_CAR (x);
3022 SCM labels = SCM_CAR (clause);
3023 if (SCM_EQ_P (labels, SCM_IM_ELSE))
3024 {
3025 x = SCM_CDR (clause);
3026 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3027 goto begin;
3028 }
3029 while (!SCM_NULLP (labels))
3030 {
3031 SCM label = SCM_CAR (labels);
3032 if (SCM_EQ_P (label, key)
3033 || !SCM_FALSEP (scm_eqv_p (label, key)))
3034 {
3035 x = SCM_CDR (clause);
3036 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3037 goto begin;
3038 }
3039 labels = SCM_CDR (labels);
3040 }
3041 x = SCM_CDR (x);
3042 }
3043 }
3044 RETURN (SCM_UNSPECIFIED);
3045
3046
3047 case (SCM_ISYMNUM (SCM_IM_COND)):
3048 x = SCM_CDR (x);
3049 while (!SCM_NULLP (x))
3050 {
3051 SCM clause = SCM_CAR (x);
3052 if (SCM_EQ_P (SCM_CAR (clause), SCM_IM_ELSE))
3053 {
3054 x = SCM_CDR (clause);
3055 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3056 goto begin;
3057 }
3058 else
3059 {
3060 arg1 = EVALCAR (clause, env);
3061 if (!SCM_FALSEP (arg1) && !SCM_NILP (arg1))
3062 {
3063 x = SCM_CDR (clause);
3064 if (SCM_NULLP (x))
3065 RETURN (arg1);
3066 else if (!SCM_EQ_P (SCM_CAR (x), SCM_IM_ARROW))
3067 {
3068 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3069 goto begin;
3070 }
3071 else
3072 {
3073 proc = SCM_CDR (x);
3074 proc = EVALCAR (proc, env);
3075 PREP_APPLY (proc, scm_list_1 (arg1));
3076 ENTER_APPLY;
3077 goto evap1;
3078 }
3079 }
3080 x = SCM_CDR (x);
3081 }
3082 }
3083 RETURN (SCM_UNSPECIFIED);
3084
3085
3086 case (SCM_ISYMNUM (SCM_IM_DO)):
3087 x = SCM_CDR (x);
3088 {
3089 /* Compute the initialization values and the initial environment. */
3090 SCM init_forms = SCM_CAR (x);
3091 SCM init_values = SCM_EOL;
3092 while (!SCM_NULLP (init_forms))
3093 {
3094 init_values = scm_cons (EVALCAR (init_forms, env), init_values);
3095 init_forms = SCM_CDR (init_forms);
3096 }
3097 x = SCM_CDR (x);
3098 env = SCM_EXTEND_ENV (SCM_CAR (x), init_values, env);
3099 }
3100 x = SCM_CDR (x);
3101 {
3102 SCM test_form = SCM_CAR (x);
3103 SCM body_forms = SCM_CADR (x);
3104 SCM step_forms = SCM_CDDR (x);
3105
3106 SCM test_result = EVALCAR (test_form, env);
3107
3108 while (SCM_FALSEP (test_result) || SCM_NILP (test_result))
3109 {
3110 {
3111 /* Evaluate body forms. */
3112 SCM temp_forms;
3113 for (temp_forms = body_forms;
3114 !SCM_NULLP (temp_forms);
3115 temp_forms = SCM_CDR (temp_forms))
3116 {
3117 SCM form = SCM_CAR (temp_forms);
3118 /* Dirk:FIXME: We only need to eval forms, that may have a
3119 * side effect here. This is only true for forms that start
3120 * with a pair. All others are just constants. However,
3121 * since in the common case there is no constant expression
3122 * in a body of a do form, we just check for immediates here
3123 * and have CEVAL take care of other cases. In the long run
3124 * it would make sense to get rid of this test and have the
3125 * macro transformer of 'do' eliminate all forms that have
3126 * no sideeffect. */
3127 EVAL (form, env);
3128 }
3129 }
3130
3131 {
3132 /* Evaluate the step expressions. */
3133 SCM temp_forms;
3134 SCM step_values = SCM_EOL;
3135 for (temp_forms = step_forms;
3136 !SCM_NULLP (temp_forms);
3137 temp_forms = SCM_CDR (temp_forms))
3138 {
3139 SCM value = EVALCAR (temp_forms, env);
3140 step_values = scm_cons (value, step_values);
3141 }
3142 env = SCM_EXTEND_ENV (SCM_CAAR (env),
3143 step_values,
3144 SCM_CDR (env));
3145 }
3146
3147 test_result = EVALCAR (test_form, env);
3148 }
3149 }
3150 x = SCM_CDAR (x);
3151 if (SCM_NULLP (x))
3152 RETURN (SCM_UNSPECIFIED);
3153 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3154 goto nontoplevel_begin;
3155
3156
3157 case (SCM_ISYMNUM (SCM_IM_IF)):
3158 x = SCM_CDR (x);
3159 {
3160 SCM test_result = EVALCAR (x, env);
3161 x = SCM_CDR (x); /* then expression */
3162 if (SCM_FALSEP (test_result) || SCM_NILP (test_result))
3163 {
3164 x = SCM_CDR (x); /* else expression */
3165 if (SCM_NULLP (x))
3166 RETURN (SCM_UNSPECIFIED);
3167 }
3168 }
3169 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3170 goto carloop;
3171
3172
3173 case (SCM_ISYMNUM (SCM_IM_LET)):
3174 x = SCM_CDR (x);
3175 {
3176 SCM init_forms = SCM_CADR (x);
3177 SCM init_values = SCM_EOL;
3178 do
3179 {
3180 init_values = scm_cons (EVALCAR (init_forms, env), init_values);
3181 init_forms = SCM_CDR (init_forms);
3182 }
3183 while (!SCM_NULLP (init_forms));
3184 env = SCM_EXTEND_ENV (SCM_CAR (x), init_values, env);
3185 }
3186 x = SCM_CDDR (x);
3187 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3188 goto nontoplevel_begin;
3189
3190
3191 case (SCM_ISYMNUM (SCM_IM_LETREC)):
3192 x = SCM_CDR (x);
3193 env = SCM_EXTEND_ENV (SCM_CAR (x), undefineds, env);
3194 x = SCM_CDR (x);
3195 {
3196 SCM init_forms = SCM_CAR (x);
3197 SCM init_values = SCM_EOL;
3198 do
3199 {
3200 init_values = scm_cons (EVALCAR (init_forms, env), init_values);
3201 init_forms = SCM_CDR (init_forms);
3202 }
3203 while (!SCM_NULLP (init_forms));
3204 SCM_SETCDR (SCM_CAR (env), init_values);
3205 }
3206 x = SCM_CDR (x);
3207 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3208 goto nontoplevel_begin;
3209
3210
3211 case (SCM_ISYMNUM (SCM_IM_LETSTAR)):
3212 x = SCM_CDR (x);
3213 {
3214 SCM bindings = SCM_CAR (x);
3215 if (SCM_NULLP (bindings))
3216 env = SCM_EXTEND_ENV (SCM_EOL, SCM_EOL, env);
3217 else
3218 {
3219 do
3220 {
3221 SCM name = SCM_CAR (bindings);
3222 SCM init = SCM_CDR (bindings);
3223 env = SCM_EXTEND_ENV (name, EVALCAR (init, env), env);
3224 bindings = SCM_CDR (init);
3225 }
3226 while (!SCM_NULLP (bindings));
3227 }
3228 }
3229 x = SCM_CDR (x);
3230 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3231 goto nontoplevel_begin;
3232
3233
3234 case (SCM_ISYMNUM (SCM_IM_OR)):
3235 x = SCM_CDR (x);
3236 while (!SCM_NULLP (SCM_CDR (x)))
3237 {
3238 SCM val = EVALCAR (x, env);
3239 if (!SCM_FALSEP (val) && !SCM_NILP (val))
3240 RETURN (val);
3241 else
3242 x = SCM_CDR (x);
3243 }
3244 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3245 goto carloop;
3246
3247
3248 case (SCM_ISYMNUM (SCM_IM_LAMBDA)):
3249 RETURN (scm_closure (SCM_CDR (x), env));
3250
3251
3252 case (SCM_ISYMNUM (SCM_IM_QUOTE)):
3253 RETURN (SCM_CADR (x));
3254
3255
3256 case (SCM_ISYMNUM (SCM_IM_SET_X)):
3257 x = SCM_CDR (x);
3258 {
3259 SCM *location;
3260 SCM variable = SCM_CAR (x);
3261 if (SCM_ILOCP (variable))
3262 location = scm_ilookup (variable, env);
3263 else if (SCM_VARIABLEP (variable))
3264 location = SCM_VARIABLE_LOC (variable);
3265 else /* (SCM_SYMBOLP (variable)) is known to be true */
3266 location = scm_lookupcar (x, env, 1);
3267 x = SCM_CDR (x);
3268 *location = EVALCAR (x, env);
3269 }
3270 RETURN (SCM_UNSPECIFIED);
3271
3272
3273 case (SCM_ISYMNUM (SCM_IM_APPLY)):
3274 /* Evaluate the procedure to be applied. */
3275 x = SCM_CDR (x);
3276 proc = EVALCAR (x, env);
3277 PREP_APPLY (proc, SCM_EOL);
3278
3279 /* Evaluate the argument holding the list of arguments */
3280 x = SCM_CDR (x);
3281 arg1 = EVALCAR (x, env);
3282
3283 apply_proc:
3284 /* Go here to tail-apply a procedure. PROC is the procedure and
3285 * ARG1 is the list of arguments. PREP_APPLY must have been called
3286 * before jumping to apply_proc. */
3287 if (SCM_CLOSUREP (proc))
3288 {
3289 SCM formals = SCM_CLOSURE_FORMALS (proc);
3290 #ifdef DEVAL
3291 debug.info->a.args = arg1;
3292 #endif
3293 if (scm_badargsp (formals, arg1))
3294 scm_wrong_num_args (proc);
3295 ENTER_APPLY;
3296 /* Copy argument list */
3297 if (SCM_NULL_OR_NIL_P (arg1))
3298 env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
3299 else
3300 {
3301 SCM args = scm_list_1 (SCM_CAR (arg1));
3302 SCM tail = args;
3303 arg1 = SCM_CDR (arg1);
3304 while (!SCM_NULL_OR_NIL_P (arg1))
3305 {
3306 SCM new_tail = scm_list_1 (SCM_CAR (arg1));
3307 SCM_SETCDR (tail, new_tail);
3308 tail = new_tail;
3309 arg1 = SCM_CDR (arg1);
3310 }
3311 env = SCM_EXTEND_ENV (formals, args, SCM_ENV (proc));
3312 }
3313
3314 x = SCM_CLOSURE_BODY (proc);
3315 goto nontoplevel_begin;
3316 }
3317 else
3318 {
3319 ENTER_APPLY;
3320 RETURN (SCM_APPLY (proc, arg1, SCM_EOL));
3321 }
3322
3323
3324 case (SCM_ISYMNUM (SCM_IM_CONT)):
3325 {
3326 int first;
3327 SCM val = scm_make_continuation (&first);
3328
3329 if (!first)
3330 RETURN (val);
3331 else
3332 {
3333 arg1 = val;
3334 proc = SCM_CDR (x);
3335 proc = EVALCAR (proc, env);
3336 PREP_APPLY (proc, scm_list_1 (arg1));
3337 ENTER_APPLY;
3338 goto evap1;
3339 }
3340 }
3341
3342
3343 case (SCM_ISYMNUM (SCM_IM_DELAY)):
3344 RETURN (scm_makprom (scm_closure (SCM_CDR (x), env)));
3345
3346
3347 case (SCM_ISYMNUM (SCM_IM_FUTURE)):
3348 RETURN (scm_i_make_future (scm_closure (SCM_CDR (x), env)));
3349
3350
3351 /* PLACEHOLDER for case (SCM_ISYMNUM (SCM_IM_DISPATCH)): The
3352 following code (type_dispatch) is intended to be the tail
3353 of the case clause for the internal macro
3354 SCM_IM_DISPATCH. Please don't remove it from this
3355 location without discussing it with Mikael
3356 <djurfeldt@nada.kth.se> */
3357
3358 /* The type dispatch code is duplicated below
3359 * (c.f. objects.c:scm_mcache_compute_cmethod) since that
3360 * cuts down execution time for type dispatch to 50%. */
3361 type_dispatch: /* inputs: x, arg1 */
3362 /* Type dispatch means to determine from the types of the function
3363 * arguments (i. e. the 'signature' of the call), which method from
3364 * a generic function is to be called. This process of selecting
3365 * the right method takes some time. To speed it up, guile uses
3366 * caching: Together with the macro call to dispatch the signatures
3367 * of some previous calls to that generic function from the same
3368 * place are stored (in the code!) in a cache that we call the
3369 * 'method cache'. This is done since it is likely, that
3370 * consecutive calls to dispatch from that position in the code will
3371 * have the same signature. Thus, the type dispatch works as
3372 * follows: First, determine a hash value from the signature of the
3373 * actual arguments. Second, use this hash value as an index to
3374 * find that same signature in the method cache stored at this
3375 * position in the code. If found, you have also found the
3376 * corresponding method that belongs to that signature. If the
3377 * signature is not found in the method cache, you have to perform a
3378 * full search over all signatures stored with the generic
3379 * function. */
3380 {
3381 unsigned long int specializers;
3382 unsigned long int hash_value;
3383 unsigned long int cache_end_pos;
3384 unsigned long int mask;
3385 SCM method_cache;
3386
3387 {
3388 SCM z = SCM_CDDR (x);
3389 SCM tmp = SCM_CADR (z);
3390 specializers = SCM_INUM (SCM_CAR (z));
3391
3392 /* Compute a hash value for searching the method cache. There
3393 * are two variants for computing the hash value, a (rather)
3394 * complicated one, and a simple one. For the complicated one
3395 * explained below, tmp holds a number that is used in the
3396 * computation. */
3397 if (SCM_INUMP (tmp))
3398 {
3399 /* Use the signature of the actual arguments to determine
3400 * the hash value. This is done as follows: Each class has
3401 * an array of random numbers, that are determined when the
3402 * class is created. The integer 'hashset' is an index into
3403 * that array of random numbers. Now, from all classes that
3404 * are part of the signature of the actual arguments, the
3405 * random numbers at index 'hashset' are taken and summed
3406 * up, giving the hash value. The value of 'hashset' is
3407 * stored at the call to dispatch. This allows to have
3408 * different 'formulas' for calculating the hash value at
3409 * different places where dispatch is called. This allows
3410 * to optimize the hash formula at every individual place
3411 * where dispatch is called, such that hopefully the hash
3412 * value that is computed will directly point to the right
3413 * method in the method cache. */
3414 unsigned long int hashset = SCM_INUM (tmp);
3415 unsigned long int counter = specializers + 1;
3416 SCM tmp_arg = arg1;
3417 hash_value = 0;
3418 while (!SCM_NULLP (tmp_arg) && counter != 0)
3419 {
3420 SCM class = scm_class_of (SCM_CAR (tmp_arg));
3421 hash_value += SCM_INSTANCE_HASH (class, hashset);
3422 tmp_arg = SCM_CDR (tmp_arg);
3423 counter--;
3424 }
3425 z = SCM_CDDR (z);
3426 method_cache = SCM_CADR (z);
3427 mask = SCM_INUM (SCM_CAR (z));
3428 hash_value &= mask;
3429 cache_end_pos = hash_value;
3430 }
3431 else
3432 {
3433 /* This method of determining the hash value is much
3434 * simpler: Set the hash value to zero and just perform a
3435 * linear search through the method cache. */
3436 method_cache = tmp;
3437 mask = (unsigned long int) ((long) -1);
3438 hash_value = 0;
3439 cache_end_pos = SCM_VECTOR_LENGTH (method_cache);
3440 }
3441 }
3442
3443 {
3444 /* Search the method cache for a method with a matching
3445 * signature. Start the search at position 'hash_value'. The
3446 * hashing implementation uses linear probing for conflict
3447 * resolution, that is, if the signature in question is not
3448 * found at the starting index in the hash table, the next table
3449 * entry is tried, and so on, until in the worst case the whole
3450 * cache has been searched, but still the signature has not been
3451 * found. */
3452 SCM z;
3453 do
3454 {
3455 SCM args = arg1; /* list of arguments */
3456 z = SCM_VELTS (method_cache)[hash_value];
3457 while (!SCM_NULLP (args))
3458 {
3459 /* More arguments than specifiers => CLASS != ENV */
3460 SCM class_of_arg = scm_class_of (SCM_CAR (args));
3461 if (!SCM_EQ_P (class_of_arg, SCM_CAR (z)))
3462 goto next_method;
3463 args = SCM_CDR (args);
3464 z = SCM_CDR (z);
3465 }
3466 /* Fewer arguments than specifiers => CAR != ENV */
3467 if (SCM_NULLP (SCM_CAR (z)) || SCM_CONSP (SCM_CAR (z)))
3468 goto apply_cmethod;
3469 next_method:
3470 hash_value = (hash_value + 1) & mask;
3471 } while (hash_value != cache_end_pos);
3472
3473 /* No appropriate method was found in the cache. */
3474 z = scm_memoize_method (x, arg1);
3475
3476 apply_cmethod: /* inputs: z, arg1 */
3477 {
3478 SCM formals = SCM_CMETHOD_FORMALS (z);
3479 env = SCM_EXTEND_ENV (formals, arg1, SCM_CMETHOD_ENV (z));
3480 x = SCM_CMETHOD_BODY (z);
3481 goto nontoplevel_begin;
3482 }
3483 }
3484 }
3485
3486
3487 case (SCM_ISYMNUM (SCM_IM_SLOT_REF)):
3488 x = SCM_CDR (x);
3489 {
3490 SCM instance = EVALCAR (x, env);
3491 unsigned long int slot = SCM_INUM (SCM_CADR (x));
3492 RETURN (SCM_PACK (SCM_STRUCT_DATA (instance) [slot]));
3493 }
3494
3495
3496 case (SCM_ISYMNUM (SCM_IM_SLOT_SET_X)):
3497 x = SCM_CDR (x);
3498 {
3499 SCM instance = EVALCAR (x, env);
3500 unsigned long int slot = SCM_INUM (SCM_CADR (x));
3501 SCM value = EVALCAR (SCM_CDDR (x), env);
3502 SCM_STRUCT_DATA (instance) [slot] = SCM_UNPACK (value);
3503 RETURN (SCM_UNSPECIFIED);
3504 }
3505
3506
3507 #if SCM_ENABLE_ELISP
3508
3509 case (SCM_ISYMNUM (SCM_IM_NIL_COND)):
3510 {
3511 SCM test_form = SCM_CDR (x);
3512 x = SCM_CDR (test_form);
3513 while (!SCM_NULL_OR_NIL_P (x))
3514 {
3515 SCM test_result = EVALCAR (test_form, env);
3516 if (!(SCM_FALSEP (test_result)
3517 || SCM_NULL_OR_NIL_P (test_result)))
3518 {
3519 if (SCM_EQ_P (SCM_CAR (x), SCM_UNSPECIFIED))
3520 RETURN (test_result);
3521 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3522 goto carloop;
3523 }
3524 else
3525 {
3526 test_form = SCM_CDR (x);
3527 x = SCM_CDR (test_form);
3528 }
3529 }
3530 x = test_form;
3531 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3532 goto carloop;
3533 }
3534
3535 #endif /* SCM_ENABLE_ELISP */
3536
3537 case (SCM_ISYMNUM (SCM_IM_BIND)):
3538 {
3539 SCM vars, exps, vals;
3540
3541 x = SCM_CDR (x);
3542 vars = SCM_CAAR (x);
3543 exps = SCM_CDAR (x);
3544 vals = SCM_EOL;
3545 while (!SCM_NULLP (exps))
3546 {
3547 vals = scm_cons (EVALCAR (exps, env), vals);
3548 exps = SCM_CDR (exps);
3549 }
3550
3551 scm_swap_bindings (vars, vals);
3552 scm_dynwinds = scm_acons (vars, vals, scm_dynwinds);
3553
3554 /* Ignore all but the last evaluation result. */
3555 for (x = SCM_CDR (x); !SCM_NULLP (SCM_CDR (x)); x = SCM_CDR (x))
3556 {
3557 if (SCM_CONSP (SCM_CAR (x)))
3558 CEVAL (SCM_CAR (x), env);
3559 }
3560 proc = EVALCAR (x, env);
3561
3562 scm_dynwinds = SCM_CDR (scm_dynwinds);
3563 scm_swap_bindings (vars, vals);
3564
3565 RETURN (proc);
3566 }
3567
3568
3569 case (SCM_ISYMNUM (SCM_IM_CALL_WITH_VALUES)):
3570 {
3571 SCM producer;
3572
3573 x = SCM_CDR (x);
3574 producer = EVALCAR (x, env);
3575 x = SCM_CDR (x);
3576 proc = EVALCAR (x, env); /* proc is the consumer. */
3577 arg1 = SCM_APPLY (producer, SCM_EOL, SCM_EOL);
3578 if (SCM_VALUESP (arg1))
3579 {
3580 /* The list of arguments is not copied. Rather, it is assumed
3581 * that this has been done by the 'values' procedure. */
3582 arg1 = scm_struct_ref (arg1, SCM_INUM0);
3583 }
3584 else
3585 {
3586 arg1 = scm_list_1 (arg1);
3587 }
3588 PREP_APPLY (proc, arg1);
3589 goto apply_proc;
3590 }
3591
3592
3593 default:
3594 break;
3595 }
3596 }
3597 else
3598 {
3599 if (SCM_VARIABLEP (SCM_CAR (x)))
3600 proc = SCM_VARIABLE_REF (SCM_CAR (x));
3601 else if (SCM_ILOCP (SCM_CAR (x)))
3602 proc = *scm_ilookup (SCM_CAR (x), env);
3603 else if (SCM_CONSP (SCM_CAR (x)))
3604 proc = CEVAL (SCM_CAR (x), env);
3605 else if (SCM_SYMBOLP (SCM_CAR (x)))
3606 {
3607 SCM orig_sym = SCM_CAR (x);
3608 {
3609 SCM *location = scm_lookupcar1 (x, env, 1);
3610 if (location == NULL)
3611 {
3612 /* we have lost the race, start again. */
3613 goto dispatch;
3614 }
3615 proc = *location;
3616 }
3617
3618 if (SCM_MACROP (proc))
3619 {
3620 SCM_SETCAR (x, orig_sym); /* Undo memoizing effect of
3621 lookupcar */
3622 handle_a_macro: /* inputs: x, env, proc */
3623 #ifdef DEVAL
3624 /* Set a flag during macro expansion so that macro
3625 application frames can be deleted from the backtrace. */
3626 SCM_SET_MACROEXP (debug);
3627 #endif
3628 arg1 = SCM_APPLY (SCM_MACRO_CODE (proc), x,
3629 scm_cons (env, scm_listofnull));
3630 #ifdef DEVAL
3631 SCM_CLEAR_MACROEXP (debug);
3632 #endif
3633 switch (SCM_MACRO_TYPE (proc))
3634 {
3635 case 3:
3636 case 2:
3637 if (scm_ilength (arg1) <= 0)
3638 arg1 = scm_list_2 (SCM_IM_BEGIN, arg1);
3639 #ifdef DEVAL
3640 if (!SCM_CLOSUREP (SCM_MACRO_CODE (proc)))
3641 {
3642 SCM_DEFER_INTS;
3643 SCM_SETCAR (x, SCM_CAR (arg1));
3644 SCM_SETCDR (x, SCM_CDR (arg1));
3645 SCM_ALLOW_INTS;
3646 goto dispatch;
3647 }
3648 /* Prevent memoizing of debug info expression. */
3649 debug.info->e.exp = scm_cons_source (debug.info->e.exp,
3650 SCM_CAR (x),
3651 SCM_CDR (x));
3652 #endif
3653 SCM_DEFER_INTS;
3654 SCM_SETCAR (x, SCM_CAR (arg1));
3655 SCM_SETCDR (x, SCM_CDR (arg1));
3656 SCM_ALLOW_INTS;
3657 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3658 goto loop;
3659 #if SCM_ENABLE_DEPRECATED == 1
3660 case 1:
3661 x = arg1;
3662 if (SCM_NIMP (x))
3663 {
3664 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
3665 goto loop;
3666 }
3667 else
3668 RETURN (arg1);
3669 #endif
3670 case 0:
3671 RETURN (arg1);
3672 }
3673 }
3674 }
3675 else
3676 proc = SCM_CAR (x);
3677
3678 if (SCM_MACROP (proc))
3679 goto handle_a_macro;
3680 }
3681
3682
3683 /* When reaching this part of the code, the following is granted: Variable x
3684 * holds the first pair of an expression of the form (<function> arg ...).
3685 * Variable proc holds the object that resulted from the evaluation of
3686 * <function>. In the following, the arguments (if any) will be evaluated,
3687 * and proc will be applied to them. If proc does not really hold a
3688 * function object, this will be signalled as an error on the scheme
3689 * level. If the number of arguments does not match the number of arguments
3690 * that are allowed to be passed to proc, also an error on the scheme level
3691 * will be signalled. */
3692 PREP_APPLY (proc, SCM_EOL);
3693 if (SCM_NULLP (SCM_CDR (x))) {
3694 ENTER_APPLY;
3695 evap0:
3696 SCM_ASRTGO (!SCM_IMP (proc), badfun);
3697 switch (SCM_TYP7 (proc))
3698 { /* no arguments given */
3699 case scm_tc7_subr_0:
3700 RETURN (SCM_SUBRF (proc) ());
3701 case scm_tc7_subr_1o:
3702 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
3703 case scm_tc7_lsubr:
3704 RETURN (SCM_SUBRF (proc) (SCM_EOL));
3705 case scm_tc7_rpsubr:
3706 RETURN (SCM_BOOL_T);
3707 case scm_tc7_asubr:
3708 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
3709 case scm_tc7_smob:
3710 if (!SCM_SMOB_APPLICABLE_P (proc))
3711 goto badfun;
3712 RETURN (SCM_SMOB_APPLY_0 (proc));
3713 case scm_tc7_cclo:
3714 arg1 = proc;
3715 proc = SCM_CCLO_SUBR (proc);
3716 #ifdef DEVAL
3717 debug.info->a.proc = proc;
3718 debug.info->a.args = scm_list_1 (arg1);
3719 #endif
3720 goto evap1;
3721 case scm_tc7_pws:
3722 proc = SCM_PROCEDURE (proc);
3723 #ifdef DEVAL
3724 debug.info->a.proc = proc;
3725 #endif
3726 if (!SCM_CLOSUREP (proc))
3727 goto evap0;
3728 /* fallthrough */
3729 case scm_tcs_closures:
3730 {
3731 const SCM formals = SCM_CLOSURE_FORMALS (proc);
3732 if (SCM_CONSP (formals))
3733 goto umwrongnumargs;
3734 x = SCM_CLOSURE_BODY (proc);
3735 env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
3736 goto nontoplevel_begin;
3737 }
3738 case scm_tcs_struct:
3739 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3740 {
3741 x = SCM_ENTITY_PROCEDURE (proc);
3742 arg1 = SCM_EOL;
3743 goto type_dispatch;
3744 }
3745 else if (SCM_I_OPERATORP (proc))
3746 {
3747 arg1 = proc;
3748 proc = (SCM_I_ENTITYP (proc)
3749 ? SCM_ENTITY_PROCEDURE (proc)
3750 : SCM_OPERATOR_PROCEDURE (proc));
3751 #ifdef DEVAL
3752 debug.info->a.proc = proc;
3753 debug.info->a.args = scm_list_1 (arg1);
3754 #endif
3755 goto evap1;
3756 }
3757 else
3758 goto badfun;
3759 case scm_tc7_subr_1:
3760 case scm_tc7_subr_2:
3761 case scm_tc7_subr_2o:
3762 case scm_tc7_dsubr:
3763 case scm_tc7_cxr:
3764 case scm_tc7_subr_3:
3765 case scm_tc7_lsubr_2:
3766 umwrongnumargs:
3767 unmemocar (x, env);
3768 scm_wrong_num_args (proc);
3769 default:
3770 badfun:
3771 scm_misc_error (NULL, "Wrong type to apply: ~S", scm_list_1 (proc));
3772 }
3773 }
3774
3775 /* must handle macros by here */
3776 x = SCM_CDR (x);
3777 if (SCM_CONSP (x))
3778 arg1 = EVALCAR (x, env);
3779 else
3780 scm_wrong_num_args (proc);
3781 #ifdef DEVAL
3782 debug.info->a.args = scm_list_1 (arg1);
3783 #endif
3784 x = SCM_CDR (x);
3785 {
3786 SCM arg2;
3787 if (SCM_NULLP (x))
3788 {
3789 ENTER_APPLY;
3790 evap1: /* inputs: proc, arg1 */
3791 SCM_ASRTGO (!SCM_IMP (proc), badfun);
3792 switch (SCM_TYP7 (proc))
3793 { /* have one argument in arg1 */
3794 case scm_tc7_subr_2o:
3795 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
3796 case scm_tc7_subr_1:
3797 case scm_tc7_subr_1o:
3798 RETURN (SCM_SUBRF (proc) (arg1));
3799 case scm_tc7_dsubr:
3800 if (SCM_INUMP (arg1))
3801 {
3802 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1))));
3803 }
3804 else if (SCM_REALP (arg1))
3805 {
3806 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
3807 }
3808 else if (SCM_BIGP (arg1))
3809 {
3810 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
3811 }
3812 else if (SCM_FRACTIONP (arg1))
3813 {
3814 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
3815 }
3816 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
3817 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
3818 case scm_tc7_cxr:
3819 {
3820 unsigned char pattern = (scm_t_bits) SCM_SUBRF (proc);
3821 do
3822 {
3823 SCM_ASSERT (SCM_CONSP (arg1), arg1, SCM_ARG1,
3824 SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
3825 arg1 = (pattern & 1) ? SCM_CAR (arg1) : SCM_CDR (arg1);
3826 pattern >>= 2;
3827 } while (pattern);
3828 RETURN (arg1);
3829 }
3830 case scm_tc7_rpsubr:
3831 RETURN (SCM_BOOL_T);
3832 case scm_tc7_asubr:
3833 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
3834 case scm_tc7_lsubr:
3835 #ifdef DEVAL
3836 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
3837 #else
3838 RETURN (SCM_SUBRF (proc) (scm_list_1 (arg1)));
3839 #endif
3840 case scm_tc7_smob:
3841 if (!SCM_SMOB_APPLICABLE_P (proc))
3842 goto badfun;
3843 RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
3844 case scm_tc7_cclo:
3845 arg2 = arg1;
3846 arg1 = proc;
3847 proc = SCM_CCLO_SUBR (proc);
3848 #ifdef DEVAL
3849 debug.info->a.args = scm_cons (arg1, debug.info->a.args);
3850 debug.info->a.proc = proc;
3851 #endif
3852 goto evap2;
3853 case scm_tc7_pws:
3854 proc = SCM_PROCEDURE (proc);
3855 #ifdef DEVAL
3856 debug.info->a.proc = proc;
3857 #endif
3858 if (!SCM_CLOSUREP (proc))
3859 goto evap1;
3860 /* fallthrough */
3861 case scm_tcs_closures:
3862 {
3863 /* clos1: */
3864 const SCM formals = SCM_CLOSURE_FORMALS (proc);
3865 if (SCM_NULLP (formals)
3866 || (SCM_CONSP (formals) && SCM_CONSP (SCM_CDR (formals))))
3867 goto umwrongnumargs;
3868 x = SCM_CLOSURE_BODY (proc);
3869 #ifdef DEVAL
3870 env = SCM_EXTEND_ENV (formals,
3871 debug.info->a.args,
3872 SCM_ENV (proc));
3873 #else
3874 env = SCM_EXTEND_ENV (formals,
3875 scm_list_1 (arg1),
3876 SCM_ENV (proc));
3877 #endif
3878 goto nontoplevel_begin;
3879 }
3880 case scm_tcs_struct:
3881 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3882 {
3883 x = SCM_ENTITY_PROCEDURE (proc);
3884 #ifdef DEVAL
3885 arg1 = debug.info->a.args;
3886 #else
3887 arg1 = scm_list_1 (arg1);
3888 #endif
3889 goto type_dispatch;
3890 }
3891 else if (SCM_I_OPERATORP (proc))
3892 {
3893 arg2 = arg1;
3894 arg1 = proc;
3895 proc = (SCM_I_ENTITYP (proc)
3896 ? SCM_ENTITY_PROCEDURE (proc)
3897 : SCM_OPERATOR_PROCEDURE (proc));
3898 #ifdef DEVAL
3899 debug.info->a.args = scm_cons (arg1, debug.info->a.args);
3900 debug.info->a.proc = proc;
3901 #endif
3902 goto evap2;
3903 }
3904 else
3905 goto badfun;
3906 case scm_tc7_subr_2:
3907 case scm_tc7_subr_0:
3908 case scm_tc7_subr_3:
3909 case scm_tc7_lsubr_2:
3910 scm_wrong_num_args (proc);
3911 default:
3912 goto badfun;
3913 }
3914 }
3915 if (SCM_CONSP (x))
3916 arg2 = EVALCAR (x, env);
3917 else
3918 scm_wrong_num_args (proc);
3919
3920 { /* have two or more arguments */
3921 #ifdef DEVAL
3922 debug.info->a.args = scm_list_2 (arg1, arg2);
3923 #endif
3924 x = SCM_CDR (x);
3925 if (SCM_NULLP (x)) {
3926 ENTER_APPLY;
3927 evap2:
3928 SCM_ASRTGO (!SCM_IMP (proc), badfun);
3929 switch (SCM_TYP7 (proc))
3930 { /* have two arguments */
3931 case scm_tc7_subr_2:
3932 case scm_tc7_subr_2o:
3933 RETURN (SCM_SUBRF (proc) (arg1, arg2));
3934 case scm_tc7_lsubr:
3935 #ifdef DEVAL
3936 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
3937 #else
3938 RETURN (SCM_SUBRF (proc) (scm_list_2 (arg1, arg2)));
3939 #endif
3940 case scm_tc7_lsubr_2:
3941 RETURN (SCM_SUBRF (proc) (arg1, arg2, SCM_EOL));
3942 case scm_tc7_rpsubr:
3943 case scm_tc7_asubr:
3944 RETURN (SCM_SUBRF (proc) (arg1, arg2));
3945 case scm_tc7_smob:
3946 if (!SCM_SMOB_APPLICABLE_P (proc))
3947 goto badfun;
3948 RETURN (SCM_SMOB_APPLY_2 (proc, arg1, arg2));
3949 cclon:
3950 case scm_tc7_cclo:
3951 #ifdef DEVAL
3952 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
3953 scm_cons (proc, debug.info->a.args),
3954 SCM_EOL));
3955 #else
3956 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc),
3957 scm_cons2 (proc, arg1,
3958 scm_cons (arg2,
3959 scm_eval_args (x,
3960 env,
3961 proc))),
3962 SCM_EOL));
3963 #endif
3964 case scm_tcs_struct:
3965 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
3966 {
3967 x = SCM_ENTITY_PROCEDURE (proc);
3968 #ifdef DEVAL
3969 arg1 = debug.info->a.args;
3970 #else
3971 arg1 = scm_list_2 (arg1, arg2);
3972 #endif
3973 goto type_dispatch;
3974 }
3975 else if (SCM_I_OPERATORP (proc))
3976 {
3977 operatorn:
3978 #ifdef DEVAL
3979 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
3980 ? SCM_ENTITY_PROCEDURE (proc)
3981 : SCM_OPERATOR_PROCEDURE (proc),
3982 scm_cons (proc, debug.info->a.args),
3983 SCM_EOL));
3984 #else
3985 RETURN (SCM_APPLY (SCM_I_ENTITYP (proc)
3986 ? SCM_ENTITY_PROCEDURE (proc)
3987 : SCM_OPERATOR_PROCEDURE (proc),
3988 scm_cons2 (proc, arg1,
3989 scm_cons (arg2,
3990 scm_eval_args (x,
3991 env,
3992 proc))),
3993 SCM_EOL));
3994 #endif
3995 }
3996 else
3997 goto badfun;
3998 case scm_tc7_subr_0:
3999 case scm_tc7_dsubr:
4000 case scm_tc7_cxr:
4001 case scm_tc7_subr_1o:
4002 case scm_tc7_subr_1:
4003 case scm_tc7_subr_3:
4004 scm_wrong_num_args (proc);
4005 default:
4006 goto badfun;
4007 case scm_tc7_pws:
4008 proc = SCM_PROCEDURE (proc);
4009 #ifdef DEVAL
4010 debug.info->a.proc = proc;
4011 #endif
4012 if (!SCM_CLOSUREP (proc))
4013 goto evap2;
4014 /* fallthrough */
4015 case scm_tcs_closures:
4016 {
4017 /* clos2: */
4018 const SCM formals = SCM_CLOSURE_FORMALS (proc);
4019 if (SCM_NULLP (formals)
4020 || (SCM_CONSP (formals)
4021 && (SCM_NULLP (SCM_CDR (formals))
4022 || (SCM_CONSP (SCM_CDR (formals))
4023 && SCM_CONSP (SCM_CDDR (formals))))))
4024 goto umwrongnumargs;
4025 #ifdef DEVAL
4026 env = SCM_EXTEND_ENV (formals,
4027 debug.info->a.args,
4028 SCM_ENV (proc));
4029 #else
4030 env = SCM_EXTEND_ENV (formals,
4031 scm_list_2 (arg1, arg2),
4032 SCM_ENV (proc));
4033 #endif
4034 x = SCM_CLOSURE_BODY (proc);
4035 goto nontoplevel_begin;
4036 }
4037 }
4038 }
4039 if (!SCM_CONSP (x))
4040 scm_wrong_num_args (proc);
4041 #ifdef DEVAL
4042 debug.info->a.args = scm_cons2 (arg1, arg2,
4043 deval_args (x, env, proc,
4044 SCM_CDRLOC (SCM_CDR (debug.info->a.args))));
4045 #endif
4046 ENTER_APPLY;
4047 evap3:
4048 SCM_ASRTGO (!SCM_IMP (proc), badfun);
4049 switch (SCM_TYP7 (proc))
4050 { /* have 3 or more arguments */
4051 #ifdef DEVAL
4052 case scm_tc7_subr_3:
4053 if (!SCM_NULLP (SCM_CDR (x)))
4054 scm_wrong_num_args (proc);
4055 else
4056 RETURN (SCM_SUBRF (proc) (arg1, arg2,
4057 SCM_CADDR (debug.info->a.args)));
4058 case scm_tc7_asubr:
4059 arg1 = SCM_SUBRF(proc)(arg1, arg2);
4060 arg2 = SCM_CDDR (debug.info->a.args);
4061 do
4062 {
4063 arg1 = SCM_SUBRF(proc)(arg1, SCM_CAR (arg2));
4064 arg2 = SCM_CDR (arg2);
4065 }
4066 while (SCM_NIMP (arg2));
4067 RETURN (arg1);
4068 case scm_tc7_rpsubr:
4069 if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, arg2)))
4070 RETURN (SCM_BOOL_F);
4071 arg1 = SCM_CDDR (debug.info->a.args);
4072 do
4073 {
4074 if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, SCM_CAR (arg1))))
4075 RETURN (SCM_BOOL_F);
4076 arg2 = SCM_CAR (arg1);
4077 arg1 = SCM_CDR (arg1);
4078 }
4079 while (SCM_NIMP (arg1));
4080 RETURN (SCM_BOOL_T);
4081 case scm_tc7_lsubr_2:
4082 RETURN (SCM_SUBRF (proc) (arg1, arg2,
4083 SCM_CDDR (debug.info->a.args)));
4084 case scm_tc7_lsubr:
4085 RETURN (SCM_SUBRF (proc) (debug.info->a.args));
4086 case scm_tc7_smob:
4087 if (!SCM_SMOB_APPLICABLE_P (proc))
4088 goto badfun;
4089 RETURN (SCM_SMOB_APPLY_3 (proc, arg1, arg2,
4090 SCM_CDDR (debug.info->a.args)));
4091 case scm_tc7_cclo:
4092 goto cclon;
4093 case scm_tc7_pws:
4094 proc = SCM_PROCEDURE (proc);
4095 debug.info->a.proc = proc;
4096 if (!SCM_CLOSUREP (proc))
4097 goto evap3;
4098 /* fallthrough */
4099 case scm_tcs_closures:
4100 {
4101 const SCM formals = SCM_CLOSURE_FORMALS (proc);
4102 if (SCM_NULLP (formals)
4103 || (SCM_CONSP (formals)
4104 && (SCM_NULLP (SCM_CDR (formals))
4105 || (SCM_CONSP (SCM_CDR (formals))
4106 && scm_badargsp (SCM_CDDR (formals), x)))))
4107 goto umwrongnumargs;
4108 SCM_SET_ARGSREADY (debug);
4109 env = SCM_EXTEND_ENV (formals,
4110 debug.info->a.args,
4111 SCM_ENV (proc));
4112 x = SCM_CLOSURE_BODY (proc);
4113 goto nontoplevel_begin;
4114 }
4115 #else /* DEVAL */
4116 case scm_tc7_subr_3:
4117 if (!SCM_NULLP (SCM_CDR (x)))
4118 scm_wrong_num_args (proc);
4119 else
4120 RETURN (SCM_SUBRF (proc) (arg1, arg2, EVALCAR (x, env)));
4121 case scm_tc7_asubr:
4122 arg1 = SCM_SUBRF (proc) (arg1, arg2);
4123 do
4124 {
4125 arg1 = SCM_SUBRF(proc)(arg1, EVALCAR(x, env));
4126 x = SCM_CDR(x);
4127 }
4128 while (!SCM_NULLP (x));
4129 RETURN (arg1);
4130 case scm_tc7_rpsubr:
4131 if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, arg2)))
4132 RETURN (SCM_BOOL_F);
4133 do
4134 {
4135 arg1 = EVALCAR (x, env);
4136 if (SCM_FALSEP (SCM_SUBRF (proc) (arg2, arg1)))
4137 RETURN (SCM_BOOL_F);
4138 arg2 = arg1;
4139 x = SCM_CDR (x);
4140 }
4141 while (!SCM_NULLP (x));
4142 RETURN (SCM_BOOL_T);
4143 case scm_tc7_lsubr_2:
4144 RETURN (SCM_SUBRF (proc) (arg1, arg2, scm_eval_args (x, env, proc)));
4145 case scm_tc7_lsubr:
4146 RETURN (SCM_SUBRF (proc) (scm_cons2 (arg1,
4147 arg2,
4148 scm_eval_args (x, env, proc))));
4149 case scm_tc7_smob:
4150 if (!SCM_SMOB_APPLICABLE_P (proc))
4151 goto badfun;
4152 RETURN (SCM_SMOB_APPLY_3 (proc, arg1, arg2,
4153 scm_eval_args (x, env, proc)));
4154 case scm_tc7_cclo:
4155 goto cclon;
4156 case scm_tc7_pws:
4157 proc = SCM_PROCEDURE (proc);
4158 if (!SCM_CLOSUREP (proc))
4159 goto evap3;
4160 /* fallthrough */
4161 case scm_tcs_closures:
4162 {
4163 const SCM formals = SCM_CLOSURE_FORMALS (proc);
4164 if (SCM_NULLP (formals)
4165 || (SCM_CONSP (formals)
4166 && (SCM_NULLP (SCM_CDR (formals))
4167 || (SCM_CONSP (SCM_CDR (formals))
4168 && scm_badargsp (SCM_CDDR (formals), x)))))
4169 goto umwrongnumargs;
4170 env = SCM_EXTEND_ENV (formals,
4171 scm_cons2 (arg1,
4172 arg2,
4173 scm_eval_args (x, env, proc)),
4174 SCM_ENV (proc));
4175 x = SCM_CLOSURE_BODY (proc);
4176 goto nontoplevel_begin;
4177 }
4178 #endif /* DEVAL */
4179 case scm_tcs_struct:
4180 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
4181 {
4182 #ifdef DEVAL
4183 arg1 = debug.info->a.args;
4184 #else
4185 arg1 = scm_cons2 (arg1, arg2, scm_eval_args (x, env, proc));
4186 #endif
4187 x = SCM_ENTITY_PROCEDURE (proc);
4188 goto type_dispatch;
4189 }
4190 else if (SCM_I_OPERATORP (proc))
4191 goto operatorn;
4192 else
4193 goto badfun;
4194 case scm_tc7_subr_2:
4195 case scm_tc7_subr_1o:
4196 case scm_tc7_subr_2o:
4197 case scm_tc7_subr_0:
4198 case scm_tc7_dsubr:
4199 case scm_tc7_cxr:
4200 case scm_tc7_subr_1:
4201 scm_wrong_num_args (proc);
4202 default:
4203 goto badfun;
4204 }
4205 }
4206 }
4207 #ifdef DEVAL
4208 exit:
4209 if (scm_check_exit_p && SCM_TRAPS_P)
4210 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
4211 {
4212 SCM_CLEAR_TRACED_FRAME (debug);
4213 if (SCM_CHEAPTRAPS_P)
4214 arg1 = scm_make_debugobj (&debug);
4215 else
4216 {
4217 int first;
4218 SCM val = scm_make_continuation (&first);
4219
4220 if (first)
4221 arg1 = val;
4222 else
4223 {
4224 proc = val;
4225 goto ret;
4226 }
4227 }
4228 SCM_TRAPS_P = 0;
4229 scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
4230 SCM_TRAPS_P = 1;
4231 }
4232 ret:
4233 scm_last_debug_frame = debug.prev;
4234 return proc;
4235 #endif
4236 }
4237
4238
4239 /* SECTION: This code is compiled once.
4240 */
4241
4242 #ifndef DEVAL
4243
4244 \f
4245
4246 /* Simple procedure calls
4247 */
4248
4249 SCM
4250 scm_call_0 (SCM proc)
4251 {
4252 return scm_apply (proc, SCM_EOL, SCM_EOL);
4253 }
4254
4255 SCM
4256 scm_call_1 (SCM proc, SCM arg1)
4257 {
4258 return scm_apply (proc, arg1, scm_listofnull);
4259 }
4260
4261 SCM
4262 scm_call_2 (SCM proc, SCM arg1, SCM arg2)
4263 {
4264 return scm_apply (proc, arg1, scm_cons (arg2, scm_listofnull));
4265 }
4266
4267 SCM
4268 scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
4269 {
4270 return scm_apply (proc, arg1, scm_cons2 (arg2, arg3, scm_listofnull));
4271 }
4272
4273 SCM
4274 scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
4275 {
4276 return scm_apply (proc, arg1, scm_cons2 (arg2, arg3,
4277 scm_cons (arg4, scm_listofnull)));
4278 }
4279
4280 /* Simple procedure applies
4281 */
4282
4283 SCM
4284 scm_apply_0 (SCM proc, SCM args)
4285 {
4286 return scm_apply (proc, args, SCM_EOL);
4287 }
4288
4289 SCM
4290 scm_apply_1 (SCM proc, SCM arg1, SCM args)
4291 {
4292 return scm_apply (proc, scm_cons (arg1, args), SCM_EOL);
4293 }
4294
4295 SCM
4296 scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
4297 {
4298 return scm_apply (proc, scm_cons2 (arg1, arg2, args), SCM_EOL);
4299 }
4300
4301 SCM
4302 scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
4303 {
4304 return scm_apply (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)),
4305 SCM_EOL);
4306 }
4307
4308 /* This code processes the arguments to apply:
4309
4310 (apply PROC ARG1 ... ARGS)
4311
4312 Given a list (ARG1 ... ARGS), this function conses the ARG1
4313 ... arguments onto the front of ARGS, and returns the resulting
4314 list. Note that ARGS is a list; thus, the argument to this
4315 function is a list whose last element is a list.
4316
4317 Apply calls this function, and applies PROC to the elements of the
4318 result. apply:nconc2last takes care of building the list of
4319 arguments, given (ARG1 ... ARGS).
4320
4321 Rather than do new consing, apply:nconc2last destroys its argument.
4322 On that topic, this code came into my care with the following
4323 beautifully cryptic comment on that topic: "This will only screw
4324 you if you do (scm_apply scm_apply '( ... ))" If you know what
4325 they're referring to, send me a patch to this comment. */
4326
4327 SCM_DEFINE (scm_nconc2last, "apply:nconc2last", 1, 0, 0,
4328 (SCM lst),
4329 "Given a list (@var{arg1} @dots{} @var{args}), this function\n"
4330 "conses the @var{arg1} @dots{} arguments onto the front of\n"
4331 "@var{args}, and returns the resulting list. Note that\n"
4332 "@var{args} is a list; thus, the argument to this function is\n"
4333 "a list whose last element is a list.\n"
4334 "Note: Rather than do new consing, @code{apply:nconc2last}\n"
4335 "destroys its argument, so use with care.")
4336 #define FUNC_NAME s_scm_nconc2last
4337 {
4338 SCM *lloc;
4339 SCM_VALIDATE_NONEMPTYLIST (1, lst);
4340 lloc = &lst;
4341 while (!SCM_NULLP (SCM_CDR (*lloc))) /* Perhaps should be
4342 SCM_NULL_OR_NIL_P, but not
4343 needed in 99.99% of cases,
4344 and it could seriously hurt
4345 performance. - Neil */
4346 lloc = SCM_CDRLOC (*lloc);
4347 SCM_ASSERT (scm_ilength (SCM_CAR (*lloc)) >= 0, lst, SCM_ARG1, FUNC_NAME);
4348 *lloc = SCM_CAR (*lloc);
4349 return lst;
4350 }
4351 #undef FUNC_NAME
4352
4353 #endif /* !DEVAL */
4354
4355
4356 /* SECTION: When DEVAL is defined this code yields scm_dapply.
4357 * It is compiled twice.
4358 */
4359
4360 #if 0
4361 SCM
4362 scm_apply (SCM proc, SCM arg1, SCM args)
4363 {}
4364 #endif
4365
4366 #if 0
4367 SCM
4368 scm_dapply (SCM proc, SCM arg1, SCM args)
4369 {}
4370 #endif
4371
4372
4373 /* Apply a function to a list of arguments.
4374
4375 This function is exported to the Scheme level as taking two
4376 required arguments and a tail argument, as if it were:
4377 (lambda (proc arg1 . args) ...)
4378 Thus, if you just have a list of arguments to pass to a procedure,
4379 pass the list as ARG1, and '() for ARGS. If you have some fixed
4380 args, pass the first as ARG1, then cons any remaining fixed args
4381 onto the front of your argument list, and pass that as ARGS. */
4382
4383 SCM
4384 SCM_APPLY (SCM proc, SCM arg1, SCM args)
4385 {
4386 #ifdef DEVAL
4387 scm_t_debug_frame debug;
4388 scm_t_debug_info debug_vect_body;
4389 debug.prev = scm_last_debug_frame;
4390 debug.status = SCM_APPLYFRAME;
4391 debug.vect = &debug_vect_body;
4392 debug.vect[0].a.proc = proc;
4393 debug.vect[0].a.args = SCM_EOL;
4394 scm_last_debug_frame = &debug;
4395 #else
4396 if (scm_debug_mode_p)
4397 return scm_dapply (proc, arg1, args);
4398 #endif
4399
4400 SCM_ASRTGO (SCM_NIMP (proc), badproc);
4401
4402 /* If ARGS is the empty list, then we're calling apply with only two
4403 arguments --- ARG1 is the list of arguments for PROC. Whatever
4404 the case, futz with things so that ARG1 is the first argument to
4405 give to PROC (or SCM_UNDEFINED if no args), and ARGS contains the
4406 rest.
4407
4408 Setting the debug apply frame args this way is pretty messy.
4409 Perhaps we should store arg1 and args directly in the frame as
4410 received, and let scm_frame_arguments unpack them, because that's
4411 a relatively rare operation. This works for now; if the Guile
4412 developer archives are still around, see Mikael's post of
4413 11-Apr-97. */
4414 if (SCM_NULLP (args))
4415 {
4416 if (SCM_NULLP (arg1))
4417 {
4418 arg1 = SCM_UNDEFINED;
4419 #ifdef DEVAL
4420 debug.vect[0].a.args = SCM_EOL;
4421 #endif
4422 }
4423 else
4424 {
4425 #ifdef DEVAL
4426 debug.vect[0].a.args = arg1;
4427 #endif
4428 args = SCM_CDR (arg1);
4429 arg1 = SCM_CAR (arg1);
4430 }
4431 }
4432 else
4433 {
4434 args = scm_nconc2last (args);
4435 #ifdef DEVAL
4436 debug.vect[0].a.args = scm_cons (arg1, args);
4437 #endif
4438 }
4439 #ifdef DEVAL
4440 if (SCM_ENTER_FRAME_P && SCM_TRAPS_P)
4441 {
4442 SCM tmp;
4443 if (SCM_CHEAPTRAPS_P)
4444 tmp = scm_make_debugobj (&debug);
4445 else
4446 {
4447 int first;
4448
4449 tmp = scm_make_continuation (&first);
4450 if (!first)
4451 goto entap;
4452 }
4453 SCM_TRAPS_P = 0;
4454 scm_call_2 (SCM_ENTER_FRAME_HDLR, scm_sym_enter_frame, tmp);
4455 SCM_TRAPS_P = 1;
4456 }
4457 entap:
4458 ENTER_APPLY;
4459 #endif
4460 tail:
4461 switch (SCM_TYP7 (proc))
4462 {
4463 case scm_tc7_subr_2o:
4464 args = SCM_NULLP (args) ? SCM_UNDEFINED : SCM_CAR (args);
4465 RETURN (SCM_SUBRF (proc) (arg1, args));
4466 case scm_tc7_subr_2:
4467 if (SCM_NULLP (args) || !SCM_NULLP (SCM_CDR (args)))
4468 scm_wrong_num_args (proc);
4469 args = SCM_CAR (args);
4470 RETURN (SCM_SUBRF (proc) (arg1, args));
4471 case scm_tc7_subr_0:
4472 if (!SCM_UNBNDP (arg1))
4473 scm_wrong_num_args (proc);
4474 else
4475 RETURN (SCM_SUBRF (proc) ());
4476 case scm_tc7_subr_1:
4477 if (SCM_UNBNDP (arg1))
4478 scm_wrong_num_args (proc);
4479 case scm_tc7_subr_1o:
4480 if (!SCM_NULLP (args))
4481 scm_wrong_num_args (proc);
4482 else
4483 RETURN (SCM_SUBRF (proc) (arg1));
4484 case scm_tc7_dsubr:
4485 if (SCM_UNBNDP (arg1) || !SCM_NULLP (args))
4486 scm_wrong_num_args (proc);
4487 if (SCM_INUMP (arg1))
4488 {
4489 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1))));
4490 }
4491 else if (SCM_REALP (arg1))
4492 {
4493 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
4494 }
4495 else if (SCM_BIGP (arg1))
4496 {
4497 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
4498 }
4499 else if (SCM_FRACTIONP (arg1))
4500 {
4501 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
4502 }
4503 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
4504 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
4505 case scm_tc7_cxr:
4506 if (SCM_UNBNDP (arg1) || !SCM_NULLP (args))
4507 scm_wrong_num_args (proc);
4508 {
4509 unsigned char pattern = (scm_t_bits) SCM_SUBRF (proc);
4510 do
4511 {
4512 SCM_ASSERT (SCM_CONSP (arg1), arg1, SCM_ARG1,
4513 SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
4514 arg1 = (pattern & 1) ? SCM_CAR (arg1) : SCM_CDR (arg1);
4515 pattern >>= 2;
4516 } while (pattern);
4517 RETURN (arg1);
4518 }
4519 case scm_tc7_subr_3:
4520 if (SCM_NULLP (args)
4521 || SCM_NULLP (SCM_CDR (args))
4522 || !SCM_NULLP (SCM_CDDR (args)))
4523 scm_wrong_num_args (proc);
4524 else
4525 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CADR (args)));
4526 case scm_tc7_lsubr:
4527 #ifdef DEVAL
4528 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args));
4529 #else
4530 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)));
4531 #endif
4532 case scm_tc7_lsubr_2:
4533 if (!SCM_CONSP (args))
4534 scm_wrong_num_args (proc);
4535 else
4536 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)));
4537 case scm_tc7_asubr:
4538 if (SCM_NULLP (args))
4539 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED));
4540 while (SCM_NIMP (args))
4541 {
4542 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
4543 arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
4544 args = SCM_CDR (args);
4545 }
4546 RETURN (arg1);
4547 case scm_tc7_rpsubr:
4548 if (SCM_NULLP (args))
4549 RETURN (SCM_BOOL_T);
4550 while (SCM_NIMP (args))
4551 {
4552 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
4553 if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
4554 RETURN (SCM_BOOL_F);
4555 arg1 = SCM_CAR (args);
4556 args = SCM_CDR (args);
4557 }
4558 RETURN (SCM_BOOL_T);
4559 case scm_tcs_closures:
4560 #ifdef DEVAL
4561 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
4562 #else
4563 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
4564 #endif
4565 if (scm_badargsp (SCM_CLOSURE_FORMALS (proc), arg1))
4566 scm_wrong_num_args (proc);
4567
4568 /* Copy argument list */
4569 if (SCM_IMP (arg1))
4570 args = arg1;
4571 else
4572 {
4573 SCM tl = args = scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED);
4574 for (arg1 = SCM_CDR (arg1); SCM_CONSP (arg1); arg1 = SCM_CDR (arg1))
4575 {
4576 SCM_SETCDR (tl, scm_cons (SCM_CAR (arg1), SCM_UNSPECIFIED));
4577 tl = SCM_CDR (tl);
4578 }
4579 SCM_SETCDR (tl, arg1);
4580 }
4581
4582 args = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
4583 args,
4584 SCM_ENV (proc));
4585 proc = SCM_CLOSURE_BODY (proc);
4586 again:
4587 arg1 = SCM_CDR (proc);
4588 while (!SCM_NULLP (arg1))
4589 {
4590 if (SCM_IMP (SCM_CAR (proc)))
4591 {
4592 if (SCM_ISYMP (SCM_CAR (proc)))
4593 {
4594 scm_rec_mutex_lock (&source_mutex);
4595 /* check for race condition */
4596 if (SCM_ISYMP (SCM_CAR (proc)))
4597 m_expand_body (proc, args);
4598 scm_rec_mutex_unlock (&source_mutex);
4599 goto again;
4600 }
4601 else
4602 SCM_VALIDATE_NON_EMPTY_COMBINATION (SCM_CAR (proc));
4603 }
4604 else
4605 EVAL (SCM_CAR (proc), args);
4606 proc = arg1;
4607 arg1 = SCM_CDR (proc);
4608 }
4609 RETURN (EVALCAR (proc, args));
4610 case scm_tc7_smob:
4611 if (!SCM_SMOB_APPLICABLE_P (proc))
4612 goto badproc;
4613 if (SCM_UNBNDP (arg1))
4614 RETURN (SCM_SMOB_APPLY_0 (proc));
4615 else if (SCM_NULLP (args))
4616 RETURN (SCM_SMOB_APPLY_1 (proc, arg1));
4617 else if (SCM_NULLP (SCM_CDR (args)))
4618 RETURN (SCM_SMOB_APPLY_2 (proc, arg1, SCM_CAR (args)));
4619 else
4620 RETURN (SCM_SMOB_APPLY_3 (proc, arg1, SCM_CAR (args), SCM_CDR (args)));
4621 case scm_tc7_cclo:
4622 #ifdef DEVAL
4623 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
4624 arg1 = proc;
4625 proc = SCM_CCLO_SUBR (proc);
4626 debug.vect[0].a.proc = proc;
4627 debug.vect[0].a.args = scm_cons (arg1, args);
4628 #else
4629 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
4630 arg1 = proc;
4631 proc = SCM_CCLO_SUBR (proc);
4632 #endif
4633 goto tail;
4634 case scm_tc7_pws:
4635 proc = SCM_PROCEDURE (proc);
4636 #ifdef DEVAL
4637 debug.vect[0].a.proc = proc;
4638 #endif
4639 goto tail;
4640 case scm_tcs_struct:
4641 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
4642 {
4643 #ifdef DEVAL
4644 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
4645 #else
4646 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
4647 #endif
4648 RETURN (scm_apply_generic (proc, args));
4649 }
4650 else if (SCM_I_OPERATORP (proc))
4651 {
4652 /* operator */
4653 #ifdef DEVAL
4654 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
4655 #else
4656 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
4657 #endif
4658 arg1 = proc;
4659 proc = (SCM_I_ENTITYP (proc)
4660 ? SCM_ENTITY_PROCEDURE (proc)
4661 : SCM_OPERATOR_PROCEDURE (proc));
4662 #ifdef DEVAL
4663 debug.vect[0].a.proc = proc;
4664 debug.vect[0].a.args = scm_cons (arg1, args);
4665 #endif
4666 if (SCM_NIMP (proc))
4667 goto tail;
4668 else
4669 goto badproc;
4670 }
4671 else
4672 goto badproc;
4673 default:
4674 badproc:
4675 scm_wrong_type_arg ("apply", SCM_ARG1, proc);
4676 }
4677 #ifdef DEVAL
4678 exit:
4679 if (scm_check_exit_p && SCM_TRAPS_P)
4680 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
4681 {
4682 SCM_CLEAR_TRACED_FRAME (debug);
4683 if (SCM_CHEAPTRAPS_P)
4684 arg1 = scm_make_debugobj (&debug);
4685 else
4686 {
4687 int first;
4688 SCM val = scm_make_continuation (&first);
4689
4690 if (first)
4691 arg1 = val;
4692 else
4693 {
4694 proc = val;
4695 goto ret;
4696 }
4697 }
4698 SCM_TRAPS_P = 0;
4699 scm_call_3 (SCM_EXIT_FRAME_HDLR, scm_sym_exit_frame, arg1, proc);
4700 SCM_TRAPS_P = 1;
4701 }
4702 ret:
4703 scm_last_debug_frame = debug.prev;
4704 return proc;
4705 #endif
4706 }
4707
4708
4709 /* SECTION: The rest of this file is only read once.
4710 */
4711
4712 #ifndef DEVAL
4713
4714 /* Trampolines
4715 *
4716 * Trampolines make it possible to move procedure application dispatch
4717 * outside inner loops. The motivation was clean implementation of
4718 * efficient replacements of R5RS primitives in SRFI-1.
4719 *
4720 * The semantics is clear: scm_trampoline_N returns an optimized
4721 * version of scm_call_N (or NULL if the procedure isn't applicable
4722 * on N args).
4723 *
4724 * Applying the optimization to map and for-each increased efficiency
4725 * noticeably. For example, (map abs ls) is now 8 times faster than
4726 * before.
4727 */
4728
4729 static SCM
4730 call_subr0_0 (SCM proc)
4731 {
4732 return SCM_SUBRF (proc) ();
4733 }
4734
4735 static SCM
4736 call_subr1o_0 (SCM proc)
4737 {
4738 return SCM_SUBRF (proc) (SCM_UNDEFINED);
4739 }
4740
4741 static SCM
4742 call_lsubr_0 (SCM proc)
4743 {
4744 return SCM_SUBRF (proc) (SCM_EOL);
4745 }
4746
4747 SCM
4748 scm_i_call_closure_0 (SCM proc)
4749 {
4750 const SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
4751 SCM_EOL,
4752 SCM_ENV (proc));
4753 const SCM result = scm_eval_body (SCM_CLOSURE_BODY (proc), env);
4754 return result;
4755 }
4756
4757 scm_t_trampoline_0
4758 scm_trampoline_0 (SCM proc)
4759 {
4760 scm_t_trampoline_0 trampoline;
4761
4762 if (SCM_IMP (proc))
4763 return NULL;
4764
4765 switch (SCM_TYP7 (proc))
4766 {
4767 case scm_tc7_subr_0:
4768 trampoline = call_subr0_0;
4769 break;
4770 case scm_tc7_subr_1o:
4771 trampoline = call_subr1o_0;
4772 break;
4773 case scm_tc7_lsubr:
4774 trampoline = call_lsubr_0;
4775 break;
4776 case scm_tcs_closures:
4777 {
4778 SCM formals = SCM_CLOSURE_FORMALS (proc);
4779 if (SCM_NULLP (formals) || !SCM_CONSP (formals))
4780 trampoline = scm_i_call_closure_0;
4781 else
4782 return NULL;
4783 break;
4784 }
4785 case scm_tcs_struct:
4786 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
4787 trampoline = scm_call_generic_0;
4788 else if (SCM_I_OPERATORP (proc))
4789 trampoline = scm_call_0;
4790 else
4791 return NULL;
4792 break;
4793 case scm_tc7_smob:
4794 if (SCM_SMOB_APPLICABLE_P (proc))
4795 trampoline = SCM_SMOB_DESCRIPTOR (proc).apply_0;
4796 else
4797 return NULL;
4798 break;
4799 case scm_tc7_asubr:
4800 case scm_tc7_rpsubr:
4801 case scm_tc7_cclo:
4802 case scm_tc7_pws:
4803 trampoline = scm_call_0;
4804 break;
4805 default:
4806 return NULL; /* not applicable on zero arguments */
4807 }
4808 /* We only reach this point if a valid trampoline was determined. */
4809
4810 /* If debugging is enabled, we want to see all calls to proc on the stack.
4811 * Thus, we replace the trampoline shortcut with scm_call_0. */
4812 if (scm_debug_mode_p)
4813 return scm_call_0;
4814 else
4815 return trampoline;
4816 }
4817
4818 static SCM
4819 call_subr1_1 (SCM proc, SCM arg1)
4820 {
4821 return SCM_SUBRF (proc) (arg1);
4822 }
4823
4824 static SCM
4825 call_subr2o_1 (SCM proc, SCM arg1)
4826 {
4827 return SCM_SUBRF (proc) (arg1, SCM_UNDEFINED);
4828 }
4829
4830 static SCM
4831 call_lsubr_1 (SCM proc, SCM arg1)
4832 {
4833 return SCM_SUBRF (proc) (scm_list_1 (arg1));
4834 }
4835
4836 static SCM
4837 call_dsubr_1 (SCM proc, SCM arg1)
4838 {
4839 if (SCM_INUMP (arg1))
4840 {
4841 RETURN (scm_make_real (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1))));
4842 }
4843 else if (SCM_REALP (arg1))
4844 {
4845 RETURN (scm_make_real (SCM_DSUBRF (proc) (SCM_REAL_VALUE (arg1))));
4846 }
4847 else if (SCM_BIGP (arg1))
4848 {
4849 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_big2dbl (arg1))));
4850 }
4851 else if (SCM_FRACTIONP (arg1))
4852 {
4853 RETURN (scm_make_real (SCM_DSUBRF (proc) (scm_i_fraction2double (arg1))));
4854 }
4855 SCM_WTA_DISPATCH_1 (*SCM_SUBR_GENERIC (proc), arg1,
4856 SCM_ARG1, SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
4857 }
4858
4859 static SCM
4860 call_cxr_1 (SCM proc, SCM arg1)
4861 {
4862 unsigned char pattern = (scm_t_bits) SCM_SUBRF (proc);
4863 do
4864 {
4865 SCM_ASSERT (SCM_CONSP (arg1), arg1, SCM_ARG1,
4866 SCM_SYMBOL_CHARS (SCM_SNAME (proc)));
4867 arg1 = (pattern & 1) ? SCM_CAR (arg1) : SCM_CDR (arg1);
4868 pattern >>= 2;
4869 } while (pattern);
4870 return arg1;
4871 }
4872
4873 static SCM
4874 call_closure_1 (SCM proc, SCM arg1)
4875 {
4876 const SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
4877 scm_list_1 (arg1),
4878 SCM_ENV (proc));
4879 const SCM result = scm_eval_body (SCM_CLOSURE_BODY (proc), env);
4880 return result;
4881 }
4882
4883 scm_t_trampoline_1
4884 scm_trampoline_1 (SCM proc)
4885 {
4886 scm_t_trampoline_1 trampoline;
4887
4888 if (SCM_IMP (proc))
4889 return NULL;
4890
4891 switch (SCM_TYP7 (proc))
4892 {
4893 case scm_tc7_subr_1:
4894 case scm_tc7_subr_1o:
4895 trampoline = call_subr1_1;
4896 break;
4897 case scm_tc7_subr_2o:
4898 trampoline = call_subr2o_1;
4899 break;
4900 case scm_tc7_lsubr:
4901 trampoline = call_lsubr_1;
4902 break;
4903 case scm_tc7_dsubr:
4904 trampoline = call_dsubr_1;
4905 break;
4906 case scm_tc7_cxr:
4907 trampoline = call_cxr_1;
4908 break;
4909 case scm_tcs_closures:
4910 {
4911 SCM formals = SCM_CLOSURE_FORMALS (proc);
4912 if (!SCM_NULLP (formals)
4913 && (!SCM_CONSP (formals) || !SCM_CONSP (SCM_CDR (formals))))
4914 trampoline = call_closure_1;
4915 else
4916 return NULL;
4917 break;
4918 }
4919 case scm_tcs_struct:
4920 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
4921 trampoline = scm_call_generic_1;
4922 else if (SCM_I_OPERATORP (proc))
4923 trampoline = scm_call_1;
4924 else
4925 return NULL;
4926 break;
4927 case scm_tc7_smob:
4928 if (SCM_SMOB_APPLICABLE_P (proc))
4929 trampoline = SCM_SMOB_DESCRIPTOR (proc).apply_1;
4930 else
4931 return NULL;
4932 break;
4933 case scm_tc7_asubr:
4934 case scm_tc7_rpsubr:
4935 case scm_tc7_cclo:
4936 case scm_tc7_pws:
4937 trampoline = scm_call_1;
4938 break;
4939 default:
4940 return NULL; /* not applicable on one arg */
4941 }
4942 /* We only reach this point if a valid trampoline was determined. */
4943
4944 /* If debugging is enabled, we want to see all calls to proc on the stack.
4945 * Thus, we replace the trampoline shortcut with scm_call_1. */
4946 if (scm_debug_mode_p)
4947 return scm_call_1;
4948 else
4949 return trampoline;
4950 }
4951
4952 static SCM
4953 call_subr2_2 (SCM proc, SCM arg1, SCM arg2)
4954 {
4955 return SCM_SUBRF (proc) (arg1, arg2);
4956 }
4957
4958 static SCM
4959 call_lsubr2_2 (SCM proc, SCM arg1, SCM arg2)
4960 {
4961 return SCM_SUBRF (proc) (arg1, arg2, SCM_EOL);
4962 }
4963
4964 static SCM
4965 call_lsubr_2 (SCM proc, SCM arg1, SCM arg2)
4966 {
4967 return SCM_SUBRF (proc) (scm_list_2 (arg1, arg2));
4968 }
4969
4970 static SCM
4971 call_closure_2 (SCM proc, SCM arg1, SCM arg2)
4972 {
4973 const SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (proc),
4974 scm_list_2 (arg1, arg2),
4975 SCM_ENV (proc));
4976 const SCM result = scm_eval_body (SCM_CLOSURE_BODY (proc), env);
4977 return result;
4978 }
4979
4980 scm_t_trampoline_2
4981 scm_trampoline_2 (SCM proc)
4982 {
4983 scm_t_trampoline_2 trampoline;
4984
4985 if (SCM_IMP (proc))
4986 return NULL;
4987
4988 switch (SCM_TYP7 (proc))
4989 {
4990 case scm_tc7_subr_2:
4991 case scm_tc7_subr_2o:
4992 case scm_tc7_rpsubr:
4993 case scm_tc7_asubr:
4994 trampoline = call_subr2_2;
4995 break;
4996 case scm_tc7_lsubr_2:
4997 trampoline = call_lsubr2_2;
4998 break;
4999 case scm_tc7_lsubr:
5000 trampoline = call_lsubr_2;
5001 break;
5002 case scm_tcs_closures:
5003 {
5004 SCM formals = SCM_CLOSURE_FORMALS (proc);
5005 if (!SCM_NULLP (formals)
5006 && (!SCM_CONSP (formals)
5007 || (!SCM_NULLP (SCM_CDR (formals))
5008 && (!SCM_CONSP (SCM_CDR (formals))
5009 || !SCM_CONSP (SCM_CDDR (formals))))))
5010 trampoline = call_closure_2;
5011 else
5012 return NULL;
5013 break;
5014 }
5015 case scm_tcs_struct:
5016 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
5017 trampoline = scm_call_generic_2;
5018 else if (SCM_I_OPERATORP (proc))
5019 trampoline = scm_call_2;
5020 else
5021 return NULL;
5022 break;
5023 case scm_tc7_smob:
5024 if (SCM_SMOB_APPLICABLE_P (proc))
5025 trampoline = SCM_SMOB_DESCRIPTOR (proc).apply_2;
5026 else
5027 return NULL;
5028 break;
5029 case scm_tc7_cclo:
5030 case scm_tc7_pws:
5031 trampoline = scm_call_2;
5032 break;
5033 default:
5034 return NULL; /* not applicable on two args */
5035 }
5036 /* We only reach this point if a valid trampoline was determined. */
5037
5038 /* If debugging is enabled, we want to see all calls to proc on the stack.
5039 * Thus, we replace the trampoline shortcut with scm_call_2. */
5040 if (scm_debug_mode_p)
5041 return scm_call_2;
5042 else
5043 return trampoline;
5044 }
5045
5046 /* Typechecking for multi-argument MAP and FOR-EACH.
5047
5048 Verify that each element of the vector ARGV, except for the first,
5049 is a proper list whose length is LEN. Attribute errors to WHO,
5050 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
5051 static inline void
5052 check_map_args (SCM argv,
5053 long len,
5054 SCM gf,
5055 SCM proc,
5056 SCM args,
5057 const char *who)
5058 {
5059 SCM const *ve = SCM_VELTS (argv);
5060 long i;
5061
5062 for (i = SCM_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
5063 {
5064 long elt_len = scm_ilength (ve[i]);
5065
5066 if (elt_len < 0)
5067 {
5068 if (gf)
5069 scm_apply_generic (gf, scm_cons (proc, args));
5070 else
5071 scm_wrong_type_arg (who, i + 2, ve[i]);
5072 }
5073
5074 if (elt_len != len)
5075 scm_out_of_range_pos (who, ve[i], SCM_MAKINUM (i + 2));
5076 }
5077
5078 scm_remember_upto_here_1 (argv);
5079 }
5080
5081
5082 SCM_GPROC (s_map, "map", 2, 0, 1, scm_map, g_map);
5083
5084 /* Note: Currently, scm_map applies PROC to the argument list(s)
5085 sequentially, starting with the first element(s). This is used in
5086 evalext.c where the Scheme procedure `map-in-order', which guarantees
5087 sequential behaviour, is implemented using scm_map. If the
5088 behaviour changes, we need to update `map-in-order'.
5089 */
5090
5091 SCM
5092 scm_map (SCM proc, SCM arg1, SCM args)
5093 #define FUNC_NAME s_map
5094 {
5095 long i, len;
5096 SCM res = SCM_EOL;
5097 SCM *pres = &res;
5098 SCM const *ve = &args; /* Keep args from being optimized away. */
5099
5100 len = scm_ilength (arg1);
5101 SCM_GASSERTn (len >= 0,
5102 g_map, scm_cons2 (proc, arg1, args), SCM_ARG2, s_map);
5103 SCM_VALIDATE_REST_ARGUMENT (args);
5104 if (SCM_NULLP (args))
5105 {
5106 scm_t_trampoline_1 call = scm_trampoline_1 (proc);
5107 SCM_GASSERT2 (call, g_map, proc, arg1, SCM_ARG1, s_map);
5108 while (SCM_NIMP (arg1))
5109 {
5110 *pres = scm_list_1 (call (proc, SCM_CAR (arg1)));
5111 pres = SCM_CDRLOC (*pres);
5112 arg1 = SCM_CDR (arg1);
5113 }
5114 return res;
5115 }
5116 if (SCM_NULLP (SCM_CDR (args)))
5117 {
5118 SCM arg2 = SCM_CAR (args);
5119 int len2 = scm_ilength (arg2);
5120 scm_t_trampoline_2 call = scm_trampoline_2 (proc);
5121 SCM_GASSERTn (call,
5122 g_map, scm_cons2 (proc, arg1, args), SCM_ARG1, s_map);
5123 SCM_GASSERTn (len2 >= 0,
5124 g_map, scm_cons2 (proc, arg1, args), SCM_ARG3, s_map);
5125 if (len2 != len)
5126 SCM_OUT_OF_RANGE (3, arg2);
5127 while (SCM_NIMP (arg1))
5128 {
5129 *pres = scm_list_1 (call (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
5130 pres = SCM_CDRLOC (*pres);
5131 arg1 = SCM_CDR (arg1);
5132 arg2 = SCM_CDR (arg2);
5133 }
5134 return res;
5135 }
5136 arg1 = scm_cons (arg1, args);
5137 args = scm_vector (arg1);
5138 ve = SCM_VELTS (args);
5139 check_map_args (args, len, g_map, proc, arg1, s_map);
5140 while (1)
5141 {
5142 arg1 = SCM_EOL;
5143 for (i = SCM_VECTOR_LENGTH (args) - 1; i >= 0; i--)
5144 {
5145 if (SCM_IMP (ve[i]))
5146 return res;
5147 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
5148 SCM_VECTOR_SET (args, i, SCM_CDR (ve[i]));
5149 }
5150 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
5151 pres = SCM_CDRLOC (*pres);
5152 }
5153 }
5154 #undef FUNC_NAME
5155
5156
5157 SCM_GPROC (s_for_each, "for-each", 2, 0, 1, scm_for_each, g_for_each);
5158
5159 SCM
5160 scm_for_each (SCM proc, SCM arg1, SCM args)
5161 #define FUNC_NAME s_for_each
5162 {
5163 SCM const *ve = &args; /* Keep args from being optimized away. */
5164 long i, len;
5165 len = scm_ilength (arg1);
5166 SCM_GASSERTn (len >= 0, g_for_each, scm_cons2 (proc, arg1, args),
5167 SCM_ARG2, s_for_each);
5168 SCM_VALIDATE_REST_ARGUMENT (args);
5169 if (SCM_NULLP (args))
5170 {
5171 scm_t_trampoline_1 call = scm_trampoline_1 (proc);
5172 SCM_GASSERT2 (call, g_for_each, proc, arg1, SCM_ARG1, s_for_each);
5173 while (SCM_NIMP (arg1))
5174 {
5175 call (proc, SCM_CAR (arg1));
5176 arg1 = SCM_CDR (arg1);
5177 }
5178 return SCM_UNSPECIFIED;
5179 }
5180 if (SCM_NULLP (SCM_CDR (args)))
5181 {
5182 SCM arg2 = SCM_CAR (args);
5183 int len2 = scm_ilength (arg2);
5184 scm_t_trampoline_2 call = scm_trampoline_2 (proc);
5185 SCM_GASSERTn (call, g_for_each,
5186 scm_cons2 (proc, arg1, args), SCM_ARG1, s_for_each);
5187 SCM_GASSERTn (len2 >= 0, g_for_each,
5188 scm_cons2 (proc, arg1, args), SCM_ARG3, s_for_each);
5189 if (len2 != len)
5190 SCM_OUT_OF_RANGE (3, arg2);
5191 while (SCM_NIMP (arg1))
5192 {
5193 call (proc, SCM_CAR (arg1), SCM_CAR (arg2));
5194 arg1 = SCM_CDR (arg1);
5195 arg2 = SCM_CDR (arg2);
5196 }
5197 return SCM_UNSPECIFIED;
5198 }
5199 arg1 = scm_cons (arg1, args);
5200 args = scm_vector (arg1);
5201 ve = SCM_VELTS (args);
5202 check_map_args (args, len, g_for_each, proc, arg1, s_for_each);
5203 while (1)
5204 {
5205 arg1 = SCM_EOL;
5206 for (i = SCM_VECTOR_LENGTH (args) - 1; i >= 0; i--)
5207 {
5208 if (SCM_IMP (ve[i]))
5209 return SCM_UNSPECIFIED;
5210 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
5211 SCM_VECTOR_SET (args, i, SCM_CDR (ve[i]));
5212 }
5213 scm_apply (proc, arg1, SCM_EOL);
5214 }
5215 }
5216 #undef FUNC_NAME
5217
5218
5219 SCM
5220 scm_closure (SCM code, SCM env)
5221 {
5222 SCM z;
5223 SCM closcar = scm_cons (code, SCM_EOL);
5224 z = scm_cell (SCM_UNPACK (closcar) + scm_tc3_closure, (scm_t_bits) env);
5225 scm_remember_upto_here (closcar);
5226 return z;
5227 }
5228
5229
5230 scm_t_bits scm_tc16_promise;
5231
5232 SCM
5233 scm_makprom (SCM code)
5234 {
5235 SCM_RETURN_NEWSMOB2 (scm_tc16_promise,
5236 SCM_UNPACK (code),
5237 scm_make_rec_mutex ());
5238 }
5239
5240 static size_t
5241 promise_free (SCM promise)
5242 {
5243 scm_rec_mutex_free (SCM_PROMISE_MUTEX (promise));
5244 return 0;
5245 }
5246
5247 static int
5248 promise_print (SCM exp, SCM port, scm_print_state *pstate)
5249 {
5250 int writingp = SCM_WRITINGP (pstate);
5251 scm_puts ("#<promise ", port);
5252 SCM_SET_WRITINGP (pstate, 1);
5253 scm_iprin1 (SCM_PROMISE_DATA (exp), port, pstate);
5254 SCM_SET_WRITINGP (pstate, writingp);
5255 scm_putc ('>', port);
5256 return !0;
5257 }
5258
5259 SCM_DEFINE (scm_force, "force", 1, 0, 0,
5260 (SCM promise),
5261 "If the promise @var{x} has not been computed yet, compute and\n"
5262 "return @var{x}, otherwise just return the previously computed\n"
5263 "value.")
5264 #define FUNC_NAME s_scm_force
5265 {
5266 SCM_VALIDATE_SMOB (1, promise, promise);
5267 scm_rec_mutex_lock (SCM_PROMISE_MUTEX (promise));
5268 if (!SCM_PROMISE_COMPUTED_P (promise))
5269 {
5270 SCM ans = scm_call_0 (SCM_PROMISE_DATA (promise));
5271 if (!SCM_PROMISE_COMPUTED_P (promise))
5272 {
5273 SCM_SET_PROMISE_DATA (promise, ans);
5274 SCM_SET_PROMISE_COMPUTED (promise);
5275 }
5276 }
5277 scm_rec_mutex_unlock (SCM_PROMISE_MUTEX (promise));
5278 return SCM_PROMISE_DATA (promise);
5279 }
5280 #undef FUNC_NAME
5281
5282
5283 SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
5284 (SCM obj),
5285 "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
5286 "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
5287 #define FUNC_NAME s_scm_promise_p
5288 {
5289 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
5290 }
5291 #undef FUNC_NAME
5292
5293
5294 SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
5295 (SCM xorig, SCM x, SCM y),
5296 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
5297 "Any source properties associated with @var{xorig} are also associated\n"
5298 "with the new pair.")
5299 #define FUNC_NAME s_scm_cons_source
5300 {
5301 SCM p, z;
5302 z = scm_cons (x, y);
5303 /* Copy source properties possibly associated with xorig. */
5304 p = scm_whash_lookup (scm_source_whash, xorig);
5305 if (!SCM_IMP (p))
5306 scm_whash_insert (scm_source_whash, z, p);
5307 return z;
5308 }
5309 #undef FUNC_NAME
5310
5311
5312 /* The function scm_copy_tree is used to copy an expression tree to allow the
5313 * memoizer to modify the expression during memoization. scm_copy_tree
5314 * creates deep copies of pairs and vectors, but not of any other data types,
5315 * since only pairs and vectors will be parsed by the memoizer.
5316 *
5317 * To avoid infinite recursion due to cyclic structures, the hare-and-tortoise
5318 * pattern is used to detect cycles. In fact, the pattern is used in two
5319 * dimensions, vertical (indicated in the code by the variable names 'hare'
5320 * and 'tortoise') and horizontal ('rabbit' and 'turtle'). In both
5321 * dimensions, the hare/rabbit will take two steps when the tortoise/turtle
5322 * takes one.
5323 *
5324 * The vertical dimension corresponds to recursive calls to function
5325 * copy_tree: This happens when descending into vector elements, into cars of
5326 * lists and into the cdr of an improper list. In this dimension, the
5327 * tortoise follows the hare by using the processor stack: Every stack frame
5328 * will hold an instance of struct t_trace. These instances are connected in
5329 * a way that represents the trace of the hare, which thus can be followed by
5330 * the tortoise. The tortoise will always point to struct t_trace instances
5331 * relating to SCM objects that have already been copied. Thus, a cycle is
5332 * detected if the tortoise and the hare point to the same object,
5333 *
5334 * The horizontal dimension is within one execution of copy_tree, when the
5335 * function cdr's along the pairs of a list. This is the standard
5336 * hare-and-tortoise implementation, found several times in guile. */
5337
5338 struct t_trace {
5339 struct t_trace *trace; // These pointers form a trace along the stack.
5340 SCM obj; // The object handled at the respective stack frame.
5341 };
5342
5343 static SCM
5344 copy_tree (
5345 struct t_trace *const hare,
5346 struct t_trace *tortoise,
5347 unsigned int tortoise_delay )
5348 {
5349 if (!SCM_CONSP (hare->obj) && !SCM_VECTORP (hare->obj))
5350 {
5351 return hare->obj;
5352 }
5353 else
5354 {
5355 /* Prepare the trace along the stack. */
5356 struct t_trace new_hare;
5357 hare->trace = &new_hare;
5358
5359 /* The tortoise will make its step after the delay has elapsed. Note
5360 * that in contrast to the typical hare-and-tortoise pattern, the step
5361 * of the tortoise happens before the hare takes its steps. This is, in
5362 * principle, no problem, except for the start of the algorithm: Then,
5363 * it has to be made sure that the hare actually gets its advantage of
5364 * two steps. */
5365 if (tortoise_delay == 0)
5366 {
5367 tortoise_delay = 1;
5368 tortoise = tortoise->trace;
5369 ASSERT_SYNTAX (!SCM_EQ_P (hare->obj, tortoise->obj),
5370 s_bad_expression, hare->obj);
5371 }
5372 else
5373 {
5374 --tortoise_delay;
5375 }
5376
5377 if (SCM_VECTORP (hare->obj))
5378 {
5379 const unsigned long int length = SCM_VECTOR_LENGTH (hare->obj);
5380 const SCM new_vector = scm_c_make_vector (length, SCM_UNSPECIFIED);
5381
5382 /* Each vector element is copied by recursing into copy_tree, having
5383 * the tortoise follow the hare into the depths of the stack. */
5384 unsigned long int i;
5385 for (i = 0; i < length; ++i)
5386 {
5387 SCM new_element;
5388 new_hare.obj = SCM_VECTOR_REF (hare->obj, i);
5389 new_element = copy_tree (&new_hare, tortoise, tortoise_delay);
5390 SCM_VECTOR_SET (new_vector, i, new_element);
5391 }
5392
5393 return new_vector;
5394 }
5395 else // SCM_CONSP (hare->obj)
5396 {
5397 SCM result;
5398 SCM tail;
5399
5400 SCM rabbit = hare->obj;
5401 SCM turtle = hare->obj;
5402
5403 SCM copy;
5404
5405 /* The first pair of the list is treated specially, in order to
5406 * preserve a potential source code position. */
5407 result = tail = scm_cons_source (rabbit, SCM_EOL, SCM_EOL);
5408 new_hare.obj = SCM_CAR (rabbit);
5409 copy = copy_tree (&new_hare, tortoise, tortoise_delay);
5410 SCM_SETCAR (tail, copy);
5411
5412 /* The remaining pairs of the list are copied by, horizontally,
5413 * having the turtle follow the rabbit, and, vertically, having the
5414 * tortoise follow the hare into the depths of the stack. */
5415 rabbit = SCM_CDR (rabbit);
5416 while (SCM_CONSP (rabbit))
5417 {
5418 new_hare.obj = SCM_CAR (rabbit);
5419 copy = copy_tree (&new_hare, tortoise, tortoise_delay);
5420 SCM_SETCDR (tail, scm_cons (copy, SCM_UNDEFINED));
5421 tail = SCM_CDR (tail);
5422
5423 rabbit = SCM_CDR (rabbit);
5424 if (SCM_CONSP (rabbit))
5425 {
5426 new_hare.obj = SCM_CAR (rabbit);
5427 copy = copy_tree (&new_hare, tortoise, tortoise_delay);
5428 SCM_SETCDR (tail, scm_cons (copy, SCM_UNDEFINED));
5429 tail = SCM_CDR (tail);
5430 rabbit = SCM_CDR (rabbit);
5431
5432 turtle = SCM_CDR (turtle);
5433 ASSERT_SYNTAX (!SCM_EQ_P (rabbit, turtle),
5434 s_bad_expression, rabbit);
5435 }
5436 }
5437
5438 /* We have to recurse into copy_tree again for the last cdr, in
5439 * order to handle the situation that it holds a vector. */
5440 new_hare.obj = rabbit;
5441 copy = copy_tree (&new_hare, tortoise, tortoise_delay);
5442 SCM_SETCDR (tail, copy);
5443
5444 return result;
5445 }
5446 }
5447 }
5448
5449 SCM_DEFINE (scm_copy_tree, "copy-tree", 1, 0, 0,
5450 (SCM obj),
5451 "Recursively copy the data tree that is bound to @var{obj}, and return a\n"
5452 "the new data structure. @code{copy-tree} recurses down the\n"
5453 "contents of both pairs and vectors (since both cons cells and vector\n"
5454 "cells may point to arbitrary objects), and stops recursing when it hits\n"
5455 "any other object.")
5456 #define FUNC_NAME s_scm_copy_tree
5457 {
5458 /* Prepare the trace along the stack. */
5459 struct t_trace trace;
5460 trace.obj = obj;
5461
5462 /* In function copy_tree, if the tortoise makes its step, it will do this
5463 * before the hare has the chance to move. Thus, we have to make sure that
5464 * the very first step of the tortoise will not happen after the hare has
5465 * really made two steps. This is achieved by passing '2' as the initial
5466 * delay for the tortoise. NOTE: Since cycles are unlikely, giving the hare
5467 * a bigger advantage may improve performance slightly. */
5468 return copy_tree (&trace, &trace, 2);
5469 }
5470 #undef FUNC_NAME
5471
5472
5473 /* We have three levels of EVAL here:
5474
5475 - scm_i_eval (exp, env)
5476
5477 evaluates EXP in environment ENV. ENV is a lexical environment
5478 structure as used by the actual tree code evaluator. When ENV is
5479 a top-level environment, then changes to the current module are
5480 tracked by updating ENV so that it continues to be in sync with
5481 the current module.
5482
5483 - scm_primitive_eval (exp)
5484
5485 evaluates EXP in the top-level environment as determined by the
5486 current module. This is done by constructing a suitable
5487 environment and calling scm_i_eval. Thus, changes to the
5488 top-level module are tracked normally.
5489
5490 - scm_eval (exp, mod)
5491
5492 evaluates EXP while MOD is the current module. This is done by
5493 setting the current module to MOD, invoking scm_primitive_eval on
5494 EXP, and then restoring the current module to the value it had
5495 previously. That is, while EXP is evaluated, changes to the
5496 current module are tracked, but these changes do not persist when
5497 scm_eval returns.
5498
5499 For each level of evals, there are two variants, distinguished by a
5500 _x suffix: the ordinary variant does not modify EXP while the _x
5501 variant can destructively modify EXP into something completely
5502 unintelligible. A Scheme data structure passed as EXP to one of the
5503 _x variants should not ever be used again for anything. So when in
5504 doubt, use the ordinary variant.
5505
5506 */
5507
5508 SCM
5509 scm_i_eval_x (SCM exp, SCM env)
5510 {
5511 if (SCM_SYMBOLP (exp))
5512 return *scm_lookupcar (scm_cons (exp, SCM_UNDEFINED), env, 1);
5513 else
5514 return SCM_XEVAL (exp, env);
5515 }
5516
5517 SCM
5518 scm_i_eval (SCM exp, SCM env)
5519 {
5520 exp = scm_copy_tree (exp);
5521 if (SCM_SYMBOLP (exp))
5522 return *scm_lookupcar (scm_cons (exp, SCM_UNDEFINED), env, 1);
5523 else
5524 return SCM_XEVAL (exp, env);
5525 }
5526
5527 SCM
5528 scm_primitive_eval_x (SCM exp)
5529 {
5530 SCM env;
5531 SCM transformer = scm_current_module_transformer ();
5532 if (SCM_NIMP (transformer))
5533 exp = scm_call_1 (transformer, exp);
5534 env = scm_top_level_env (scm_current_module_lookup_closure ());
5535 return scm_i_eval_x (exp, env);
5536 }
5537
5538 SCM_DEFINE (scm_primitive_eval, "primitive-eval", 1, 0, 0,
5539 (SCM exp),
5540 "Evaluate @var{exp} in the top-level environment specified by\n"
5541 "the current module.")
5542 #define FUNC_NAME s_scm_primitive_eval
5543 {
5544 SCM env;
5545 SCM transformer = scm_current_module_transformer ();
5546 if (SCM_NIMP (transformer))
5547 exp = scm_call_1 (transformer, exp);
5548 env = scm_top_level_env (scm_current_module_lookup_closure ());
5549 return scm_i_eval (exp, env);
5550 }
5551 #undef FUNC_NAME
5552
5553
5554 /* Eval does not take the second arg optionally. This is intentional
5555 * in order to be R5RS compatible, and to prepare for the new module
5556 * system, where we would like to make the choice of evaluation
5557 * environment explicit. */
5558
5559 static void
5560 change_environment (void *data)
5561 {
5562 SCM pair = SCM_PACK (data);
5563 SCM new_module = SCM_CAR (pair);
5564 SCM old_module = scm_current_module ();
5565 SCM_SETCDR (pair, old_module);
5566 scm_set_current_module (new_module);
5567 }
5568
5569 static void
5570 restore_environment (void *data)
5571 {
5572 SCM pair = SCM_PACK (data);
5573 SCM old_module = SCM_CDR (pair);
5574 SCM new_module = scm_current_module ();
5575 SCM_SETCAR (pair, new_module);
5576 scm_set_current_module (old_module);
5577 }
5578
5579 static SCM
5580 inner_eval_x (void *data)
5581 {
5582 return scm_primitive_eval_x (SCM_PACK(data));
5583 }
5584
5585 SCM
5586 scm_eval_x (SCM exp, SCM module)
5587 #define FUNC_NAME "eval!"
5588 {
5589 SCM_VALIDATE_MODULE (2, module);
5590
5591 return scm_internal_dynamic_wind
5592 (change_environment, inner_eval_x, restore_environment,
5593 (void *) SCM_UNPACK (exp),
5594 (void *) SCM_UNPACK (scm_cons (module, SCM_BOOL_F)));
5595 }
5596 #undef FUNC_NAME
5597
5598 static SCM
5599 inner_eval (void *data)
5600 {
5601 return scm_primitive_eval (SCM_PACK(data));
5602 }
5603
5604 SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
5605 (SCM exp, SCM module),
5606 "Evaluate @var{exp}, a list representing a Scheme expression,\n"
5607 "in the top-level environment specified by @var{module}.\n"
5608 "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
5609 "@var{module} is made the current module. The current module\n"
5610 "is reset to its previous value when @var{eval} returns.")
5611 #define FUNC_NAME s_scm_eval
5612 {
5613 SCM_VALIDATE_MODULE (2, module);
5614
5615 return scm_internal_dynamic_wind
5616 (change_environment, inner_eval, restore_environment,
5617 (void *) SCM_UNPACK (exp),
5618 (void *) SCM_UNPACK (scm_cons (module, SCM_BOOL_F)));
5619 }
5620 #undef FUNC_NAME
5621
5622
5623 /* At this point, deval and scm_dapply are generated.
5624 */
5625
5626 #define DEVAL
5627 #include "eval.c"
5628
5629
5630 #if (SCM_ENABLE_DEPRECATED == 1)
5631
5632 /* Deprecated in guile 1.7.0 on 2004-03-29. */
5633 SCM scm_ceval (SCM x, SCM env)
5634 {
5635 if (SCM_CONSP (x))
5636 return ceval (x, env);
5637 else if (SCM_SYMBOLP (x))
5638 return *scm_lookupcar (scm_cons (x, SCM_UNDEFINED), env, 1);
5639 else
5640 return SCM_XEVAL (x, env);
5641 }
5642
5643 /* Deprecated in guile 1.7.0 on 2004-03-29. */
5644 SCM scm_deval (SCM x, SCM env)
5645 {
5646 if (SCM_CONSP (x))
5647 return deval (x, env);
5648 else if (SCM_SYMBOLP (x))
5649 return *scm_lookupcar (scm_cons (x, SCM_UNDEFINED), env, 1);
5650 else
5651 return SCM_XEVAL (x, env);
5652 }
5653
5654 static SCM
5655 dispatching_eval (SCM x, SCM env)
5656 {
5657 if (scm_debug_mode_p)
5658 return scm_deval (x, env);
5659 else
5660 return scm_ceval (x, env);
5661 }
5662
5663 /* Deprecated in guile 1.7.0 on 2004-03-29. */
5664 SCM (*scm_ceval_ptr) (SCM x, SCM env) = dispatching_eval;
5665
5666 #endif
5667
5668
5669 void
5670 scm_init_eval ()
5671 {
5672 scm_init_opts (scm_evaluator_traps,
5673 scm_evaluator_trap_table,
5674 SCM_N_EVALUATOR_TRAPS);
5675 scm_init_opts (scm_eval_options_interface,
5676 scm_eval_opts,
5677 SCM_N_EVAL_OPTIONS);
5678
5679 scm_tc16_promise = scm_make_smob_type ("promise", 0);
5680 scm_set_smob_mark (scm_tc16_promise, scm_markcdr);
5681 scm_set_smob_free (scm_tc16_promise, promise_free);
5682 scm_set_smob_print (scm_tc16_promise, promise_print);
5683
5684 undefineds = scm_list_1 (SCM_UNDEFINED);
5685 SCM_SETCDR (undefineds, undefineds);
5686 scm_permanent_object (undefineds);
5687
5688 scm_listofnull = scm_list_1 (SCM_EOL);
5689
5690 f_apply = scm_c_define_subr ("apply", scm_tc7_lsubr_2, scm_apply);
5691 scm_permanent_object (f_apply);
5692
5693 #include "libguile/eval.x"
5694
5695 scm_add_feature ("delay");
5696 }
5697
5698 #endif /* !DEVAL */
5699
5700 /*
5701 Local Variables:
5702 c-file-style: "gnu"
5703 End:
5704 */