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