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