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