* srcprop.c (scm_source_properties, scm_set_source_properties_x,
[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_CDR (er);
162 return &SCM_CAR (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_CAR (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_CAR (vloc) = iloc + SCM_ICDR;
189 #endif
190 return &SCM_CDR (*al);
191 }
192 else
193 break;
194 al = &SCM_CDR (*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_CAR (vloc) = iloc;
206 #endif
207 return &SCM_CAR (*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_error (scm_misc_error_key,
239 NULL,
240 SCM_NULLP (env)
241 ? "Unbound variable: %S"
242 : "Damaged environment: %S",
243 scm_listify (var, SCM_UNDEFINED),
244 SCM_BOOL_F);
245 }
246 #endif
247 SCM_CAR (vloc) = var + 1;
248 /* Except wait...what if the var is not a vcell,
249 * but syntax or something....
250 */
251 return &SCM_CDR (var);
252 }
253
254 #define unmemocar scm_unmemocar
255
256 SCM
257 scm_unmemocar (form, env)
258 SCM form;
259 SCM env;
260 {
261 #ifdef DEBUG_EXTENSIONS
262 register int ir;
263 #endif
264 SCM c;
265
266 if (SCM_IMP (form))
267 return form;
268 c = SCM_CAR (form);
269 if (1 == (c & 7))
270 SCM_CAR (form) = SCM_CAR (c - 1);
271 #ifdef MEMOIZE_LOCALS
272 #ifdef DEBUG_EXTENSIONS
273 else if (SCM_ILOCP (c))
274 {
275 for (ir = SCM_IFRAME (c); ir != 0; --ir)
276 env = SCM_CDR (env);
277 env = SCM_CAR (SCM_CAR (env));
278 for (ir = SCM_IDIST (c); ir != 0; --ir)
279 env = SCM_CDR (env);
280 SCM_CAR (form) = SCM_ICDRP (c) ? env : SCM_CAR (env);
281 }
282 #endif
283 #endif
284 return form;
285 }
286
287
288 SCM
289 scm_eval_car (pair, env)
290 SCM pair;
291 SCM env;
292 {
293 return XEVALCAR (pair, env);
294 }
295
296 \f
297 /*
298 * The following rewrite expressions and
299 * some memoized forms have different syntax
300 */
301
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);
310
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;
313 SCM scm_i_name;
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;
322 SCM scm_i_trace;
323 #endif
324
325 #define ASRTSYNTAX(cond_, msg_) if(!(cond_))scm_wta(xorig, (msg_), what);
326
327
328
329 static void bodycheck SCM_P ((SCM xorig, SCM *bodyloc, char *what));
330
331 static void
332 bodycheck (xorig, bodyloc, what)
333 SCM xorig;
334 SCM *bodyloc;
335 char *what;
336 {
337 ASRTSYNTAX (scm_ilength (*bodyloc) >= 1, s_expression);
338 }
339
340
341
342 SCM
343 scm_m_quote (xorig, env)
344 SCM xorig;
345 SCM env;
346 {
347 ASSYNT (scm_ilength (SCM_CDR (xorig)) == 1, xorig, s_expression, "quote");
348 return scm_cons (SCM_IM_QUOTE, SCM_CDR (xorig));
349 }
350
351
352
353 SCM
354 scm_m_begin (xorig, env)
355 SCM xorig;
356 SCM env;
357 {
358 ASSYNT (scm_ilength (SCM_CDR (xorig)) >= 1, xorig, s_expression, "begin");
359 return scm_cons (SCM_IM_BEGIN, SCM_CDR (xorig));
360 }
361
362
363
364 SCM
365 scm_m_if (xorig, env)
366 SCM xorig;
367 SCM env;
368 {
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));
372 }
373
374
375
376 SCM
377 scm_m_set (xorig, env)
378 SCM xorig;
379 SCM env;
380 {
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);
386 }
387
388
389 #if 0
390
391 SCM
392 scm_m_vref (xorig, env)
393 SCM xorig;
394 SCM env;
395 {
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)))
399 {
400 /* scm_everr (SCM_UNDEFINED, env,..., "global variable reference") */
401 scm_error (scm_misc_error_key,
402 NULL,
403 "Bad variable: %S",
404 scm_listify (SCM_CAR (SCM_CDR (x)), SCM_UNDEFINED),
405 SCM_BOOL_F);
406 }
407 ASSYNT (SCM_NIMP(x) && DEFSCM_VARIABLEP (SCM_CAR (x)),
408 xorig, s_variable, s_vref);
409 return
410 return scm_cons (IM_VREF, x);
411 }
412
413
414
415 SCM
416 scm_m_vset (xorig, env)
417 SCM xorig;
418 SCM env;
419 {
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);
426 }
427 #endif
428
429
430
431 SCM
432 scm_m_and (xorig, env)
433 SCM xorig;
434 SCM env;
435 {
436 int len = scm_ilength (SCM_CDR (xorig));
437 ASSYNT (len >= 0, xorig, s_test, "and");
438 if (len >= 1)
439 return scm_cons (SCM_IM_AND, SCM_CDR (xorig));
440 else
441 return SCM_BOOL_T;
442 }
443
444
445
446 SCM
447 scm_m_or (xorig, env)
448 SCM xorig;
449 SCM env;
450 {
451 int len = scm_ilength (SCM_CDR (xorig));
452 ASSYNT (len >= 0, xorig, s_test, "or");
453 if (len >= 1)
454 return scm_cons (SCM_IM_OR, SCM_CDR (xorig));
455 else
456 return SCM_BOOL_F;
457 }
458
459
460
461 SCM
462 scm_m_case (xorig, env)
463 SCM xorig;
464 SCM env;
465 {
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)))
469 {
470 proc = SCM_CAR (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");
474 }
475 return scm_cons (SCM_IM_CASE, SCM_CDR (xorig));
476 }
477
478
479
480 SCM
481 scm_m_cond (xorig, env)
482 SCM xorig;
483 SCM env;
484 {
485 SCM arg1, x = SCM_CDR (xorig);
486 int len = scm_ilength (x);
487 ASSYNT (len >= 1, xorig, s_clauses, "cond");
488 while (SCM_NIMP (x))
489 {
490 arg1 = SCM_CAR (x);
491 len = scm_ilength (arg1);
492 ASSYNT (len >= 1, xorig, s_clauses, "cond");
493 if (scm_i_else == SCM_CAR (arg1))
494 {
495 ASSYNT (SCM_NULLP (SCM_CDR (x)) && len >= 2, xorig, "bad ELSE clause", "cond");
496 SCM_CAR (arg1) = SCM_BOOL_T;
497 }
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");
501 x = SCM_CDR (x);
502 }
503 return scm_cons (SCM_IM_COND, SCM_CDR (xorig));
504 }
505
506
507
508 SCM
509 scm_m_lambda (xorig, env)
510 SCM xorig;
511 SCM env;
512 {
513 SCM proc, x = SCM_CDR (xorig);
514 if (scm_ilength (x) < 2)
515 goto badforms;
516 proc = SCM_CAR (x);
517 if SCM_NULLP
518 (proc) goto memlambda;
519 if SCM_IMP
520 (proc) goto badforms;
521 if SCM_SYMBOLP
522 (proc) goto memlambda;
523 if SCM_NCONSP
524 (proc) goto badforms;
525 while SCM_NIMP
526 (proc)
527 {
528 if SCM_NCONSP
529 (proc)
530 if (!SCM_SYMBOLP (proc))
531 goto badforms;
532 else
533 goto memlambda;
534 if (!(SCM_NIMP (SCM_CAR (proc)) && SCM_SYMBOLP (SCM_CAR (proc))))
535 goto badforms;
536 proc = SCM_CDR (proc);
537 }
538 if SCM_NNULLP
539 (proc)
540 badforms:scm_wta (xorig, s_formals, "lambda");
541 memlambda:
542 bodycheck (xorig, &SCM_CDR (x), "lambda");
543 return scm_cons (SCM_IM_LAMBDA, SCM_CDR (xorig));
544 }
545
546
547
548 SCM
549 scm_m_letstar (xorig, env)
550 SCM xorig;
551 SCM env;
552 {
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*");
556 proc = SCM_CAR (x);
557 ASSYNT (scm_ilength (proc) >= 0, xorig, s_bindings, "let*");
558 while SCM_NIMP (proc)
559 {
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);
566 }
567 x = scm_cons (vars, SCM_CDR (x));
568 bodycheck (xorig, &SCM_CDR (x), "let*");
569 return scm_cons (SCM_IM_LETSTAR, x);
570 }
571
572 /* DO gets the most radically altered syntax
573 (do ((<var1> <init1> <step1>)
574 (<var2> <init2>)
575 ... )
576 (<test> <return>)
577 <body>)
578 ;; becomes
579 (do_mem (varn ... var2 var1)
580 (<init1> <init2> ... <initn>)
581 (<test> <return>)
582 (<body>)
583 <step1> <step2> ... <stepn>) ;; missing steps replaced by var
584 */
585
586
587
588 SCM
589 scm_m_do (xorig, env)
590 SCM xorig;
591 SCM env;
592 {
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");
598 proc = SCM_CAR (x);
599 ASSYNT (scm_ilength (proc) >= 0, xorig, s_bindings, "do");
600 while SCM_NIMP
601 (proc)
602 {
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);
616 }
617 x = SCM_CDR (x);
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);
623 }
624
625 /* evalcar is small version of inline EVALCAR when we don't care about
626 * speed
627 */
628 #define evalcar scm_eval_car
629
630
631 static SCM iqq SCM_P ((SCM form, SCM env, int depth));
632
633 static SCM
634 iqq (form, env, depth)
635 SCM form;
636 SCM env;
637 int depth;
638 {
639 SCM tmp;
640 int edepth = depth;
641 if SCM_IMP
642 (form) return form;
643 if (SCM_VECTORP (form))
644 {
645 long i = SCM_LENGTH (form);
646 SCM *data = SCM_VELTS (form);
647 tmp = SCM_EOL;
648 for (; --i >= 0;)
649 tmp = scm_cons (data[i], tmp);
650 return scm_vector (iqq (tmp, env, depth));
651 }
652 if SCM_NCONSP
653 (form) return form;
654 tmp = SCM_CAR (form);
655 if (scm_i_quasiquote == tmp)
656 {
657 depth++;
658 goto label;
659 }
660 if (scm_i_unquote == tmp)
661 {
662 --depth;
663 label:
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);
668 if (0 == depth)
669 return evalcar (form, env);
670 return scm_cons2 (tmp, iqq (SCM_CAR (form), env, depth), SCM_EOL);
671 }
672 if (SCM_NIMP (tmp) && (scm_i_uq_splicing == SCM_CAR (tmp)))
673 {
674 tmp = SCM_CDR (tmp);
675 if (0 == --edepth)
676 return scm_append (scm_cons2 (evalcar (tmp, env), iqq (SCM_CDR (form), env, depth), SCM_EOL));
677 }
678 return scm_cons (iqq (SCM_CAR (form), env, edepth), iqq (SCM_CDR (form), env, depth));
679 }
680
681 /* Here are acros which return values rather than code. */
682
683
684 SCM
685 scm_m_quasiquote (xorig, env)
686 SCM xorig;
687 SCM env;
688 {
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);
692 }
693
694
695 SCM
696 scm_m_delay (xorig, env)
697 SCM xorig;
698 SCM env;
699 {
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)),
703 env));
704 }
705
706
707 static SCM env_top_level SCM_P ((SCM env));
708
709 static SCM
710 env_top_level (env)
711 SCM env;
712 {
713 while (SCM_NIMP(env))
714 {
715 if (SCM_BOOL_T == scm_procedure_p (SCM_CAR(env)))
716 return SCM_CAR(env);
717 env = SCM_CDR (env);
718 }
719 return SCM_BOOL_F;
720 }
721
722
723 SCM
724 scm_m_define (x, env)
725 SCM x;
726 SCM env;
727 {
728 SCM proc, arg1 = x;
729 x = SCM_CDR (x);
730 /* ASSYNT(SCM_NULLP(env), x, "bad placement", s_define);*/
731 ASSYNT (scm_ilength (x) >= 2, arg1, s_expression, "define");
732 proc = SCM_CAR (x);
733 x = SCM_CDR (x);
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);
738 }
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))
742 {
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);
747 #endif
748 arg1 = scm_sym2vcell (proc, env_top_level (env), SCM_BOOL_T);
749 #if 0
750 #ifndef RECKLESS
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));
754 else
755 #endif
756 if (5 <= scm_verbose && SCM_UNDEFINED != SCM_CDR (arg1))
757 scm_warn ("redefining ", SCM_CHARS (proc));
758 #endif
759 SCM_CDR (arg1) = x;
760 #ifdef SICP
761 return scm_cons2 (scm_i_quote, SCM_CAR (arg1), SCM_EOL);
762 #else
763 return SCM_UNSPECIFIED;
764 #endif
765 }
766 return scm_cons2 (SCM_IM_DEFINE, proc, x);
767 }
768
769 SCM
770 scm_m_undefine (x, env)
771 SCM x, env;
772 {
773 SCM arg1 = x;
774 x = SCM_CDR (x);
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);
778 x = SCM_CAR (x);
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);
783 #if 0
784 #ifndef RECKLESS
785 if (SCM_NIMP (SCM_CDR (arg1)) && ((SCM) SCM_SNAME (SCM_CDR (arg1)) == x))
786 scm_warn ("undefining built-in ", SCM_CHARS (x));
787 else
788 #endif
789 if (5 <= scm_verbose && SCM_UNDEFINED != SCM_CDR (arg1))
790 scm_warn ("redefining ", SCM_CHARS (x));
791 #endif
792 SCM_CDR (arg1) = SCM_UNDEFINED;
793 #ifdef SICP
794 return SCM_CAR (arg1);
795 #else
796 return SCM_UNSPECIFIED;
797 #endif
798 }
799
800 /* end of acros */
801
802
803 SCM
804 scm_m_letrec (xorig, env)
805 SCM xorig;
806 SCM env;
807 {
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;
812
813 ASRTSYNTAX (scm_ilength (x) >= 2, s_body);
814 proc = SCM_CAR (x);
815 if SCM_NULLP
816 (proc) return scm_m_letstar (xorig, env); /* null binding, let* faster */
817 ASRTSYNTAX (scm_ilength (proc) >= 1, s_bindings);
818 do
819 {
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);
827 }
828 while SCM_NIMP
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);
833 }
834
835
836 SCM
837 scm_m_let (xorig, env)
838 SCM xorig;
839 SCM env;
840 {
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;
844
845 ASSYNT (scm_ilength (x) >= 2, xorig, s_body, "let");
846 proc = SCM_CAR (x);
847 if (SCM_NULLP (proc)
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 */
857 x = SCM_CDR (x);
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");
861 while SCM_NIMP
862 (proc)
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);
872 }
873 return
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 */
877 env);
878 }
879
880
881
882 SCM
883 scm_m_apply (xorig, env)
884 SCM xorig;
885 SCM env;
886 {
887 ASSYNT (scm_ilength (SCM_CDR (xorig)) == 2, xorig, s_expression, "@apply");
888 return scm_cons (SCM_IM_APPLY, SCM_CDR (xorig));
889 }
890
891 #define s_atcall_cc (SCM_ISYMCHARS(SCM_IM_CONT)+1)
892
893
894 SCM
895 scm_m_cont (xorig, env)
896 SCM xorig;
897 SCM env;
898 {
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));
901 }
902
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.
909 */
910
911
912 static SCM unmemocopy SCM_P ((SCM x, SCM env));
913
914 static SCM
915 unmemocopy (x, env)
916 SCM x;
917 SCM env;
918 {
919 SCM ls, z;
920 #ifdef DEBUG_EXTENSIONS
921 SCM p;
922 #endif
923 if (SCM_NCELLP (x) || SCM_NECONSP (x))
924 return x;
925 #ifdef DEBUG_EXTENSIONS
926 p = scm_whash_lookup (scm_source_whash, x);
927 #endif
928 switch (SCM_TYP7 (x))
929 {
930 case (127 & SCM_IM_AND):
931 ls = z = scm_cons (scm_i_and, SCM_UNSPECIFIED);
932 break;
933 case (127 & SCM_IM_BEGIN):
934 ls = z = scm_cons (scm_i_begin, SCM_UNSPECIFIED);
935 break;
936 case (127 & SCM_IM_CASE):
937 ls = z = scm_cons (scm_i_case, SCM_UNSPECIFIED);
938 break;
939 case (127 & SCM_IM_COND):
940 ls = z = scm_cons (scm_i_cond, SCM_UNSPECIFIED);
941 break;
942 case (127 & SCM_IM_DO):
943 ls = scm_cons (scm_i_do, SCM_UNSPECIFIED);
944 goto transform;
945 case (127 & SCM_IM_IF):
946 ls = z = scm_cons (scm_i_if, SCM_UNSPECIFIED);
947 break;
948 case (127 & SCM_IM_LET):
949 ls = scm_cons (scm_i_let, SCM_UNSPECIFIED);
950 goto transform;
951 case (127 & SCM_IM_LETREC):
952 {
953 SCM f, v, e, s;
954 ls = scm_cons (scm_i_letrec, SCM_UNSPECIFIED);
955 transform:
956 x = SCM_CDR (x);
957 f = v = SCM_CAR (x);
958 x = SCM_CDR (x);
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));
962 env = z;
963 s = SCM_CAR (ls) == scm_i_do
964 ? scm_reverse (unmemocopy (SCM_CDR (SCM_CDR (SCM_CDR (x))), env))
965 : f;
966 z = SCM_EOL;
967 do
968 {
969 z = scm_acons (SCM_CAR (v),
970 scm_cons (SCM_CAR (e),
971 SCM_CAR (s) == SCM_CAR (v)
972 ? SCM_EOL
973 : scm_cons (SCM_CAR (s), SCM_EOL)),
974 z);
975 v = SCM_CDR (v);
976 e = SCM_CDR (e);
977 s = SCM_CDR (s);
978 }
979 while SCM_NIMP (v);
980 SCM_CDR (ls) = z = scm_cons (z, SCM_UNSPECIFIED);
981 if (SCM_CAR (ls) == scm_i_do)
982 {
983 x = SCM_CDR (x);
984 z = (SCM_CDR (z) = scm_cons (unmemocopy (SCM_CAR (x), env),
985 SCM_UNSPECIFIED));
986 x = (SCM) (&SCM_CAR (SCM_CDR (x)) - 1);
987 }
988 break;
989 }
990 case (127 & SCM_IM_LETSTAR):
991 {
992 SCM b, y;
993 x = SCM_CDR (x);
994 b = SCM_CAR (x);
995 y = SCM_EOL;
996 if SCM_IMP (b)
997 {
998 env = EXTEND_ENV (SCM_EOL, SCM_EOL, env);
999 goto letstar;
1000 }
1001 y = z = scm_acons (SCM_CAR (b),
1002 unmemocar (
1003 scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b)), env), SCM_EOL), env),
1004 SCM_UNSPECIFIED);
1005 env = EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
1006 b = SCM_CDR (SCM_CDR (b));
1007 if (SCM_IMP (b))
1008 {
1009 SCM_SETCDR (y, SCM_EOL);
1010 ls = scm_cons (scm_i_let, z = scm_cons (y, SCM_UNSPECIFIED));
1011 break;
1012 }
1013 do
1014 {
1015 z = (SCM_CDR (z) = scm_acons (SCM_CAR (b),
1016 unmemocar (
1017 scm_cons (unmemocopy (SCM_CAR (SCM_CDR (b)), env), SCM_EOL), env),
1018 SCM_UNSPECIFIED));
1019 env = EXTEND_ENV (SCM_CAR (b), SCM_BOOL_F, env);
1020 b = SCM_CDR (SCM_CDR (b));
1021 }
1022 while SCM_NIMP (b);
1023 SCM_CDR (z) = SCM_EOL;
1024 letstar:
1025 ls = scm_cons (scm_i_letstar, z = scm_cons (y, SCM_UNSPECIFIED));
1026 break;
1027 }
1028 case (127 & SCM_IM_OR):
1029 ls = z = scm_cons (scm_i_or, SCM_UNSPECIFIED);
1030 break;
1031 case (127 & SCM_IM_LAMBDA):
1032 x = SCM_CDR (x);
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);
1036 break;
1037 case (127 & SCM_IM_QUOTE):
1038 ls = z = scm_cons (scm_i_quote, SCM_UNSPECIFIED);
1039 break;
1040 case (127 & SCM_IM_SET):
1041 ls = z = scm_cons (scm_i_set, SCM_UNSPECIFIED);
1042 break;
1043 case (127 & SCM_IM_DEFINE):
1044 {
1045 SCM n;
1046 x = SCM_CDR (x);
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)));
1051 break;
1052 }
1053 case (127 & SCM_MAKISYM (0)):
1054 z = SCM_CAR (x);
1055 if (!SCM_ISYMP (z))
1056 goto unmemo;
1057 switch SCM_ISYMNUM (z)
1058 {
1059 case (SCM_ISYMNUM (SCM_IM_APPLY)):
1060 ls = z = scm_cons (scm_i_atapply, SCM_UNSPECIFIED);
1061 goto loop;
1062 case (SCM_ISYMNUM (SCM_IM_CONT)):
1063 ls = z = scm_cons (scm_i_atcall_cc, SCM_UNSPECIFIED);
1064 goto loop;
1065 default:
1066 }
1067 unmemo:
1068 default:
1069 ls = z = unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
1070 SCM_UNSPECIFIED),
1071 env);
1072 }
1073 loop:
1074 while (SCM_CELLP (x = SCM_CDR (x)) && SCM_ECONSP (x))
1075 z = (SCM_CDR (z) = unmemocar (scm_cons (unmemocopy (SCM_CAR (x), env),
1076 SCM_UNSPECIFIED),
1077 env));
1078 SCM_CDR (z) = x;
1079 #ifdef DEBUG_EXTENSIONS
1080 if (SCM_NFALSEP (p))
1081 scm_whash_insert (scm_source_whash, ls, p);
1082 #endif
1083 return ls;
1084 }
1085
1086
1087 SCM
1088 scm_unmemocopy (x, env)
1089 SCM x;
1090 SCM env;
1091 {
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)));
1096 else
1097 return unmemocopy (x, env);
1098 }
1099
1100 #ifndef RECKLESS
1101
1102 int
1103 scm_badargsp (formals, args)
1104 SCM formals;
1105 SCM args;
1106 {
1107 while SCM_NIMP
1108 (formals)
1109 {
1110 if SCM_NCONSP
1111 (formals) return 0;
1112 if SCM_IMP
1113 (args) return 1;
1114 formals = SCM_CDR (formals);
1115 args = SCM_CDR (args);
1116 }
1117 return SCM_NNULLP (args) ? 1 : 0;
1118 }
1119 #endif
1120
1121
1122 \f
1123 long scm_tc16_macro;
1124
1125
1126 SCM
1127 scm_eval_args (l, env)
1128 SCM l;
1129 SCM env;
1130 {
1131 SCM res = SCM_EOL, *lloc = &res;
1132 while (SCM_NIMP (l))
1133 {
1134 *lloc = scm_cons (EVALCAR (l, env), SCM_EOL);
1135 lloc = &SCM_CDR (*lloc);
1136 l = SCM_CDR (l);
1137 }
1138 return res;
1139 }
1140 #endif /* !DEVAL */
1141
1142
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
1145 * defined.
1146 */
1147
1148 #ifndef DEVAL
1149
1150 #define SCM_APPLY scm_apply
1151 #define PREP_APPLY(proc, args)
1152 #define ENTER_APPLY
1153 #define RETURN(x) return x;
1154 #ifdef STACK_CHECKING
1155 #ifndef NO_CEVAL_STACK_CHECKING
1156 #define EVAL_STACK_CHECKING
1157 #endif
1158 #endif
1159
1160 #else /* !DEVAL */
1161
1162 #undef SCM_CEVAL
1163 #define SCM_CEVAL scm_deval /* Substitute all uses of scm_ceval */
1164 #undef SCM_APPLY
1165 #define SCM_APPLY scm_dapply
1166 #undef PREP_APPLY
1167 #define PREP_APPLY(p, l) \
1168 { ++debug.info; debug.info->a.proc = p; debug.info->a.args = l; }
1169 #undef ENTER_APPLY
1170 #define ENTER_APPLY \
1171 {\
1172 SCM_SET_ARGSREADY (debug);\
1173 if (CHECK_APPLY)\
1174 if (SCM_APPLY_FRAME_P || (SCM_TRACE_P && PROCTRACEP (proc)))\
1175 {\
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)\
1179 {\
1180 tmp = scm_make_debugobj ((scm_debug_frame *) &debug);\
1181 scm_ithrow (scm_i_apply_frame, scm_cons2 (tmp, tail, SCM_EOL), 0);\
1182 }\
1183 else\
1184 {\
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);\
1188 }\
1189 }\
1190 }
1191 #undef RETURN
1192 #define RETURN(e) {proc = (e); goto exit;}
1193 #ifdef STACK_CHECKING
1194 #ifndef EVAL_STACK_CHECKING
1195 #define EVAL_STACK_CHECKING
1196 #endif
1197 #endif
1198
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
1202 * current repl.
1203 */
1204
1205
1206 SCM (*scm_ceval_ptr) SCM_P ((SCM x, SCM env));
1207
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
1212 * any stack swaps.
1213 */
1214
1215 #ifndef USE_THREADS
1216 scm_debug_frame *scm_last_debug_frame;
1217 #endif
1218
1219 /* scm_debug_eframe_size is the number of slots available for pseudo
1220 * stack frames at each real stack frame.
1221 */
1222
1223 int scm_debug_eframe_size;
1224
1225 int scm_debug_mode, scm_check_entry_p, scm_check_apply_p, scm_check_exit_p;
1226
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)." }
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_CDR (*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_CDR (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 (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_CAR (env) = scm_cons (proc, SCM_CAR (env));
1662 SCM_CDR (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_error (scm_misc_error_key,
1734 NULL,
1735 "Wrong type to apply: %S",
1736 scm_listify (proc, SCM_UNDEFINED),
1737 SCM_BOOL_F);
1738 case scm_tc7_vector:
1739 case scm_tc7_wvect:
1740 case scm_tc7_bvect:
1741 case scm_tc7_byvect:
1742 case scm_tc7_svect:
1743 case scm_tc7_ivect:
1744 case scm_tc7_uvect:
1745 case scm_tc7_fvect:
1746 case scm_tc7_dvect:
1747 case scm_tc7_cvect:
1748 #ifdef LONGLONGS
1749 case scm_tc7_llvect:
1750 #endif
1751 case scm_tc7_string:
1752 case scm_tc7_mb_string:
1753 case scm_tc7_substring:
1754 case scm_tc7_mb_substring:
1755 case scm_tc7_smob:
1756 case scm_tcs_closures:
1757 case scm_tcs_subrs:
1758 RETURN (x);
1759
1760 #ifdef MEMOIZE_LOCALS
1761 case (127 & SCM_ILOC00):
1762 proc = *scm_ilookup (SCM_CAR (x), env);
1763 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1764 #ifndef RECKLESS
1765 #ifdef CAUTIOUS
1766 goto checkargs;
1767 #endif
1768 #endif
1769 break;
1770 #endif /* ifdef MEMOIZE_LOCALS */
1771
1772
1773 case scm_tcs_cons_gloc:
1774 proc = SCM_GLOC_VAL (SCM_CAR (x));
1775 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1776 #ifndef RECKLESS
1777 #ifdef CAUTIOUS
1778 goto checkargs;
1779 #endif
1780 #endif
1781 break;
1782
1783
1784 case scm_tcs_cons_nimcar:
1785 if (SCM_SYMBOLP (SCM_CAR (x)))
1786 {
1787 proc = *scm_lookupcar (x, env);
1788 if (SCM_IMP (proc))
1789 {
1790 unmemocar (x, env);
1791 goto badfun;
1792 }
1793 if (scm_tc16_macro == SCM_TYP16 (proc))
1794 {
1795 unmemocar (x, env);
1796
1797 handle_a_macro:
1798 t.arg1 = SCM_APPLY (SCM_CDR (proc), x, scm_cons (env, scm_listofnull));
1799 switch ((int) (SCM_CAR (proc) >> 16))
1800 {
1801 case 2:
1802 if (scm_ilength (t.arg1) <= 0)
1803 t.arg1 = scm_cons2 (SCM_IM_BEGIN, t.arg1, SCM_EOL);
1804 #ifdef DEVAL
1805 if (!SCM_CLOSUREP (SCM_CDR (proc)))
1806 {
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 */
1810 {
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));
1814 }
1815 #endif
1816 SCM_DEFER_INTS;
1817 SCM_CAR (x) = SCM_CAR (t.arg1);
1818 SCM_CDR (x) = SCM_CDR (t.arg1);
1819 SCM_ALLOW_INTS;
1820 goto dispatch;
1821 }
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));
1826 #endif
1827 SCM_DEFER_INTS;
1828 SCM_CAR (x) = SCM_CAR (t.arg1);
1829 SCM_CDR (x) = SCM_CDR (t.arg1);
1830 SCM_ALLOW_INTS;
1831 goto loopnoap;
1832 case 1:
1833 if (SCM_NIMP (x = t.arg1))
1834 goto loopnoap;
1835 case 0:
1836 RETURN (t.arg1);
1837 }
1838 }
1839 }
1840 else
1841 proc = SCM_CEVAL (SCM_CAR (x), env);
1842 SCM_ASRTGO (SCM_NIMP (proc), badfun);
1843 #ifndef RECKLESS
1844 #ifdef CAUTIOUS
1845 checkargs:
1846 #endif
1847 if (SCM_CLOSUREP (proc))
1848 {
1849 arg2 = SCM_CAR (SCM_CODE (proc));
1850 t.arg1 = SCM_CDR (x);
1851 while (SCM_NIMP (arg2))
1852 {
1853 if (SCM_NCONSP (arg2))
1854 goto evapply;
1855 if (SCM_IMP (t.arg1))
1856 goto umwrongnumargs;
1857 arg2 = SCM_CDR (arg2);
1858 t.arg1 = SCM_CDR (t.arg1);
1859 }
1860 if (SCM_NNULLP (t.arg1))
1861 goto umwrongnumargs;
1862 }
1863 else if (scm_tc16_macro == SCM_TYP16 (proc))
1864 goto handle_a_macro;
1865 #endif
1866 }
1867
1868
1869 evapply:
1870 PREP_APPLY (proc, SCM_EOL);
1871 if (SCM_NULLP (SCM_CDR (x))) {
1872 ENTER_APPLY;
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));
1879 case scm_tc7_lsubr:
1880 RETURN (SCM_SUBRF (proc) (SCM_EOL));
1881 case scm_tc7_rpsubr:
1882 RETURN (SCM_BOOL_T);
1883 case scm_tc7_asubr:
1884 RETURN (SCM_SUBRF (proc) (SCM_UNDEFINED, SCM_UNDEFINED));
1885 #ifdef CCLO
1886 case scm_tc7_cclo:
1887 t.arg1 = proc;
1888 proc = SCM_CCLO_SUBR (proc);
1889 #ifdef DEVAL
1890 debug.info->a.proc = proc;
1891 debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
1892 #endif
1893 goto evap1;
1894 #endif
1895 case scm_tcs_closures:
1896 x = SCM_CODE (proc);
1897 env = EXTEND_ENV (SCM_CAR (x), SCM_EOL, SCM_ENV (proc));
1898 goto cdrxbegin;
1899 case scm_tc7_contin:
1900 case scm_tc7_subr_1:
1901 case scm_tc7_subr_2:
1902 case scm_tc7_subr_2o:
1903 case scm_tc7_cxr:
1904 case scm_tc7_subr_3:
1905 case scm_tc7_lsubr_2:
1906 umwrongnumargs:
1907 unmemocar (x, env);
1908 wrongnumargs:
1909 /* scm_everr (x, env,...) */
1910 scm_wrong_num_args (proc);
1911 default:
1912 /* handle macros here */
1913 goto badfun;
1914 }
1915 }
1916
1917 /* must handle macros by here */
1918 x = SCM_CDR (x);
1919 #ifdef CAUTIOUS
1920 if (SCM_IMP (x))
1921 goto wrongnumargs;
1922 #endif
1923 t.arg1 = EVALCAR (x, env);
1924 #ifdef DEVAL
1925 debug.info->a.args = scm_cons (t.arg1, SCM_EOL);
1926 #endif
1927 x = SCM_CDR (x);
1928 if (SCM_NULLP (x))
1929 {
1930 ENTER_APPLY;
1931 evap1:
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));
1939 case scm_tc7_cxr:
1940 #ifdef SCM_FLOATS
1941 if (SCM_SUBRF (proc))
1942 {
1943 if (SCM_INUMP (t.arg1))
1944 {
1945 RETURN (scm_makdbl (SCM_DSUBRF (proc) ((double) SCM_INUM (t.arg1)),
1946 0.0));
1947 }
1948 SCM_ASRTGO (SCM_NIMP (t.arg1), floerr);
1949 if (SCM_REALP (t.arg1))
1950 {
1951 RETURN (scm_makdbl (SCM_DSUBRF (proc) (SCM_REALPART (t.arg1)), 0.0));
1952 }
1953 #ifdef SCM_BIGDIG
1954 if (SCM_BIGP (t.arg1))
1955 {
1956 RETURN (scm_makdbl (SCM_DSUBRF (proc) (scm_big2dbl (t.arg1)), 0.0));
1957 }
1958 #endif
1959 floerr:
1960 scm_wta (t.arg1, (char *) SCM_ARG1, SCM_CHARS (SCM_SNAME (proc)));
1961 }
1962 #endif
1963 proc = (SCM) SCM_SNAME (proc);
1964 {
1965 char *chrs = SCM_CHARS (proc) + SCM_LENGTH (proc) - 1;
1966 while ('c' != *--chrs)
1967 {
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);
1971 }
1972 RETURN (t.arg1);
1973 }
1974 case scm_tc7_rpsubr:
1975 RETURN (SCM_BOOL_T);
1976 case scm_tc7_asubr:
1977 RETURN (SCM_SUBRF (proc) (t.arg1, SCM_UNDEFINED));
1978 case scm_tc7_lsubr:
1979 #ifdef DEVAL
1980 RETURN (SCM_SUBRF (proc) (debug.info->a.args))
1981 #else
1982 RETURN (SCM_SUBRF (proc) (scm_cons (t.arg1, SCM_EOL)));
1983 #endif
1984 #ifdef CCLO
1985 case scm_tc7_cclo:
1986 arg2 = t.arg1;
1987 t.arg1 = proc;
1988 proc = SCM_CCLO_SUBR (proc);
1989 #ifdef DEVAL
1990 debug.info->a.args = scm_cons (t.arg1, debug.info->a.args);
1991 debug.info->a.proc = proc;
1992 #endif
1993 goto evap2;
1994 #endif
1995 case scm_tcs_closures:
1996 x = SCM_CODE (proc);
1997 #ifdef DEVAL
1998 env = EXTEND_ENV (SCM_CAR (x), debug.info->a.args, SCM_ENV (proc));
1999 #else
2000 env = EXTEND_ENV (SCM_CAR (x), scm_cons (t.arg1, SCM_EOL), SCM_ENV (proc));
2001 #endif
2002 goto cdrxbegin;
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:
2009 goto wrongnumargs;
2010 default:
2011 goto badfun;
2012 }
2013 }
2014 #ifdef CAUTIOUS
2015 if (SCM_IMP (x))
2016 goto wrongnumargs;
2017 #endif
2018 { /* have two or more arguments */
2019 arg2 = EVALCAR (x, env);
2020 #ifdef DEVAL
2021 debug.info->a.args = scm_cons2 (t.arg1, arg2, SCM_EOL);
2022 #endif
2023 x = SCM_CDR (x);
2024 if (SCM_NULLP (x)) {
2025 ENTER_APPLY;
2026 #ifdef CCLO
2027 evap2:
2028 #endif
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));
2034 case scm_tc7_lsubr:
2035 #ifdef DEVAL
2036 RETURN (SCM_SUBRF (proc) (debug.info->a.args))
2037 #else
2038 RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1, arg2, SCM_EOL)));
2039 #endif
2040 case scm_tc7_lsubr_2:
2041 RETURN (SCM_SUBRF (proc) (t.arg1, arg2, SCM_EOL));
2042 case scm_tc7_rpsubr:
2043 case scm_tc7_asubr:
2044 RETURN (SCM_SUBRF (proc) (t.arg1, arg2));
2045 #ifdef CCLO
2046 cclon:
2047 case scm_tc7_cclo:
2048 #ifdef DEVAL
2049 RETURN (SCM_APPLY (SCM_CCLO_SUBR (proc), proc,
2050 scm_cons (debug.info->a.args, SCM_EOL)));
2051 #else
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))));
2055 #endif
2056 /* case scm_tc7_cclo:
2057 x = scm_cons(arg2, scm_eval_args(x, env));
2058 arg2 = t.arg1;
2059 t.arg1 = proc;
2060 proc = SCM_CCLO_SUBR(proc);
2061 goto evap3; */
2062 #endif
2063 case scm_tc7_subr_0:
2064 case scm_tc7_cxr:
2065 case scm_tc7_subr_1o:
2066 case scm_tc7_subr_1:
2067 case scm_tc7_subr_3:
2068 case scm_tc7_contin:
2069 goto wrongnumargs;
2070 default:
2071 goto badfun;
2072 case scm_tcs_closures:
2073 #ifdef DEVAL
2074 env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)), debug.info->a.args, SCM_ENV (proc));
2075 #else
2076 env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)), scm_cons2 (t.arg1, arg2, SCM_EOL), SCM_ENV (proc));
2077 #endif
2078 x = SCM_CODE (proc);
2079 goto cdrxbegin;
2080 }
2081 }
2082 #ifdef DEVAL
2083 debug.info->a.args = scm_cons2 (t.arg1, arg2,
2084 scm_deval_args (x, env, &SCM_CDR (SCM_CDR (debug.info->a.args))));
2085 #endif
2086 ENTER_APPLY;
2087 switch (SCM_TYP7 (proc))
2088 { /* have 3 or more arguments */
2089 #ifdef DEVAL
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)))));
2093 case scm_tc7_asubr:
2094 /* t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
2095 while SCM_NIMP(x) {
2096 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
2097 x = SCM_CDR(x);
2098 }
2099 RETURN (t.arg1) */
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))))
2104 case scm_tc7_lsubr:
2105 RETURN (SCM_SUBRF (proc) (debug.info->a.args))
2106 #ifdef CCLO
2107 case scm_tc7_cclo:
2108 goto cclon;
2109 #endif
2110 case scm_tcs_closures:
2111 SCM_SET_ARGSREADY (debug);
2112 env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)),
2113 debug.info->a.args,
2114 SCM_ENV (proc));
2115 x = SCM_CODE (proc);
2116 goto cdrxbegin;
2117 #else /* DEVAL */
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)));
2121 case scm_tc7_asubr:
2122 /* t.arg1 = SCM_SUBRF(proc)(t.arg1, arg2);
2123 while SCM_NIMP(x) {
2124 t.arg1 = SCM_SUBRF(proc)(t.arg1, EVALCAR(x, env));
2125 x = SCM_CDR(x);
2126 }
2127 RETURN (t.arg1) */
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)));
2132 case scm_tc7_lsubr:
2133 RETURN (SCM_SUBRF (proc) (scm_cons2 (t.arg1, arg2, scm_eval_args (x, env))));
2134 #ifdef CCLO
2135 case scm_tc7_cclo:
2136 goto cclon;
2137 #endif
2138 case scm_tcs_closures:
2139 #ifdef DEVAL
2140 SCM_SET_ARGSREADY (debug);
2141 #endif
2142 env = EXTEND_ENV (SCM_CAR (SCM_CODE (proc)),
2143 scm_cons2 (t.arg1, arg2, scm_eval_args (x, env)),
2144 SCM_ENV (proc));
2145 x = SCM_CODE (proc);
2146 goto cdrxbegin;
2147 #endif /* DEVAL */
2148 case scm_tc7_subr_2:
2149 case scm_tc7_subr_1o:
2150 case scm_tc7_subr_2o:
2151 case scm_tc7_subr_0:
2152 case scm_tc7_cxr:
2153 case scm_tc7_subr_1:
2154 case scm_tc7_contin:
2155 goto wrongnumargs;
2156 default:
2157 goto badfun;
2158 }
2159 }
2160 #ifdef DEVAL
2161 exit:
2162 if (CHECK_EXIT)
2163 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
2164 {
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);
2170 else
2171 {
2172 scm_make_cont (&t.arg1);
2173 if (setjmp (SCM_JMPBUF (t.arg1)))
2174 {
2175 proc = SCM_THROW_VALUE (t.arg1);
2176 goto ret;
2177 }
2178 }
2179 scm_ithrow (scm_i_exit_frame, scm_cons2 (t.arg1, proc, SCM_EOL), 0);
2180 }
2181 ret:
2182 scm_last_debug_frame = debug.prev;
2183 return proc;
2184 #endif
2185 }
2186
2187
2188 /* SECTION: This code is compiled once.
2189 */
2190
2191 #ifndef DEVAL
2192
2193 SCM_PROC(s_procedure_documentation, "procedure-documentation", 1, 0, 0, scm_procedure_documentation);
2194
2195 SCM
2196 scm_procedure_documentation (proc)
2197 SCM proc;
2198 {
2199 SCM code;
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))
2203 {
2204 case scm_tcs_closures:
2205 code = SCM_CDR (SCM_CODE (proc));
2206 if (SCM_IMP (SCM_CDR (code)))
2207 return SCM_BOOL_F;
2208 code = SCM_CAR (code);
2209 if (SCM_IMP (code))
2210 return SCM_BOOL_F;
2211 if (SCM_STRINGP (code))
2212 return code;
2213 default:
2214 return SCM_BOOL_F;
2215 /*
2216 case scm_tcs_subrs:
2217 #ifdef CCLO
2218 case scm_tc7_cclo:
2219 #endif
2220 */
2221 }
2222 }
2223
2224 /* This code processes the 'arg ...' parameters to apply.
2225
2226 (apply PROC ARG1 ... ARGS)
2227
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).
2232
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. */
2238
2239 SCM_PROC(s_nconc2last, "apply:nconc2last", 1, 0, 0, scm_nconc2last);
2240
2241 SCM
2242 scm_nconc2last (lst)
2243 SCM lst;
2244 {
2245 SCM *lloc;
2246 SCM_ASSERT (scm_ilength (lst) > 0, lst, SCM_ARG1, s_nconc2last);
2247 lloc = &lst;
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);
2252 return lst;
2253 }
2254
2255 #endif /* !DEVAL */
2256
2257
2258 /* SECTION: When DEVAL is defined this code yields scm_dapply.
2259 * It is compiled twice.
2260 */
2261
2262 #if 0
2263
2264 SCM
2265 scm_apply (proc, arg1, args)
2266 SCM proc;
2267 SCM arg1;
2268 SCM args;
2269 {}
2270 #endif
2271
2272 #if 0
2273
2274 SCM
2275 scm_dapply (proc, arg1, args)
2276 SCM proc;
2277 SCM arg1;
2278 SCM args;
2279 {}
2280 #endif
2281
2282
2283 SCM
2284 SCM_APPLY (proc, arg1, args)
2285 SCM proc;
2286 SCM arg1;
2287 SCM args;
2288 {
2289 #ifdef DEBUG_EXTENSIONS
2290 #ifdef DEVAL
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;
2297 #else
2298 if (SCM_DEBUGGINGP)
2299 return scm_dapply (proc, arg1, args);
2300 #endif
2301 #endif
2302
2303 SCM_ASRTGO (SCM_NIMP (proc), badproc);
2304 if (SCM_NULLP (args))
2305 {
2306 if (SCM_NULLP (arg1))
2307 arg1 = SCM_UNDEFINED;
2308 else
2309 {
2310 args = SCM_CDR (arg1);
2311 arg1 = SCM_CAR (arg1);
2312 }
2313 }
2314 else
2315 {
2316 /* SCM_ASRTGO(SCM_NIMP(args) && SCM_CONSP(args), wrongnumargs); */
2317 args = scm_nconc2last (args);
2318 }
2319 #ifdef DEVAL
2320 debug.vect[0].a.args = scm_cons (arg1, args);
2321 if (SCM_ENTER_FRAME_P)
2322 {
2323 SCM tmp;
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);
2328 else
2329 {
2330 scm_make_cont (&tmp);
2331 if (setjmp (SCM_JMPBUF (tmp)))
2332 goto entap;
2333 }
2334 scm_ithrow (scm_i_enter_frame, scm_cons (tmp, SCM_EOL), 0);
2335 }
2336 entap:
2337 ENTER_APPLY;
2338 #endif
2339 #ifdef CCLO
2340 tail:
2341 #endif
2342 switch (SCM_TYP7 (proc))
2343 {
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))
2358 case scm_tc7_cxr:
2359 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
2360 #ifdef SCM_FLOATS
2361 if (SCM_SUBRF (proc))
2362 {
2363 if (SCM_INUMP (arg1))
2364 {
2365 RETURN (scm_makdbl (SCM_DSUBRF (proc) ((double) SCM_INUM (arg1)), 0.0));
2366 }
2367 SCM_ASRTGO (SCM_NIMP (arg1), floerr);
2368 if (SCM_REALP (arg1))
2369 {
2370 RETURN (scm_makdbl (SCM_DSUBRF (proc) (SCM_REALPART (arg1)), 0.0));
2371 }
2372 #ifdef SCM_BIGDIG
2373 if SCM_BIGP
2374 (arg1)
2375 RETURN (scm_makdbl (SCM_DSUBRF (proc) (scm_big2dbl (arg1)), 0.0))
2376 #endif
2377 floerr:
2378 scm_wta (arg1, (char *) SCM_ARG1, SCM_CHARS (SCM_SNAME (proc)));
2379 }
2380 #endif
2381 proc = (SCM) SCM_SNAME (proc);
2382 {
2383 char *chrs = SCM_CHARS (proc) + SCM_LENGTH (proc) - 1;
2384 while ('c' != *--chrs)
2385 {
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);
2389 }
2390 RETURN (arg1)
2391 }
2392 case scm_tc7_subr_3:
2393 RETURN (SCM_SUBRF (proc) (arg1, SCM_CAR (args), SCM_CAR (SCM_CDR (args))))
2394 case scm_tc7_lsubr:
2395 #ifdef DEVAL
2396 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args))
2397 #else
2398 RETURN (SCM_SUBRF (proc) (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args)))
2399 #endif
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)))
2403 case scm_tc7_asubr:
2404 if (SCM_NULLP (args))
2405 RETURN (SCM_SUBRF (proc) (arg1, SCM_UNDEFINED))
2406 while (SCM_NIMP (args))
2407 {
2408 SCM_ASSERT (SCM_CONSP (args), args, SCM_ARG2, "apply");
2409 arg1 = SCM_SUBRF (proc) (arg1, SCM_CAR (args));
2410 args = SCM_CDR (args);
2411 }
2412 RETURN (arg1);
2413 case scm_tc7_rpsubr:
2414 if (SCM_NULLP (args))
2415 RETURN (SCM_BOOL_T);
2416 while (SCM_NIMP (args))
2417 {
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);
2423 }
2424 RETURN (SCM_BOOL_T);
2425 case scm_tcs_closures:
2426 #ifdef DEVAL
2427 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : debug.vect[0].a.args);
2428 #else
2429 arg1 = (SCM_UNBNDP (arg1) ? SCM_EOL : scm_cons (arg1, args));
2430 #endif
2431 #ifndef RECKLESS
2432 if (scm_badargsp (SCM_CAR (SCM_CODE (proc)), arg1))
2433 goto wrongnumargs;
2434 #endif
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);
2439 RETURN (arg1);
2440 case scm_tc7_contin:
2441 SCM_ASRTGO (SCM_NULLP (args), wrongnumargs);
2442 scm_call_continuation (proc, arg1);
2443 #ifdef CCLO
2444 case scm_tc7_cclo:
2445 #ifdef DEVAL
2446 args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args);
2447 arg1 = proc;
2448 proc = SCM_CCLO_SUBR (proc);
2449 debug.vect[0].a.proc = proc;
2450 debug.vect[0].a.args = scm_cons (arg1, args);
2451 #else
2452 args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args));
2453 arg1 = proc;
2454 proc = SCM_CCLO_SUBR (proc);
2455 #endif
2456 goto tail;
2457 #endif
2458 wrongnumargs:
2459 scm_wrong_num_args (proc);
2460 default:
2461 badproc:
2462 scm_wta (proc, (char *) SCM_ARG1, "apply");
2463 RETURN (arg1);
2464 }
2465 #ifdef DEVAL
2466 exit:
2467 if (CHECK_EXIT)
2468 if (SCM_EXIT_FRAME_P || (SCM_TRACE_P && SCM_TRACED_FRAME_P (debug)))
2469 {
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);
2475 else
2476 {
2477 scm_make_cont (&arg1);
2478 if (setjmp (SCM_JMPBUF (arg1)))
2479 {
2480 proc = SCM_THROW_VALUE (arg1);
2481 goto ret;
2482 }
2483 }
2484 scm_ithrow (scm_i_exit_frame, scm_cons2 (arg1, proc, SCM_EOL), 0);
2485 }
2486 ret:
2487 scm_last_debug_frame = debug.prev;
2488 return proc;
2489 #endif
2490 }
2491
2492
2493 /* SECTION: The rest of this file is only read once.
2494 */
2495
2496 #ifndef DEVAL
2497
2498 SCM_PROC(s_map, "map", 2, 0, 1, scm_map);
2499
2500 SCM
2501 scm_map (proc, arg1, args)
2502 SCM proc;
2503 SCM arg1;
2504 SCM args;
2505 {
2506 long i;
2507 SCM res = SCM_EOL;
2508 SCM *pres = &res;
2509 SCM *ve = &args; /* Keep args from being optimized away. */
2510
2511 if (SCM_NULLP (arg1))
2512 return res;
2513 SCM_ASSERT (SCM_NIMP (arg1), arg1, SCM_ARG2, s_map);
2514 if (SCM_NULLP (args))
2515 {
2516 while (SCM_NIMP (arg1))
2517 {
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);
2522 }
2523 return res;
2524 }
2525 args = scm_vector (scm_cons (arg1, args));
2526 ve = SCM_VELTS (args);
2527 #ifndef RECKLESS
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);
2530 #endif
2531 while (1)
2532 {
2533 arg1 = SCM_EOL;
2534 for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
2535 {
2536 if SCM_IMP
2537 (ve[i]) return res;
2538 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
2539 ve[i] = SCM_CDR (ve[i]);
2540 }
2541 *pres = scm_cons (scm_apply (proc, arg1, SCM_EOL), SCM_EOL);
2542 pres = &SCM_CDR (*pres);
2543 }
2544 }
2545
2546
2547 SCM_PROC(s_for_each, "for-each", 2, 0, 1, scm_for_each);
2548
2549 SCM
2550 scm_for_each (proc, arg1, args)
2551 SCM proc;
2552 SCM arg1;
2553 SCM args;
2554 {
2555 SCM *ve = &args; /* Keep args from being optimized away. */
2556 long i;
2557 if SCM_NULLP (arg1)
2558 return SCM_UNSPECIFIED;
2559 SCM_ASSERT (SCM_NIMP (arg1), arg1, SCM_ARG2, s_for_each);
2560 if SCM_NULLP (args)
2561 {
2562 while SCM_NIMP (arg1)
2563 {
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);
2567 }
2568 return SCM_UNSPECIFIED;
2569 }
2570 args = scm_vector (scm_cons (arg1, args));
2571 ve = SCM_VELTS (args);
2572 #ifndef RECKLESS
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);
2575 #endif
2576 while (1)
2577 {
2578 arg1 = SCM_EOL;
2579 for (i = SCM_LENGTH (args) - 1; i >= 0; i--)
2580 {
2581 if SCM_IMP
2582 (ve[i]) return SCM_UNSPECIFIED;
2583 arg1 = scm_cons (SCM_CAR (ve[i]), arg1);
2584 ve[i] = SCM_CDR (ve[i]);
2585 }
2586 scm_apply (proc, arg1, SCM_EOL);
2587 }
2588 }
2589
2590
2591
2592 SCM
2593 scm_closure (code, env)
2594 SCM code;
2595 SCM env;
2596 {
2597 register SCM z;
2598 SCM_NEWCELL (z);
2599 SCM_SETCODE (z, code);
2600 SCM_ENV (z) = env;
2601 return z;
2602 }
2603
2604
2605 long scm_tc16_promise;
2606
2607 SCM
2608 scm_makprom (code)
2609 SCM code;
2610 {
2611 register SCM z;
2612 SCM_NEWCELL (z);
2613 SCM_CDR (z) = code;
2614 SCM_CAR (z) = scm_tc16_promise;
2615 return z;
2616 }
2617
2618
2619
2620 static int prinprom SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
2621
2622 static int
2623 prinprom (exp, port, pstate)
2624 SCM exp;
2625 SCM port;
2626 scm_print_state *pstate;
2627 {
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);
2634 return !0;
2635 }
2636
2637
2638 SCM_PROC(s_makacro, "procedure->syntax", 1, 0, 0, scm_makacro);
2639
2640 SCM
2641 scm_makacro (code)
2642 SCM code;
2643 {
2644 register SCM z;
2645 SCM_NEWCELL (z);
2646 SCM_CDR (z) = code;
2647 SCM_CAR (z) = scm_tc16_macro;
2648 return z;
2649 }
2650
2651
2652 SCM_PROC(s_makmacro, "procedure->macro", 1, 0, 0, scm_makmacro);
2653
2654 SCM
2655 scm_makmacro (code)
2656 SCM code;
2657 {
2658 register SCM z;
2659 SCM_NEWCELL (z);
2660 SCM_CDR (z) = code;
2661 SCM_CAR (z) = scm_tc16_macro | (1L << 16);
2662 return z;
2663 }
2664
2665
2666 SCM_PROC(s_makmmacro, "procedure->memoizing-macro", 1, 0, 0, scm_makmmacro);
2667
2668 SCM
2669 scm_makmmacro (code)
2670 SCM code;
2671 {
2672 register SCM z;
2673 SCM_NEWCELL (z);
2674 SCM_CDR (z) = code;
2675 SCM_CAR (z) = scm_tc16_macro | (2L << 16);
2676 return z;
2677 }
2678
2679
2680
2681 static int prinmacro SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
2682
2683 static int
2684 prinmacro (exp, port, pstate)
2685 SCM exp;
2686 SCM port;
2687 scm_print_state *pstate;
2688 {
2689 int writingp = SCM_WRITINGP (pstate);
2690 if (SCM_CAR (exp) & (3L << 16))
2691 scm_gen_puts (scm_regular_string, "#<macro", port);
2692 else
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);
2701 return !0;
2702 }
2703
2704 SCM_PROC(s_force, "force", 1, 0, 0, scm_force);
2705
2706 SCM
2707 scm_force (x)
2708 SCM x;
2709 {
2710 SCM_ASSERT ((SCM_TYP16 (x) == scm_tc16_promise), x, SCM_ARG1, s_force);
2711 if (!((1L << 16) & SCM_CAR (x)))
2712 {
2713 SCM ans = scm_apply (SCM_CDR (x), SCM_EOL, SCM_EOL);
2714 if (!((1L << 16) & SCM_CAR (x)))
2715 {
2716 SCM_DEFER_INTS;
2717 SCM_CDR (x) = ans;
2718 SCM_CAR (x) |= (1L << 16);
2719 SCM_ALLOW_INTS;
2720 }
2721 }
2722 return SCM_CDR (x);
2723 }
2724
2725 SCM_PROC (s_promise_p, "promise?", 1, 0, 0, scm_promise_p);
2726
2727 SCM
2728 scm_promise_p (x)
2729 SCM x;
2730 {
2731 return ((SCM_NIMP (x) && (SCM_TYP16 (x) == scm_tc16_promise))
2732 ? SCM_BOOL_T
2733 : SCM_BOOL_F);
2734 }
2735
2736 SCM_PROC(s_copy_tree, "copy-tree", 1, 0, 0, scm_copy_tree);
2737
2738 SCM
2739 scm_copy_tree (obj)
2740 SCM obj;
2741 {
2742 SCM ans, tl;
2743 if SCM_IMP
2744 (obj) return obj;
2745 if (SCM_VECTORP (obj))
2746 {
2747 scm_sizet i = SCM_LENGTH (obj);
2748 ans = scm_make_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED, SCM_UNDEFINED);
2749 while (i--)
2750 SCM_VELTS (ans)[i] = scm_copy_tree (SCM_VELTS (obj)[i]);
2751 return ans;
2752 }
2753 if SCM_NCONSP (obj)
2754 return obj;
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));
2759 SCM_CDR (tl) = obj;
2760 return ans;
2761 }
2762
2763
2764 SCM
2765 scm_eval_3 (obj, copyp, env)
2766 SCM obj;
2767 int copyp;
2768 SCM env;
2769 {
2770 if (SCM_NIMP (SCM_CDR (scm_system_transformer)))
2771 obj = scm_apply (SCM_CDR (scm_system_transformer), obj, scm_listofnull);
2772 else if (copyp)
2773 obj = scm_copy_tree (obj);
2774 return XEVAL (obj, env);
2775 }
2776
2777
2778 SCM
2779 scm_top_level_env (thunk)
2780 SCM thunk;
2781 {
2782 if (SCM_IMP(thunk))
2783 return SCM_EOL;
2784 else
2785 return scm_cons(thunk, (SCM)SCM_EOL);
2786 }
2787
2788 SCM_PROC(s_eval2, "eval2", 2, 0, 0, scm_eval2);
2789
2790 SCM
2791 scm_eval2 (obj, env_thunk)
2792 SCM obj;
2793 SCM env_thunk;
2794 {
2795 return scm_eval_3 (obj, 1, scm_top_level_env(env_thunk));
2796 }
2797
2798 SCM_PROC(s_eval, "eval", 1, 0, 0, scm_eval);
2799
2800 SCM
2801 scm_eval (obj)
2802 SCM obj;
2803 {
2804 return
2805 scm_eval_3(obj, 1, scm_top_level_env(SCM_CDR(scm_top_level_lookup_thunk_var)));
2806 }
2807
2808 SCM_PROC(s_eval_x, "eval!", 1, 0, 0, scm_eval_x);
2809
2810 SCM
2811 scm_eval_x (obj)
2812 SCM obj;
2813 {
2814 return
2815 scm_eval_3(obj,
2816 0,
2817 scm_top_level_env (SCM_CDR (scm_top_level_lookup_thunk_var)));
2818 }
2819
2820 SCM_PROC (s_macro_eval_x, "macro-eval!", 2, 0, 0, scm_macro_eval_x);
2821
2822 SCM
2823 scm_macro_eval_x (exp, env)
2824 SCM exp;
2825 SCM env;
2826 {
2827 return scm_eval_3 (exp, 0, env);
2828 }
2829
2830
2831 SCM
2832 scm_definedp (x, env)
2833 SCM x;
2834 SCM env;
2835 {
2836 SCM proc = SCM_CAR (x = SCM_CDR (x));
2837 if (SCM_ISYMP (proc))
2838 return SCM_BOOL_T;
2839 else if(SCM_IMP(proc) || !SCM_SYMBOLP(proc))
2840 return SCM_BOOL_F;
2841 else
2842 {
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;
2845 }
2846 }
2847
2848 static scm_smobfuns promsmob =
2849 {scm_markcdr, scm_free0, prinprom};
2850
2851 static scm_smobfuns macrosmob =
2852 {scm_markcdr, scm_free0, prinmacro};
2853
2854
2855 SCM
2856 scm_make_synt (name, macroizer, fcn)
2857 char *name;
2858 SCM (*macroizer) ();
2859 SCM (*fcn) ();
2860 {
2861 SCM symcell = scm_sysintern (name, SCM_UNDEFINED);
2862 long tmp = ((((SCM_CELLPTR) (SCM_CAR (symcell))) - scm_heap_org) << 8);
2863 register SCM z;
2864 if ((tmp >> 8) != ((SCM_CELLPTR) (SCM_CAR (symcell)) - scm_heap_org))
2865 tmp = 0;
2866 SCM_NEWCELL (z);
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);
2871 }
2872
2873
2874 /* At this point, scm_deval and scm_dapply are generated.
2875 */
2876
2877 #ifdef DEBUG_EXTENSIONS
2878 # define DEVAL
2879 # include "eval.c"
2880 #endif
2881
2882
2883
2884 void
2885 scm_init_eval ()
2886 {
2887 scm_tc16_promise = scm_newsmob (&promsmob);
2888 scm_tc16_macro = scm_newsmob (&macrosmob);
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));
2896
2897 /* acros */
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);
2901 /* end of acros */
2902
2903 scm_top_level_lookup_thunk_var =
2904 scm_sysintern("*top-level-lookup-thunk*", SCM_BOOL_F);
2905
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);
2923
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);
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 */