1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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)
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.
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.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
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.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
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.
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.
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:".
52 /* SECTION: This code is compiled once.
62 #include "sequences.h"
64 #include "continuations.h"
72 #ifdef DEBUG_EXTENSIONS
74 #endif /* DEBUG_EXTENSIONS */
82 /* The evaluator contains a plethora of EVAL symbols.
83 * This is an attempt at explanation.
85 * The following macros should be used in code which is read twice
86 * (where the choice of evaluator is hard soldered):
88 * SCM_CEVAL is the symbol used within one evaluator to call itself.
89 * Originally, it is defined to scm_ceval, but is redefined to
90 * scm_deval during the second pass.
92 * SIDEVAL corresponds to SCM_CEVAL, but is used in situations where
93 * only side effects of expressions matter. All immediates are
96 * EVALIM is used when it is known that the expression is an
97 * immediate. (This macro never calls an evaluator.)
99 * EVALCAR evaluates the car of an expression.
101 * EVALCELLCAR is like EVALCAR, but is used when it is known that the
102 * car is a lisp cell.
104 * The following macros should be used in code which is read once
105 * (where the choice of evaluator is dynamic):
107 * XEVAL takes care of immediates without calling an evaluator. It
108 * then calls scm_ceval *or* scm_deval, depending on the debugging
111 * XEVALCAR corresponds to EVALCAR, but uses scm_ceval *or* scm_deval
112 * depending on the debugging mode.
114 * The main motivation for keeping this plethora is efficiency
115 * together with maintainability (=> locality of code).
118 #define EVALCELLCAR(x, env) (SCM_SYMBOLP (SCM_CAR(x)) \
119 ? *scm_lookupcar(x, env) \
120 : SCM_CEVAL(SCM_CAR(x), env))
122 #ifdef MEMOIZE_LOCALS
123 #define EVALIM(x, env) (SCM_ILOCP(x)?*scm_ilookup((x), env):x)
125 #define EVALIM(x, env) x
127 #define EVALCAR(x, env) (SCM_NCELLP(SCM_CAR(x))\
128 ? (SCM_IMP(SCM_CAR(x)) \
129 ? EVALIM(SCM_CAR(x), env) \
130 : SCM_GLOC_VAL(SCM_CAR(x))) \
131 : EVALCELLCAR(x, env))
132 #ifdef DEBUG_EXTENSIONS
133 #define XEVALCAR(x, env) (SCM_NCELLP(SCM_CAR(x)) \
134 ? (SCM_IMP(SCM_CAR(x)) \
135 ? EVALIM(SCM_CAR(x), env) \
136 : SCM_GLOC_VAL(SCM_CAR(x))) \
137 : (SCM_SYMBOLP(SCM_CAR(x)) \
138 ? *scm_lookupcar(x, env) \
139 : (*scm_ceval_ptr) (SCM_CAR(x), env)))
141 #define XEVALCAR(x, env) EVALCAR(x, env)
144 #define EXTEND_ENV SCM_EXTEND_ENV
146 #ifdef MEMOIZE_LOCALS
149 scm_ilookup (iloc
, env
)
153 register int ir
= SCM_IFRAME (iloc
);
154 register SCM er
= env
;
155 for (; 0 != ir
; --ir
)
158 for (ir
= SCM_IDIST (iloc
); 0 != ir
; --ir
)
160 if (SCM_ICDRP (iloc
))
161 return &SCM_CDR (er
);
162 return &SCM_CAR (SCM_CDR (er
));
168 scm_lookupcar (vloc
, genv
)
173 register SCM
*al
, fl
, var
= SCM_CAR (vloc
);
174 #ifdef MEMOIZE_LOCALS
175 register SCM iloc
= SCM_ILOC00
;
177 for (; SCM_NIMP (env
); env
= SCM_CDR (env
))
179 if (SCM_BOOL_T
== scm_procedure_p (SCM_CAR (env
)))
182 for (fl
= SCM_CAR (*al
); SCM_NIMP (fl
); fl
= SCM_CDR (fl
))
187 #ifdef MEMOIZE_LOCALS
188 SCM_CAR (vloc
) = iloc
+ SCM_ICDR
;
190 return &SCM_CDR (*al
);
195 if (SCM_CAR (fl
) == var
)
197 #ifdef MEMOIZE_LOCALS
198 #ifndef RECKLESS /* letrec inits to SCM_UNDEFINED */
199 if (SCM_UNBNDP (SCM_CAR (*al
)))
205 SCM_CAR (vloc
) = iloc
;
207 return &SCM_CAR (*al
);
209 #ifdef MEMOIZE_LOCALS
213 #ifdef MEMOIZE_LOCALS
214 iloc
= (~SCM_IDSTMSK
) & (iloc
+ SCM_IFRINC
);
218 SCM top_thunk
, vcell
;
221 top_thunk
= SCM_CAR(env
); /* env now refers to a top level env thunk */
225 top_thunk
= SCM_BOOL_F
;
226 vcell
= scm_sym2vcell (var
, top_thunk
, SCM_BOOL_F
);
227 if (vcell
== SCM_BOOL_F
)
233 if (SCM_NNULLP (env
) || SCM_UNBNDP (SCM_CDR (var
)))
237 /* scm_everr (vloc, genv,...) */
238 scm_error (scm_misc_error_key
,
241 ? "Unbound variable: %S"
242 : "Damaged environment: %S",
243 scm_listify (var
, SCM_UNDEFINED
),
247 SCM_CAR (vloc
) = var
+ 1;
248 /* Except wait...what if the var is not a vcell,
249 * but syntax or something....
251 return &SCM_CDR (var
);
254 #define unmemocar scm_unmemocar
257 scm_unmemocar (form
, env
)
261 #ifdef DEBUG_EXTENSIONS
270 SCM_CAR (form
) = SCM_CAR (c
- 1);
271 #ifdef MEMOIZE_LOCALS
272 #ifdef DEBUG_EXTENSIONS
273 else if (SCM_ILOCP (c
))
275 for (ir
= SCM_IFRAME (c
); ir
!= 0; --ir
)
277 env
= SCM_CAR (SCM_CAR (env
));
278 for (ir
= SCM_IDIST (c
); ir
!= 0; --ir
)
280 SCM_CAR (form
) = SCM_ICDRP (c
) ? env
: SCM_CAR (env
);
289 scm_eval_car (pair
, env
)
293 return XEVALCAR (pair
, env
);
298 * The following rewrite expressions and
299 * some memoized forms have different syntax
302 static char s_expression
[] = "missing or extra expression";
303 static char s_test
[] = "bad test";
304 static char s_body
[] = "bad body";
305 static char s_bindings
[] = "bad bindings";
306 static char s_variable
[] = "bad variable";
307 static char s_clauses
[] = "bad or missing clauses";
308 static char s_formals
[] = "bad formals";
309 #define ASSYNT(_cond, _arg, _pos, _subr) if(!(_cond))scm_wta(_arg, (char *)_pos, _subr);
311 SCM scm_i_dot
, scm_i_quote
, scm_i_quasiquote
, scm_i_lambda
, scm_i_let
,
312 scm_i_arrow
, scm_i_else
, scm_i_unquote
, scm_i_uq_splicing
, scm_i_apply
;
314 SCM scm_i_define
, scm_i_and
, scm_i_begin
, scm_i_case
, scm_i_cond
,
315 scm_i_do
, scm_i_if
, scm_i_let
, scm_i_letrec
, scm_i_letstar
,
316 scm_i_or
, scm_i_set
, scm_i_atapply
, scm_i_atcall_cc
;
317 static char s_quasiquote
[] = "quasiquote";
318 static char s_delay
[] = "delay";
319 static char s_undefine
[] = "undefine";
320 #ifdef DEBUG_EXTENSIONS
321 SCM scm_i_enter_frame
, scm_i_apply_frame
, scm_i_exit_frame
;
325 #define ASRTSYNTAX(cond_, msg_) if(!(cond_))scm_wta(xorig, (msg_), what);
329 static void bodycheck
SCM_P ((SCM xorig
, SCM
*bodyloc
, char *what
));
332 bodycheck (xorig
, bodyloc
, what
)
337 ASRTSYNTAX (scm_ilength (*bodyloc
) >= 1, s_expression
);
343 scm_m_quote (xorig
, env
)
347 ASSYNT (scm_ilength (SCM_CDR (xorig
)) == 1, xorig
, s_expression
, "quote");
348 return scm_cons (SCM_IM_QUOTE
, SCM_CDR (xorig
));
354 scm_m_begin (xorig
, env
)
358 ASSYNT (scm_ilength (SCM_CDR (xorig
)) >= 1, xorig
, s_expression
, "begin");
359 return scm_cons (SCM_IM_BEGIN
, SCM_CDR (xorig
));
365 scm_m_if (xorig
, env
)
369 int len
= scm_ilength (SCM_CDR (xorig
));
370 ASSYNT (len
>= 2 && len
<= 3, xorig
, s_expression
, "if");
371 return scm_cons (SCM_IM_IF
, SCM_CDR (xorig
));
377 scm_m_set (xorig
, env
)
381 SCM x
= SCM_CDR (xorig
);
382 ASSYNT (2 == scm_ilength (x
), xorig
, s_expression
, "set!");
383 ASSYNT (SCM_NIMP (SCM_CAR (x
)) && SCM_SYMBOLP (SCM_CAR (x
)),
384 xorig
, s_variable
, "set!");
385 return scm_cons (SCM_IM_SET
, x
);
392 scm_m_vref (xorig
, env
)
396 SCM x
= SCM_CDR (xorig
);
397 ASSYNT (1 == scm_ilength (x
), xorig
, s_expression
, s_vref
);
398 if (SCM_NIMP(x
) && UDSCM_VARIABLEP (SCM_CAR (x
)))
400 /* scm_everr (SCM_UNDEFINED, env,..., "global variable reference") */
401 scm_error (scm_misc_error_key
,
404 scm_listify (SCM_CAR (SCM_CDR (x
)), SCM_UNDEFINED
),
407 ASSYNT (SCM_NIMP(x
) && DEFSCM_VARIABLEP (SCM_CAR (x
)),
408 xorig
, s_variable
, s_vref
);
410 return scm_cons (IM_VREF
, x
);
416 scm_m_vset (xorig
, env
)
420 SCM x
= SCM_CDR (xorig
);
421 ASSYNT (3 == scm_ilength (x
), xorig
, s_expression
, s_vset
);
422 ASSYNT (( DEFSCM_VARIABLEP (SCM_CAR (x
))
423 || UDSCM_VARIABLEP (SCM_CAR (x
))),
424 xorig
, s_variable
, s_vset
);
425 return scm_cons (IM_VSET
, x
);
432 scm_m_and (xorig
, env
)
436 int len
= scm_ilength (SCM_CDR (xorig
));
437 ASSYNT (len
>= 0, xorig
, s_test
, "and");
439 return scm_cons (SCM_IM_AND
, SCM_CDR (xorig
));
447 scm_m_or (xorig
, env
)
451 int len
= scm_ilength (SCM_CDR (xorig
));
452 ASSYNT (len
>= 0, xorig
, s_test
, "or");
454 return scm_cons (SCM_IM_OR
, SCM_CDR (xorig
));
462 scm_m_case (xorig
, env
)
466 SCM proc
, x
= SCM_CDR (xorig
);
467 ASSYNT (scm_ilength (x
) >= 2, xorig
, s_clauses
, "case");
468 while (SCM_NIMP (x
= SCM_CDR (x
)))
471 ASSYNT (scm_ilength (proc
) >= 2, xorig
, s_clauses
, "case");
472 ASSYNT (scm_ilength (SCM_CAR (proc
)) >= 0 || scm_i_else
== SCM_CAR (proc
),
473 xorig
, s_clauses
, "case");
475 return scm_cons (SCM_IM_CASE
, SCM_CDR (xorig
));
481 scm_m_cond (xorig
, env
)
485 SCM arg1
, x
= SCM_CDR (xorig
);
486 int len
= scm_ilength (x
);
487 ASSYNT (len
>= 1, xorig
, s_clauses
, "cond");
491 len
= scm_ilength (arg1
);
492 ASSYNT (len
>= 1, xorig
, s_clauses
, "cond");
493 if (scm_i_else
== SCM_CAR (arg1
))
495 ASSYNT (SCM_NULLP (SCM_CDR (x
)) && len
>= 2, xorig
, "bad ELSE clause", "cond");
496 SCM_CAR (arg1
) = SCM_BOOL_T
;
498 if (len
>= 2 && scm_i_arrow
== SCM_CAR (SCM_CDR (arg1
)))
499 ASSYNT (3 == len
&& SCM_NIMP (SCM_CAR (SCM_CDR (SCM_CDR (arg1
)))),
500 xorig
, "bad recipient", "cond");
503 return scm_cons (SCM_IM_COND
, SCM_CDR (xorig
));
509 scm_m_lambda (xorig
, env
)
513 SCM proc
, x
= SCM_CDR (xorig
);
514 if (scm_ilength (x
) < 2)
518 (proc
) goto memlambda
;
520 (proc
) goto badforms
;
522 (proc
) goto memlambda
;
524 (proc
) goto badforms
;
530 if (!SCM_SYMBOLP (proc
))
534 if (!(SCM_NIMP (SCM_CAR (proc
)) && SCM_SYMBOLP (SCM_CAR (proc
))))
536 proc
= SCM_CDR (proc
);
540 badforms
:scm_wta (xorig
, s_formals
, "lambda");
542 bodycheck (xorig
, &SCM_CDR (x
), "lambda");
543 return scm_cons (SCM_IM_LAMBDA
, SCM_CDR (xorig
));
549 scm_m_letstar (xorig
, env
)
553 SCM x
= SCM_CDR (xorig
), arg1
, proc
, vars
= SCM_EOL
, *varloc
= &vars
;
554 int len
= scm_ilength (x
);
555 ASSYNT (len
>= 2, xorig
, s_body
, "let*");
557 ASSYNT (scm_ilength (proc
) >= 0, xorig
, s_bindings
, "let*");
558 while SCM_NIMP (proc
)
560 arg1
= SCM_CAR (proc
);
561 ASSYNT (2 == scm_ilength (arg1
), xorig
, s_bindings
, "let*");
562 ASSYNT (SCM_NIMP (SCM_CAR (arg1
)) && SCM_SYMBOLP (SCM_CAR (arg1
)), xorig
, s_variable
, "let*");
563 *varloc
= scm_cons2 (SCM_CAR (arg1
), SCM_CAR (SCM_CDR (arg1
)), SCM_EOL
);
564 varloc
= &SCM_CDR (SCM_CDR (*varloc
));
565 proc
= SCM_CDR (proc
);
567 x
= scm_cons (vars
, SCM_CDR (x
));
568 bodycheck (xorig
, &SCM_CDR (x
), "let*");
569 return scm_cons (SCM_IM_LETSTAR
, x
);
572 /* DO gets the most radically altered syntax
573 (do ((<var1> <init1> <step1>)
579 (do_mem (varn ... var2 var1)
580 (<init1> <init2> ... <initn>)
583 <step1> <step2> ... <stepn>) ;; missing steps replaced by var
589 scm_m_do (xorig
, env
)
593 SCM x
= SCM_CDR (xorig
), arg1
, proc
;
594 SCM vars
= SCM_EOL
, inits
= SCM_EOL
, steps
= SCM_EOL
;
595 SCM
*initloc
= &inits
, *steploc
= &steps
;
596 int len
= scm_ilength (x
);
597 ASSYNT (len
>= 2, xorig
, s_test
, "do");
599 ASSYNT (scm_ilength (proc
) >= 0, xorig
, s_bindings
, "do");
603 arg1
= SCM_CAR (proc
);
604 len
= scm_ilength (arg1
);
605 ASSYNT (2 == len
|| 3 == len
, xorig
, s_bindings
, "do");
606 ASSYNT (SCM_NIMP (SCM_CAR (arg1
)) && SCM_SYMBOLP (SCM_CAR (arg1
)), xorig
, s_variable
, "do");
607 /* vars reversed here, inits and steps reversed at evaluation */
608 vars
= scm_cons (SCM_CAR (arg1
), vars
); /* variable */
609 arg1
= SCM_CDR (arg1
);
610 *initloc
= scm_cons (SCM_CAR (arg1
), SCM_EOL
); /* init */
611 initloc
= &SCM_CDR (*initloc
);
612 arg1
= SCM_CDR (arg1
);
613 *steploc
= scm_cons (SCM_IMP (arg1
) ? SCM_CAR (vars
) : SCM_CAR (arg1
), SCM_EOL
); /* step */
614 steploc
= &SCM_CDR (*steploc
);
615 proc
= SCM_CDR (proc
);
618 ASSYNT (scm_ilength (SCM_CAR (x
)) >= 1, xorig
, s_test
, "do");
619 x
= scm_cons2 (SCM_CAR (x
), SCM_CDR (x
), steps
);
620 x
= scm_cons2 (vars
, inits
, x
);
621 bodycheck (xorig
, &SCM_CAR (SCM_CDR (SCM_CDR (x
))), "do");
622 return scm_cons (SCM_IM_DO
, x
);
625 /* evalcar is small version of inline EVALCAR when we don't care about
628 #define evalcar scm_eval_car
631 static SCM iqq
SCM_P ((SCM form
, SCM env
, int depth
));
634 iqq (form
, env
, depth
)
643 if (SCM_VECTORP (form
))
645 long i
= SCM_LENGTH (form
);
646 SCM
*data
= SCM_VELTS (form
);
649 tmp
= scm_cons (data
[i
], tmp
);
650 return scm_vector (iqq (tmp
, env
, depth
));
654 tmp
= SCM_CAR (form
);
655 if (scm_i_quasiquote
== tmp
)
660 if (scm_i_unquote
== tmp
)
664 form
= SCM_CDR (form
);
665 /* !!! might need a check here to be sure that form isn't a struct. */
666 SCM_ASSERT (SCM_NIMP (form
) && SCM_ECONSP (form
) && SCM_NULLP (SCM_CDR (form
)),
667 form
, SCM_ARG1
, s_quasiquote
);
669 return evalcar (form
, env
);
670 return scm_cons2 (tmp
, iqq (SCM_CAR (form
), env
, depth
), SCM_EOL
);
672 if (SCM_NIMP (tmp
) && (scm_i_uq_splicing
== SCM_CAR (tmp
)))
676 return scm_append (scm_cons2 (evalcar (tmp
, env
), iqq (SCM_CDR (form
), env
, depth
), SCM_EOL
));
678 return scm_cons (iqq (SCM_CAR (form
), env
, edepth
), iqq (SCM_CDR (form
), env
, depth
));
681 /* Here are acros which return values rather than code. */
685 scm_m_quasiquote (xorig
, env
)
689 SCM x
= SCM_CDR (xorig
);
690 ASSYNT (scm_ilength (x
) == 1, xorig
, s_expression
, s_quasiquote
);
691 return iqq (SCM_CAR (x
), env
, 1);
696 scm_m_delay (xorig
, env
)
700 ASSYNT (scm_ilength (xorig
) == 2, xorig
, s_expression
, s_delay
);
701 xorig
= SCM_CDR (xorig
);
702 return scm_makprom (scm_closure (scm_cons2 (SCM_EOL
, SCM_CAR (xorig
), SCM_CDR (xorig
)),
707 static SCM env_top_level
SCM_P ((SCM env
));
713 while (SCM_NIMP(env
))
715 if (SCM_BOOL_T
== scm_procedure_p (SCM_CAR(env
)))
724 scm_m_define (x
, env
)
730 /* ASSYNT(SCM_NULLP(env), x, "bad placement", s_define);*/
731 ASSYNT (scm_ilength (x
) >= 2, arg1
, s_expression
, "define");
734 while (SCM_NIMP (proc
) && SCM_CONSP (proc
))
735 { /* nested define syntax */
736 x
= scm_cons (scm_cons2 (scm_i_lambda
, SCM_CDR (proc
), x
), SCM_EOL
);
737 proc
= SCM_CAR (proc
);
739 ASSYNT (SCM_NIMP (proc
) && SCM_SYMBOLP (proc
), arg1
, s_variable
, "define");
740 ASSYNT (1 == scm_ilength (x
), arg1
, s_expression
, "define");
741 if (SCM_TOP_LEVEL (env
))
743 x
= evalcar (x
, env
);
744 #ifdef DEBUG_EXTENSIONS
745 if (SCM_REC_PROCNAMES_P
&& SCM_NIMP (x
) && SCM_CLOSUREP (x
))
746 scm_set_procedure_property_x (x
, scm_i_name
, proc
);
748 arg1
= scm_sym2vcell (proc
, env_top_level (env
), SCM_BOOL_T
);
751 if (SCM_NIMP (SCM_CDR (arg1
)) && ((SCM
) SCM_SNAME (SCM_CDR (arg1
)) == proc
)
752 && (SCM_CDR (arg1
) != x
))
753 scm_warn ("redefining built-in ", SCM_CHARS (proc
));
756 if (5 <= scm_verbose
&& SCM_UNDEFINED
!= SCM_CDR (arg1
))
757 scm_warn ("redefining ", SCM_CHARS (proc
));
761 return scm_cons2 (scm_i_quote
, SCM_CAR (arg1
), SCM_EOL
);
763 return SCM_UNSPECIFIED
;
766 return scm_cons2 (SCM_IM_DEFINE
, proc
, x
);
770 scm_m_undefine (x
, env
)
775 ASSYNT (SCM_TOP_LEVEL (env
), arg1
, "bad placement ", s_undefine
);
776 ASSYNT (SCM_NIMP (x
) && SCM_CONSP (x
) && SCM_CDR (x
) == SCM_EOL
,
777 arg1
, s_expression
, s_undefine
);
779 ASSYNT (SCM_NIMP (x
) && SCM_SYMBOLP (x
), arg1
, s_variable
, s_undefine
);
780 arg1
= scm_sym2vcell (x
, env_top_level (env
), SCM_BOOL_F
);
781 ASSYNT (SCM_NFALSEP (arg1
) && !SCM_UNBNDP (SCM_CDR (arg1
)),
782 x
, "variable already unbound ", s_undefine
);
785 if (SCM_NIMP (SCM_CDR (arg1
)) && ((SCM
) SCM_SNAME (SCM_CDR (arg1
)) == x
))
786 scm_warn ("undefining built-in ", SCM_CHARS (x
));
789 if (5 <= scm_verbose
&& SCM_UNDEFINED
!= SCM_CDR (arg1
))
790 scm_warn ("redefining ", SCM_CHARS (x
));
792 SCM_CDR (arg1
) = SCM_UNDEFINED
;
794 return SCM_CAR (arg1
);
796 return SCM_UNSPECIFIED
;
804 scm_m_letrec (xorig
, env
)
808 SCM cdrx
= SCM_CDR (xorig
); /* locally mutable version of form */
809 char *what
= SCM_CHARS (SCM_CAR (xorig
));
810 SCM x
= cdrx
, proc
, arg1
; /* structure traversers */
811 SCM vars
= SCM_EOL
, inits
= SCM_EOL
, *initloc
= &inits
;
813 ASRTSYNTAX (scm_ilength (x
) >= 2, s_body
);
816 (proc
) return scm_m_letstar (xorig
, env
); /* null binding, let* faster */
817 ASRTSYNTAX (scm_ilength (proc
) >= 1, s_bindings
);
820 /* vars scm_list reversed here, inits reversed at evaluation */
821 arg1
= SCM_CAR (proc
);
822 ASRTSYNTAX (2 == scm_ilength (arg1
), s_bindings
);
823 ASRTSYNTAX (SCM_NIMP (SCM_CAR (arg1
)) && SCM_SYMBOLP (SCM_CAR (arg1
)), s_variable
);
824 vars
= scm_cons (SCM_CAR (arg1
), vars
);
825 *initloc
= scm_cons (SCM_CAR (SCM_CDR (arg1
)), SCM_EOL
);
826 initloc
= &SCM_CDR (*initloc
);
829 (proc
= SCM_CDR (proc
));
830 cdrx
= scm_cons2 (vars
, inits
, SCM_CDR (x
));
831 bodycheck (xorig
, &SCM_CDR (SCM_CDR (cdrx
)), what
);
832 return scm_cons (SCM_IM_LETREC
, cdrx
);
837 scm_m_let (xorig
, env
)
841 SCM cdrx
= SCM_CDR (xorig
); /* locally mutable version of form */
842 SCM x
= cdrx
, proc
, arg1
, name
; /* structure traversers */
843 SCM vars
= SCM_EOL
, inits
= SCM_EOL
, *varloc
= &vars
, *initloc
= &inits
;
845 ASSYNT (scm_ilength (x
) >= 2, xorig
, s_body
, "let");
848 || (SCM_NIMP (proc
) && SCM_CONSP (proc
)
849 && SCM_NIMP (SCM_CAR (proc
)) && SCM_CONSP (SCM_CAR (proc
)) && SCM_NULLP (SCM_CDR (proc
))))
850 return scm_m_letstar (xorig
, env
); /* null or single binding, let* is faster */
851 ASSYNT (SCM_NIMP (proc
), xorig
, s_bindings
, "let");
852 if (SCM_CONSP (proc
)) /* plain let, proc is <bindings> */
853 return scm_cons (SCM_IM_LET
, SCM_CDR (scm_m_letrec (xorig
, env
)));
854 if (!SCM_SYMBOLP (proc
))
855 scm_wta (xorig
, s_bindings
, "let"); /* bad let */
856 name
= proc
; /* named let, build equiv letrec */
858 ASSYNT (scm_ilength (x
) >= 2, xorig
, s_body
, "let");
859 proc
= SCM_CAR (x
); /* bindings scm_list */
860 ASSYNT (scm_ilength (proc
) >= 0, xorig
, s_bindings
, "let");
863 { /* vars and inits both in order */
864 arg1
= SCM_CAR (proc
);
865 ASSYNT (2 == scm_ilength (arg1
), xorig
, s_bindings
, "let");
866 ASSYNT (SCM_NIMP (SCM_CAR (arg1
)) && SCM_SYMBOLP (SCM_CAR (arg1
)), xorig
, s_variable
, "let");
867 *varloc
= scm_cons (SCM_CAR (arg1
), SCM_EOL
);
868 varloc
= &SCM_CDR (*varloc
);
869 *initloc
= scm_cons (SCM_CAR (SCM_CDR (arg1
)), SCM_EOL
);
870 initloc
= &SCM_CDR (*initloc
);
871 proc
= SCM_CDR (proc
);
874 scm_m_letrec (scm_cons2 (scm_i_let
,
875 scm_cons (scm_cons2 (name
, scm_cons2 (scm_i_lambda
, vars
, SCM_CDR (x
)), SCM_EOL
), SCM_EOL
),
876 scm_acons (name
, inits
, SCM_EOL
)), /* body */
883 scm_m_apply (xorig
, env
)
887 ASSYNT (scm_ilength (SCM_CDR (xorig
)) == 2, xorig
, s_expression
, "@apply");
888 return scm_cons (SCM_IM_APPLY
, SCM_CDR (xorig
));
891 #define s_atcall_cc (SCM_ISYMCHARS(SCM_IM_CONT)+1)
895 scm_m_cont (xorig
, env
)
899 ASSYNT (scm_ilength (SCM_CDR (xorig
)) == 1, xorig
, s_expression
, "@call-with-current-continuation");
900 return scm_cons (SCM_IM_CONT
, SCM_CDR (xorig
));
903 /* scm_unmemocopy takes a memoized expression together with its
904 * environment and rewrites it to its original form. Thus, it is the
905 * inversion of the rewrite rules above. The procedure is not
906 * optimized for speed. It's used in scm_iprin1 when printing the
907 * code of a closure, in scm_procedure_source and in scm_expr_stack
908 * when generating the source for a stackframe.
912 static SCM unmemocopy
SCM_P ((SCM x
, SCM env
));
920 #ifdef DEBUG_EXTENSIONS
923 if (SCM_NCELLP (x
) || SCM_NECONSP (x
))
925 #ifdef DEBUG_EXTENSIONS
926 p
= scm_whash_lookup (scm_source_whash
, x
);
928 switch (SCM_TYP7 (x
))
930 case (127 & SCM_IM_AND
):
931 ls
= z
= scm_cons (scm_i_and
, SCM_UNSPECIFIED
);
933 case (127 & SCM_IM_BEGIN
):
934 ls
= z
= scm_cons (scm_i_begin
, SCM_UNSPECIFIED
);
936 case (127 & SCM_IM_CASE
):
937 ls
= z
= scm_cons (scm_i_case
, SCM_UNSPECIFIED
);
939 case (127 & SCM_IM_COND
):
940 ls
= z
= scm_cons (scm_i_cond
, SCM_UNSPECIFIED
);
942 case (127 & SCM_IM_DO
):
943 ls
= scm_cons (scm_i_do
, SCM_UNSPECIFIED
);
945 case (127 & SCM_IM_IF
):
946 ls
= z
= scm_cons (scm_i_if
, SCM_UNSPECIFIED
);
948 case (127 & SCM_IM_LET
):
949 ls
= scm_cons (scm_i_let
, SCM_UNSPECIFIED
);
951 case (127 & SCM_IM_LETREC
):
954 ls
= scm_cons (scm_i_letrec
, SCM_UNSPECIFIED
);
959 z
= EXTEND_ENV (f
, SCM_EOL
, env
);
960 e
= scm_reverse (unmemocopy (SCM_CAR (x
),
961 SCM_CAR (ls
) == scm_i_letrec
? z
: env
));
963 s
= SCM_CAR (ls
) == scm_i_do
964 ? scm_reverse (unmemocopy (SCM_CDR (SCM_CDR (SCM_CDR (x
))), env
))
969 z
= scm_acons (SCM_CAR (v
),
970 scm_cons (SCM_CAR (e
),
971 SCM_CAR (s
) == SCM_CAR (v
)
973 : scm_cons (SCM_CAR (s
), SCM_EOL
)),
980 SCM_CDR (ls
) = z
= scm_cons (z
, SCM_UNSPECIFIED
);
981 if (SCM_CAR (ls
) == scm_i_do
)
984 z
= (SCM_CDR (z
) = scm_cons (unmemocopy (SCM_CAR (x
), env
),
986 x
= (SCM
) (&SCM_CAR (SCM_CDR (x
)) - 1);
990 case (127 & SCM_IM_LETSTAR
):
998 env
= EXTEND_ENV (SCM_EOL
, SCM_EOL
, env
);
1001 y
= z
= scm_acons (SCM_CAR (b
),
1003 scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b
)), env
), SCM_EOL
), env
),
1005 env
= EXTEND_ENV (SCM_CAR (b
), SCM_BOOL_F
, env
);
1006 b
= SCM_CDR (SCM_CDR (b
));
1009 SCM_SETCDR (y
, SCM_EOL
);
1010 ls
= scm_cons (scm_i_let
, z
= scm_cons (y
, SCM_UNSPECIFIED
));
1015 z
= (SCM_CDR (z
) = scm_acons (SCM_CAR (b
),
1017 scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b
)), env
), SCM_EOL
), env
),
1019 env
= EXTEND_ENV (SCM_CAR (b
), SCM_BOOL_F
, env
);
1020 b
= SCM_CDR (SCM_CDR (b
));
1023 SCM_CDR (z
) = SCM_EOL
;
1025 ls
= scm_cons (scm_i_letstar
, z
= scm_cons (y
, SCM_UNSPECIFIED
));
1028 case (127 & SCM_IM_OR
):
1029 ls
= z
= scm_cons (scm_i_or
, SCM_UNSPECIFIED
);
1031 case (127 & SCM_IM_LAMBDA
):
1033 ls
= scm_cons (scm_i_lambda
,
1034 z
= scm_cons (SCM_CAR (x
), SCM_UNSPECIFIED
));
1035 env
= EXTEND_ENV (SCM_CAR (x
), SCM_EOL
, env
);
1037 case (127 & SCM_IM_QUOTE
):
1038 ls
= z
= scm_cons (scm_i_quote
, SCM_UNSPECIFIED
);
1040 case (127 & SCM_IM_SET
):
1041 ls
= z
= scm_cons (scm_i_set
, SCM_UNSPECIFIED
);
1043 case (127 & SCM_IM_DEFINE
):
1047 ls
= scm_cons (scm_i_define
,
1048 z
= scm_cons (n
= SCM_CAR (x
), SCM_UNSPECIFIED
));
1049 if (SCM_NNULLP (env
))
1050 SCM_CAR (SCM_CAR (env
)) = scm_cons (n
, SCM_CAR (SCM_CAR (env
)));
1053 case (127 & SCM_MAKISYM (0)):
1057 switch SCM_ISYMNUM (z
)
1059 case (SCM_ISYMNUM (SCM_IM_APPLY
)):
1060 ls
= z
= scm_cons (scm_i_atapply
, SCM_UNSPECIFIED
);
1062 case (SCM_ISYMNUM (SCM_IM_CONT
)):
1063 ls
= z
= scm_cons (scm_i_atcall_cc
, SCM_UNSPECIFIED
);
1069 ls
= z
= unmemocar (scm_cons (unmemocopy (SCM_CAR (x
), env
),
1074 while (SCM_CELLP (x
= SCM_CDR (x
)) && SCM_ECONSP (x
))
1075 z
= (SCM_CDR (z
) = unmemocar (scm_cons (unmemocopy (SCM_CAR (x
), env
),
1079 #ifdef DEBUG_EXTENSIONS
1080 if (SCM_NFALSEP (p
))
1081 scm_whash_insert (scm_source_whash
, ls
, p
);
1088 scm_unmemocopy (x
, env
)
1092 if (SCM_NNULLP (env
))
1093 /* Make a copy of the lowest frame to protect it from
1094 modifications by SCM_IM_DEFINE */
1095 return unmemocopy (x
, scm_cons (SCM_CAR (env
), SCM_CDR (env
)));
1097 return unmemocopy (x
, env
);
1103 scm_badargsp (formals
, args
)
1114 formals
= SCM_CDR (formals
);
1115 args
= SCM_CDR (args
);
1117 return SCM_NNULLP (args
) ? 1 : 0;
1123 long scm_tc16_macro
;
1127 scm_eval_args (l
, env
)
1131 SCM res
= SCM_EOL
, *lloc
= &res
;
1132 while (SCM_NIMP (l
))
1134 *lloc
= scm_cons (EVALCAR (l
, env
), SCM_EOL
);
1135 lloc
= &SCM_CDR (*lloc
);
1143 /* SECTION: This code is specific for the debugging support. One
1144 * branch is read when DEVAL isn't defined, the other when DEVAL is
1150 #define SCM_APPLY scm_apply
1151 #define PREP_APPLY(proc, args)
1153 #define RETURN(x) return x;
1154 #ifdef STACK_CHECKING
1155 #ifndef NO_CEVAL_STACK_CHECKING
1156 #define EVAL_STACK_CHECKING
1163 #define SCM_CEVAL scm_deval /* Substitute all uses of scm_ceval */
1165 #define SCM_APPLY scm_dapply
1167 #define PREP_APPLY(p, l) \
1168 { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
1170 #define ENTER_APPLY \
1172 SCM_SET_ARGSREADY (debug);\
1174 if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && PROCTRACEP (proc)))\
1176 SCM tmp, tail = SCM_TRACED_FRAME_P (debug) ? SCM_BOOL_T : SCM_BOOL_F;\
1177 SCM_SET_TRACED_FRAME (debug);\
1178 if (SCM_CHEAPTRAPS_P)\
1180 tmp = scm_make_debugobj ((scm_debug_frame *) &debug);\
1181 scm_ithrow (scm_i_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
1185 scm_make_cont (&tmp);\
1186 if (!setjmp (SCM_JMPBUF (tmp)))\
1187 scm_ithrow (scm_i_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
1192 #define RETURN(e) {proc = (e); goto exit;}
1193 #ifdef STACK_CHECKING
1194 #ifndef EVAL_STACK_CHECKING
1195 #define EVAL_STACK_CHECKING
1199 /* scm_ceval_ptr points to the currently selected evaluator.
1200 * *fixme*: Although efficiency is important here, this state variable
1201 * should probably not be a global. It should be related to the
1206 SCM (*scm_ceval_ptr
) SCM_P ((SCM x
, SCM env
));
1208 /* scm_last_debug_frame contains a pointer to the last debugging
1209 * information stack frame. It is accessed very often from the
1210 * debugging evaluator, so it should probably not be indirectly
1211 * addressed. Better to save and restore it from the current root at
1216 scm_debug_frame
*scm_last_debug_frame
;
1219 /* scm_debug_eframe_size is the number of slots available for pseudo
1220 * stack frames at each real stack frame.
1223 int scm_debug_eframe_size
;
1225 int scm_debug_mode
, scm_check_entry_p
, scm_check_apply_p
, scm_check_exit_p
;
1227 scm_option scm_debug_opts
[] = {
1228 { SCM_OPTION_BOOLEAN
, "cheap", 1,
1229 "*Flyweight representation of the stack at traps." },
1230 { SCM_OPTION_BOOLEAN
, "breakpoints", 0, "*Check for breakpoints." },
1231 { SCM_OPTION_BOOLEAN
, "trace", 0, "*Trace mode." },
1232 { SCM_OPTION_BOOLEAN
, "procnames", 1,
1233 "Record procedure names at definition." },
1234 { SCM_OPTION_BOOLEAN
, "backwards", 0,
1235 "Display backtrace in anti-chronological order." },
1236 { SCM_OPTION_INTEGER
, "indent", 10, "Maximal indentation in backtrace." },
1237 { SCM_OPTION_INTEGER
, "frames", 3,
1238 "Maximum number of tail-recursive frames in backtrace." },
1239 { SCM_OPTION_INTEGER
, "maxdepth", 1000,
1240 "Maximal number of stored backtrace frames." },
1241 { SCM_OPTION_INTEGER
, "depth", 20, "Maximal length of printed backtrace." },
1242 { SCM_OPTION_BOOLEAN
, "backtrace", 1,
1243 "Show backtrace on error (use debugging evaluator)." },
1244 { SCM_OPTION_BOOLEAN
, "deval", 0, "Use the debugging evaluator." },
1245 { SCM_OPTION_INTEGER
, "stack", 20000, "Stack size limit (0 = no check)." }
1248 scm_option scm_evaluator_trap_table
[] = {
1249 { SCM_OPTION_BOOLEAN
, "enter-frame", 0, "Trap when eval enters new frame." },
1250 { SCM_OPTION_BOOLEAN
, "apply-frame", 0, "Trap when entering apply." },
1251 { SCM_OPTION_BOOLEAN
, "exit-frame", 0, "Trap when exiting eval or apply." }
1255 scm_deval_args (l
, env
, lloc
)
1259 while (SCM_NIMP (l
))
1261 *lloc
= scm_cons (EVALCAR (l
, env
), SCM_EOL
);
1262 lloc
= &SCM_CDR (*lloc
);
1271 /* SECTION: Some local definitions for the evaluator.
1276 #define CHECK_EQVISH(A,B) (((A) == (B)) || (SCM_NFALSEP (scm_eqv_p ((A), (B)))))
1278 #define CHECK_EQVISH(A,B) ((A) == (B))
1283 /* SECTION: This is the evaluator. Like any real monster, it has
1284 * three heads. This code is compiled twice.
1319 scm_debug_frame
*prev
;
1321 scm_debug_info vect
[scm_debug_eframe_size
];
1322 scm_debug_info
*info
;
1324 debug
.prev
= scm_last_debug_frame
;
1325 debug
.status
= scm_debug_eframe_size
;
1326 debug
.info
= &debug
.vect
[0];
1327 scm_last_debug_frame
= (scm_debug_frame
*) &debug
;
1329 #ifdef EVAL_STACK_CHECKING
1330 if (SCM_STACK_OVERFLOW_P ((SCM_STACKITEM
*) &proc
)
1331 && scm_stack_checking_enabled_p
)
1334 debug
.info
->e
.exp
= x
;
1335 debug
.info
->e
.env
= env
;
1337 scm_report_stack_overflow ();
1344 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1347 #if 0 /* This will probably never have any practical use ... */
1350 if (SINGLE_STEP
|| (SCM_TRACE_P
&& SCM_TRACED_FRAME_P (debug
)))
1353 SCM_RESET_DEBUG_MODE
;
1354 SCM_CLEAR_TRACED_FRAME (debug
);
1355 scm_make_cont (&t
.arg1
);
1356 if (!setjmp (SCM_JMPBUF (t
.arg1
)))
1357 scm_ithrow (scm_i_exit_tail
, scm_cons (t
.arg1
, SCM_EOL
), 0);
1362 SCM_CLEAR_ARGSREADY (debug
);
1363 if (SCM_OVERFLOWP (debug
))
1365 else if (++debug
.info
== (scm_debug_info
*) &debug
.info
)
1367 SCM_SET_OVERFLOW (debug
);
1371 debug
.info
->e
.exp
= x
;
1372 debug
.info
->e
.env
= env
;
1374 if (SCM_ENTER_FRAME_P
|| (SCM_BREAKPOINTS_P
&& SRCBRKP (x
)))
1376 SCM tail
= SCM_TAILRECP (debug
) ? SCM_BOOL_T
: SCM_BOOL_F
;
1377 SCM_SET_TAILREC (debug
);
1378 SCM_ENTER_FRAME_P
= 0;
1379 SCM_RESET_DEBUG_MODE
;
1380 if (SCM_CHEAPTRAPS_P
)
1381 t
.arg1
= scm_make_debugobj ((scm_debug_frame
*) &debug
);
1384 scm_make_cont (&t
.arg1
);
1385 if (setjmp (SCM_JMPBUF (t
.arg1
)))
1387 x
= SCM_THROW_VALUE (t
.arg1
);
1393 /* This gives the possibility for the debugger to
1394 modify the source expression before evaluation. */
1398 scm_ithrow (scm_i_enter_frame
,
1399 scm_cons2 (t
.arg1
, tail
,
1400 scm_cons (scm_unmemocopy (x
, env
), SCM_EOL
)),
1406 switch (SCM_TYP7 (x
))
1408 case scm_tcs_symbols
:
1409 /* Only happens when called at top level.
1411 x
= scm_cons (x
, SCM_UNDEFINED
);
1414 case (127 & SCM_IM_AND
):
1417 while (SCM_NNULLP (t
.arg1
= SCM_CDR (t
.arg1
)))
1418 if (SCM_FALSEP (EVALCAR (x
, env
)))
1420 RETURN (SCM_BOOL_F
);
1424 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1427 case (127 & SCM_IM_BEGIN
):
1429 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1435 while (SCM_NNULLP (t
.arg1
= SCM_CDR (t
.arg1
)))
1437 SIDEVAL (SCM_CAR (x
), env
);
1441 carloop
: /* scm_eval car of last form in list */
1442 if (SCM_NCELLP (SCM_CAR (x
)))
1445 RETURN (SCM_IMP (x
) ? EVALIM (x
, env
) : SCM_GLOC_VAL (x
))
1448 if (SCM_SYMBOLP (SCM_CAR (x
)))
1451 RETURN (*scm_lookupcar (x
, env
))
1455 goto loop
; /* tail recurse */
1458 case (127 & SCM_IM_CASE
):
1460 t
.arg1
= EVALCAR (x
, env
);
1461 while (SCM_NIMP (x
= SCM_CDR (x
)))
1464 if (scm_i_else
== SCM_CAR (proc
))
1467 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1470 proc
= SCM_CAR (proc
);
1471 while (SCM_NIMP (proc
))
1473 if (CHECK_EQVISH (SCM_CAR (proc
), t
.arg1
))
1475 x
= SCM_CDR (SCM_CAR (x
));
1476 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1479 proc
= SCM_CDR (proc
);
1482 RETURN (SCM_UNSPECIFIED
)
1485 case (127 & SCM_IM_COND
):
1486 while (SCM_NIMP (x
= SCM_CDR (x
)))
1489 t
.arg1
= EVALCAR (proc
, env
);
1490 if (SCM_NFALSEP (t
.arg1
))
1497 if (scm_i_arrow
!= SCM_CAR (x
))
1499 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1503 proc
= EVALCAR (proc
, env
);
1504 SCM_ASRTGO (SCM_NIMP (proc
), badfun
);
1505 PREP_APPLY (proc
, scm_cons (t
.arg1
, SCM_EOL
));
1510 RETURN (SCM_UNSPECIFIED
)
1513 case (127 & SCM_IM_DO
):
1515 proc
= SCM_CAR (SCM_CDR (x
)); /* inits */
1516 t
.arg1
= SCM_EOL
; /* values */
1517 while (SCM_NIMP (proc
))
1519 t
.arg1
= scm_cons (EVALCAR (proc
, env
), t
.arg1
);
1520 proc
= SCM_CDR (proc
);
1522 env
= EXTEND_ENV (SCM_CAR (x
), t
.arg1
, env
);
1523 x
= SCM_CDR (SCM_CDR (x
));
1524 while (proc
= SCM_CAR (x
), SCM_FALSEP (EVALCAR (proc
, env
)))
1526 for (proc
= SCM_CAR (SCM_CDR (x
)); SCM_NIMP (proc
); proc
= SCM_CDR (proc
))
1528 t
.arg1
= SCM_CAR (proc
); /* body */
1529 SIDEVAL (t
.arg1
, env
);
1531 for (t
.arg1
= SCM_EOL
, proc
= SCM_CDR (SCM_CDR (x
)); SCM_NIMP (proc
); proc
= SCM_CDR (proc
))
1532 t
.arg1
= scm_cons (EVALCAR (proc
, env
), t
.arg1
); /* steps */
1533 env
= EXTEND_ENV (SCM_CAR (SCM_CAR (env
)), t
.arg1
, SCM_CDR (env
));
1537 RETURN (SCM_UNSPECIFIED
);
1538 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1542 case (127 & SCM_IM_IF
):
1544 if (SCM_NFALSEP (EVALCAR (x
, env
)))
1546 else if (SCM_IMP (x
= SCM_CDR (SCM_CDR (x
))))
1548 RETURN (SCM_UNSPECIFIED
);
1550 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1554 case (127 & SCM_IM_LET
):
1556 proc
= SCM_CAR (SCM_CDR (x
));
1560 t
.arg1
= scm_cons (EVALCAR (proc
, env
), t
.arg1
);
1562 while (SCM_NIMP (proc
= SCM_CDR (proc
)));
1563 env
= EXTEND_ENV (SCM_CAR (x
), t
.arg1
, env
);
1568 case (127 & SCM_IM_LETREC
):
1570 env
= EXTEND_ENV (SCM_CAR (x
), scm_undefineds
, env
);
1576 t
.arg1
= scm_cons (EVALCAR (proc
, env
), t
.arg1
);
1578 while (SCM_NIMP (proc
= SCM_CDR (proc
)));
1579 SCM_CDR (SCM_CAR (env
)) = t
.arg1
;
1583 case (127 & SCM_IM_LETSTAR
):
1588 env
= EXTEND_ENV (SCM_EOL
, SCM_EOL
, env
);
1593 t
.arg1
= SCM_CAR (proc
);
1594 proc
= SCM_CDR (proc
);
1595 env
= EXTEND_ENV (t
.arg1
, EVALCAR (proc
, env
), env
);
1597 while (SCM_NIMP (proc
= SCM_CDR (proc
)));
1600 case (127 & SCM_IM_OR
):
1603 while (SCM_NNULLP (t
.arg1
= SCM_CDR (t
.arg1
)))
1605 x
= EVALCAR (x
, env
);
1606 if (SCM_NFALSEP (x
))
1612 PREP_APPLY (SCM_UNDEFINED
, SCM_EOL
);
1616 case (127 & SCM_IM_LAMBDA
):
1617 RETURN (scm_closure (SCM_CDR (x
), env
));
1620 case (127 & SCM_IM_QUOTE
):
1621 RETURN (SCM_CAR (SCM_CDR (x
)));
1624 case (127 & SCM_IM_SET
):
1627 switch (7 & (int) proc
)
1630 t
.lloc
= scm_lookupcar (x
, env
);
1633 t
.lloc
= &SCM_GLOC_VAL (proc
);
1635 #ifdef MEMOIZE_LOCALS
1637 t
.lloc
= scm_ilookup (proc
, env
);
1642 *t
.lloc
= EVALCAR (x
, env
);
1646 RETURN (SCM_UNSPECIFIED
);
1650 case (127 & SCM_IM_DEFINE
): /* only for internal defines */
1654 x
= evalcar (x
, env
);
1655 #ifdef DEBUG_EXTENSIONS
1656 if (SCM_REC_PROCNAMES_P
&& SCM_NIMP (x
) && SCM_CLOSUREP (x
))
1657 scm_set_procedure_property_x (x
, scm_i_name
, proc
);
1659 env
= SCM_CAR (env
);
1661 SCM_CAR (env
) = scm_cons (proc
, SCM_CAR (env
));
1662 SCM_CDR (env
) = scm_cons (x
, SCM_CDR (env
));
1664 RETURN (SCM_UNSPECIFIED
);
1668 /* new syntactic forms go here. */
1669 case (127 & SCM_MAKISYM (0)):
1671 SCM_ASRTGO (SCM_ISYMP (proc
), badfun
);
1672 switch SCM_ISYMNUM (proc
)
1675 case (SCM_ISYMNUM (IM_VREF
)):
1678 var
= SCM_CAR (SCM_CDR (x
));
1679 RETURN (SCM_CDR(var
));
1681 case (SCM_ISYMNUM (IM_VSET
)):
1682 SCM_CDR (SCM_CAR ( SCM_CDR (x
))) = EVALCAR( SCM_CDR ( SCM_CDR (x
)), env
);
1683 SCM_CAR (SCM_CAR ( SCM_CDR (x
))) = scm_tc16_variable
;
1684 RETURN (SCM_UNSPECIFIED
)
1687 case (SCM_ISYMNUM (SCM_IM_APPLY
)):
1689 proc
= EVALCAR (proc
, env
);
1690 SCM_ASRTGO (SCM_NIMP (proc
), badfun
);
1691 if (SCM_CLOSUREP (proc
))
1693 PREP_APPLY (proc
, SCM_EOL
);
1694 t
.arg1
= SCM_CDR (SCM_CDR (x
));
1695 t
.arg1
= EVALCAR (t
.arg1
, env
);
1697 debug
.info
->a
.args
= t
.arg1
;
1700 if (scm_badargsp (SCM_CAR (SCM_CODE (proc
)), t
.arg1
))
1703 env
= EXTEND_ENV (SCM_CAR (SCM_CODE (proc
)), t
.arg1
, SCM_ENV (proc
));
1704 x
= SCM_CODE (proc
);
1710 case (SCM_ISYMNUM (SCM_IM_CONT
)):
1711 scm_make_cont (&t
.arg1
);
1712 if (setjmp (SCM_JMPBUF (t
.arg1
)))
1715 val
= SCM_THROW_VALUE (t
.arg1
);
1719 proc
= evalcar (proc
, env
);
1720 SCM_ASRTGO (SCM_NIMP (proc
), badfun
);
1721 PREP_APPLY (proc
, scm_cons (t
.arg1
, SCM_EOL
));
1732 /* scm_everr (x, env,...) */
1733 scm_error (scm_misc_error_key
,
1735 "Wrong type to apply: %S",
1736 scm_listify (proc
, SCM_UNDEFINED
),
1738 case scm_tc7_vector
:
1741 case scm_tc7_byvect
:
1749 case scm_tc7_llvect
:
1751 case scm_tc7_string
:
1752 case scm_tc7_mb_string
:
1753 case scm_tc7_substring
:
1754 case scm_tc7_mb_substring
:
1756 case scm_tcs_closures
:
1760 #ifdef MEMOIZE_LOCALS
1761 case (127 & SCM_ILOC00
):
1762 proc
= *scm_ilookup (SCM_CAR (x
), env
);
1763 SCM_ASRTGO (SCM_NIMP (proc
), badfun
);
1770 #endif /* ifdef MEMOIZE_LOCALS */
1773 case scm_tcs_cons_gloc
:
1774 proc
= SCM_GLOC_VAL (SCM_CAR (x
));
1775 SCM_ASRTGO (SCM_NIMP (proc
), badfun
);
1784 case scm_tcs_cons_nimcar
:
1785 if (SCM_SYMBOLP (SCM_CAR (x
)))
1787 proc
= *scm_lookupcar (x
, env
);
1793 if (scm_tc16_macro
== SCM_TYP16 (proc
))
1798 t
.arg1
= SCM_APPLY (SCM_CDR (proc
), x
, scm_cons (env
, scm_listofnull
));
1799 switch ((int) (SCM_CAR (proc
) >> 16))
1802 if (scm_ilength (t
.arg1
) <= 0)
1803 t
.arg1
= scm_cons2 (SCM_IM_BEGIN
, t
.arg1
, SCM_EOL
);
1805 if (!SCM_CLOSUREP (SCM_CDR (proc
)))
1807 #if 0 /* Top-level defines doesn't very often occur in backtraces */
1808 if (scm_m_define
== SCM_SUBRF (SCM_CDR (proc
)) && SCM_TOP_LEVEL (env
))
1809 /* Prevent memoizing result of define macro */
1811 debug
.info
->e
.exp
= scm_cons (SCM_CAR (x
), SCM_CDR (x
));
1812 scm_set_source_properties_x (debug
.info
->e
.exp
,
1813 scm_source_properties (x
));
1817 SCM_CAR (x
) = SCM_CAR (t
.arg1
);
1818 SCM_CDR (x
) = SCM_CDR (t
.arg1
);
1822 /* Prevent memoizing of debug info expression. */
1823 debug
.info
->e
.exp
= scm_cons (SCM_CAR (x
), SCM_CDR (x
));
1824 scm_set_source_properties_x (debug
.info
->e
.exp
,
1825 scm_source_properties (x
));
1828 SCM_CAR (x
) = SCM_CAR (t
.arg1
);
1829 SCM_CDR (x
) = SCM_CDR (t
.arg1
);
1833 if (SCM_NIMP (x
= t
.arg1
))
1841 proc
= SCM_CEVAL (SCM_CAR (x
), env
);
1842 SCM_ASRTGO (SCM_NIMP (proc
), badfun
);
1847 if (SCM_CLOSUREP (proc
))
1849 arg2
= SCM_CAR (SCM_CODE (proc
));
1850 t
.arg1
= SCM_CDR (x
);
1851 while (SCM_NIMP (arg2
))
1853 if (SCM_NCONSP (arg2
))
1855 if (SCM_IMP (t
.arg1
))
1856 goto umwrongnumargs
;
1857 arg2
= SCM_CDR (arg2
);
1858 t
.arg1
= SCM_CDR (t
.arg1
);
1860 if (SCM_NNULLP (t
.arg1
))
1861 goto umwrongnumargs
;
1863 else if (scm_tc16_macro
== SCM_TYP16 (proc
))
1864 goto handle_a_macro
;
1870 PREP_APPLY (proc
, SCM_EOL
);
1871 if (SCM_NULLP (SCM_CDR (x
))) {
1873 switch (SCM_TYP7 (proc
))
1874 { /* no arguments given */
1875 case scm_tc7_subr_0
:
1876 RETURN (SCM_SUBRF (proc
) ());
1877 case scm_tc7_subr_1o
:
1878 RETURN (SCM_SUBRF (proc
) (SCM_UNDEFINED
));
1880 RETURN (SCM_SUBRF (proc
) (SCM_EOL
));
1881 case scm_tc7_rpsubr
:
1882 RETURN (SCM_BOOL_T
);
1884 RETURN (SCM_SUBRF (proc
) (SCM_UNDEFINED
, SCM_UNDEFINED
));
1888 proc
= SCM_CCLO_SUBR (proc
);
1890 debug
.info
->a
.proc
= proc
;
1891 debug
.info
->a
.args
= scm_cons (t
.arg1
, SCM_EOL
);
1895 case scm_tcs_closures
:
1896 x
= SCM_CODE (proc
);
1897 env
= EXTEND_ENV (SCM_CAR (x
), SCM_EOL
, SCM_ENV (proc
));
1899 case scm_tc7_contin
:
1900 case scm_tc7_subr_1
:
1901 case scm_tc7_subr_2
:
1902 case scm_tc7_subr_2o
:
1904 case scm_tc7_subr_3
:
1905 case scm_tc7_lsubr_2
:
1909 /* scm_everr (x, env,...) */
1910 scm_wrong_num_args (proc
);
1912 /* handle macros here */
1917 /* must handle macros by here */
1923 t
.arg1
= EVALCAR (x
, env
);
1925 debug
.info
->a
.args
= scm_cons (t
.arg1
, SCM_EOL
);
1932 switch (SCM_TYP7 (proc
))
1933 { /* have one argument in t.arg1 */
1934 case scm_tc7_subr_2o
:
1935 RETURN (SCM_SUBRF (proc
) (t
.arg1
, SCM_UNDEFINED
));
1936 case scm_tc7_subr_1
:
1937 case scm_tc7_subr_1o
:
1938 RETURN (SCM_SUBRF (proc
) (t
.arg1
));
1941 if (SCM_SUBRF (proc
))
1943 if (SCM_INUMP (t
.arg1
))
1945 RETURN (scm_makdbl (SCM_DSUBRF (proc
) ((double) SCM_INUM (t
.arg1
)),
1948 SCM_ASRTGO (SCM_NIMP (t
.arg1
), floerr
);
1949 if (SCM_REALP (t
.arg1
))
1951 RETURN (scm_makdbl (SCM_DSUBRF (proc
) (SCM_REALPART (t
.arg1
)), 0.0));
1954 if (SCM_BIGP (t
.arg1
))
1956 RETURN (scm_makdbl (SCM_DSUBRF (proc
) (scm_big2dbl (t
.arg1
)), 0.0));
1960 scm_wta (t
.arg1
, (char *) SCM_ARG1
, SCM_CHARS (SCM_SNAME (proc
)));
1963 proc
= (SCM
) SCM_SNAME (proc
);
1965 char *chrs
= SCM_CHARS (proc
) + SCM_LENGTH (proc
) - 1;
1966 while ('c' != *--chrs
)
1968 SCM_ASSERT (SCM_NIMP (t
.arg1
) && SCM_CONSP (t
.arg1
),
1969 t
.arg1
, SCM_ARG1
, SCM_CHARS (proc
));
1970 t
.arg1
= ('a' == *chrs
) ? SCM_CAR (t
.arg1
) : SCM_CDR (t
.arg1
);
1974 case scm_tc7_rpsubr
:
1975 RETURN (SCM_BOOL_T
);
1977 RETURN (SCM_SUBRF (proc
) (t
.arg1
, SCM_UNDEFINED
));
1980 RETURN (SCM_SUBRF (proc
) (debug
.info
->a
.args
))
1982 RETURN (SCM_SUBRF (proc
) (scm_cons (t
.arg1
, SCM_EOL
)));
1988 proc
= SCM_CCLO_SUBR (proc
);
1990 debug
.info
->a
.args
= scm_cons (t
.arg1
, debug
.info
->a
.args
);
1991 debug
.info
->a
.proc
= proc
;
1995 case scm_tcs_closures
:
1996 x
= SCM_CODE (proc
);
1998 env
= EXTEND_ENV (SCM_CAR (x
), debug
.info
->a
.args
, SCM_ENV (proc
));
2000 env
= EXTEND_ENV (SCM_CAR (x
), scm_cons (t
.arg1
, SCM_EOL
), SCM_ENV (proc
));
2003 case scm_tc7_contin
:
2004 scm_call_continuation (proc
, t
.arg1
);
2005 case scm_tc7_subr_2
:
2006 case scm_tc7_subr_0
:
2007 case scm_tc7_subr_3
:
2008 case scm_tc7_lsubr_2
:
2018 { /* have two or more arguments */
2019 arg2
= EVALCAR (x
, env
);
2021 debug
.info
->a
.args
= scm_cons2 (t
.arg1
, arg2
, SCM_EOL
);
2024 if (SCM_NULLP (x
)) {
2029 switch (SCM_TYP7 (proc
))
2030 { /* have two arguments */
2031 case scm_tc7_subr_2
:
2032 case scm_tc7_subr_2o
:
2033 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
));
2036 RETURN (SCM_SUBRF (proc
) (debug
.info
->a
.args
))
2038 RETURN (SCM_SUBRF (proc
) (scm_cons2 (t
.arg1
, arg2
, SCM_EOL
)));
2040 case scm_tc7_lsubr_2
:
2041 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
, SCM_EOL
));
2042 case scm_tc7_rpsubr
:
2044 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
));
2049 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc
), proc
,
2050 scm_cons (debug
.info
->a
.args
, SCM_EOL
)));
2052 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc
), proc
,
2053 scm_cons2 (t
.arg1
, arg2
,
2054 scm_cons (scm_eval_args (x
, env
), SCM_EOL
))));
2056 /* case scm_tc7_cclo:
2057 x = scm_cons(arg2, scm_eval_args(x, env));
2060 proc = SCM_CCLO_SUBR(proc);
2063 case scm_tc7_subr_0
:
2065 case scm_tc7_subr_1o
:
2066 case scm_tc7_subr_1
:
2067 case scm_tc7_subr_3
:
2068 case scm_tc7_contin
:
2072 case scm_tcs_closures
:
2074 env
= EXTEND_ENV (SCM_CAR (SCM_CODE (proc
)), debug
.info
->a
.args
, SCM_ENV (proc
));
2076 env
= EXTEND_ENV (SCM_CAR (SCM_CODE (proc
)), scm_cons2 (t
.arg1
, arg2
, SCM_EOL
), SCM_ENV (proc
));
2078 x
= SCM_CODE (proc
);
2083 debug
.info
->a
.args
= scm_cons2 (t
.arg1
, arg2
,
2084 scm_deval_args (x
, env
, &SCM_CDR (SCM_CDR (debug
.info
->a
.args
))));
2087 switch (SCM_TYP7 (proc
))
2088 { /* have 3 or more arguments */
2090 case scm_tc7_subr_3
:
2091 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x
)), wrongnumargs
);
2092 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
, SCM_CAR (SCM_CDR (SCM_CDR (debug
.info
->a
.args
)))));
2094 /* t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
2096 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
2100 case scm_tc7_rpsubr
:
2101 RETURN (SCM_APPLY (proc
, t
.arg1
, scm_acons (arg2
, SCM_CDR (SCM_CDR (debug
.info
->a
.args
)), SCM_EOL
)))
2102 case scm_tc7_lsubr_2
:
2103 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
, SCM_CDR (SCM_CDR (debug
.info
->a
.args
))))
2105 RETURN (SCM_SUBRF (proc
) (debug
.info
->a
.args
))
2110 case scm_tcs_closures
:
2111 SCM_SET_ARGSREADY (debug
);
2112 env
= EXTEND_ENV (SCM_CAR (SCM_CODE (proc
)),
2115 x
= SCM_CODE (proc
);
2118 case scm_tc7_subr_3
:
2119 SCM_ASRTGO (SCM_NULLP (SCM_CDR (x
)), wrongnumargs
);
2120 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
, EVALCAR (x
, env
)));
2122 /* t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
2124 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
2128 case scm_tc7_rpsubr
:
2129 RETURN (SCM_APPLY (proc
, t
.arg1
, scm_acons (arg2
, scm_eval_args (x
, env
), SCM_EOL
)));
2130 case scm_tc7_lsubr_2
:
2131 RETURN (SCM_SUBRF (proc
) (t
.arg1
, arg2
, scm_eval_args (x
, env
)));
2133 RETURN (SCM_SUBRF (proc
) (scm_cons2 (t
.arg1
, arg2
, scm_eval_args (x
, env
))));
2138 case scm_tcs_closures
:
2140 SCM_SET_ARGSREADY (debug
);
2142 env
= EXTEND_ENV (SCM_CAR (SCM_CODE (proc
)),
2143 scm_cons2 (t
.arg1
, arg2
, scm_eval_args (x
, env
)),
2145 x
= SCM_CODE (proc
);
2148 case scm_tc7_subr_2
:
2149 case scm_tc7_subr_1o
:
2150 case scm_tc7_subr_2o
:
2151 case scm_tc7_subr_0
:
2153 case scm_tc7_subr_1
:
2154 case scm_tc7_contin
:
2163 if (SCM_EXIT_FRAME_P
|| (SCM_TRACE_P
&& SCM_TRACED_FRAME_P (debug
)))
2165 SCM_EXIT_FRAME_P
= 0;
2166 SCM_RESET_DEBUG_MODE
;
2167 SCM_CLEAR_TRACED_FRAME (debug
);
2168 if (SCM_CHEAPTRAPS_P
)
2169 t
.arg1
= scm_make_debugobj ((scm_debug_frame
*) &debug
);
2172 scm_make_cont (&t
.arg1
);
2173 if (setjmp (SCM_JMPBUF (t
.arg1
)))
2175 proc
= SCM_THROW_VALUE (t
.arg1
);
2179 scm_ithrow (scm_i_exit_frame
, scm_cons2 (t
.arg1
, proc
, SCM_EOL
), 0);
2182 scm_last_debug_frame
= debug
.prev
;
2188 /* SECTION: This code is compiled once.
2193 SCM_PROC(s_procedure_documentation
, "procedure-documentation", 1, 0, 0, scm_procedure_documentation
);
2196 scm_procedure_documentation (proc
)
2200 SCM_ASSERT (SCM_BOOL_T
== scm_procedure_p (proc
) && SCM_NIMP (proc
) && SCM_TYP7 (proc
) != scm_tc7_contin
,
2201 proc
, SCM_ARG1
, s_procedure_documentation
);
2202 switch (SCM_TYP7 (proc
))
2204 case scm_tcs_closures
:
2205 code
= SCM_CDR (SCM_CODE (proc
));
2206 if (SCM_IMP (SCM_CDR (code
)))
2208 code
= SCM_CAR (code
);
2211 if (SCM_STRINGP (code
))
2224 /* This code processes the 'arg ...' parameters to apply.
2226 (apply PROC ARG1 ... ARGS)
2228 The ARG1 ... arguments are consed on to the front of ARGS (which
2229 must be a list), and then PROC is applied to the elements of the
2230 result. apply:nconc2last takes care of building the list of
2231 arguments, given (ARG1 ... ARGS).
2233 apply:nconc2last destroys its argument. On that topic, this code
2234 came into my care with the following beautifully cryptic comment on
2235 that topic: "This will only screw you if you do (scm_apply
2236 scm_apply '( ... ))" If you know what they're referring to, send
2237 me a patch to this comment. */
2239 SCM_PROC(s_nconc2last
, "apply:nconc2last", 1, 0, 0, scm_nconc2last
);
2242 scm_nconc2last (lst
)
2246 SCM_ASSERT (scm_ilength (lst
) > 0, lst
, SCM_ARG1
, s_nconc2last
);
2248 while (SCM_NNULLP (SCM_CDR (*lloc
)))
2249 lloc
= &SCM_CDR (*lloc
);
2250 SCM_ASSERT (scm_ilength (SCM_CAR (*lloc
)) >= 0, lst
, SCM_ARG1
, s_nconc2last
);
2251 *lloc
= SCM_CAR (*lloc
);
2258 /* SECTION: When DEVAL is defined this code yields scm_dapply.
2259 * It is compiled twice.
2265 scm_apply (proc
, arg1
, args
)
2275 scm_dapply (proc
, arg1
, args
)
2284 SCM_APPLY (proc
, arg1
, args
)
2289 #ifdef DEBUG_EXTENSIONS
2291 scm_debug_frame debug
;
2292 debug
.prev
= scm_last_debug_frame
;
2293 debug
.status
= SCM_APPLYFRAME
;
2294 debug
.vect
[0].a
.proc
= proc
;
2295 debug
.vect
[0].a
.args
= SCM_EOL
;
2296 scm_last_debug_frame
= &debug
;
2299 return scm_dapply (proc
, arg1
, args
);
2303 SCM_ASRTGO (SCM_NIMP (proc
), badproc
);
2304 if (SCM_NULLP (args
))
2306 if (SCM_NULLP (arg1
))
2307 arg1
= SCM_UNDEFINED
;
2310 args
= SCM_CDR (arg1
);
2311 arg1
= SCM_CAR (arg1
);
2316 /* SCM_ASRTGO(SCM_NIMP(args) && SCM_CONSP(args), wrongnumargs); */
2317 args
= scm_nconc2last (args
);
2320 debug
.vect
[0].a
.args
= scm_cons (arg1
, args
);
2321 if (SCM_ENTER_FRAME_P
)
2324 SCM_ENTER_FRAME_P
= 0;
2325 SCM_RESET_DEBUG_MODE
;
2326 if (SCM_CHEAPTRAPS_P
)
2327 tmp
= scm_make_debugobj ((scm_debug_frame
*) &debug
);
2330 scm_make_cont (&tmp
);
2331 if (setjmp (SCM_JMPBUF (tmp
)))
2334 scm_ithrow (scm_i_enter_frame
, scm_cons (tmp
, SCM_EOL
), 0);
2342 switch (SCM_TYP7 (proc
))
2344 case scm_tc7_subr_2o
:
2345 args
= SCM_NULLP (args
) ? SCM_UNDEFINED
: SCM_CAR (args
);
2346 RETURN (SCM_SUBRF (proc
) (arg1
, args
))
2347 case scm_tc7_subr_2
:
2348 SCM_ASRTGO (SCM_NULLP (SCM_CDR (args
)), wrongnumargs
);
2349 args
= SCM_CAR (args
);
2350 RETURN (SCM_SUBRF (proc
) (arg1
, args
))
2351 case scm_tc7_subr_0
:
2352 SCM_ASRTGO (SCM_UNBNDP (arg1
), wrongnumargs
);
2353 RETURN (SCM_SUBRF (proc
) ())
2354 case scm_tc7_subr_1
:
2355 case scm_tc7_subr_1o
:
2356 SCM_ASRTGO (SCM_NULLP (args
), wrongnumargs
);
2357 RETURN (SCM_SUBRF (proc
) (arg1
))
2359 SCM_ASRTGO (SCM_NULLP (args
), wrongnumargs
);
2361 if (SCM_SUBRF (proc
))
2363 if (SCM_INUMP (arg1
))
2365 RETURN (scm_makdbl (SCM_DSUBRF (proc
) ((double) SCM_INUM (arg1
)), 0.0));
2367 SCM_ASRTGO (SCM_NIMP (arg1
), floerr
);
2368 if (SCM_REALP (arg1
))
2370 RETURN (scm_makdbl (SCM_DSUBRF (proc
) (SCM_REALPART (arg1
)), 0.0));
2375 RETURN (scm_makdbl (SCM_DSUBRF (proc
) (scm_big2dbl (arg1
)), 0.0))
2378 scm_wta (arg1
, (char *) SCM_ARG1
, SCM_CHARS (SCM_SNAME (proc
)));
2381 proc
= (SCM
) SCM_SNAME (proc
);
2383 char *chrs
= SCM_CHARS (proc
) + SCM_LENGTH (proc
) - 1;
2384 while ('c' != *--chrs
)
2386 SCM_ASSERT (SCM_NIMP (arg1
) && SCM_CONSP (arg1
),
2387 arg1
, SCM_ARG1
, SCM_CHARS (proc
));
2388 arg1
= ('a' == *chrs
) ? SCM_CAR (arg1
) : SCM_CDR (arg1
);
2392 case scm_tc7_subr_3
:
2393 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
), SCM_CAR (SCM_CDR (args
))))
2396 RETURN (SCM_SUBRF (proc
) (SCM_UNBNDP (arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
))
2398 RETURN (SCM_SUBRF (proc
) (SCM_UNBNDP (arg1
) ? SCM_EOL
: scm_cons (arg1
, args
)))
2400 case scm_tc7_lsubr_2
:
2401 SCM_ASRTGO (SCM_NIMP (args
) && SCM_CONSP (args
), wrongnumargs
);
2402 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
), SCM_CDR (args
)))
2404 if (SCM_NULLP (args
))
2405 RETURN (SCM_SUBRF (proc
) (arg1
, SCM_UNDEFINED
))
2406 while (SCM_NIMP (args
))
2408 SCM_ASSERT (SCM_CONSP (args
), args
, SCM_ARG2
, "apply");
2409 arg1
= SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
));
2410 args
= SCM_CDR (args
);
2413 case scm_tc7_rpsubr
:
2414 if (SCM_NULLP (args
))
2415 RETURN (SCM_BOOL_T
);
2416 while (SCM_NIMP (args
))
2418 SCM_ASSERT (SCM_CONSP (args
), args
, SCM_ARG2
, "apply");
2419 if (SCM_FALSEP (SCM_SUBRF (proc
) (arg1
, SCM_CAR (args
))))
2420 RETURN (SCM_BOOL_F
);
2421 arg1
= SCM_CAR (args
);
2422 args
= SCM_CDR (args
);
2424 RETURN (SCM_BOOL_T
);
2425 case scm_tcs_closures
:
2427 arg1
= (SCM_UNBNDP (arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
);
2429 arg1
= (SCM_UNBNDP (arg1
) ? SCM_EOL
: scm_cons (arg1
, args
));
2432 if (scm_badargsp (SCM_CAR (SCM_CODE (proc
)), arg1
))
2435 args
= EXTEND_ENV (SCM_CAR (SCM_CODE (proc
)), arg1
, SCM_ENV (proc
));
2436 proc
= SCM_CODE (proc
);
2437 while (SCM_NNULLP (proc
= SCM_CDR (proc
)))
2438 arg1
= EVALCAR (proc
, args
);
2440 case scm_tc7_contin
:
2441 SCM_ASRTGO (SCM_NULLP (args
), wrongnumargs
);
2442 scm_call_continuation (proc
, arg1
);
2446 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: debug
.vect
[0].a
.args
);
2448 proc
= SCM_CCLO_SUBR (proc
);
2449 debug
.vect
[0].a
.proc
= proc
;
2450 debug
.vect
[0].a
.args
= scm_cons (arg1
, args
);
2452 args
= (SCM_UNBNDP(arg1
) ? SCM_EOL
: scm_cons (arg1
, args
));
2454 proc
= SCM_CCLO_SUBR (proc
);
2459 scm_wrong_num_args (proc
);
2462 scm_wta (proc
, (char *) SCM_ARG1
, "apply");
2468 if (SCM_EXIT_FRAME_P
|| (SCM_TRACE_P
&& SCM_TRACED_FRAME_P (debug
)))
2470 SCM_EXIT_FRAME_P
= 0;
2471 SCM_RESET_DEBUG_MODE
;
2472 SCM_CLEAR_TRACED_FRAME (debug
);
2473 if (SCM_CHEAPTRAPS_P
)
2474 arg1
= scm_make_debugobj ((scm_debug_frame
*) &debug
);
2477 scm_make_cont (&arg1
);
2478 if (setjmp (SCM_JMPBUF (arg1
)))
2480 proc
= SCM_THROW_VALUE (arg1
);
2484 scm_ithrow (scm_i_exit_frame
, scm_cons2 (arg1
, proc
, SCM_EOL
), 0);
2487 scm_last_debug_frame
= debug
.prev
;
2493 /* SECTION: The rest of this file is only read once.
2498 SCM_PROC(s_map
, "map", 2, 0, 1, scm_map
);
2501 scm_map (proc
, arg1
, args
)
2509 SCM
*ve
= &args
; /* Keep args from being optimized away. */
2511 if (SCM_NULLP (arg1
))
2513 SCM_ASSERT (SCM_NIMP (arg1
), arg1
, SCM_ARG2
, s_map
);
2514 if (SCM_NULLP (args
))
2516 while (SCM_NIMP (arg1
))
2518 SCM_ASSERT (SCM_CONSP (arg1
), arg1
, SCM_ARG2
, s_map
);
2519 *pres
= scm_cons (scm_apply (proc
, SCM_CAR (arg1
), scm_listofnull
), SCM_EOL
);
2520 pres
= &SCM_CDR (*pres
);
2521 arg1
= SCM_CDR (arg1
);
2525 args
= scm_vector (scm_cons (arg1
, args
));
2526 ve
= SCM_VELTS (args
);
2528 for (i
= SCM_LENGTH (args
) - 1; i
>= 0; i
--)
2529 SCM_ASSERT (SCM_NIMP (ve
[i
]) && SCM_CONSP (ve
[i
]), args
, SCM_ARG2
, s_map
);
2534 for (i
= SCM_LENGTH (args
) - 1; i
>= 0; i
--)
2538 arg1
= scm_cons (SCM_CAR (ve
[i
]), arg1
);
2539 ve
[i
] = SCM_CDR (ve
[i
]);
2541 *pres
= scm_cons (scm_apply (proc
, arg1
, SCM_EOL
), SCM_EOL
);
2542 pres
= &SCM_CDR (*pres
);
2547 SCM_PROC(s_for_each
, "for-each", 2, 0, 1, scm_for_each
);
2550 scm_for_each (proc
, arg1
, args
)
2555 SCM
*ve
= &args
; /* Keep args from being optimized away. */
2558 return SCM_UNSPECIFIED
;
2559 SCM_ASSERT (SCM_NIMP (arg1
), arg1
, SCM_ARG2
, s_for_each
);
2562 while SCM_NIMP (arg1
)
2564 SCM_ASSERT (SCM_CONSP (arg1
), arg1
, SCM_ARG2
, s_for_each
);
2565 scm_apply (proc
, SCM_CAR (arg1
), scm_listofnull
);
2566 arg1
= SCM_CDR (arg1
);
2568 return SCM_UNSPECIFIED
;
2570 args
= scm_vector (scm_cons (arg1
, args
));
2571 ve
= SCM_VELTS (args
);
2573 for (i
= SCM_LENGTH (args
) - 1; i
>= 0; i
--)
2574 SCM_ASSERT (SCM_NIMP (ve
[i
]) && SCM_CONSP (ve
[i
]), args
, SCM_ARG2
, s_for_each
);
2579 for (i
= SCM_LENGTH (args
) - 1; i
>= 0; i
--)
2582 (ve
[i
]) return SCM_UNSPECIFIED
;
2583 arg1
= scm_cons (SCM_CAR (ve
[i
]), arg1
);
2584 ve
[i
] = SCM_CDR (ve
[i
]);
2586 scm_apply (proc
, arg1
, SCM_EOL
);
2593 scm_closure (code
, env
)
2599 SCM_SETCODE (z
, code
);
2605 long scm_tc16_promise
;
2614 SCM_CAR (z
) = scm_tc16_promise
;
2620 static int prinprom
SCM_P ((SCM exp
, SCM port
, scm_print_state
*pstate
));
2623 prinprom (exp
, port
, pstate
)
2626 scm_print_state
*pstate
;
2628 int writingp
= SCM_WRITINGP (pstate
);
2629 scm_gen_puts (scm_regular_string
, "#<promise ", port
);
2630 SCM_SET_WRITINGP (pstate
, 1);
2631 scm_iprin1 (SCM_CDR (exp
), port
, pstate
);
2632 SCM_SET_WRITINGP (pstate
, writingp
);
2633 scm_gen_putc ('>', port
);
2638 SCM_PROC(s_makacro
, "procedure->syntax", 1, 0, 0, scm_makacro
);
2647 SCM_CAR (z
) = scm_tc16_macro
;
2652 SCM_PROC(s_makmacro
, "procedure->macro", 1, 0, 0, scm_makmacro
);
2661 SCM_CAR (z
) = scm_tc16_macro
| (1L << 16);
2666 SCM_PROC(s_makmmacro
, "procedure->memoizing-macro", 1, 0, 0, scm_makmmacro
);
2669 scm_makmmacro (code
)
2675 SCM_CAR (z
) = scm_tc16_macro
| (2L << 16);
2681 static int prinmacro
SCM_P ((SCM exp
, SCM port
, scm_print_state
*pstate
));
2684 prinmacro (exp
, port
, pstate
)
2687 scm_print_state
*pstate
;
2689 int writingp
= SCM_WRITINGP (pstate
);
2690 if (SCM_CAR (exp
) & (3L << 16))
2691 scm_gen_puts (scm_regular_string
, "#<macro", port
);
2693 scm_gen_puts (scm_regular_string
, "#<syntax", port
);
2694 if (SCM_CAR (exp
) & (2L << 16))
2695 scm_gen_putc ('!', port
);
2696 scm_gen_putc (' ', port
);
2697 SCM_SET_WRITINGP (pstate
, 1);
2698 scm_iprin1 (SCM_CDR (exp
), port
, pstate
);
2699 SCM_SET_WRITINGP (pstate
, writingp
);
2700 scm_gen_putc ('>', port
);
2704 SCM_PROC(s_force
, "force", 1, 0, 0, scm_force
);
2710 SCM_ASSERT ((SCM_TYP16 (x
) == scm_tc16_promise
), x
, SCM_ARG1
, s_force
);
2711 if (!((1L << 16) & SCM_CAR (x
)))
2713 SCM ans
= scm_apply (SCM_CDR (x
), SCM_EOL
, SCM_EOL
);
2714 if (!((1L << 16) & SCM_CAR (x
)))
2718 SCM_CAR (x
) |= (1L << 16);
2725 SCM_PROC (s_promise_p
, "promise?", 1, 0, 0, scm_promise_p
);
2731 return ((SCM_NIMP (x
) && (SCM_TYP16 (x
) == scm_tc16_promise
))
2736 SCM_PROC(s_copy_tree
, "copy-tree", 1, 0, 0, scm_copy_tree
);
2745 if (SCM_VECTORP (obj
))
2747 scm_sizet i
= SCM_LENGTH (obj
);
2748 ans
= scm_make_vector (SCM_MAKINUM (i
), SCM_UNSPECIFIED
, SCM_UNDEFINED
);
2750 SCM_VELTS (ans
)[i
] = scm_copy_tree (SCM_VELTS (obj
)[i
]);
2755 /* return scm_cons(scm_copy_tree(SCM_CAR(obj)), scm_copy_tree(SCM_CDR(obj))); */
2756 ans
= tl
= scm_cons (scm_copy_tree (SCM_CAR (obj
)), SCM_UNSPECIFIED
);
2757 while (SCM_NIMP (obj
= SCM_CDR (obj
)) && SCM_CONSP (obj
))
2758 tl
= (SCM_CDR (tl
) = scm_cons (scm_copy_tree (SCM_CAR (obj
)), SCM_UNSPECIFIED
));
2765 scm_eval_3 (obj
, copyp
, env
)
2770 if (SCM_NIMP (SCM_CDR (scm_system_transformer
)))
2771 obj
= scm_apply (SCM_CDR (scm_system_transformer
), obj
, scm_listofnull
);
2773 obj
= scm_copy_tree (obj
);
2774 return XEVAL (obj
, env
);
2779 scm_top_level_env (thunk
)
2785 return scm_cons(thunk
, (SCM
)SCM_EOL
);
2788 SCM_PROC(s_eval2
, "eval2", 2, 0, 0, scm_eval2
);
2791 scm_eval2 (obj
, env_thunk
)
2795 return scm_eval_3 (obj
, 1, scm_top_level_env(env_thunk
));
2798 SCM_PROC(s_eval
, "eval", 1, 0, 0, scm_eval
);
2805 scm_eval_3(obj
, 1, scm_top_level_env(SCM_CDR(scm_top_level_lookup_thunk_var
)));
2808 SCM_PROC(s_eval_x
, "eval!", 1, 0, 0, scm_eval_x
);
2817 scm_top_level_env (SCM_CDR (scm_top_level_lookup_thunk_var
)));
2820 SCM_PROC (s_macro_eval_x
, "macro-eval!", 2, 0, 0, scm_macro_eval_x
);
2823 scm_macro_eval_x (exp
, env
)
2827 return scm_eval_3 (exp
, 0, env
);
2832 scm_definedp (x
, env
)
2836 SCM proc
= SCM_CAR (x
= SCM_CDR (x
));
2837 if (SCM_ISYMP (proc
))
2839 else if(SCM_IMP(proc
) || !SCM_SYMBOLP(proc
))
2843 SCM vcell
= scm_sym2vcell(proc
, env_top_level(env
), SCM_BOOL_F
);
2844 return (vcell
== SCM_BOOL_F
|| SCM_UNBNDP(SCM_CDR(vcell
))) ? SCM_BOOL_F
: SCM_BOOL_T
;
2848 static scm_smobfuns promsmob
=
2849 {scm_markcdr
, scm_free0
, prinprom
};
2851 static scm_smobfuns macrosmob
=
2852 {scm_markcdr
, scm_free0
, prinmacro
};
2856 scm_make_synt (name
, macroizer
, fcn
)
2858 SCM (*macroizer
) ();
2861 SCM symcell
= scm_sysintern (name
, SCM_UNDEFINED
);
2862 long tmp
= ((((SCM_CELLPTR
) (SCM_CAR (symcell
))) - scm_heap_org
) << 8);
2864 if ((tmp
>> 8) != ((SCM_CELLPTR
) (SCM_CAR (symcell
)) - scm_heap_org
))
2867 SCM_SUBRF (z
) = fcn
;
2868 SCM_CAR (z
) = tmp
+ scm_tc7_subr_2
;
2869 SCM_CDR (symcell
) = macroizer (z
);
2870 return SCM_CAR (symcell
);
2874 /* At this point, scm_deval and scm_dapply are generated.
2877 #ifdef DEBUG_EXTENSIONS
2887 scm_tc16_promise
= scm_newsmob (&promsmob
);
2888 scm_tc16_macro
= scm_newsmob (¯osmob
);
2889 scm_i_apply
= scm_make_subr ("apply", scm_tc7_lsubr_2
, scm_apply
);
2890 scm_system_transformer
= scm_sysintern ("scm:eval-transformer", SCM_UNDEFINED
);
2891 scm_i_dot
= SCM_CAR (scm_sysintern (".", SCM_UNDEFINED
));
2892 scm_i_arrow
= SCM_CAR (scm_sysintern ("=>", SCM_UNDEFINED
));
2893 scm_i_else
= SCM_CAR (scm_sysintern ("else", SCM_UNDEFINED
));
2894 scm_i_unquote
= SCM_CAR (scm_sysintern ("unquote", SCM_UNDEFINED
));
2895 scm_i_uq_splicing
= SCM_CAR (scm_sysintern ("unquote-splicing", SCM_UNDEFINED
));
2898 scm_i_quasiquote
= scm_make_synt (s_quasiquote
, scm_makacro
, scm_m_quasiquote
);
2899 scm_make_synt (s_undefine
, scm_makacro
, scm_m_undefine
);
2900 scm_make_synt (s_delay
, scm_makacro
, scm_m_delay
);
2903 scm_top_level_lookup_thunk_var
=
2904 scm_sysintern("*top-level-lookup-thunk*", SCM_BOOL_F
);
2906 scm_i_and
= scm_make_synt ("and", scm_makmmacro
, scm_m_and
);
2907 scm_i_begin
= scm_make_synt ("begin", scm_makmmacro
, scm_m_begin
);
2908 scm_i_case
= scm_make_synt ("case", scm_makmmacro
, scm_m_case
);
2909 scm_i_cond
= scm_make_synt ("cond", scm_makmmacro
, scm_m_cond
);
2910 scm_i_define
= scm_make_synt ("define", scm_makmmacro
, scm_m_define
);
2911 scm_i_do
= scm_make_synt ("do", scm_makmmacro
, scm_m_do
);
2912 scm_i_if
= scm_make_synt ("if", scm_makmmacro
, scm_m_if
);
2913 scm_i_lambda
= scm_make_synt ("lambda", scm_makmmacro
, scm_m_lambda
);
2914 scm_i_let
= scm_make_synt ("let", scm_makmmacro
, scm_m_let
);
2915 scm_i_letrec
= scm_make_synt ("letrec", scm_makmmacro
, scm_m_letrec
);
2916 scm_i_letstar
= scm_make_synt ("let*", scm_makmmacro
, scm_m_letstar
);
2917 scm_i_or
= scm_make_synt ("or", scm_makmmacro
, scm_m_or
);
2918 scm_i_quote
= scm_make_synt ("quote", scm_makmmacro
, scm_m_quote
);
2919 scm_i_set
= scm_make_synt ("set!", scm_makmmacro
, scm_m_set
);
2920 scm_i_atapply
= scm_make_synt ("@apply", scm_makmmacro
, scm_m_apply
);
2921 scm_i_atcall_cc
= scm_make_synt ("@call-with-current-continuation",
2922 scm_makmmacro
, scm_m_cont
);
2924 scm_make_synt ("defined?", scm_makmmacro
, scm_definedp
);
2925 scm_i_name
= SCM_CAR (scm_sysintern ("name", SCM_UNDEFINED
));
2926 scm_permanent_object (scm_i_name
);
2928 #ifdef DEBUG_EXTENSIONS
2929 scm_i_enter_frame
= SCM_CAR (scm_sysintern ("enter-frame", SCM_UNDEFINED
));
2930 scm_i_apply_frame
= SCM_CAR (scm_sysintern ("apply-frame", SCM_UNDEFINED
));
2931 scm_i_exit_frame
= SCM_CAR (scm_sysintern ("exit-frame", SCM_UNDEFINED
));
2932 scm_i_trace
= SCM_CAR (scm_sysintern ("trace", SCM_UNDEFINED
));