* _scm.h (SCM_PROC): Extraneous semicolon (outside functions)
[bpt/guile.git] / libguile / eval.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
6dbd0af5
MD
43/* This file is read twice in order to produce debugging versions of
44 * scm_ceval and scm_apply. These functions, scm_deval and
45 * scm_dapply, are produced when we define the preprocessor macro
46 * DEVAL. The file is divided into sections which are treated
47 * differently with respect to DEVAL. The heads of these sections are
48 * marked with the string "SECTION:".
49 */
50
51
52/* SECTION: This code is compiled once.
0f2d19dd
JB
53 */
54
55#ifndef DEVAL
56
57#include <stdio.h>
58#include "_scm.h"
20e6290e
JB
59#include "debug.h"
60#include "append.h"
61#include "alist.h"
62#include "sequences.h"
63#include "eq.h"
64#include "continuations.h"
65#include "throw.h"
66#include "smob.h"
67#include "markers.h"
68#include "procprop.h"
69#include "hashtab.h"
0f2d19dd 70
20e6290e
JB
71#ifdef DEBUG_EXTENSIONS
72#include "debug.h"
73#endif /* DEBUG_EXTENSIONS */
74
75#include "srcprop.h"
76#include "stackchk.h"
77
78#include "eval.h"
0f2d19dd
JB
79\f
80
6dbd0af5
MD
81/* The evaluator contains a plethora of EVAL symbols.
82 * This is an attempt at explanation.
83 *
84 * The following macros should be used in code which is read twice
85 * (where the choice of evaluator is hard soldered):
86 *
87 * SCM_CEVAL is the symbol used within one evaluator to call itself.
88 * Originally, it is defined to scm_ceval, but is redefined to
89 * scm_deval during the second pass.
90 *
91 * SIDEVAL corresponds to SCM_CEVAL, but is used in situations where
92 * only side effects of expressions matter. All immediates are
93 * ignored.
94 *
95 * EVALIM is used when it is known that the expression is an
96 * immediate. (This macro never calls an evaluator.)
97 *
98 * EVALCAR evaluates the car of an expression.
99 *
100 * EVALCELLCAR is like EVALCAR, but is used when it is known that the
101 * car is a lisp cell.
102 *
103 * The following macros should be used in code which is read once
104 * (where the choice of evaluator is dynamic):
105 *
106 * XEVAL takes care of immediates without calling an evaluator. It
107 * then calls scm_ceval *or* scm_deval, depending on the debugging
108 * mode.
109 *
110 * XEVALCAR corresponds to EVALCAR, but uses scm_ceval *or* scm_deval
111 * depending on the debugging mode.
112 *
113 * The main motivation for keeping this plethora is efficiency
114 * together with maintainability (=> locality of code).
115 */
116
0f2d19dd
JB
117#define EVALCELLCAR(x, env) (SCM_SYMBOLP (SCM_CAR(x)) \
118 ? *scm_lookupcar(x, env) \
119 : SCM_CEVAL(SCM_CAR(x), env))
120
121#ifdef MEMOIZE_LOCALS
6dbd0af5 122#define EVALIM(x, env) (SCM_ILOCP(x)?*scm_ilookup((x), env):x)
0f2d19dd 123#else
6dbd0af5 124#define EVALIM(x, env) x
0f2d19dd
JB
125#endif
126#define EVALCAR(x, env) (SCM_NCELLP(SCM_CAR(x))\
127 ? (SCM_IMP(SCM_CAR(x)) \
6dbd0af5 128 ? EVALIM(SCM_CAR(x), env) \
0f2d19dd
JB
129 : SCM_GLOC_VAL(SCM_CAR(x))) \
130 : EVALCELLCAR(x, env))
131#ifdef DEBUG_EXTENSIONS
132#define XEVALCAR(x, env) (SCM_NCELLP(SCM_CAR(x)) \
133 ? (SCM_IMP(SCM_CAR(x)) \
6dbd0af5 134 ? EVALIM(SCM_CAR(x), env) \
0f2d19dd
JB
135 : SCM_GLOC_VAL(SCM_CAR(x))) \
136 : (SCM_SYMBOLP(SCM_CAR(x)) \
137 ? *scm_lookupcar(x, env) \
138 : (*scm_ceval_ptr) (SCM_CAR(x), env)))
139#else
140#define XEVALCAR(x, env) EVALCAR(x, env)
141#endif
142
143#define EXTEND_SCM_ENV SCM_EXTEND_SCM_ENV
144
145#ifdef MEMOIZE_LOCALS
146#ifdef __STDC__
147SCM *
148scm_ilookup (SCM iloc, SCM env)
149#else
150SCM *
151scm_ilookup (iloc, env)
152 SCM iloc;
153 SCM env;
154#endif
155{
156 register int ir = SCM_IFRAME (iloc);
157 register SCM er = env;
158 for (; 0 != ir; --ir)
159 er = SCM_CDR (er);
160 er = SCM_CAR (er);
161 for (ir = SCM_IDIST (iloc); 0 != ir; --ir)
162 er = SCM_CDR (er);
163 if (SCM_ICDRP (iloc))
164 return &SCM_CDR (er);
165 return &SCM_CAR (SCM_CDR (er));
166}
167#endif
168
169#ifdef __STDC__
170SCM *
171scm_lookupcar (SCM vloc, SCM genv)
172#else
173SCM *
174scm_lookupcar (vloc, genv)
175 SCM vloc;
176 SCM genv;
177#endif
178{
179 SCM env = genv;
180 register SCM *al, fl, var = SCM_CAR (vloc);
181#ifdef MEMOIZE_LOCALS
182 register SCM iloc = SCM_ILOC00;
183#endif
184 for (; SCM_NIMP (env); env = SCM_CDR (env))
185 {
186 if (SCM_BOOL_T == scm_procedure_p (SCM_CAR (env)))
187 break;
188 al = &SCM_CAR (env);
189 for (fl = SCM_CAR (*al); SCM_NIMP (fl); fl = SCM_CDR (fl))
190 {
191 if (SCM_NCONSP (fl))
192 if (fl == var)
193 {
194#ifdef MEMOIZE_LOCALS
195 SCM_CAR (vloc) = iloc + SCM_ICDR;
196#endif
197 return &SCM_CDR (*al);
198 }
199 else
200 break;
201 al = &SCM_CDR (*al);
202 if (SCM_CAR (fl) == var)
203 {
204#ifdef MEMOIZE_LOCALS
205#ifndef RECKLESS /* letrec inits to SCM_UNDEFINED */
206 if (SCM_UNBNDP (SCM_CAR (*al)))
207 {
208 env = SCM_EOL;
209 goto errout;
210 }
211#endif
212 SCM_CAR (vloc) = iloc;
213#endif
214 return &SCM_CAR (*al);
215 }
216#ifdef MEMOIZE_LOCALS
217 iloc += SCM_IDINC;
218#endif
219 }
220#ifdef MEMOIZE_LOCALS
221 iloc = (~SCM_IDSTMSK) & (iloc + SCM_IFRINC);
222#endif
223 }
224 {
225 SCM top_thunk, vcell;
226 if (SCM_NIMP(env))
227 {
228 top_thunk = SCM_CAR(env); /* env now refers to a top level env thunk */
229 env = SCM_CDR (env);
230 }
231 else
232 top_thunk = SCM_BOOL_F;
233 vcell = scm_sym2vcell (var, top_thunk, SCM_BOOL_F);
234 if (vcell == SCM_BOOL_F)
235 goto errout;
236 else
237 var = vcell;
238 }
239#ifndef RECKLESS
240 if (SCM_NNULLP (env) || SCM_UNBNDP (SCM_CDR (var)))
241 {
242 var = SCM_CAR (var);
243 errout:
244 scm_everr (vloc, genv, var,
245 (SCM_NULLP (env)
246 ? "unbound variable: "
247 : "damaged environment"),
248 "");
249 }
250#endif
251 SCM_CAR (vloc) = var + 1;
252 /* Except wait...what if the var is not a vcell,
253 * but syntax or something....
254 */
255 return &SCM_CDR (var);
256}
257
258#define unmemocar scm_unmemocar
259#ifdef __STDC__
260SCM
261scm_unmemocar (SCM form, SCM env)
262#else
263SCM
264scm_unmemocar (form, env)
265 SCM form;
266 SCM env;
267#endif
268{
6dbd0af5 269#ifdef DEBUG_EXTENSIONS
0f2d19dd 270 register int ir;
6dbd0af5 271#endif
0f2d19dd
JB
272 SCM c;
273
274 if (SCM_IMP (form))
275 return form;
276 c = SCM_CAR (form);
277 if (1 == (c & 7))
278 SCM_CAR (form) = SCM_CAR (c - 1);
279#ifdef MEMOIZE_LOCALS
6dbd0af5 280#ifdef DEBUG_EXTENSIONS
0f2d19dd
JB
281 else if (SCM_ILOCP (c))
282 {
283 for (ir = SCM_IFRAME (c); ir != 0; --ir)
284 env = SCM_CDR (env);
285 env = SCM_CAR (SCM_CAR (env));
286 for (ir = SCM_IDIST (c); ir != 0; --ir)
287 env = SCM_CDR (env);
288 SCM_CAR (form) = SCM_ICDRP (c) ? env : SCM_CAR (env);
289 }
6dbd0af5 290#endif
0f2d19dd
JB
291#endif
292 return form;
293}
294
295#ifdef __STDC__
296SCM
297scm_eval_car (SCM pair, SCM env)
298#else
299SCM
300scm_eval_car (pair, env)
301 SCM pair;
302 SCM env;
303#endif
304{
6dbd0af5 305 return XEVALCAR (pair, env);
0f2d19dd
JB
306}
307
308\f
309/*
310 * The following rewrite expressions and
311 * some memoized forms have different syntax
312 */
313
314static char s_expression[] = "missing or extra expression";
315static char s_test[] = "bad test";
316static char s_body[] = "bad body";
317static char s_bindings[] = "bad bindings";
318static char s_variable[] = "bad variable";
319static char s_clauses[] = "bad or missing clauses";
320static char s_formals[] = "bad formals";
321#define ASSYNT(_cond, _arg, _pos, _subr) if(!(_cond))scm_wta(_arg, (char *)_pos, _subr);
322
323SCM scm_i_dot, scm_i_quote, scm_i_quasiquote, scm_i_lambda, scm_i_let,
324 scm_i_arrow, scm_i_else, scm_i_unquote, scm_i_uq_splicing, scm_i_apply;
325SCM scm_i_name;
6dbd0af5
MD
326SCM scm_i_define, scm_i_and, scm_i_begin, scm_i_case, scm_i_cond,
327 scm_i_do, scm_i_if, scm_i_let, scm_i_letrec, scm_i_letstar,
328 scm_i_or, scm_i_set, scm_i_atapply, scm_i_atcall_cc;
0f2d19dd
JB
329static char s_quasiquote[] = "quasiquote";
330static char s_delay[] = "delay";
6dbd0af5
MD
331static char s_undefine[] = "undefine";
332#ifdef DEBUG_EXTENSIONS
333SCM scm_i_enter_frame, scm_i_apply_frame, scm_i_exit_frame;
334SCM scm_i_trace;
335#endif
0f2d19dd
JB
336
337#define ASRTSYNTAX(cond_, msg_) if(!(cond_))scm_wta(xorig, (msg_), what);
338
339
340#ifdef __STDC__
341static void
342bodycheck (SCM xorig, SCM *bodyloc, char *what)
343#else
344static void
345bodycheck (xorig, bodyloc, what)
346 SCM xorig;
347 SCM *bodyloc;
348 char *what;
349#endif
350{
351 ASRTSYNTAX (scm_ilength (*bodyloc) >= 1, s_expression);
352}
353
354
355#ifdef __STDC__
356SCM
357scm_m_quote (SCM xorig, SCM env)
358#else
359SCM
360scm_m_quote (xorig, env)
361 SCM xorig;
362 SCM env;
363#endif
364{
365 ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, xorig, s_expression, "quote");
366 return scm_cons (SCM_IM_QUOTE, SCM_CDR (xorig));
367}
368
369
370#ifdef __STDC__
371SCM
372scm_m_begin (SCM xorig, SCM env)
373#else
374SCM
375scm_m_begin (xorig, env)
376 SCM xorig;
377 SCM env;
378#endif
379{
380 ASSYNT (scm_ilength (SCM_CDR (xorig)) >= 1, xorig, s_expression, "begin");
381 return scm_cons (SCM_IM_BEGIN, SCM_CDR (xorig));
382}
383
384
385#ifdef __STDC__
386SCM
387scm_m_if (SCM xorig, SCM env)
388#else
389SCM
390scm_m_if (xorig, env)
391 SCM xorig;
392 SCM env;
393#endif
394{
395 int len = scm_ilength (SCM_CDR (xorig));
396 ASSYNT (len >= 2 && len <= 3, xorig, s_expression, "if");
397 return scm_cons (SCM_IM_IF, SCM_CDR (xorig));
398}
399
400
401#ifdef __STDC__
402SCM
403scm_m_set (SCM xorig, SCM env)
404#else
405SCM
406scm_m_set (xorig, env)
407 SCM xorig;
408 SCM env;
409#endif
410{
6dbd0af5
MD
411 SCM x = SCM_CDR (xorig);
412 ASSYNT (2 == scm_ilength (x), xorig, s_expression, "set!");
413 ASSYNT (SCM_NIMP (SCM_CAR (x)) && SCM_SYMBOLP (SCM_CAR (x)),
414 xorig, s_variable, "set!");
0f2d19dd
JB
415 return scm_cons (SCM_IM_SET, x);
416}
417
418
419#if 0
420#ifdef __STDC__
421SCM
422scm_m_vref (SCM xorig, SCM env)
423#else
424SCM
425scm_m_vref (xorig, env)
426 SCM xorig;
427 SCM env;
428#endif
429{
430 SCM x = SCM_CDR (xorig);
431 ASSYNT (1 == scm_ilength (x), xorig, s_expression, s_vref);
432 if (SCM_NIMP(x) && UDSCM_VARIABLEP (SCM_CAR (x)))
433 {
434 scm_everr (SCM_UNDEFINED, env, SCM_CAR(SCM_CDR(x)), s_variable,
435 "global variable reference");
436 }
437 ASSYNT (SCM_NIMP(x) && DEFSCM_VARIABLEP (SCM_CAR (x)),
438 xorig, s_variable, s_vref);
439 return
440 return scm_cons (IM_VREF, x);
441}
442
443
444#ifdef __STDC__
445SCM
446scm_m_vset (SCM xorig, SCM env)
447#else
448SCM
449scm_m_vset (xorig, env)
450 SCM xorig;
451 SCM env;
452#endif
453{
454 SCM x = SCM_CDR (xorig);
455 ASSYNT (3 == scm_ilength (x), xorig, s_expression, s_vset);
456 ASSYNT (( DEFSCM_VARIABLEP (SCM_CAR (x))
457 || UDSCM_VARIABLEP (SCM_CAR (x))),
458 xorig, s_variable, s_vset);
459 return scm_cons (IM_VSET, x);
460}
461#endif
462
463
464#ifdef __STDC__
465SCM
466scm_m_and (SCM xorig, SCM env)
467#else
468SCM
469scm_m_and (xorig, env)
470 SCM xorig;
471 SCM env;
472#endif
473{
474 int len = scm_ilength (SCM_CDR (xorig));
475 ASSYNT (len >= 0, xorig, s_test, "and");
476 if (len >= 1)
477 return scm_cons (SCM_IM_AND, SCM_CDR (xorig));
478 else
479 return SCM_BOOL_T;
480}
481
482
483#ifdef __STDC__
484SCM
485scm_m_or (SCM xorig, SCM env)
486#else
487SCM
488scm_m_or (xorig, env)
489 SCM xorig;
490 SCM env;
491#endif
492{
493 int len = scm_ilength (SCM_CDR (xorig));
494 ASSYNT (len >= 0, xorig, s_test, "or");
495 if (len >= 1)
496 return scm_cons (SCM_IM_OR, SCM_CDR (xorig));
497 else
498 return SCM_BOOL_F;
499}
500
501
502#ifdef __STDC__
503SCM
504scm_m_case (SCM xorig, SCM env)
505#else
506SCM
507scm_m_case (xorig, env)
508 SCM xorig;
509 SCM env;
510#endif
511{
512 SCM proc, x = SCM_CDR (xorig);
513 ASSYNT (scm_ilength (x) >= 2, xorig, s_clauses, "case");
514 while (SCM_NIMP (x = SCM_CDR (x)))
515 {
516 proc = SCM_CAR (x);
517 ASSYNT (scm_ilength (proc) >= 2, xorig, s_clauses, "case");
518 ASSYNT (scm_ilength (SCM_CAR (proc)) >= 0 || scm_i_else == SCM_CAR (proc),
519 xorig, s_clauses, "case");
520 }
521 return scm_cons (SCM_IM_CASE, SCM_CDR (xorig));
522}
523
524
525#ifdef __STDC__
526SCM
527scm_m_cond (SCM xorig, SCM env)
528#else
529SCM
530scm_m_cond (xorig, env)
531 SCM xorig;
532 SCM env;
533#endif
534{
535 SCM arg1, x = SCM_CDR (xorig);
536 int len = scm_ilength (x);
537 ASSYNT (len >= 1, xorig, s_clauses, "cond");
538 while (SCM_NIMP (x))
539 {
540 arg1 = SCM_CAR (x);
541 len = scm_ilength (arg1);
542 ASSYNT (len >= 1, xorig, s_clauses, "cond");
543 if (scm_i_else == SCM_CAR (arg1))
544 {
545 ASSYNT (SCM_NULLP (SCM_CDR (x)) && len >= 2, xorig, "bad ELSE clause", "cond");
546 SCM_CAR (arg1) = SCM_BOOL_T;
547 }
548 if (len >= 2 && scm_i_arrow == SCM_CAR (SCM_CDR (arg1)))
549 ASSYNT (3 == len && SCM_NIMP (SCM_CAR (SCM_CDR (SCM_CDR (arg1)))),
550 xorig, "bad recipient", "cond");
551 x = SCM_CDR (x);
552 }
553 return scm_cons (SCM_IM_COND, SCM_CDR (xorig));
554}
555
556
557#ifdef __STDC__
558SCM
559scm_m_lambda (SCM xorig, SCM env)
560#else
561SCM
562scm_m_lambda (xorig, env)
563 SCM xorig;
564 SCM env;
565#endif
566{
567 SCM proc, x = SCM_CDR (xorig);
568 if (scm_ilength (x) < 2)
569 goto badforms;
570 proc = SCM_CAR (x);
571 if SCM_NULLP
572 (proc) goto memlambda;
573 if SCM_IMP
574 (proc) goto badforms;
575 if SCM_SYMBOLP
576 (proc) goto memlambda;
577 if SCM_NCONSP
578 (proc) goto badforms;
579 while SCM_NIMP
580 (proc)
581 {
582 if SCM_NCONSP
583 (proc)
584 if (!SCM_SYMBOLP (proc))
585 goto badforms;
586 else
587 goto memlambda;
588 if (!(SCM_NIMP (SCM_CAR (proc)) && SCM_SYMBOLP (SCM_CAR (proc))))
589 goto badforms;
590 proc = SCM_CDR (proc);
591 }
592 if SCM_NNULLP
593 (proc)
594 badforms:scm_wta (xorig, s_formals, "lambda");
595memlambda:
596 bodycheck (xorig, &SCM_CDR (x), "lambda");
597 return scm_cons (SCM_IM_LAMBDA, SCM_CDR (xorig));
598}
599
600
601#ifdef __STDC__
602SCM
603scm_m_letstar (SCM xorig, SCM env)
604#else
605SCM
606scm_m_letstar (xorig, env)
607 SCM xorig;
608 SCM env;
609#endif
610{
611 SCM x = SCM_CDR (xorig), arg1, proc, vars = SCM_EOL, *varloc = &vars;
612 int len = scm_ilength (x);
613 ASSYNT (len >= 2, xorig, s_body, "let*");
614 proc = SCM_CAR (x);
615 ASSYNT (scm_ilength (proc) >= 0, xorig, s_bindings, "let*");
6dbd0af5 616 while SCM_NIMP (proc)
0f2d19dd
JB
617 {
618 arg1 = SCM_CAR (proc);
619 ASSYNT (2 == scm_ilength (arg1), xorig, s_bindings, "let*");
620 ASSYNT (SCM_NIMP (SCM_CAR (arg1)) && SCM_SYMBOLP (SCM_CAR (arg1)), xorig, s_variable, "let*");
621 *varloc = scm_cons2 (SCM_CAR (arg1), SCM_CAR (SCM_CDR (arg1)), SCM_EOL);
622 varloc = &SCM_CDR (SCM_CDR (*varloc));
623 proc = SCM_CDR (proc);
624 }
625 x = scm_cons (vars, SCM_CDR (x));
626 bodycheck (xorig, &SCM_CDR (x), "let*");
627 return scm_cons (SCM_IM_LETSTAR, x);
628}
629
630/* DO gets the most radically altered syntax
631 (do ((<var1> <init1> <step1>)
632 (<var2> <init2>)
633 ... )
634 (<test> <return>)
635 <body>)
636 ;; becomes
637 (do_mem (varn ... var2 var1)
638 (<init1> <init2> ... <initn>)
639 (<test> <return>)
640 (<body>)
641 <step1> <step2> ... <stepn>) ;; missing steps replaced by var
642 */
643
644
645#ifdef __STDC__
646SCM
647scm_m_do (SCM xorig, SCM env)
648#else
649SCM
650scm_m_do (xorig, env)
651 SCM xorig;
652 SCM env;
653#endif
654{
655 SCM x = SCM_CDR (xorig), arg1, proc;
656 SCM vars = SCM_EOL, inits = SCM_EOL, steps = SCM_EOL;
657 SCM *initloc = &inits, *steploc = &steps;
658 int len = scm_ilength (x);
659 ASSYNT (len >= 2, xorig, s_test, "do");
660 proc = SCM_CAR (x);
661 ASSYNT (scm_ilength (proc) >= 0, xorig, s_bindings, "do");
662 while SCM_NIMP
663 (proc)
664 {
665 arg1 = SCM_CAR (proc);
666 len = scm_ilength (arg1);
667 ASSYNT (2 == len || 3 == len, xorig, s_bindings, "do");
668 ASSYNT (SCM_NIMP (SCM_CAR (arg1)) && SCM_SYMBOLP (SCM_CAR (arg1)), xorig, s_variable, "do");
669 /* vars reversed here, inits and steps reversed at evaluation */
670 vars = scm_cons (SCM_CAR (arg1), vars); /* variable */
671 arg1 = SCM_CDR (arg1);
672 *initloc = scm_cons (SCM_CAR (arg1), SCM_EOL); /* init */
673 initloc = &SCM_CDR (*initloc);
674 arg1 = SCM_CDR (arg1);
675 *steploc = scm_cons (SCM_IMP (arg1) ? SCM_CAR (vars) : SCM_CAR (arg1), SCM_EOL); /* step */
676 steploc = &SCM_CDR (*steploc);
677 proc = SCM_CDR (proc);
678 }
679 x = SCM_CDR (x);
680 ASSYNT (scm_ilength (SCM_CAR (x)) >= 1, xorig, s_test, "do");
681 x = scm_cons2 (SCM_CAR (x), SCM_CDR (x), steps);
682 x = scm_cons2 (vars, inits, x);
683 bodycheck (xorig, &SCM_CAR (SCM_CDR (SCM_CDR (x))), "do");
684 return scm_cons (SCM_IM_DO, x);
685}
686
6dbd0af5
MD
687/* evalcar is small version of inline EVALCAR when we don't care about
688 * speed
689 */
690#define evalcar scm_eval_car
0f2d19dd
JB
691
692#ifdef __STDC__
693static SCM
694iqq (SCM form, SCM env, int depth)
695#else
696static SCM
697iqq (form, env, depth)
698 SCM form;
699 SCM env;
700 int depth;
701#endif
702{
703 SCM tmp;
704 int edepth = depth;
705 if SCM_IMP
706 (form) return form;
707 if (SCM_VECTORP (form))
708 {
709 long i = SCM_LENGTH (form);
710 SCM *data = SCM_VELTS (form);
711 tmp = SCM_EOL;
712 for (; --i >= 0;)
713 tmp = scm_cons (data[i], tmp);
714 return scm_vector (iqq (tmp, env, depth));
715 }
716 if SCM_NCONSP
717 (form) return form;
718 tmp = SCM_CAR (form);
719 if (scm_i_quasiquote == tmp)
720 {
721 depth++;
722 goto label;
723 }
724 if (scm_i_unquote == tmp)
725 {
726 --depth;
727 label:
728 form = SCM_CDR (form);
729 /* !!! might need a check here to be sure that form isn't a struct. */
730 SCM_ASSERT (SCM_NIMP (form) && SCM_ECONSP (form) && SCM_NULLP (SCM_CDR (form)),
731 form, SCM_ARG1, s_quasiquote);
732 if (0 == depth)
733 return evalcar (form, env);
734 return scm_cons2 (tmp, iqq (SCM_CAR (form), env, depth), SCM_EOL);
735 }
736 if (SCM_NIMP (tmp) && (scm_i_uq_splicing == SCM_CAR (tmp)))
737 {
738 tmp = SCM_CDR (tmp);
739 if (0 == --edepth)
740 return scm_append (scm_cons2 (evalcar (tmp, env), iqq (SCM_CDR (form), env, depth), SCM_EOL));
741 }
742 return scm_cons (iqq (SCM_CAR (form), env, edepth), iqq (SCM_CDR (form), env, depth));
743}
744
745/* Here are acros which return values rather than code. */
746
747#ifdef __STDC__
748SCM
749scm_m_quasiquote (SCM xorig, SCM env)
750#else
751SCM
752scm_m_quasiquote (xorig, env)
753 SCM xorig;
754 SCM env;
755#endif
756{
757 SCM x = SCM_CDR (xorig);
758 ASSYNT (scm_ilength (x) == 1, xorig, s_expression, s_quasiquote);
759 return iqq (SCM_CAR (x), env, 1);
760}
761
762#ifdef __STDC__
763SCM
764scm_m_delay (SCM xorig, SCM env)
765#else
766SCM
767scm_m_delay (xorig, env)
768 SCM xorig;
769 SCM env;
770#endif
771{
772 ASSYNT (scm_ilength (xorig) == 2, xorig, s_expression, s_delay);
773 xorig = SCM_CDR (xorig);
774 return scm_makprom (scm_closure (scm_cons2 (SCM_EOL, SCM_CAR (xorig), SCM_CDR (xorig)),
775 env));
776}
777
778#ifdef __STDC__
779static SCM
780env_top_level (SCM env)
781#else
782static SCM
783env_top_level (env)
784 SCM env;
785#endif
786{
787 while (SCM_NIMP(env))
788 {
789 if (SCM_BOOL_T == scm_procedure_p (SCM_CAR(env)))
790 return SCM_CAR(env);
791 env = SCM_CDR (env);
792 }
793 return SCM_BOOL_F;
794}
795
796#ifdef __STDC__
797SCM
798scm_m_define (SCM x, SCM env)
799#else
800SCM
801scm_m_define (x, env)
802 SCM x;
803 SCM env;
804#endif
805{
806 SCM proc, arg1 = x;
807 x = SCM_CDR (x);
808 /* ASSYNT(SCM_NULLP(env), x, "bad placement", s_define);*/
809 ASSYNT (scm_ilength (x) >= 2, arg1, s_expression, "define");
810 proc = SCM_CAR (x);
811 x = SCM_CDR (x);
812 while (SCM_NIMP (proc) && SCM_CONSP (proc))
813 { /* nested define syntax */
814 x = scm_cons (scm_cons2 (scm_i_lambda, SCM_CDR (proc), x), SCM_EOL);
815 proc = SCM_CAR (proc);
816 }
817 ASSYNT (SCM_NIMP (proc) && SCM_SYMBOLP (proc), arg1, s_variable, "define");
818 ASSYNT (1 == scm_ilength (x), arg1, s_expression, "define");
819 if (SCM_TOP_LEVEL (env))
820 {
821 x = evalcar (x, env);
6dbd0af5 822#ifdef DEBUG_EXTENSIONS
b7ff98dd 823 if (SCM_REC_PROCNAMES_P && SCM_NIMP (x) && SCM_CLOSUREP (x))
6dbd0af5
MD
824 scm_set_procedure_property_x (x, scm_i_name, proc);
825#endif
0f2d19dd
JB
826 arg1 = scm_sym2vcell (proc, env_top_level (env), SCM_BOOL_T);
827#if 0
828#ifndef RECKLESS
829 if (SCM_NIMP (SCM_CDR (arg1)) && ((SCM) SCM_SNAME (SCM_CDR (arg1)) == proc)
830 && (SCM_CDR (arg1) != x))
831 scm_warn ("redefining built-in ", SCM_CHARS (proc));
832 else
833#endif
834 if (5 <= scm_verbose && SCM_UNDEFINED != SCM_CDR (arg1))
835 scm_warn ("redefining ", SCM_CHARS (proc));
0f2d19dd
JB
836#endif
837 SCM_CDR (arg1) = x;
838#ifdef SICP
839 return scm_cons2 (scm_i_quote, SCM_CAR (arg1), SCM_EOL);
840#else
841 return SCM_UNSPECIFIED;
842#endif
843 }
844 return scm_cons2 (SCM_IM_DEFINE, proc, x);
845}
6dbd0af5
MD
846
847SCM
848scm_m_undefine (x, env)
849 SCM x, env;
850{
851 SCM arg1 = x;
852 x = SCM_CDR (x);
853 ASSYNT (SCM_TOP_LEVEL (env), arg1, "bad placement ", s_undefine);
854 ASSYNT (SCM_NIMP (x) && SCM_CONSP (x) && SCM_CDR (x) == SCM_EOL,
855 arg1, s_expression, s_undefine);
856 x = SCM_CAR (x);
857 ASSYNT (SCM_NIMP (x) && SCM_SYMBOLP (x), arg1, s_variable, s_undefine);
858 arg1 = scm_sym2vcell (x, env_top_level (env), SCM_BOOL_F);
859 ASSYNT (SCM_NFALSEP (arg1) && !SCM_UNBNDP (SCM_CDR (arg1)),
860 x, "variable already unbound ", s_undefine);
861#if 0
862#ifndef RECKLESS
863 if (SCM_NIMP (SCM_CDR (arg1)) && ((SCM) SCM_SNAME (SCM_CDR (arg1)) == x))
864 scm_warn ("undefining built-in ", SCM_CHARS (x));
865 else
866#endif
867 if (5 <= scm_verbose && SCM_UNDEFINED != SCM_CDR (arg1))
868 scm_warn ("redefining ", SCM_CHARS (x));
869#endif
870 SCM_CDR (arg1) = SCM_UNDEFINED;
871#ifdef SICP
872 return SCM_CAR (arg1);
873#else
874 return SCM_UNSPECIFIED;
875#endif
876}
877
0f2d19dd
JB
878/* end of acros */
879
880#ifdef __STDC__
881SCM
882scm_m_letrec (SCM xorig, SCM env)
883#else
884SCM
885scm_m_letrec (xorig, env)
886 SCM xorig;
887 SCM env;
888#endif
889{
890 SCM cdrx = SCM_CDR (xorig); /* locally mutable version of form */
891 char *what = SCM_CHARS (SCM_CAR (xorig));
892 SCM x = cdrx, proc, arg1; /* structure traversers */
893 SCM vars = SCM_EOL, inits = SCM_EOL, *initloc = &inits;
894
895 ASRTSYNTAX (scm_ilength (x) >= 2, s_body);
896 proc = SCM_CAR (x);
897 if SCM_NULLP
898 (proc) return scm_m_letstar (xorig, env); /* null binding, let* faster */
899 ASRTSYNTAX (scm_ilength (proc) >= 1, s_bindings);
900 do
901 {
902 /* vars scm_list reversed here, inits reversed at evaluation */
903 arg1 = SCM_CAR (proc);
904 ASRTSYNTAX (2 == scm_ilength (arg1), s_bindings);
905 ASRTSYNTAX (SCM_NIMP (SCM_CAR (arg1)) && SCM_SYMBOLP (SCM_CAR (arg1)), s_variable);
906 vars = scm_cons (SCM_CAR (arg1), vars);
907 *initloc = scm_cons (SCM_CAR (SCM_CDR (arg1)), SCM_EOL);
908 initloc = &SCM_CDR (*initloc);
909 }
910 while SCM_NIMP
911 (proc = SCM_CDR (proc));
912 cdrx = scm_cons2 (vars, inits, SCM_CDR (x));
913 bodycheck (xorig, &SCM_CDR (SCM_CDR (cdrx)), what);
914 return scm_cons (SCM_IM_LETREC, cdrx);
915}
916
917#ifdef __STDC__
918SCM
919scm_m_let (SCM xorig, SCM env)
920#else
921SCM
922scm_m_let (xorig, env)
923 SCM xorig;
924 SCM env;
925#endif
926{
927 SCM cdrx = SCM_CDR (xorig); /* locally mutable version of form */
928 SCM x = cdrx, proc, arg1, name; /* structure traversers */
929 SCM vars = SCM_EOL, inits = SCM_EOL, *varloc = &vars, *initloc = &inits;
930
931 ASSYNT (scm_ilength (x) >= 2, xorig, s_body, "let");
932 proc = SCM_CAR (x);
933 if (SCM_NULLP (proc)
934 || (SCM_NIMP (proc) && SCM_CONSP (proc)
935 && SCM_NIMP (SCM_CAR (proc)) && SCM_CONSP (SCM_CAR (proc)) && SCM_NULLP (SCM_CDR (proc))))
936 return scm_m_letstar (xorig, env); /* null or single binding, let* is faster */
937 ASSYNT (SCM_NIMP (proc), xorig, s_bindings, "let");
938 if (SCM_CONSP (proc)) /* plain let, proc is <bindings> */
939 return scm_cons (SCM_IM_LET, SCM_CDR (scm_m_letrec (xorig, env)));
940 if (!SCM_SYMBOLP (proc))
941 scm_wta (xorig, s_bindings, "let"); /* bad let */
942 name = proc; /* named let, build equiv letrec */
943 x = SCM_CDR (x);
944 ASSYNT (scm_ilength (x) >= 2, xorig, s_body, "let");
945 proc = SCM_CAR (x); /* bindings scm_list */
946 ASSYNT (scm_ilength (proc) >= 0, xorig, s_bindings, "let");
947 while SCM_NIMP
948 (proc)
949 { /* vars and inits both in order */
950 arg1 = SCM_CAR (proc);
951 ASSYNT (2 == scm_ilength (arg1), xorig, s_bindings, "let");
952 ASSYNT (SCM_NIMP (SCM_CAR (arg1)) && SCM_SYMBOLP (SCM_CAR (arg1)), xorig, s_variable, "let");
953 *varloc = scm_cons (SCM_CAR (arg1), SCM_EOL);
954 varloc = &SCM_CDR (*varloc);
955 *initloc = scm_cons (SCM_CAR (SCM_CDR (arg1)), SCM_EOL);
956 initloc = &SCM_CDR (*initloc);
957 proc = SCM_CDR (proc);
958 }
959 return
960 scm_m_letrec (scm_cons2 (scm_i_let,
961 scm_cons (scm_cons2 (name, scm_cons2 (scm_i_lambda, vars, SCM_CDR (x)), SCM_EOL), SCM_EOL),
962 scm_acons (name, inits, SCM_EOL)), /* body */
963 env);
964}
965
966
967#ifdef __STDC__
968SCM
969scm_m_apply (SCM xorig, SCM env)
970#else
971SCM
972scm_m_apply (xorig, env)
973 SCM xorig;
974 SCM env;
975#endif
976{
977 ASSYNT (scm_ilength (SCM_CDR (xorig)) == 2, xorig, s_expression, "@apply");
978 return scm_cons (SCM_IM_APPLY, SCM_CDR (xorig));
979}
980
981#define s_atcall_cc (SCM_ISYMSCM_CHARS(SCM_IM_CONT)+1)
982
983#ifdef __STDC__
984SCM
985scm_m_cont (SCM xorig, SCM env)
986#else
987SCM
988scm_m_cont (xorig, env)
989 SCM xorig;
990 SCM env;
991#endif
992{
993 ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, xorig, s_expression, "@call-with-current-continuation");
994 return scm_cons (SCM_IM_CONT, SCM_CDR (xorig));
995}
996
6dbd0af5
MD
997/* scm_unmemocopy takes a memoized expression together with its
998 * environment and rewrites it to its original form. Thus, it is the
999 * inversion of the rewrite rules above. The procedure is not
1000 * optimized for speed. It's used in scm_iprin1 when printing the
1001 * code of a closure, in scm_procedure_source and in scm_expr_stack
1002 * when generating the source for a stackframe.
1003 */
1004
1005#ifdef __STDC__
1006static SCM
1007unmemocopy (SCM x, SCM env)
1008#else
1009static SCM
1010unmemocopy (x, env)
1011 SCM x;
1012 SCM env;
1013#endif
1014{
1015 SCM ls, z;
1016#ifdef DEBUG_EXTENSIONS
1017 SCM p;
1018#endif
1019 if (SCM_NCELLP (x) || SCM_NECONSP (x))
1020 return x;
1021#ifdef DEBUG_EXTENSIONS
1022 p = scm_whash_lookup (scm_source_whash, x);
1023#endif
1024 switch (SCM_TYP7 (x))
1025 {
1026 case (127 & SCM_IM_AND):
1027 ls = z = scm_cons (scm_i_and, SCM_UNSPECIFIED);
1028 break;
1029 case (127 & SCM_IM_BEGIN):
1030 ls = z = scm_cons (scm_i_begin, SCM_UNSPECIFIED);
1031 break;
1032 case (127 & SCM_IM_CASE):
1033 ls = z = scm_cons (scm_i_case, SCM_UNSPECIFIED);
1034 break;
1035 case (127 & SCM_IM_COND):
1036 ls = z = scm_cons (scm_i_cond, SCM_UNSPECIFIED);
1037 break;
1038 case (127 & SCM_IM_DO):
1039 ls = scm_cons (scm_i_do, SCM_UNSPECIFIED);
1040 goto transform;
1041 case (127 & SCM_IM_IF):
1042 ls = z = scm_cons (scm_i_if, SCM_UNSPECIFIED);
1043 break;
1044 case (127 & SCM_IM_LET):
1045 ls = scm_cons (scm_i_let, SCM_UNSPECIFIED);
1046 goto transform;
1047 case (127 & SCM_IM_LETREC):
1048 {
1049 SCM f, v, e, s;
1050 ls = scm_cons (scm_i_letrec, SCM_UNSPECIFIED);
1051 transform:
1052 x = SCM_CDR (x);
1053 f = v = SCM_CAR (x);
1054 x = SCM_CDR (x);
1055 z = EXTEND_SCM_ENV (f, SCM_EOL, env);
1056 e = scm_reverse (unmemocopy (SCM_CAR (x),
1057 SCM_CAR (ls) == scm_i_letrec ? z : env));
1058 env = z;
1059 s = SCM_CAR (ls) == scm_i_do
1060 ? scm_reverse (unmemocopy (SCM_CDR (SCM_CDR (SCM_CDR (x))), env))
1061 : f;
1062 z = SCM_EOL;
1063 do
1064 {
1065 z = scm_acons (SCM_CAR (v),
1066 scm_cons (SCM_CAR (e),
1067 SCM_CAR (s) == SCM_CAR (v)
1068 ? SCM_EOL
1069 : scm_cons (SCM_CAR (s), SCM_EOL)),
1070 z);
1071 v = SCM_CDR (v);
1072 e = SCM_CDR (e);
1073 s = SCM_CDR (s);
1074 }
1075 while SCM_NIMP (v);
1076 SCM_CDR (ls) = z = scm_cons (z, SCM_UNSPECIFIED);
1077 if (SCM_CAR (ls) == scm_i_do)
1078 {
1079 x = SCM_CDR (x);
1080 z = (SCM_CDR (z) = scm_cons (unmemocopy (SCM_CAR (x), env),
1081 SCM_UNSPECIFIED));
1082 x = (SCM) (&SCM_CAR (SCM_CDR (x)) - 1);
1083 }
1084 break;
1085 }
1086 case (127 & SCM_IM_LETSTAR):
1087 {
1088 SCM b, y;
1089 x = SCM_CDR (x);
1090 b = SCM_CAR (x);
1091 y = SCM_EOL;
1092 if SCM_IMP (b)
1093 {
1094 env = EXTEND_SCM_ENV (SCM_EOL, SCM_EOL, env);
1095 goto letstar;
1096 }
1097 y = z = scm_acons (SCM_CAR (b),
1098 unmemocar (
1099 scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b)), env), SCM_EOL), env),
1100 SCM_UNSPECIFIED);
1101 env = EXTEND_SCM_ENV (SCM_CAR (b), SCM_BOOL_F, env);
1102 b = SCM_CDR (SCM_CDR (b));
1103 if (SCM_IMP (b))
1104 {
1105 SCM_SETCDR (y, SCM_EOL);
1106 ls = scm_cons (scm_i_let, z = scm_cons (y, SCM_UNSPECIFIED));
1107 break;
1108 }
1109 do
1110 {
1111 z = (SCM_CDR (z) = scm_acons (SCM_CAR (b),
1112 unmemocar (
1113 scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b)), env), SCM_EOL), env),
1114 SCM_UNSPECIFIED));
1115 env = EXTEND_SCM_ENV (SCM_CAR (b), SCM_BOOL_F, env);
1116 b = SCM_CDR (SCM_CDR (b));
1117 }
1118 while SCM_NIMP (b);
1119 SCM_CDR (z) = SCM_EOL;
1120 letstar:
1121 ls = scm_cons (scm_i_letstar, z = scm_cons (y, SCM_UNSPECIFIED));
1122 break;
1123 }
1124 case (127 & SCM_IM_OR):
1125 ls = z = scm_cons (scm_i_or, SCM_UNSPECIFIED);
1126 break;
1127 case (127 & SCM_IM_LAMBDA):
1128 x = SCM_CDR (x);
1129 ls = scm_cons (scm_i_lambda,
1130 z = scm_cons (SCM_CAR (x), SCM_UNSPECIFIED));
1131 env = EXTEND_SCM_ENV (SCM_CAR (x), SCM_EOL, env);
1132 break;
1133 case (127 & SCM_IM_QUOTE):
1134 ls = z = scm_cons (scm_i_quote, SCM_UNSPECIFIED);
1135 break;
1136 case (127 & SCM_IM_SET):
1137 ls = z = scm_cons (scm_i_set, SCM_UNSPECIFIED);
1138 break;
1139 case (127 & SCM_IM_DEFINE):
1140 {
1141 SCM n;
1142 x = SCM_CDR (x);
1143 ls = scm_cons (scm_i_define,
1144 z = scm_cons (n = SCM_CAR (x), SCM_UNSPECIFIED));
1145 if (SCM_NNULLP (env))
1146 SCM_CAR (SCM_CAR (env)) = scm_cons (n, SCM_CAR (SCM_CAR (env)));
1147 break;
1148 }
1149 case (127 & SCM_MAKISYM (0)):
1150 z = SCM_CAR (x);
1151 if (!SCM_ISYMP (z))
1152 goto unmemo;
1153 switch SCM_ISYMNUM (z)
1154 {
1155 case (SCM_ISYMNUM (SCM_IM_APPLY)):
1156 ls = z = scm_cons (scm_i_atapply, SCM_UNSPECIFIED);
1157 goto loop;
1158 case (SCM_ISYMNUM (SCM_IM_CONT)):
1159 ls = z = scm_cons (scm_i_atcall_cc, SCM_UNSPECIFIED);
1160 goto loop;
1161 default:
1162 }
1163 unmemo:
1164 default:
1165 ls = z = unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
1166 SCM_UNSPECIFIED),
1167 env);
1168 }
1169loop:
1170 while (SCM_CELLP (x = SCM_CDR (x)) && SCM_ECONSP (x))
1171 z = (SCM_CDR (z) = unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
1172 SCM_UNSPECIFIED),
1173 env));
1174 SCM_CDR (z) = x;
1175#ifdef DEBUG_EXTENSIONS
1176 if (SCM_NFALSEP (p))
1177 scm_whash_insert (scm_source_whash, ls, p);
1178#endif
1179 return ls;
1180}
1181
1182#ifdef __STDC__
1183SCM
1184scm_unmemocopy (SCM x, SCM env)
1185#else
1186SCM
1187scm_unmemocopy (x, env)
1188 SCM x;
1189 SCM env;
1190#endif
1191{
1192 if (SCM_NNULLP (env))
1193 /* Make a copy of the lowest frame to protect it from
1194 modifications by SCM_IM_DEFINE */
1195 return unmemocopy (x, scm_cons (SCM_CAR (env), SCM_CDR (env)));
1196 else
1197 return unmemocopy (x, env);
1198}
1199
0f2d19dd
JB
1200#ifndef RECKLESS
1201#ifdef __STDC__
1202int
1203scm_badargsp (SCM formals, SCM args)
1204#else
1205int
1206scm_badargsp (formals, args)
1207 SCM formals;
1208 SCM args;
1209#endif
1210{
1211 while SCM_NIMP
1212 (formals)
1213 {
1214 if SCM_NCONSP
1215 (formals) return 0;
1216 if SCM_IMP
1217 (args) return 1;
1218 formals = SCM_CDR (formals);
1219 args = SCM_CDR (args);
1220 }
1221 return SCM_NNULLP (args) ? 1 : 0;
1222}
1223#endif
1224
1225
1226\f
1227long scm_tc16_macro;
1228
6dbd0af5
MD
1229#ifdef __STDC__
1230SCM
1231scm_eval_args (SCM l, SCM env)
1232#else
1233SCM
1234scm_eval_args (l, env)
1235 SCM l;
1236 SCM env;
1237#endif
1238{
1239 SCM res = SCM_EOL, *lloc = &res;
1240 while (SCM_NIMP (l))
1241 {
1242 *lloc = scm_cons (EVALCAR (l, env), SCM_EOL);
1243 lloc = &SCM_CDR (*lloc);
1244 l = SCM_CDR (l);
1245 }
1246 return res;
1247}
0f2d19dd
JB
1248#endif /* !DEVAL */
1249
6dbd0af5
MD
1250
1251/* SECTION: This code is specific for the debugging support. One
1252 * branch is read when DEVAL isn't defined, the other when DEVAL is
1253 * defined.
1254 */
1255
1256#ifndef DEVAL
1257
1258#define SCM_APPLY scm_apply
1259#define PREP_APPLY(proc, args)
1260#define ENTER_APPLY
1261#define RETURN(x) return x;
b7ff98dd
MD
1262#ifdef STACK_CHECKING
1263#ifndef NO_CEVAL_STACK_CHECKING
1264#define EVAL_STACK_CHECKING
1265#endif
6dbd0af5
MD
1266#endif
1267
1268#else /* !DEVAL */
1269
0f2d19dd
JB
1270#undef SCM_CEVAL
1271#define SCM_CEVAL scm_deval /* Substitute all uses of scm_ceval */
1272#undef SCM_APPLY
1273#define SCM_APPLY scm_dapply
6dbd0af5
MD
1274#undef PREP_APPLY
1275#define PREP_APPLY(p, l) \
1276{ ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
1277#undef ENTER_APPLY
1278#define ENTER_APPLY \
1279{\
b7ff98dd 1280 SCM_SET_ARGSREADY (debug);\
6dbd0af5 1281 if (CHECK_APPLY)\
b7ff98dd 1282 if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && PROCTRACEP (proc)))\
6dbd0af5 1283 {\
b7ff98dd
MD
1284 SCM tmp, tail = SCM_TRACED_FRAME_P (debug) ? SCM_BOOL_T : SCM_BOOL_F;\
1285 SCM_SET_TRACED_FRAME (debug);\
1286 if (SCM_CHEAPTRAPS_P)\
6dbd0af5
MD
1287 {\
1288 tmp = scm_make_debugobj ((scm_debug_frame *) &debug);\
1289 scm_ithrow (scm_i_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
1290 }\
1291 else\
1292 {\
1293 scm_make_cont (&tmp);\
1294 if (!setjmp (SCM_JMPBUF (tmp)))\
1295 scm_ithrow (scm_i_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
1296 }\
1297 }\
1298}
0f2d19dd
JB
1299#undef RETURN
1300#define RETURN(e) {proc = (e); goto exit;}
b7ff98dd
MD
1301#ifdef STACK_CHECKING
1302#ifndef EVAL_STACK_CHECKING
1303#define EVAL_STACK_CHECKING
1304#endif
6dbd0af5
MD
1305#endif
1306
1307/* scm_ceval_ptr points to the currently selected evaluator.
1308 * *fixme*: Although efficiency is important here, this state variable
1309 * should probably not be a global. It should be related to the
1310 * current repl.
1311 */
1312
1313#ifdef __STDC__
1314SCM (*scm_ceval_ptr) (SCM exp, SCM env);
0f2d19dd 1315#else
6dbd0af5 1316SCM (*scm_ceval_ptr) ();
0f2d19dd
JB
1317#endif
1318
6dbd0af5
MD
1319/* last_debug_info_frame contains a pointer to the last debugging
1320 * information stack frame. It is accessed very often from the
1321 * debugging evaluator, so it should probably not be indirectly
1322 * addressed. Better to save and restore it from the current root at
1323 * any stack swaps.
1324 */
1325
1326scm_debug_frame *last_debug_info_frame;
1327
1328/* scm_debug_eframe_size is the number of slots available for pseudo
1329 * stack frames at each real stack frame.
1330 */
1331
1332int scm_debug_eframe_size;
1333
b7ff98dd 1334int scm_debug_mode, scm_check_entry_p, scm_check_apply_p, scm_check_exit_p;
6dbd0af5
MD
1335
1336scm_option scm_debug_opts[] = {
b7ff98dd
MD
1337 { SCM_OPTION_BOOLEAN, "cheap", 1,
1338 "*Flyweight representation of the stack at traps." },
1339 { SCM_OPTION_BOOLEAN, "breakpoints", 0, "*Check for breakpoints." },
1340 { SCM_OPTION_BOOLEAN, "trace", 0, "*Trace mode." },
1341 { SCM_OPTION_BOOLEAN, "procnames", 1,
1342 "Record procedure names at definition." },
1343 { SCM_OPTION_BOOLEAN, "backwards", 0,
1344 "Display backtrace in anti-chronological order." },
1345 { SCM_OPTION_INTEGER, "frames", 2,
1346 "Maximum number of tail-recursive frames in backtrace." },
1347 { SCM_OPTION_INTEGER, "depth", 80, "Maximal length of backtrace." },
1348 { SCM_OPTION_BOOLEAN, "backtrace", 0,
1349 "Show backtrace on error (use debugging evaluator)." },
1350 { SCM_OPTION_BOOLEAN, "deval", 0, "Use the debugging evaluator." },
1351 { SCM_OPTION_INTEGER, "stack", 20000, "Stack size limit (0 = no check)." }
6dbd0af5
MD
1352};
1353
1354scm_option scm_evaluator_trap_table[] = {
b7ff98dd
MD
1355 { SCM_OPTION_BOOLEAN, "enter-frame", 0, "Trap when eval enters new frame." },
1356 { SCM_OPTION_BOOLEAN, "apply-frame", 0, "Trap when entering apply." },
1357 { SCM_OPTION_BOOLEAN, "exit-frame", 0, "Trap when exiting eval or apply." }
6dbd0af5
MD
1358};
1359
1360SCM
1361scm_deval_args (l, env, lloc)
1362 SCM l, env, *lloc;
0f2d19dd 1363{
6dbd0af5 1364 SCM *res = lloc;
0f2d19dd
JB
1365 while (SCM_NIMP (l))
1366 {
1367 *lloc = scm_cons (EVALCAR (l, env), SCM_EOL);
1368 lloc = &SCM_CDR (*lloc);
1369 l = SCM_CDR (l);
1370 }
6dbd0af5 1371 return *res;
0f2d19dd
JB
1372}
1373
6dbd0af5
MD
1374#endif /* !DEVAL */
1375
1376
1377/* SECTION: Some local definitions for the evaluator.
1378 */
1379
1380#ifndef DEVAL
1381#ifdef SCM_FLOATS
1382#define CHECK_EQVISH(A,B) (((A) == (B)) || (SCM_NFALSEP (scm_eqv_p ((A), (B)))))
1383#else
1384#define CHECK_EQVISH(A,B) ((A) == (B))
1385#endif
1386#endif /* DEVAL */
1387
1388
1389/* SECTION: This is the evaluator. Like any real monster, it has
1390 * three heads. This code is compiled twice.
1391 */
1392
0f2d19dd
JB
1393#if 0
1394#ifdef __STDC__
1395SCM
1396scm_ceval (SCM x, SCM env)
1397#else
1398SCM
1399scm_ceval (x, env)
1400 SCM x;
1401 SCM env;
1402#endif
1403{}
1404#endif
1405#if 0
1406#ifdef __STDC__
1407SCM
1408scm_deval (SCM x, SCM env)
1409#else
1410SCM
1411scm_deval (x, env)
1412 SCM x;
1413 SCM env;
1414#endif
1415{}
1416#endif
1417
6dbd0af5
MD
1418#ifdef __STDC__
1419SCM
1420SCM_CEVAL (SCM x, SCM env)
0f2d19dd 1421#else
6dbd0af5 1422SCM
0f2d19dd
JB
1423SCM_CEVAL (x, env)
1424 SCM x;
1425 SCM env;
6dbd0af5 1426#endif
0f2d19dd
JB
1427{
1428 union
1429 {
1430 SCM *lloc;
1431 SCM arg1;
1432 } t;
6dbd0af5
MD
1433 SCM proc, arg2;
1434#ifdef DEVAL
1435 struct
1436 {
1437 scm_debug_frame *prev;
1438 long status;
1439 scm_debug_info vect[scm_debug_eframe_size];
1440 scm_debug_info *info;
1441 } debug;
1442 debug.prev = last_debug_info_frame;
1443 debug.status = scm_debug_eframe_size;
1444 debug.info = &debug.vect[0];
1445 last_debug_info_frame = (scm_debug_frame *) &debug;
1446#endif
b7ff98dd
MD
1447#ifdef EVAL_STACK_CHECKING
1448 if (SCM_STACK_OVERFLOW_P ((SCM_STACKITEM *) &proc)
1449 && scm_stack_checking_enabled_p)
6dbd0af5 1450 {
b7ff98dd 1451#ifdef DEVAL
6dbd0af5
MD
1452 debug.info->e.exp = x;
1453 debug.info->e.env = env;
b7ff98dd 1454#endif
6dbd0af5
MD
1455 scm_report_stack_overflow ();
1456 }
1457#endif
1458#ifdef DEVAL
1459 goto start;
1460#endif
1461loopnoap:
1462 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
1463loop:
1464#ifdef DEVAL
1465#if 0 /* This will probably never have any practical use ... */
1466 if (CHECK_EXIT)
1467 {
b7ff98dd 1468 if (SINGLE_STEP || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
6dbd0af5
MD
1469 {
1470 SINGLE_STEP = 0;
b7ff98dd
MD
1471 SCM_RESET_DEBUG_MODE;
1472 SCM_CLEAR_TRACED_FRAME (debug);
6dbd0af5
MD
1473 scm_make_cont (&t.arg1);
1474 if (!setjmp (SCM_JMPBUF (t.arg1)))
1475 scm_ithrow (scm_i_exit_tail, scm_cons (t.arg1, SCM_EOL), 0);
1476 }
1477 }
1478nextframe:
1479#endif
b7ff98dd
MD
1480 SCM_CLEAR_ARGSREADY (debug);
1481 if (SCM_OVERFLOWP (debug))
6dbd0af5
MD
1482 --debug.info;
1483 else if (++debug.info == (scm_debug_info *) &debug.info)
1484 {
b7ff98dd 1485 SCM_SET_OVERFLOW (debug);
6dbd0af5
MD
1486 debug.info -= 2;
1487 }
1488start:
1489 debug.info->e.exp = x;
1490 debug.info->e.env = env;
1491 if (CHECK_ENTRY)
b7ff98dd 1492 if (SCM_ENTER_FRAME_P || (SCM_BREAKPOINTS_P && SRCBRKP (x)))
6dbd0af5 1493 {
b7ff98dd
MD
1494 SCM tail = SCM_TAILRECP (debug) ? SCM_BOOL_T : SCM_BOOL_F;
1495 SCM_SET_TAILREC (debug);
1496 SCM_ENTER_FRAME_P = 0;
1497 SCM_RESET_DEBUG_MODE;
1498 if (SCM_CHEAPTRAPS_P)
6dbd0af5
MD
1499 t.arg1 = scm_make_debugobj ((scm_debug_frame *) &debug);
1500 else
1501 {
1502 scm_make_cont (&t.arg1);
1503 if (setjmp (SCM_JMPBUF (t.arg1)))
1504 {
1505 x = SCM_THROW_VALUE (t.arg1);
1506 if (SCM_IMP (x))
1507 {
1508 RETURN (x);
1509 }
1510 else
1511 /* This gives the possibility for the debugger to
1512 modify the source expression before evaluation. */
1513 goto dispatch;
1514 }
1515 }
1516 scm_ithrow (scm_i_enter_frame,
1517 scm_cons2 (t.arg1, tail,
1518 scm_cons (scm_unmemocopy (x, env), SCM_EOL)),
1519 0);
1520 }
1521dispatch:
1522#endif
0f2d19dd 1523 SCM_ASYNC_TICK;
0f2d19dd
JB
1524 switch (SCM_TYP7 (x))
1525 {
1526 case scm_tcs_symbols:
1527 /* Only happens when called at top level.
1528 */
1529 x = scm_cons (x, SCM_UNDEFINED);
1530 goto retval;
1531
1532 case (127 & SCM_IM_AND):
1533 x = SCM_CDR (x);
1534 t.arg1 = x;
1535 while (SCM_NNULLP (t.arg1 = SCM_CDR (t.arg1)))
1536 if (SCM_FALSEP (EVALCAR (x, env)))
1537 {
1538 RETURN (SCM_BOOL_F);
1539 }
1540 else
1541 x = t.arg1;
6dbd0af5 1542 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1543 goto carloop;
1544
1545 case (127 & SCM_IM_BEGIN):
6dbd0af5
MD
1546 cdrxnoap:
1547 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1548 cdrxbegin:
1549 x = SCM_CDR (x);
1550
1551 begin:
1552 t.arg1 = x;
1553 while (SCM_NNULLP (t.arg1 = SCM_CDR (t.arg1)))
1554 {
1555 SIDEVAL (SCM_CAR (x), env);
1556 x = t.arg1;
1557 }
1558
1559 carloop: /* scm_eval car of last form in list */
1560 if (SCM_NCELLP (SCM_CAR (x)))
1561 {
1562 x = SCM_CAR (x);
6dbd0af5 1563 RETURN (SCM_IMP (x) ? EVALIM (x, env) : SCM_GLOC_VAL (x))
0f2d19dd
JB
1564 }
1565
1566 if (SCM_SYMBOLP (SCM_CAR (x)))
1567 {
1568 retval:
6dbd0af5 1569 RETURN (*scm_lookupcar (x, env))
0f2d19dd
JB
1570 }
1571
1572 x = SCM_CAR (x);
1573 goto loop; /* tail recurse */
1574
1575
1576 case (127 & SCM_IM_CASE):
1577 x = SCM_CDR (x);
1578 t.arg1 = EVALCAR (x, env);
1579 while (SCM_NIMP (x = SCM_CDR (x)))
1580 {
1581 proc = SCM_CAR (x);
1582 if (scm_i_else == SCM_CAR (proc))
1583 {
1584 x = SCM_CDR (proc);
6dbd0af5 1585 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1586 goto begin;
1587 }
1588 proc = SCM_CAR (proc);
1589 while (SCM_NIMP (proc))
1590 {
1591 if (CHECK_EQVISH (SCM_CAR (proc), t.arg1))
1592 {
1593 x = SCM_CDR (SCM_CAR (x));
6dbd0af5 1594 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1595 goto begin;
1596 }
1597 proc = SCM_CDR (proc);
1598 }
1599 }
6dbd0af5 1600 RETURN (SCM_UNSPECIFIED)
0f2d19dd
JB
1601
1602
1603 case (127 & SCM_IM_COND):
1604 while (SCM_NIMP (x = SCM_CDR (x)))
1605 {
1606 proc = SCM_CAR (x);
1607 t.arg1 = EVALCAR (proc, env);
1608 if (SCM_NFALSEP (t.arg1))
1609 {
1610 x = SCM_CDR (proc);
6dbd0af5 1611 if SCM_NULLP (x)
0f2d19dd 1612 {
6dbd0af5 1613 RETURN (t.arg1)
0f2d19dd
JB
1614 }
1615 if (scm_i_arrow != SCM_CAR (x))
6dbd0af5
MD
1616 {
1617 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
1618 goto begin;
1619 }
0f2d19dd
JB
1620 proc = SCM_CDR (x);
1621 proc = EVALCAR (proc, env);
1622 SCM_ASRTGO (SCM_NIMP (proc), badfun);
6dbd0af5
MD
1623 PREP_APPLY (proc, scm_cons (t.arg1, SCM_EOL));
1624 ENTER_APPLY;
0f2d19dd
JB
1625 goto evap1;
1626 }
1627 }
6dbd0af5 1628 RETURN (SCM_UNSPECIFIED)
0f2d19dd
JB
1629
1630
1631 case (127 & SCM_IM_DO):
1632 x = SCM_CDR (x);
1633 proc = SCM_CAR (SCM_CDR (x)); /* inits */
1634 t.arg1 = SCM_EOL; /* values */
1635 while (SCM_NIMP (proc))
1636 {
1637 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
1638 proc = SCM_CDR (proc);
1639 }
1640 env = EXTEND_SCM_ENV (SCM_CAR (x), t.arg1, env);
1641 x = SCM_CDR (SCM_CDR (x));
1642 while (proc = SCM_CAR (x), SCM_FALSEP (EVALCAR (proc, env)))
1643 {
1644 for (proc = SCM_CAR (SCM_CDR (x)); SCM_NIMP (proc); proc = SCM_CDR (proc))
1645 {
1646 t.arg1 = SCM_CAR (proc); /* body */
1647 SIDEVAL (t.arg1, env);
1648 }
1649 for (t.arg1 = SCM_EOL, proc = SCM_CDR (SCM_CDR (x)); SCM_NIMP (proc); proc = SCM_CDR (proc))
1650 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1); /* steps */
1651 env = EXTEND_SCM_ENV (SCM_CAR (SCM_CAR (env)), t.arg1, SCM_CDR (env));
1652 }
1653 x = SCM_CDR (proc);
1654 if (SCM_NULLP (x))
6dbd0af5
MD
1655 RETURN (SCM_UNSPECIFIED);
1656 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1657 goto begin;
1658
1659
1660 case (127 & SCM_IM_IF):
1661 x = SCM_CDR (x);
1662 if (SCM_NFALSEP (EVALCAR (x, env)))
1663 x = SCM_CDR (x);
1664 else if (SCM_IMP (x = SCM_CDR (SCM_CDR (x))))
1665 {
1666 RETURN (SCM_UNSPECIFIED);
1667 }
6dbd0af5 1668 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1669 goto carloop;
1670
1671
1672 case (127 & SCM_IM_LET):
1673 x = SCM_CDR (x);
1674 proc = SCM_CAR (SCM_CDR (x));
1675 t.arg1 = SCM_EOL;
1676 do
1677 {
1678 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
1679 }
1680 while (SCM_NIMP (proc = SCM_CDR (proc)));
1681 env = EXTEND_SCM_ENV (SCM_CAR (x), t.arg1, env);
1682 x = SCM_CDR (x);
6dbd0af5 1683 goto cdrxnoap;
0f2d19dd
JB
1684
1685
1686 case (127 & SCM_IM_LETREC):
1687 x = SCM_CDR (x);
1688 env = EXTEND_SCM_ENV (SCM_CAR (x), scm_undefineds, env);
1689 x = SCM_CDR (x);
1690 proc = SCM_CAR (x);
1691 t.arg1 = SCM_EOL;
1692 do
1693 {
1694 t.arg1 = scm_cons (EVALCAR (proc, env), t.arg1);
1695 }
1696 while (SCM_NIMP (proc = SCM_CDR (proc)));
1697 SCM_CDR (SCM_CAR (env)) = t.arg1;
6dbd0af5 1698 goto cdrxnoap;
0f2d19dd
JB
1699
1700
1701 case (127 & SCM_IM_LETSTAR):
1702 x = SCM_CDR (x);
1703 proc = SCM_CAR (x);
1704 if (SCM_IMP (proc))
1705 {
1706 env = EXTEND_SCM_ENV (SCM_EOL, SCM_EOL, env);
6dbd0af5 1707 goto cdrxnoap;
0f2d19dd
JB
1708 }
1709 do
1710 {
1711 t.arg1 = SCM_CAR (proc);
1712 proc = SCM_CDR (proc);
1713 env = EXTEND_SCM_ENV (t.arg1, EVALCAR (proc, env), env);
1714 }
1715 while (SCM_NIMP (proc = SCM_CDR (proc)));
6dbd0af5 1716 goto cdrxnoap;
0f2d19dd
JB
1717
1718 case (127 & SCM_IM_OR):
1719 x = SCM_CDR (x);
1720 t.arg1 = x;
1721 while (SCM_NNULLP (t.arg1 = SCM_CDR (t.arg1)))
1722 {
1723 x = EVALCAR (x, env);
1724 if (SCM_NFALSEP (x))
1725 {
1726 RETURN (x);
1727 }
1728 x = t.arg1;
1729 }
6dbd0af5 1730 PREP_APPLY (SCM_UNDEFINED, SCM_EOL);
0f2d19dd
JB
1731 goto carloop;
1732
1733
1734 case (127 & SCM_IM_LAMBDA):
1735 RETURN (scm_closure (SCM_CDR (x), env));
1736
1737
1738 case (127 & SCM_IM_QUOTE):
1739 RETURN (SCM_CAR (SCM_CDR (x)));
1740
1741
1742 case (127 & SCM_IM_SET):
0f2d19dd
JB
1743 x = SCM_CDR (x);
1744 proc = SCM_CAR (x);
6dbd0af5 1745 switch (7 & (int) proc)
0f2d19dd
JB
1746 {
1747 case 0:
1748 t.lloc = scm_lookupcar (x, env);
1749 break;
1750 case 1:
1751 t.lloc = &SCM_GLOC_VAL (proc);
1752 break;
1753#ifdef MEMOIZE_LOCALS
1754 case 4:
1755 t.lloc = scm_ilookup (proc, env);
1756 break;
1757#endif
1758 }
1759 x = SCM_CDR (x);
1760 *t.lloc = EVALCAR (x, env);
0f2d19dd
JB
1761#ifdef SICP
1762 RETURN (*t.lloc);
1763#else
1764 RETURN (SCM_UNSPECIFIED);
1765#endif
1766
1767
1768 case (127 & SCM_IM_DEFINE): /* only for internal defines */
1769 x = SCM_CDR (x);
1770 proc = SCM_CAR (x);
1771 x = SCM_CDR (x);
1772 x = evalcar (x, env);
6dbd0af5 1773#ifdef DEBUG_EXTENSIONS
b7ff98dd 1774 if (SCM_REC_PROCNAMES_P && SCM_NIMP (x) && SCM_CLOSUREP (x))
6dbd0af5
MD
1775 scm_set_procedure_property_x (x, scm_i_name, proc);
1776#endif
0f2d19dd
JB
1777 env = SCM_CAR (env);
1778 SCM_DEFER_INTS;
1779 SCM_CAR (env) = scm_cons (proc, SCM_CAR (env));
1780 SCM_CDR (env) = scm_cons (x, SCM_CDR (env));
1781 SCM_ALLOW_INTS;
1782 RETURN (SCM_UNSPECIFIED);
1783
1784
1785
1786 /* new syntactic forms go here. */
1787 case (127 & SCM_MAKISYM (0)):
1788 proc = SCM_CAR (x);
1789 SCM_ASRTGO (SCM_ISYMP (proc), badfun);
1790 switch SCM_ISYMNUM (proc)
1791 {
1792#if 0
1793 case (SCM_ISYMNUM (IM_VREF)):
1794 {
1795 SCM var;
1796 var = SCM_CAR (SCM_CDR (x));
1797 RETURN (SCM_CDR(var));
1798 }
1799 case (SCM_ISYMNUM (IM_VSET)):
1800 SCM_CDR (SCM_CAR ( SCM_CDR (x))) = EVALCAR( SCM_CDR ( SCM_CDR (x)), env);
1801 SCM_CAR (SCM_CAR ( SCM_CDR (x))) = scm_tc16_variable;
6dbd0af5 1802 RETURN (SCM_UNSPECIFIED)
0f2d19dd
JB
1803#endif
1804
1805 case (SCM_ISYMNUM (SCM_IM_APPLY)):
1806 proc = SCM_CDR (x);
1807 proc = EVALCAR (proc, env);
1808 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1809 if (SCM_CLOSUREP (proc))
1810 {
6dbd0af5 1811 PREP_APPLY (proc, SCM_EOL);
0f2d19dd
JB
1812 t.arg1 = SCM_CDR (SCM_CDR (x));
1813 t.arg1 = EVALCAR (t.arg1, env);
6dbd0af5
MD
1814#ifdef DEVAL
1815 debug.info->a.args = t.arg1;
1816#endif
0f2d19dd
JB
1817#ifndef RECKLESS
1818 if (scm_badargsp (SCM_CAR (SCM_CODE (proc)), t.arg1))
1819 goto wrongnumargs;
1820#endif
1821 env = EXTEND_SCM_ENV (SCM_CAR (SCM_CODE (proc)), t.arg1, SCM_ENV (proc));
1822 x = SCM_CODE (proc);
1823 goto cdrxbegin;
1824 }
1825 proc = scm_i_apply;
1826 goto evapply;
1827
1828 case (SCM_ISYMNUM (SCM_IM_CONT)):
1829 scm_make_cont (&t.arg1);
1830 if (setjmp (SCM_JMPBUF (t.arg1)))
1831 {
1832 SCM val;
1833 val = SCM_THROW_VALUE (t.arg1);
6dbd0af5 1834 RETURN (val);
0f2d19dd
JB
1835 }
1836 proc = SCM_CDR (x);
1837 proc = evalcar (proc, env);
1838 SCM_ASRTGO (SCM_NIMP (proc), badfun);
6dbd0af5
MD
1839 PREP_APPLY (proc, scm_cons (t.arg1, SCM_EOL));
1840 ENTER_APPLY;
0f2d19dd
JB
1841 goto evap1;
1842
1843 default:
1844 goto badfun;
1845 }
1846
1847 default:
1848 proc = x;
1849 badfun:
1850 scm_everr (x, env, proc, "Wrong type to apply: ", "");
1851
1852 case scm_tc7_vector:
1853 case scm_tc7_wvect:
1854 case scm_tc7_bvect:
1855 case scm_tc7_byvect:
1856 case scm_tc7_svect:
1857 case scm_tc7_ivect:
1858 case scm_tc7_uvect:
1859 case scm_tc7_fvect:
1860 case scm_tc7_dvect:
1861 case scm_tc7_cvect:
1862#ifdef LONGLONGS
1863 case scm_tc7_llvect:
1864#endif
1865 case scm_tc7_string:
1866 case scm_tc7_mb_string:
1867 case scm_tc7_substring:
1868 case scm_tc7_mb_substring:
1869 case scm_tc7_smob:
1870 case scm_tcs_closures:
1871 case scm_tcs_subrs:
1872 RETURN (x);
1873
1874#ifdef MEMOIZE_LOCALS
1875 case (127 & SCM_ILOC00):
1876 proc = *scm_ilookup (SCM_CAR (x), env);
1877 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1878#ifndef RECKLESS
1879#ifdef CAUTIOUS
1880 goto checkargs;
1881#endif
1882#endif
1883 break;
1884#endif /* ifdef MEMOIZE_LOCALS */
1885
1886
1887 case scm_tcs_cons_gloc:
1888 proc = SCM_GLOC_VAL (SCM_CAR (x));
1889 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1890#ifndef RECKLESS
1891#ifdef CAUTIOUS
1892 goto checkargs;
1893#endif
1894#endif
1895 break;
1896
1897
1898 case scm_tcs_cons_nimcar:
1899 if (SCM_SYMBOLP (SCM_CAR (x)))
1900 {
1901 proc = *scm_lookupcar (x, env);
1902 if (SCM_IMP (proc))
1903 {
1904 unmemocar (x, env);
1905 goto badfun;
1906 }
1907 if (scm_tc16_macro == SCM_TYP16 (proc))
1908 {
1909 unmemocar (x, env);
1910
1911 handle_a_macro:
1912 t.arg1 = SCM_APPLY (SCM_CDR (proc), x, scm_cons (env, scm_listofnull));
1913 switch ((int) (SCM_CAR (proc) >> 16))
1914 {
1915 case 2:
1916 if (scm_ilength (t.arg1) <= 0)
1917 t.arg1 = scm_cons2 (SCM_IM_BEGIN, t.arg1, SCM_EOL);
6dbd0af5
MD
1918#ifdef DEVAL
1919 if (!SCM_CLOSUREP (SCM_CDR (proc)))
1920 {
1921#if 0 /* Top-level defines doesn't very often occur in backtraces */
1922 if (scm_m_define == SCM_SUBRF (SCM_CDR (proc)) && SCM_TOP_LEVEL (env))
1923 /* Prevent memoizing result of define macro */
1924 {
1925 debug.info->e.exp = scm_cons (SCM_CAR (x), SCM_CDR (x));
1926 scm_set_source_properties_x (debug.info->e.exp,
1927 scm_source_properties (x));
1928 }
1929#endif
1930 SCM_DEFER_INTS;
1931 SCM_CAR (x) = SCM_CAR (t.arg1);
1932 SCM_CDR (x) = SCM_CDR (t.arg1);
1933 SCM_ALLOW_INTS;
1934 goto dispatch;
1935 }
1936 /* Prevent memoizing of debug info expression. */
1937 debug.info->e.exp = scm_cons (SCM_CAR (x), SCM_CDR (x));
1938 scm_set_source_properties_x (debug.info->e.exp,
1939 scm_source_properties (x));
1940#endif
0f2d19dd
JB
1941 SCM_DEFER_INTS;
1942 SCM_CAR (x) = SCM_CAR (t.arg1);
1943 SCM_CDR (x) = SCM_CDR (t.arg1);
1944 SCM_ALLOW_INTS;
6dbd0af5 1945 goto loopnoap;
0f2d19dd
JB
1946 case 1:
1947 if (SCM_NIMP (x = t.arg1))
6dbd0af5 1948 goto loopnoap;
0f2d19dd
JB
1949 case 0:
1950 RETURN (t.arg1);
1951 }
1952 }
1953 }
1954 else
1955 proc = SCM_CEVAL (SCM_CAR (x), env);
1956 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1957#ifndef RECKLESS
1958#ifdef CAUTIOUS
1959 checkargs:
1960#endif
1961 if (SCM_CLOSUREP (proc))
1962 {
1963 arg2 = SCM_CAR (SCM_CODE (proc));
1964 t.arg1 = SCM_CDR (x);
1965 while (SCM_NIMP (arg2))
1966 {
1967 if (SCM_NCONSP (arg2))
1968 goto evapply;
1969 if (SCM_IMP (t.arg1))
1970 goto umwrongnumargs;
1971 arg2 = SCM_CDR (arg2);
1972 t.arg1 = SCM_CDR (t.arg1);
1973 }
1974 if (SCM_NNULLP (t.arg1))
1975 goto umwrongnumargs;
1976 }
1977 else if (scm_tc16_macro == SCM_TYP16 (proc))
1978 goto handle_a_macro;
1979#endif
1980 }
1981
1982
6dbd0af5
MD
1983evapply:
1984 PREP_APPLY (proc, SCM_EOL);
1985 if (SCM_NULLP (SCM_CDR (x))) {
1986 ENTER_APPLY;
0f2d19dd
JB
1987 switch (SCM_TYP7 (proc))
1988 { /* no arguments given */
1989 case scm_tc7_subr_0:
1990 RETURN (SCM_SUBRF (proc) ());
1991 case scm_tc7_subr_1o:
1992 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED));
1993 case scm_tc7_lsubr:
1994 RETURN (SCM_SUBRF (proc) (SCM_EOL));
1995 case scm_tc7_rpsubr:
1996 RETURN (SCM_BOOL_T);
1997 case scm_tc7_asubr:
1998 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
6dbd0af5 1999#ifdef CCLO
0f2d19dd
JB
2000 case scm_tc7_cclo:
2001 t.arg1 = proc;
2002 proc = SCM_CCLO_SUBR (proc);
6dbd0af5
MD
2003#ifdef DEVAL
2004 debug.info->a.proc = proc;
2005 debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
2006#endif
0f2d19dd 2007 goto evap1;
6dbd0af5 2008#endif
0f2d19dd
JB
2009 case scm_tcs_closures:
2010 x = SCM_CODE (proc);
2011 env = EXTEND_SCM_ENV (SCM_CAR (x), SCM_EOL, SCM_ENV (proc));
2012 goto cdrxbegin;
2013 case scm_tc7_contin:
2014 case scm_tc7_subr_1:
2015 case scm_tc7_subr_2:
2016 case scm_tc7_subr_2o:
2017 case scm_tc7_cxr:
2018 case scm_tc7_subr_3:
2019 case scm_tc7_lsubr_2:
2020 umwrongnumargs:
2021 unmemocar (x, env);
2022 wrongnumargs:
2023 scm_everr (x, env, proc, (char *) SCM_WNA, "");
2024 default:
2025 /* handle macros here */
2026 goto badfun;
2027 }
6dbd0af5 2028 }
0f2d19dd
JB
2029
2030 /* must handle macros by here */
2031 x = SCM_CDR (x);
2032#ifdef CAUTIOUS
2033 if (SCM_IMP (x))
2034 goto wrongnumargs;
2035#endif
2036 t.arg1 = EVALCAR (x, env);
6dbd0af5
MD
2037#ifdef DEVAL
2038 debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
2039#endif
0f2d19dd
JB
2040 x = SCM_CDR (x);
2041 if (SCM_NULLP (x))
2042 {
6dbd0af5 2043 ENTER_APPLY;
0f2d19dd
JB
2044 evap1:
2045 switch (SCM_TYP7 (proc))
6dbd0af5 2046 { /* have one argument in t.arg1 */
0f2d19dd
JB
2047 case scm_tc7_subr_2o:
2048 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
2049 case scm_tc7_subr_1:
2050 case scm_tc7_subr_1o:
2051 RETURN (SCM_SUBRF (proc) (t.arg1));
2052 case scm_tc7_cxr:
2053#ifdef SCM_FLOATS
2054 if (SCM_SUBRF (proc))
2055 {
2056 if (SCM_INUMP (t.arg1))
2057 {
2058 RETURN (scm_makdbl (SCM_DSUBRF (proc) ((double) SCM_INUM (t.arg1)),
2059 0.0));
2060 }
2061 SCM_ASRTGO (SCM_NIMP (t.arg1), floerr);
2062 if (SCM_REALP (t.arg1))
2063 {
2064 RETURN (scm_makdbl (SCM_DSUBRF (proc) (SCM_REALPART (t.arg1)), 0.0));
2065 }
2066#ifdef SCM_BIGDIG
2067 if (SCM_BIGP (t.arg1))
2068 {
2069 RETURN (scm_makdbl (SCM_DSUBRF (proc) (scm_big2dbl (t.arg1)), 0.0));
2070 }
2071#endif
2072 floerr:
2073 scm_wta (t.arg1, (char *) SCM_ARG1, SCM_CHARS (SCM_SNAME (proc)));
2074 }
2075#endif
2076 proc = (SCM) SCM_SNAME (proc);
2077 {
2078 char *chrs = SCM_CHARS (proc) + SCM_LENGTH (proc) - 1;
2079 while ('c' != *--chrs)
2080 {
2081 SCM_ASSERT (SCM_NIMP (t.arg1) && SCM_CONSP (t.arg1),
2082 t.arg1, SCM_ARG1, SCM_CHARS (proc));
2083 t.arg1 = ('a' == *chrs) ? SCM_CAR (t.arg1) : SCM_CDR (t.arg1);
2084 }
2085 RETURN (t.arg1);
2086 }
2087 case scm_tc7_rpsubr:
2088 RETURN (SCM_BOOL_T);
2089 case scm_tc7_asubr:
2090 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
2091 case scm_tc7_lsubr:
2092#ifdef DEVAL
6dbd0af5 2093 RETURN (SCM_SUBRF (proc) (debug.info->a.args))
0f2d19dd
JB
2094#else
2095 RETURN (SCM_SUBRF (proc) (scm_cons (t.arg1, SCM_EOL)));
2096#endif
6dbd0af5 2097#ifdef CCLO
0f2d19dd
JB
2098 case scm_tc7_cclo:
2099 arg2 = t.arg1;
2100 t.arg1 = proc;
2101 proc = SCM_CCLO_SUBR (proc);
6dbd0af5
MD
2102#ifdef DEVAL
2103 debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
2104 debug.info->a.proc = proc;
2105#endif
0f2d19dd 2106 goto evap2;
6dbd0af5 2107#endif
0f2d19dd
JB
2108 case scm_tcs_closures:
2109 x = SCM_CODE (proc);
2110#ifdef DEVAL
6dbd0af5 2111 env = EXTEND_SCM_ENV (SCM_CAR (x), debug.info->a.args, SCM_ENV (proc));
0f2d19dd
JB
2112#else
2113 env = EXTEND_SCM_ENV (SCM_CAR (x), scm_cons (t.arg1, SCM_EOL), SCM_ENV (proc));
2114#endif
2115 goto cdrxbegin;
2116 case scm_tc7_contin:
2117 scm_call_continuation (proc, t.arg1);
2118 case scm_tc7_subr_2:
2119 case scm_tc7_subr_0:
2120 case scm_tc7_subr_3:
2121 case scm_tc7_lsubr_2:
2122 goto wrongnumargs;
2123 default:
2124 goto badfun;
2125 }
2126 }
2127#ifdef CAUTIOUS
2128 if (SCM_IMP (x))
2129 goto wrongnumargs;
2130#endif
2131 { /* have two or more arguments */
2132 arg2 = EVALCAR (x, env);
6dbd0af5
MD
2133#ifdef DEVAL
2134 debug.info->a.args = scm_cons2 (t.arg1, arg2, SCM_EOL);
2135#endif
0f2d19dd
JB
2136 x = SCM_CDR (x);
2137 if (SCM_NULLP (x)) {
6dbd0af5 2138 ENTER_APPLY;
0f2d19dd
JB
2139#ifdef CCLO
2140 evap2:
2141#endif
6dbd0af5
MD
2142 switch (SCM_TYP7 (proc))
2143 { /* have two arguments */
2144 case scm_tc7_subr_2:
2145 case scm_tc7_subr_2o:
2146 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
2147 case scm_tc7_lsubr:
0f2d19dd 2148#ifdef DEVAL
6dbd0af5
MD
2149 RETURN (SCM_SUBRF (proc) (debug.info->a.args))
2150#else
2151 RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1, arg2, SCM_EOL)));
0f2d19dd 2152#endif
6dbd0af5
MD
2153 case scm_tc7_lsubr_2:
2154 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_EOL));
2155 case scm_tc7_rpsubr:
2156 case scm_tc7_asubr:
2157 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
2158#ifdef CCLO
2159 cclon:
2160 case scm_tc7_cclo:
0f2d19dd 2161#ifdef DEVAL
6dbd0af5
MD
2162 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc), proc,
2163 scm_cons (debug.info->a.args, SCM_EOL)));
0f2d19dd 2164#else
6dbd0af5
MD
2165 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc), proc,
2166 scm_cons2 (t.arg1, arg2,
2167 scm_cons (scm_eval_args (x, env), SCM_EOL))));
0f2d19dd 2168#endif
6dbd0af5
MD
2169 /* case scm_tc7_cclo:
2170 x = scm_cons(arg2, scm_eval_args(x, env));
2171 arg2 = t.arg1;
2172 t.arg1 = proc;
2173 proc = SCM_CCLO_SUBR(proc);
2174 goto evap3; */
2175#endif
2176 case scm_tc7_subr_0:
2177 case scm_tc7_cxr:
2178 case scm_tc7_subr_1o:
2179 case scm_tc7_subr_1:
2180 case scm_tc7_subr_3:
2181 case scm_tc7_contin:
2182 goto wrongnumargs;
2183 default:
2184 goto badfun;
2185 case scm_tcs_closures:
0f2d19dd 2186#ifdef DEVAL
6dbd0af5 2187 env = EXTEND_SCM_ENV (SCM_CAR (SCM_CODE (proc)), debug.info->a.args, SCM_ENV (proc));
0f2d19dd 2188#else
6dbd0af5 2189 env = EXTEND_SCM_ENV (SCM_CAR (SCM_CODE (proc)), scm_cons2 (t.arg1, arg2, SCM_EOL), SCM_ENV (proc));
0f2d19dd 2190#endif
6dbd0af5
MD
2191 x = SCM_CODE (proc);
2192 goto cdrxbegin;
2193 }
0f2d19dd
JB
2194 }
2195#ifdef DEVAL
6dbd0af5
MD
2196 debug.info->a.args = scm_cons2 (t.arg1, arg2,
2197 scm_deval_args (x, env, &SCM_CDR (SCM_CDR (debug.info->a.args))));
0f2d19dd 2198#endif
6dbd0af5
MD
2199 ENTER_APPLY;
2200 switch (SCM_TYP7 (proc))
2201 { /* have 3 or more arguments */
0f2d19dd 2202#ifdef DEVAL
6dbd0af5
MD
2203 case scm_tc7_subr_3:
2204 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
2205 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_CAR (SCM_CDR (SCM_CDR (debug.info->a.args)))));
2206 case scm_tc7_asubr:
2207 /* t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
2208 while SCM_NIMP(x) {
2209 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
2210 x = SCM_CDR(x);
2211 }
2212 RETURN (t.arg1) */
2213 case scm_tc7_rpsubr:
2214 RETURN (SCM_APPLY (proc, t.arg1, scm_acons (arg2, SCM_CDR (SCM_CDR (debug.info->a.args)), SCM_EOL)))
2215 case scm_tc7_lsubr_2:
2216 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_CDR (SCM_CDR (debug.info->a.args))))
2217 case scm_tc7_lsubr:
2218 RETURN (SCM_SUBRF (proc) (debug.info->a.args))
0f2d19dd 2219#ifdef CCLO
6dbd0af5
MD
2220 case scm_tc7_cclo:
2221 goto cclon;
0f2d19dd 2222#endif
6dbd0af5 2223 case scm_tcs_closures:
b7ff98dd 2224 SCM_SET_ARGSREADY (debug);
6dbd0af5
MD
2225 env = EXTEND_SCM_ENV (SCM_CAR (SCM_CODE (proc)),
2226 debug.info->a.args,
2227 SCM_ENV (proc));
2228 x = SCM_CODE (proc);
2229 goto cdrxbegin;
2230#else /* DEVAL */
2231 case scm_tc7_subr_3:
2232 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x)), wrongnumargs);
2233 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, EVALCAR (x, env)));
2234 case scm_tc7_asubr:
2235 /* t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
2236 while SCM_NIMP(x) {
2237 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
2238 x = SCM_CDR(x);
2239 }
2240 RETURN (t.arg1) */
2241 case scm_tc7_rpsubr:
2242 RETURN (SCM_APPLY (proc, t.arg1, scm_acons (arg2, scm_eval_args (x, env), SCM_EOL)));
2243 case scm_tc7_lsubr_2:
2244 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, scm_eval_args (x, env)));
2245 case scm_tc7_lsubr:
2246 RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1, arg2, scm_eval_args (x, env))));
0f2d19dd 2247#ifdef CCLO
6dbd0af5
MD
2248 case scm_tc7_cclo:
2249 goto cclon;
0f2d19dd 2250#endif
6dbd0af5
MD
2251 case scm_tcs_closures:
2252#ifdef DEVAL
b7ff98dd 2253 SCM_SET_ARGSREADY (debug);
6dbd0af5
MD
2254#endif
2255 env = EXTEND_SCM_ENV (SCM_CAR (SCM_CODE (proc)),
2256 scm_cons2 (t.arg1, arg2, scm_eval_args (x, env)),
2257 SCM_ENV (proc));
2258 x = SCM_CODE (proc);
2259 goto cdrxbegin;
0f2d19dd 2260#endif /* DEVAL */
6dbd0af5
MD
2261 case scm_tc7_subr_2:
2262 case scm_tc7_subr_1o:
2263 case scm_tc7_subr_2o:
2264 case scm_tc7_subr_0:
2265 case scm_tc7_cxr:
2266 case scm_tc7_subr_1:
2267 case scm_tc7_contin:
2268 goto wrongnumargs;
2269 default:
2270 goto badfun;
2271 }
0f2d19dd
JB
2272 }
2273#ifdef DEVAL
6dbd0af5
MD
2274exit:
2275 if (CHECK_EXIT)
b7ff98dd 2276 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
6dbd0af5 2277 {
b7ff98dd
MD
2278 SCM_EXIT_FRAME_P = 0;
2279 SCM_RESET_DEBUG_MODE;
2280 SCM_CLEAR_TRACED_FRAME (debug);
2281 if (SCM_CHEAPTRAPS_P)
6dbd0af5
MD
2282 t.arg1 = scm_make_debugobj ((scm_debug_frame *) &debug);
2283 else
2284 {
2285 scm_make_cont (&t.arg1);
2286 if (setjmp (SCM_JMPBUF (t.arg1)))
2287 {
2288 proc = SCM_THROW_VALUE (t.arg1);
2289 goto ret;
2290 }
2291 }
2292 scm_ithrow (scm_i_exit_frame, scm_cons2 (t.arg1, proc, SCM_EOL), 0);
2293 }
2294ret:
2295 last_debug_info_frame = debug.prev;
0f2d19dd
JB
2296 return proc;
2297#endif
2298}
2299
6dbd0af5
MD
2300
2301/* SECTION: This code is compiled once.
2302 */
2303
0f2d19dd
JB
2304#ifndef DEVAL
2305
2306SCM_PROC(s_procedure_documentation, "procedure-documentation", 1, 0, 0, scm_procedure_documentation);
2307#ifdef __STDC__
2308SCM
2309scm_procedure_documentation (SCM proc)
2310#else
2311SCM
2312scm_procedure_documentation (proc)
2313 SCM proc;
2314#endif
2315{
2316 SCM code;
2317 SCM_ASSERT (SCM_BOOL_T == scm_procedure_p (proc) && SCM_NIMP (proc) && SCM_TYP7 (proc) != scm_tc7_contin,
2318 proc, SCM_ARG1, s_procedure_documentation);
2319 switch (SCM_TYP7 (proc))
2320 {
2321 case scm_tcs_closures:
2322 code = SCM_CDR (SCM_CODE (proc));
2323 if (SCM_IMP (SCM_CDR (code)))
2324 return SCM_BOOL_F;
2325 code = SCM_CAR (code);
2326 if (SCM_IMP (code))
2327 return SCM_BOOL_F;
2328 if (SCM_STRINGP (code))
2329 return code;
2330 default:
2331 return SCM_BOOL_F;
2332/*
2333 case scm_tcs_subrs:
2334#ifdef CCLO
2335 case scm_tc7_cclo:
2336#endif
2337*/
2338 }
2339}
2340
2341/* This code is for scm_apply. it is destructive on multiple args.
2342 * This will only screw you if you do (scm_apply scm_apply '( ... ))
2343 */
2344SCM_PROC(s_nconc2last, "apply:nconc2last", 1, 0, 0, scm_nconc2last);
2345#ifdef __STDC__
2346SCM
2347scm_nconc2last (SCM lst)
2348#else
2349SCM
2350scm_nconc2last (lst)
2351 SCM lst;
2352#endif
2353{
2354 SCM *lloc;
2355 if (SCM_EOL == lst)
2356 return lst;
2357 SCM_ASSERT (SCM_NIMP (lst) && SCM_CONSP (lst), lst, SCM_ARG1, s_nconc2last);
2358 lloc = &lst;
2359 while (SCM_NNULLP (SCM_CDR (*lloc)))
2360 {
2361 lloc = &SCM_CDR (*lloc);
2362 SCM_ASSERT (SCM_NIMP (*lloc) && SCM_CONSP (*lloc), lst, SCM_ARG1, s_nconc2last);
2363 }
2364 *lloc = SCM_CAR (*lloc);
2365 return lst;
2366}
2367
2368#endif /* !DEVAL */
2369
6dbd0af5
MD
2370
2371/* SECTION: When DEVAL is defined this code yields scm_dapply.
2372 * It is compiled twice.
2373 */
2374
0f2d19dd
JB
2375#if 0
2376#ifdef __STDC__
2377SCM
2378scm_apply (SCM proc, SCM arg1, SCM args)
2379#else
2380SCM
2381scm_apply (proc, arg1, args)
2382 SCM proc;
2383 SCM arg1;
2384 SCM args;
2385#endif
2386{}
2387#endif
2388
2389#if 0
2390#ifdef __STDC__
2391SCM
2392scm_dapply (SCM proc, SCM arg1, SCM args)
2393#else
2394SCM
2395scm_dapply (proc, arg1, args)
2396 SCM proc;
2397 SCM arg1;
2398 SCM args;
2399#endif
2400{}
2401#endif
2402
0f2d19dd
JB
2403#ifdef __STDC__
2404SCM
2405SCM_APPLY (SCM proc, SCM arg1, SCM args)
2406#else
2407SCM
2408SCM_APPLY (proc, arg1, args)
2409 SCM proc;
2410 SCM arg1;
2411 SCM args;
2412#endif
2413{
2414#ifdef DEBUG_EXTENSIONS
2415#ifdef DEVAL
6dbd0af5
MD
2416 scm_debug_frame debug;
2417 debug.prev = last_debug_info_frame;
b7ff98dd 2418 debug.status = SCM_APPLYFRAME;
6dbd0af5
MD
2419 debug.vect[0].a.proc = proc;
2420 debug.vect[0].a.args = SCM_EOL;
2421 last_debug_info_frame = &debug;
0f2d19dd 2422#else
b7ff98dd 2423 if (SCM_DEBUGGINGP)
0f2d19dd
JB
2424 return scm_dapply (proc, arg1, args);
2425#endif
2426#endif
2427
2428 SCM_ASRTGO (SCM_NIMP (proc), badproc);
2429 if (SCM_NULLP (args))
2430 {
2431 if (SCM_NULLP (arg1))
2432 arg1 = SCM_UNDEFINED;
2433 else
2434 {
2435 args = SCM_CDR (arg1);
2436 arg1 = SCM_CAR (arg1);
2437 }
2438 }
2439 else
2440 {
2441 /* SCM_ASRTGO(SCM_NIMP(args) && SCM_CONSP(args), wrongnumargs); */
2442 args = scm_nconc2last (args);
2443 }
0f2d19dd 2444#ifdef DEVAL
6dbd0af5 2445 debug.vect[0].a.args = scm_cons (arg1, args);
b7ff98dd 2446 if (SCM_ENTER_FRAME_P)
6dbd0af5
MD
2447 {
2448 SCM tmp;
b7ff98dd
MD
2449 SCM_ENTER_FRAME_P = 0;
2450 SCM_RESET_DEBUG_MODE;
2451 if (SCM_CHEAPTRAPS_P)
6dbd0af5
MD
2452 tmp = scm_make_debugobj ((scm_debug_frame *) &debug);
2453 else
2454 {
2455 scm_make_cont (&tmp);
2456 if (setjmp (SCM_JMPBUF (tmp)))
2457 goto entap;
2458 }
2459 scm_ithrow (scm_i_enter_frame, scm_cons (tmp, SCM_EOL), 0);
2460 }
2461entap:
2462 ENTER_APPLY;
2463#endif
2464#ifdef CCLO
2465tail:
0f2d19dd
JB
2466#endif
2467 switch (SCM_TYP7 (proc))
2468 {
2469 case scm_tc7_subr_2o:
2470 args = SCM_NULLP (args) ? SCM_UNDEFINED : SCM_CAR (args);
2471 RETURN (SCM_SUBRF (proc) (arg1, args))
2472 case scm_tc7_subr_2:
2473 SCM_ASRTGO (SCM_NULLP (SCM_CDR (args)), wrongnumargs);
2474 args = SCM_CAR (args);
2475 RETURN (SCM_SUBRF (proc) (arg1, args))
2476 case scm_tc7_subr_0:
2477 SCM_ASRTGO (SCM_UNBNDP (arg1), wrongnumargs);
2478 RETURN (SCM_SUBRF (proc) ())
2479 case scm_tc7_subr_1:
2480 case scm_tc7_subr_1o:
2481 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
2482 RETURN (SCM_SUBRF (proc) (arg1))
2483 case scm_tc7_cxr:
2484 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
2485#ifdef SCM_FLOATS
2486 if (SCM_SUBRF (proc))
2487 {
6dbd0af5
MD
2488 if (SCM_INUMP (arg1))
2489 {
2490 RETURN (scm_makdbl (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1)), 0.0));
2491 }
0f2d19dd 2492 SCM_ASRTGO (SCM_NIMP (arg1), floerr);
6dbd0af5
MD
2493 if (SCM_REALP (arg1))
2494 {
2495 RETURN (scm_makdbl (SCM_DSUBRF (proc) (SCM_REALPART (arg1)), 0.0));
2496 }
0f2d19dd
JB
2497#ifdef SCM_BIGDIG
2498 if SCM_BIGP
2499 (arg1)
2500 RETURN (scm_makdbl (SCM_DSUBRF (proc) (scm_big2dbl (arg1)), 0.0))
2501#endif
2502 floerr:
2503 scm_wta (arg1, (char *) SCM_ARG1, SCM_CHARS (SCM_SNAME (proc)));
2504 }
2505#endif
2506 proc = (SCM) SCM_SNAME (proc);
2507 {
2508 char *chrs = SCM_CHARS (proc) + SCM_LENGTH (proc) - 1;
2509 while ('c' != *--chrs)
2510 {
2511 SCM_ASSERT (SCM_NIMP (arg1) && SCM_CONSP (arg1),
2512 arg1, SCM_ARG1, SCM_CHARS (proc));
2513 arg1 = ('a' == *chrs) ? SCM_CAR (arg1) : SCM_CDR (arg1);
2514 }
2515 RETURN (arg1)
2516 }
2517 case scm_tc7_subr_3:
2518 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CAR (SCM_CDR (args))))
2519 case scm_tc7_lsubr:
2520#ifdef DEVAL
6dbd0af5 2521 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args))
0f2d19dd
JB
2522#else
2523 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)))
2524#endif
2525 case scm_tc7_lsubr_2:
2526 SCM_ASRTGO (SCM_NIMP (args) && SCM_CONSP (args), wrongnumargs);
2527 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CDR (args)))
2528 case scm_tc7_asubr:
2529 if (SCM_NULLP (args))
2530 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED))
2531 while (SCM_NIMP (args))
2532 {
2533 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
2534 arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
2535 args = SCM_CDR (args);
2536 }
2537 RETURN (arg1);
2538 case scm_tc7_rpsubr:
2539 if (SCM_NULLP (args))
2540 RETURN (SCM_BOOL_T);
2541 while (SCM_NIMP (args))
2542 {
2543 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
2544 if (SCM_FALSEP (SCM_SUBRF (proc) (arg1, SCM_CAR (args))))
2545 RETURN (SCM_BOOL_F);
2546 arg1 = SCM_CAR (args);
2547 args = SCM_CDR (args);
2548 }
2549 RETURN (SCM_BOOL_T);
2550 case scm_tcs_closures:
2551#ifdef DEVAL
6dbd0af5 2552 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
0f2d19dd
JB
2553#else
2554 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
2555#endif
2556#ifndef RECKLESS
2557 if (scm_badargsp (SCM_CAR (SCM_CODE (proc)), arg1))
2558 goto wrongnumargs;
2559#endif
2560 args = EXTEND_SCM_ENV (SCM_CAR (SCM_CODE (proc)), arg1, SCM_ENV (proc));
2561 proc = SCM_CODE (proc);
2562 while (SCM_NNULLP (proc = SCM_CDR (proc)))
2563 arg1 = EVALCAR (proc, args);
2564 RETURN (arg1);
2565 case scm_tc7_contin:
2566 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
2567 scm_call_continuation (proc, arg1);
2568#ifdef CCLO
2569 case scm_tc7_cclo:
2570#ifdef DEVAL
6dbd0af5
MD
2571 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
2572 arg1 = proc;
2573 proc = SCM_CCLO_SUBR (proc);
2574 debug.vect[0].a.proc = proc;
2575 debug.vect[0].a.args = scm_cons (arg1, args);
0f2d19dd
JB
2576#else
2577 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
0f2d19dd
JB
2578 arg1 = proc;
2579 proc = SCM_CCLO_SUBR (proc);
6dbd0af5 2580#endif
0f2d19dd
JB
2581 goto tail;
2582#endif
2583 wrongnumargs:
2584 scm_wta (proc, (char *) SCM_WNA, "apply");
2585 default:
2586 badproc:
2587 scm_wta (proc, (char *) SCM_ARG1, "apply");
2588 RETURN (arg1);
2589 }
2590#ifdef DEVAL
6dbd0af5
MD
2591exit:
2592 if (CHECK_EXIT)
b7ff98dd 2593 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
6dbd0af5 2594 {
b7ff98dd
MD
2595 SCM_EXIT_FRAME_P = 0;
2596 SCM_RESET_DEBUG_MODE;
2597 SCM_CLEAR_TRACED_FRAME (debug);
2598 if (SCM_CHEAPTRAPS_P)
6dbd0af5
MD
2599 arg1 = scm_make_debugobj ((scm_debug_frame *) &debug);
2600 else
2601 {
2602 scm_make_cont (&arg1);
2603 if (setjmp (SCM_JMPBUF (arg1)))
2604 {
2605 proc = SCM_THROW_VALUE (arg1);
2606 goto ret;
2607 }
2608 }
2609 scm_ithrow (scm_i_exit_frame, scm_cons2 (arg1, proc, SCM_EOL), 0);
2610 }
2611ret:
2612 last_debug_info_frame = debug.prev;
0f2d19dd
JB
2613 return proc;
2614#endif
2615}
2616
6dbd0af5
MD
2617
2618/* SECTION: The rest of this file is only read once.
2619 */
2620
0f2d19dd
JB
2621#ifndef DEVAL
2622
2623SCM_PROC(s_map, "map", 2, 0, 1, scm_map);
2624#ifdef __STDC__
2625SCM
2626scm_map (SCM proc, SCM arg1, SCM args)
2627#else
2628SCM
2629scm_map (proc, arg1, args)
2630 SCM proc;
2631 SCM arg1;
2632 SCM args;
2633#endif
2634{
2635 long i;
2636 SCM res = SCM_EOL;
2637 SCM *pres = &res;
2638 SCM *ve = &args; /* Keep args from being optimized away. */
2639
2640 if (SCM_NULLP (arg1))
2641 return res;
2642 SCM_ASSERT (SCM_NIMP (arg1), arg1, SCM_ARG2, s_map);
2643 if (SCM_NULLP (args))
2644 {
2645 while (SCM_NIMP (arg1))
2646 {
2647 SCM_ASSERT (SCM_CONSP (arg1), arg1, SCM_ARG2, s_map);
2648 *pres = scm_cons (scm_apply (proc, SCM_CAR (arg1), scm_listofnull), SCM_EOL);
2649 pres = &SCM_CDR (*pres);
2650 arg1 = SCM_CDR (arg1);
2651 }
2652 return res;
2653 }
2654 args = scm_vector (scm_cons (arg1, args));
2655 ve = SCM_VELTS (args);
2656#ifndef RECKLESS
2657 for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
2658 SCM_ASSERT (SCM_NIMP (ve[i]) && SCM_CONSP (ve[i]), args, SCM_ARG2, s_map);
2659#endif
2660 while (1)
2661 {
2662 arg1 = SCM_EOL;
2663 for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
2664 {
2665 if SCM_IMP
2666 (ve[i]) return res;
2667 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
2668 ve[i] = SCM_CDR (ve[i]);
2669 }
2670 *pres = scm_cons (scm_apply (proc, arg1, SCM_EOL), SCM_EOL);
2671 pres = &SCM_CDR (*pres);
2672 }
2673}
2674
2675
2676SCM_PROC(s_for_each, "for-each", 2, 0, 1, scm_for_each);
2677#ifdef __STDC__
2678SCM
2679scm_for_each (SCM proc, SCM arg1, SCM args)
2680#else
2681SCM
2682scm_for_each (proc, arg1, args)
2683 SCM proc;
2684 SCM arg1;
2685 SCM args;
2686#endif
2687{
2688 SCM *ve = &args; /* Keep args from being optimized away. */
2689 long i;
2690 if SCM_NULLP (arg1)
2691 return SCM_UNSPECIFIED;
2692 SCM_ASSERT (SCM_NIMP (arg1), arg1, SCM_ARG2, s_for_each);
2693 if SCM_NULLP (args)
2694 {
2695 while SCM_NIMP (arg1)
2696 {
2697 SCM_ASSERT (SCM_CONSP (arg1), arg1, SCM_ARG2, s_for_each);
2698 scm_apply (proc, SCM_CAR (arg1), scm_listofnull);
2699 arg1 = SCM_CDR (arg1);
2700 }
2701 return SCM_UNSPECIFIED;
2702 }
2703 args = scm_vector (scm_cons (arg1, args));
2704 ve = SCM_VELTS (args);
2705#ifndef RECKLESS
2706 for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
2707 SCM_ASSERT (SCM_NIMP (ve[i]) && SCM_CONSP (ve[i]), args, SCM_ARG2, s_for_each);
2708#endif
2709 while (1)
2710 {
2711 arg1 = SCM_EOL;
2712 for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
2713 {
2714 if SCM_IMP
2715 (ve[i]) return SCM_UNSPECIFIED;
2716 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
2717 ve[i] = SCM_CDR (ve[i]);
2718 }
2719 scm_apply (proc, arg1, SCM_EOL);
2720 }
2721}
2722
2723
2724#ifdef __STDC__
2725SCM
2726scm_closure (SCM code, SCM env)
2727#else
2728SCM
2729scm_closure (code, env)
2730 SCM code;
2731 SCM env;
2732#endif
2733{
2734 register SCM z;
2735 SCM_NEWCELL (z);
2736 SCM_SETCODE (z, code);
2737 SCM_ENV (z) = env;
2738 return z;
2739}
2740
2741
2742long scm_tc16_promise;
2743#ifdef __STDC__
2744SCM
2745scm_makprom (SCM code)
2746#else
2747SCM
2748scm_makprom (code)
2749 SCM code;
2750#endif
2751{
2752 register SCM z;
2753 SCM_NEWCELL (z);
2754 SCM_CDR (z) = code;
2755 SCM_CAR (z) = scm_tc16_promise;
2756 return z;
2757}
2758
2759
2760#ifdef __STDC__
2761static int
2762prinprom (SCM exp, SCM port, int writing)
2763#else
2764static int
2765prinprom (exp, port, writing)
2766 SCM exp;
2767 SCM port;
2768 int writing;
2769#endif
2770{
2771 scm_gen_puts (scm_regular_string, "#<promise ", port);
2772 scm_iprin1 (SCM_CDR (exp), port, writing);
2773 scm_gen_putc ('>', port);
2774 return !0;
2775}
2776
2777
2778SCM_PROC(s_makacro, "procedure->syntax", 1, 0, 0, scm_makacro);
2779#ifdef __STDC__
2780SCM
2781scm_makacro (SCM code)
2782#else
2783SCM
2784scm_makacro (code)
2785 SCM code;
2786#endif
2787{
2788 register SCM z;
2789 SCM_NEWCELL (z);
2790 SCM_CDR (z) = code;
2791 SCM_CAR (z) = scm_tc16_macro;
2792 return z;
2793}
2794
2795
2796SCM_PROC(s_makmacro, "procedure->macro", 1, 0, 0, scm_makmacro);
2797#ifdef __STDC__
2798SCM
2799scm_makmacro (SCM code)
2800#else
2801SCM
2802scm_makmacro (code)
2803 SCM code;
2804#endif
2805{
2806 register SCM z;
2807 SCM_NEWCELL (z);
2808 SCM_CDR (z) = code;
2809 SCM_CAR (z) = scm_tc16_macro | (1L << 16);
2810 return z;
2811}
2812
2813
2814SCM_PROC(s_makmmacro, "procedure->memoizing-macro", 1, 0, 0, scm_makmmacro);
2815#ifdef __STDC__
2816SCM
2817scm_makmmacro (SCM code)
2818#else
2819SCM
2820scm_makmmacro (code)
2821 SCM code;
2822#endif
2823{
2824 register SCM z;
2825 SCM_NEWCELL (z);
2826 SCM_CDR (z) = code;
2827 SCM_CAR (z) = scm_tc16_macro | (2L << 16);
2828 return z;
2829}
2830
2831
2832#ifdef __STDC__
2833static int
2834prinmacro (SCM exp, SCM port, int writing)
2835#else
2836static int
2837prinmacro (exp, port, writing)
2838 SCM exp;
2839 SCM port;
2840 int writing;
2841#endif
2842{
2843 if (SCM_CAR (exp) & (3L << 16))
2844 scm_gen_puts (scm_regular_string, "#<macro", port);
2845 else
2846 scm_gen_puts (scm_regular_string, "#<syntax", port);
2847 if (SCM_CAR (exp) & (2L << 16))
2848 scm_gen_putc ('!', port);
2849 scm_gen_putc (' ', port);
2850 scm_iprin1 (SCM_CDR (exp), port, writing);
2851 scm_gen_putc ('>', port);
2852 return !0;
2853}
2854
2855SCM_PROC(s_force, "force", 1, 0, 0, scm_force);
2856#ifdef __STDC__
2857SCM
2858scm_force (SCM x)
2859#else
2860SCM
2861scm_force (x)
2862 SCM x;
2863#endif
2864{
2865 SCM_ASSERT ((SCM_TYP16 (x) == scm_tc16_promise), x, SCM_ARG1, s_force);
2866 if (!((1L << 16) & SCM_CAR (x)))
2867 {
2868 SCM ans = scm_apply (SCM_CDR (x), SCM_EOL, SCM_EOL);
2869 if (!((1L << 16) & SCM_CAR (x)))
2870 {
2871 SCM_DEFER_INTS;
2872 SCM_CDR (x) = ans;
2873 SCM_CAR (x) |= (1L << 16);
2874 SCM_ALLOW_INTS;
2875 }
2876 }
2877 return SCM_CDR (x);
2878}
2879
2880SCM_PROC (s_promise_p, "promise?", 1, 0, 0, scm_promise_p);
2881#ifdef __STDC__
2882SCM
2883scm_promise_p (SCM x)
2884#else
2885SCM
2886scm_promise_p (x)
2887 SCM x;
2888#endif
2889{
2890 return ((SCM_NIMP (x) && (SCM_TYP16 (x) == scm_tc16_promise))
2891 ? SCM_BOOL_T
2892 : SCM_BOOL_F);
2893}
2894
2895SCM_PROC(s_copy_tree, "copy-tree", 1, 0, 0, scm_copy_tree);
2896#ifdef __STDC__
2897SCM
2898scm_copy_tree (SCM obj)
2899#else
2900SCM
2901scm_copy_tree (obj)
2902 SCM obj;
2903#endif
2904{
2905 SCM ans, tl;
2906 if SCM_IMP
2907 (obj) return obj;
2908 if (SCM_VECTORP (obj))
2909 {
2910 scm_sizet i = SCM_LENGTH (obj);
2911 ans = scm_make_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED, SCM_UNDEFINED);
2912 while (i--)
2913 SCM_VELTS (ans)[i] = scm_copy_tree (SCM_VELTS (obj)[i]);
2914 return ans;
2915 }
2916 if SCM_NCONSP (obj)
2917 return obj;
2918/* return scm_cons(scm_copy_tree(SCM_CAR(obj)), scm_copy_tree(SCM_CDR(obj))); */
2919 ans = tl = scm_cons (scm_copy_tree (SCM_CAR (obj)), SCM_UNSPECIFIED);
2920 while (SCM_NIMP (obj = SCM_CDR (obj)) && SCM_CONSP (obj))
2921 tl = (SCM_CDR (tl) = scm_cons (scm_copy_tree (SCM_CAR (obj)), SCM_UNSPECIFIED));
2922 SCM_CDR (tl) = obj;
2923 return ans;
2924}
2925
2926#ifdef __STDC__
2927SCM
2928scm_eval_3 (SCM obj, int copyp, SCM env)
2929#else
2930SCM
2931scm_eval_3 (obj, copyp, env)
2932 SCM obj;
2933 int copyp;
2934 SCM env;
2935#endif
2936{
2937 if (SCM_NIMP (SCM_CDR (scm_system_transformer)))
2938 obj = scm_apply (SCM_CDR (scm_system_transformer), obj, scm_listofnull);
2939 else if (copyp)
2940 obj = scm_copy_tree (obj);
6dbd0af5 2941 return XEVAL (obj, env);
0f2d19dd
JB
2942}
2943
2944#ifdef __STDC__
2945SCM
2946scm_top_level_env (SCM thunk)
2947#else
2948SCM
2949scm_top_level_env (thunk)
2950 SCM thunk;
2951#endif
2952{
2953 if (SCM_IMP(thunk))
2954 return SCM_EOL;
2955 else
2956 return scm_cons(thunk, (SCM)SCM_EOL);
2957}
2958
2959SCM_PROC(s_eval2, "eval2", 2, 0, 0, scm_eval2);
2960#ifdef __STDC__
2961SCM
2962scm_eval2 (SCM obj, SCM env_thunk)
2963#else
2964SCM
2965scm_eval2 (obj, env_thunk)
2966 SCM obj;
2967 SCM env_thunk;
2968#endif
2969{
2970 return scm_eval_3 (obj, 1, scm_top_level_env(env_thunk));
2971}
2972
2973SCM_PROC(s_eval, "eval", 1, 0, 0, scm_eval);
2974#ifdef __STDC__
2975SCM
2976scm_eval (SCM obj)
2977#else
2978SCM
2979scm_eval (obj)
2980 SCM obj;
2981#endif
2982{
2983 return
2984 scm_eval_3(obj, 1, scm_top_level_env(SCM_CDR(scm_top_level_lookup_thunk_var)));
2985}
2986
2987SCM_PROC(s_eval_x, "eval!", 1, 0, 0, scm_eval_x);
2988#ifdef __STDC__
2989SCM
2990scm_eval_x (SCM obj)
2991#else
2992SCM
2993scm_eval_x (obj)
2994 SCM obj;
2995#endif
2996{
2997 return
2998 scm_eval_3(obj,
2999 0,
3000 scm_top_level_env (SCM_CDR (scm_top_level_lookup_thunk_var)));
3001}
3002
3003SCM_PROC (s_macro_eval_x, "macro-eval!", 2, 0, 0, scm_macro_eval_x);
3004#ifdef __STDC__
3005SCM
3006scm_macro_eval_x (SCM exp, SCM env)
3007#else
3008SCM
3009scm_macro_eval_x (exp, env)
3010 SCM exp;
3011 SCM env;
3012#endif
3013{
3014 return scm_eval_3 (exp, 0, env);
3015}
3016
3017#ifdef __STDC__
3018SCM
3019scm_definedp (SCM x, SCM env)
3020#else
3021SCM
3022scm_definedp (x, env)
3023 SCM x;
3024 SCM env;
3025#endif
3026{
3027 SCM proc = SCM_CAR (x = SCM_CDR (x));
3028 if (SCM_ISYMP (proc))
3029 return SCM_BOOL_T;
3030 else if(SCM_IMP(proc) || !SCM_SYMBOLP(proc))
3031 return SCM_BOOL_F;
3032 else
3033 {
3034 SCM vcell = scm_sym2vcell(proc, env_top_level(env), SCM_BOOL_F);
3035 return (vcell == SCM_BOOL_F || SCM_UNBNDP(SCM_CDR(vcell))) ? SCM_BOOL_F : SCM_BOOL_T;
3036 }
3037}
3038
3039static scm_smobfuns promsmob =
3040{scm_markcdr, scm_free0, prinprom};
3041
3042static scm_smobfuns macrosmob =
3043{scm_markcdr, scm_free0, prinmacro};
3044
3045#ifdef __STDC__
3046SCM
3047scm_make_synt (char *name, SCM (*macroizer) (), SCM (*fcn) ())
3048#else
3049SCM
3050scm_make_synt (name, macroizer, fcn)
3051 char *name;
3052 SCM (*macroizer) ();
3053 SCM (*fcn) ();
3054#endif
3055{
3056 SCM symcell = scm_sysintern (name, SCM_UNDEFINED);
3057 long tmp = ((((SCM_CELLPTR) (SCM_CAR (symcell))) - scm_heap_org) << 8);
3058 register SCM z;
3059 if ((tmp >> 8) != ((SCM_CELLPTR) (SCM_CAR (symcell)) - scm_heap_org))
3060 tmp = 0;
3061 SCM_NEWCELL (z);
3062 SCM_SUBRF (z) = fcn;
3063 SCM_CAR (z) = tmp + scm_tc7_subr_2;
3064 SCM_CDR (symcell) = macroizer (z);
3065 return SCM_CAR (symcell);
3066}
3067
6dbd0af5
MD
3068
3069/* At this point, scm_deval and scm_dapply are generated.
3070 */
3071
0f2d19dd 3072#ifdef DEBUG_EXTENSIONS
6dbd0af5
MD
3073# define DEVAL
3074# include "eval.c"
0f2d19dd
JB
3075#endif
3076
3077
3078#ifdef __STDC__
3079void
3080scm_init_eval (void)
3081#else
3082void
3083scm_init_eval ()
3084#endif
3085{
0f2d19dd
JB
3086 scm_tc16_promise = scm_newsmob (&promsmob);
3087 scm_tc16_macro = scm_newsmob (&macrosmob);
3088 scm_i_apply = scm_make_subr ("apply", scm_tc7_lsubr_2, scm_apply);
3089 scm_system_transformer = scm_sysintern ("scm:eval-transformer", SCM_UNDEFINED);
3090 scm_i_dot = SCM_CAR (scm_sysintern (".", SCM_UNDEFINED));
3091 scm_i_arrow = SCM_CAR (scm_sysintern ("=>", SCM_UNDEFINED));
3092 scm_i_else = SCM_CAR (scm_sysintern ("else", SCM_UNDEFINED));
3093 scm_i_unquote = SCM_CAR (scm_sysintern ("unquote", SCM_UNDEFINED));
3094 scm_i_uq_splicing = SCM_CAR (scm_sysintern ("unquote-splicing", SCM_UNDEFINED));
3095
3096 /* acros */
3097 scm_i_quasiquote = scm_make_synt (s_quasiquote, scm_makacro, scm_m_quasiquote);
6dbd0af5 3098 scm_make_synt (s_undefine, scm_makacro, scm_m_undefine);
0f2d19dd
JB
3099 scm_make_synt (s_delay, scm_makacro, scm_m_delay);
3100 /* end of acros */
3101
3102 scm_top_level_lookup_thunk_var =
3103 scm_sysintern("*top-level-lookup-thunk*", SCM_BOOL_F);
3104
6dbd0af5
MD
3105 scm_i_and = scm_make_synt ("and", scm_makmmacro, scm_m_and);
3106 scm_i_begin = scm_make_synt ("begin", scm_makmmacro, scm_m_begin);
3107 scm_i_case = scm_make_synt ("case", scm_makmmacro, scm_m_case);
3108 scm_i_cond = scm_make_synt ("cond", scm_makmmacro, scm_m_cond);
3109 scm_i_define = scm_make_synt ("define", scm_makmmacro, scm_m_define);
3110 scm_i_do = scm_make_synt ("do", scm_makmmacro, scm_m_do);
3111 scm_i_if = scm_make_synt ("if", scm_makmmacro, scm_m_if);
0f2d19dd
JB
3112 scm_i_lambda = scm_make_synt ("lambda", scm_makmmacro, scm_m_lambda);
3113 scm_i_let = scm_make_synt ("let", scm_makmmacro, scm_m_let);
6dbd0af5
MD
3114 scm_i_letrec = scm_make_synt ("letrec", scm_makmmacro, scm_m_letrec);
3115 scm_i_letstar = scm_make_synt ("let*", scm_makmmacro, scm_m_letstar);
3116 scm_i_or = scm_make_synt ("or", scm_makmmacro, scm_m_or);
0f2d19dd 3117 scm_i_quote = scm_make_synt ("quote", scm_makmmacro, scm_m_quote);
6dbd0af5
MD
3118 scm_i_set = scm_make_synt ("set!", scm_makmmacro, scm_m_set);
3119 scm_i_atapply = scm_make_synt ("@apply", scm_makmmacro, scm_m_apply);
3120 scm_i_atcall_cc = scm_make_synt ("@call-with-current-continuation",
3121 scm_makmmacro, scm_m_cont);
3122
0f2d19dd
JB
3123 scm_make_synt ("defined?", scm_makmmacro, scm_definedp);
3124 scm_i_name = SCM_CAR (scm_sysintern ("name", SCM_UNDEFINED));
3125 scm_permanent_object (scm_i_name);
6dbd0af5
MD
3126
3127#ifdef DEBUG_EXTENSIONS
3128 scm_i_enter_frame = SCM_CAR (scm_sysintern ("enter-frame", SCM_UNDEFINED));
3129 scm_i_apply_frame = SCM_CAR (scm_sysintern ("apply-frame", SCM_UNDEFINED));
3130 scm_i_exit_frame = SCM_CAR (scm_sysintern ("exit-frame", SCM_UNDEFINED));
3131 scm_i_trace = SCM_CAR (scm_sysintern ("trace", SCM_UNDEFINED));
3132#endif
3133
0f2d19dd
JB
3134#include "eval.x"
3135}
0f2d19dd 3136
6dbd0af5 3137#endif /* !DEVAL */