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