Bump version number for 1.9.9.
[bpt/guile.git] / libguile / eval.c
CommitLineData
997659f8 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010
434f2f7a 2 * Free Software Foundation, Inc.
0f2d19dd 3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
0f2d19dd 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
0f2d19dd 13 *
73be1d9e
MV
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
1bbd0b84 19
0f2d19dd
JB
20\f
21
dbb605f5 22#ifdef HAVE_CONFIG_H
3d05f2e0
RB
23# include <config.h>
24#endif
0f2d19dd 25
f7439099 26#include <alloca.h>
b7ecadca 27#include <assert.h>
3d05f2e0 28
f7439099 29#include "libguile/__scm.h"
48b96f4b 30
a0599745 31#include "libguile/_scm.h"
21628685
DH
32#include "libguile/alist.h"
33#include "libguile/async.h"
34#include "libguile/continuations.h"
747022e4 35#include "libguile/control.h"
a0599745 36#include "libguile/debug.h"
328dc9a3 37#include "libguile/deprecation.h"
09074dbf 38#include "libguile/dynwind.h"
a0599745 39#include "libguile/eq.h"
21628685
DH
40#include "libguile/feature.h"
41#include "libguile/fluids.h"
21628685
DH
42#include "libguile/goops.h"
43#include "libguile/hash.h"
44#include "libguile/hashtab.h"
45#include "libguile/lang.h"
4610b011 46#include "libguile/list.h"
a0599745 47#include "libguile/macros.h"
b7742c6b 48#include "libguile/memoize.h"
a0599745
MD
49#include "libguile/modules.h"
50#include "libguile/ports.h"
7e6e6b37 51#include "libguile/print.h"
21628685 52#include "libguile/procprop.h"
4abef68f 53#include "libguile/programs.h"
a0599745 54#include "libguile/root.h"
21628685
DH
55#include "libguile/smob.h"
56#include "libguile/srcprop.h"
57#include "libguile/stackchk.h"
58#include "libguile/strings.h"
9de87eea 59#include "libguile/threads.h"
21628685
DH
60#include "libguile/throw.h"
61#include "libguile/validate.h"
a513ead3 62#include "libguile/values.h"
21628685 63#include "libguile/vectors.h"
4abef68f 64#include "libguile/vm.h"
a0599745 65
a0599745 66#include "libguile/eval.h"
0ee05b85 67#include "libguile/private-options.h"
89efbff4 68
0f2d19dd
JB
69\f
70
0ee05b85 71
b7742c6b 72/* We have three levels of EVAL here:
609a8b86 73
b7742c6b 74 - eval (exp, env)
89bff2fc 75
b7742c6b
AW
76 evaluates EXP in environment ENV. ENV is a lexical environment
77 structure as used by the actual tree code evaluator. When ENV is
78 a top-level environment, then changes to the current module are
79 tracked by updating ENV so that it continues to be in sync with
80 the current module.
e6729603 81
b7742c6b 82 - scm_primitive_eval (exp)
e6729603 83
b7742c6b
AW
84 evaluates EXP in the top-level environment as determined by the
85 current module. This is done by constructing a suitable
86 environment and calling eval. Thus, changes to the
87 top-level module are tracked normally.
e6729603 88
b7742c6b 89 - scm_eval (exp, mod)
e6729603 90
b7742c6b
AW
91 evaluates EXP while MOD is the current module. This is done
92 by setting the current module to MOD_OR_STATE, invoking
93 scm_primitive_eval on EXP, and then restoring the current module
94 to the value it had previously. That is, while EXP is evaluated,
95 changes to the current module (or dynamic state) are tracked,
96 but these changes do not persist when scm_eval returns.
e6729603 97
b7742c6b 98*/
e6729603 99
e6729603 100
314b8716
AW
101/* Boot closures. We only see these when compiling eval.scm, because once
102 eval.scm is in the house, closures are standard VM closures.
103 */
104
105static scm_t_bits scm_tc16_boot_closure;
106#define RETURN_BOOT_CLOSURE(code, env) SCM_RETURN_NEWSMOB2 (scm_tc16_boot_closure, (code), (env))
107#define BOOT_CLOSURE_P(obj) SCM_TYP16_PREDICATE (scm_tc16_boot_closure, (obj))
108#define BOOT_CLOSURE_CODE(x) SCM_SMOB_OBJECT (x)
109#define BOOT_CLOSURE_ENV(x) SCM_SMOB_OBJECT_2 (x)
110#define BOOT_CLOSURE_NUM_REQUIRED_ARGS(x) SCM_I_INUM (CAR (BOOT_CLOSURE_CODE (x)))
111#define BOOT_CLOSURE_HAS_REST_ARGS(x) scm_is_true (CADR (BOOT_CLOSURE_CODE (x)))
112#define BOOT_CLOSURE_BODY(x) CDDR (BOOT_CLOSURE_CODE (x))
113
114
115
b7742c6b
AW
116#define CAR(x) SCM_CAR(x)
117#define CDR(x) SCM_CDR(x)
118#define CAAR(x) SCM_CAAR(x)
119#define CADR(x) SCM_CADR(x)
120#define CDAR(x) SCM_CDAR(x)
121#define CDDR(x) SCM_CDDR(x)
122#define CADDR(x) SCM_CADDR(x)
123#define CDDDR(x) SCM_CDDDR(x)
e6729603
DH
124
125
b7742c6b 126SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
e6729603 127
b7742c6b 128static void error_used_before_defined (void)
d0624e39 129{
b7742c6b
AW
130 scm_error (scm_unbound_variable_key, NULL,
131 "Variable used before given a value", SCM_EOL, SCM_BOOL_F);
d0624e39 132}
d0624e39 133
b7742c6b
AW
134int
135scm_badargsp (SCM formals, SCM args)
7e6e6b37 136{
b7742c6b
AW
137 while (!scm_is_null (formals))
138 {
139 if (!scm_is_pair (formals))
140 return 0;
141 if (scm_is_null (args))
142 return 1;
143 formals = CDR (formals);
144 args = CDR (args);
145 }
146 return !scm_is_null (args) ? 1 : 0;
7e6e6b37
DH
147}
148
b7742c6b 149/* the environment:
3149a5b6 150 (VAL ... . MOD)
b7742c6b
AW
151 If MOD is #f, it means the environment was captured before modules were
152 booted.
153 If MOD is the literal value '(), we are evaluating at the top level, and so
154 should track changes to the current module. You have to be careful in this
155 case, because further lexical contours should capture the current module.
156*/
157#define CAPTURE_ENV(env) \
158 ((env == SCM_EOL) ? scm_current_module () : \
159 ((env == SCM_BOOL_F) ? scm_the_root_module () : env))
6f81708a
DH
160
161static SCM
b7742c6b 162eval (SCM x, SCM env)
6f81708a 163{
b7742c6b
AW
164 SCM mx;
165 SCM proc = SCM_UNDEFINED, args = SCM_EOL;
b7ecadca 166 unsigned int argc;
6f81708a 167
b7742c6b
AW
168 loop:
169 SCM_TICK;
170 if (!SCM_MEMOIZED_P (x))
171 abort ();
172
173 mx = SCM_MEMOIZED_ARGS (x);
174 switch (SCM_MEMOIZED_TAG (x))
175 {
176 case SCM_M_BEGIN:
177 for (; !scm_is_null (CDR (mx)); mx = CDR (mx))
178 eval (CAR (mx), env);
179 x = CAR (mx);
180 goto loop;
181
182 case SCM_M_IF:
183 if (scm_is_true (eval (CAR (mx), env)))
184 x = CADR (mx);
6f81708a 185 else
b7742c6b
AW
186 x = CDDR (mx);
187 goto loop;
5fb64383 188
b7742c6b
AW
189 case SCM_M_LET:
190 {
191 SCM inits = CAR (mx);
192 SCM new_env = CAPTURE_ENV (env);
193 for (; scm_is_pair (inits); inits = CDR (inits))
194 new_env = scm_cons (eval (CAR (inits), env), new_env);
195 env = new_env;
196 x = CDR (mx);
197 goto loop;
198 }
199
200 case SCM_M_LAMBDA:
314b8716 201 RETURN_BOOT_CLOSURE (mx, CAPTURE_ENV (env));
5fb64383 202
b7742c6b
AW
203 case SCM_M_QUOTE:
204 return mx;
0f2d19dd 205
b7742c6b
AW
206 case SCM_M_DEFINE:
207 scm_define (CAR (mx), eval (CDR (mx), env));
208 return SCM_UNSPECIFIED;
212e58ed 209
d69531e2
AW
210 case SCM_M_DYNWIND:
211 {
212 SCM in, out, res, old_winds;
213 in = eval (CAR (mx), env);
214 out = eval (CDDR (mx), env);
215 scm_call_0 (in);
216 old_winds = scm_i_dynwinds ();
217 scm_i_set_dynwinds (scm_acons (in, out, old_winds));
218 res = eval (CADR (mx), env);
219 scm_i_set_dynwinds (old_winds);
220 scm_call_0 (out);
221 return res;
222 }
223
bb0229b5
AW
224 case SCM_M_WITH_FLUIDS:
225 {
226 long i, len;
227 SCM *fluidv, *valuesv, walk, wf, res;
228 len = scm_ilength (CAR (mx));
229 fluidv = alloca (sizeof (SCM)*len);
230 for (i = 0, walk = CAR (mx); i < len; i++, walk = CDR (walk))
231 fluidv[i] = eval (CAR (walk), env);
232 valuesv = alloca (sizeof (SCM)*len);
233 for (i = 0, walk = CADR (mx); i < len; i++, walk = CDR (walk))
234 valuesv[i] = eval (CAR (walk), env);
235
236 wf = scm_i_make_with_fluids (len, fluidv, valuesv);
237 scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state);
238 scm_i_set_dynwinds (scm_cons (wf, scm_i_dynwinds ()));
239 res = eval (CDDR (mx), env);
240 scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state);
241 scm_i_set_dynwinds (CDR (scm_i_dynwinds ()));
242
243 return res;
244 }
245
b7742c6b
AW
246 case SCM_M_APPLY:
247 /* Evaluate the procedure to be applied. */
248 proc = eval (CAR (mx), env);
249 /* Evaluate the argument holding the list of arguments */
250 args = eval (CADR (mx), env);
251
252 apply_proc:
253 /* Go here to tail-apply a procedure. PROC is the procedure and
254 * ARGS is the list of arguments. */
314b8716 255 if (BOOT_CLOSURE_P (proc))
b7742c6b 256 {
314b8716
AW
257 int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (proc);
258 SCM new_env = BOOT_CLOSURE_ENV (proc);
259 if (BOOT_CLOSURE_HAS_REST_ARGS (proc))
b7742c6b
AW
260 {
261 if (SCM_UNLIKELY (scm_ilength (args) < nreq))
262 scm_wrong_num_args (proc);
263 for (; nreq; nreq--, args = CDR (args))
264 new_env = scm_cons (CAR (args), new_env);
265 new_env = scm_cons (args, new_env);
266 }
267 else
268 {
269 if (SCM_UNLIKELY (scm_ilength (args) != nreq))
270 scm_wrong_num_args (proc);
271 for (; scm_is_pair (args); args = CDR (args))
272 new_env = scm_cons (CAR (args), new_env);
273 }
314b8716 274 x = BOOT_CLOSURE_BODY (proc);
b7742c6b
AW
275 env = new_env;
276 goto loop;
277 }
278 else
67e2d80a 279 return scm_vm_apply (scm_the_vm (), proc, args);
212e58ed 280
b7742c6b
AW
281 case SCM_M_CALL:
282 /* Evaluate the procedure to be applied. */
283 proc = eval (CAR (mx), env);
b7ecadca 284 argc = SCM_I_INUM (CADR (mx));
9331f91c 285 mx = CDDR (mx);
212e58ed 286
314b8716 287 if (BOOT_CLOSURE_P (proc))
5fa0939c 288 {
314b8716
AW
289 int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (proc);
290 SCM new_env = BOOT_CLOSURE_ENV (proc);
291 if (BOOT_CLOSURE_HAS_REST_ARGS (proc))
b7742c6b 292 {
b7ecadca 293 if (SCM_UNLIKELY (argc < nreq))
b7742c6b
AW
294 scm_wrong_num_args (proc);
295 for (; nreq; nreq--, mx = CDR (mx))
296 new_env = scm_cons (eval (CAR (mx), env), new_env);
297 {
298 SCM rest = SCM_EOL;
299 for (; scm_is_pair (mx); mx = CDR (mx))
300 rest = scm_cons (eval (CAR (mx), env), rest);
301 new_env = scm_cons (scm_reverse (rest),
302 new_env);
303 }
304 }
305 else
306 {
307 for (; scm_is_pair (mx); mx = CDR (mx), nreq--)
308 new_env = scm_cons (eval (CAR (mx), env), new_env);
309 if (SCM_UNLIKELY (nreq != 0))
310 scm_wrong_num_args (proc);
311 }
314b8716 312 x = BOOT_CLOSURE_BODY (proc);
b7742c6b
AW
313 env = new_env;
314 goto loop;
5fa0939c 315 }
b7742c6b
AW
316 else
317 {
e2cf8eb9 318 SCM *argv;
b7ecadca
LC
319 unsigned int i;
320
e2cf8eb9 321 argv = alloca (argc * sizeof (SCM));
b7ecadca
LC
322 for (i = 0; i < argc; i++, mx = CDR (mx))
323 argv[i] = eval (CAR (mx), env);
324
325 return scm_c_vm_run (scm_the_vm (), proc, argv, argc);
b7742c6b 326 }
b7ecadca 327
b7742c6b 328 case SCM_M_CONT:
babfc7b2 329 return scm_i_call_with_current_continuation (eval (mx, env));
212e58ed 330
b7742c6b
AW
331 case SCM_M_CALL_WITH_VALUES:
332 {
333 SCM producer;
334 SCM v;
335
336 producer = eval (CAR (mx), env);
337 proc = eval (CDR (mx), env); /* proc is the consumer. */
67e2d80a 338 v = scm_vm_apply (scm_the_vm (), producer, SCM_EOL);
b7742c6b
AW
339 if (SCM_VALUESP (v))
340 args = scm_struct_ref (v, SCM_INUM0);
341 else
342 args = scm_list_1 (v);
343 goto apply_proc;
344 }
26d5b9b4 345
b7742c6b
AW
346 case SCM_M_LEXICAL_REF:
347 {
348 int n;
349 SCM ret;
350 for (n = SCM_I_INUM (mx); n; n--)
351 env = CDR (env);
352 ret = CAR (env);
353 if (SCM_UNLIKELY (SCM_UNBNDP (ret)))
354 /* we don't know what variable, though, because we don't have its
355 name */
356 error_used_before_defined ();
357 return ret;
358 }
1cc91f1b 359
b7742c6b
AW
360 case SCM_M_LEXICAL_SET:
361 {
362 int n;
363 SCM val = eval (CDR (mx), env);
364 for (n = SCM_I_INUM (CAR (mx)); n; n--)
365 env = CDR (env);
366 SCM_SETCAR (env, val);
367 return SCM_UNSPECIFIED;
368 }
910b5125 369
b7742c6b
AW
370 case SCM_M_TOPLEVEL_REF:
371 if (SCM_VARIABLEP (mx))
372 return SCM_VARIABLE_REF (mx);
373 else
57d23e25 374 {
b7742c6b 375 while (scm_is_pair (env))
f3a8d1b7 376 env = CDR (env);
3149a5b6
AW
377 return SCM_VARIABLE_REF
378 (scm_memoize_variable_access_x (x, CAPTURE_ENV (env)));
57d23e25 379 }
57d23e25 380
b7742c6b
AW
381 case SCM_M_TOPLEVEL_SET:
382 {
383 SCM var = CAR (mx);
384 SCM val = eval (CDR (mx), env);
385 if (SCM_VARIABLEP (var))
386 {
387 SCM_VARIABLE_SET (var, val);
388 return SCM_UNSPECIFIED;
389 }
390 else
391 {
392 while (scm_is_pair (env))
f3a8d1b7 393 env = CDR (env);
3149a5b6
AW
394 SCM_VARIABLE_SET
395 (scm_memoize_variable_access_x (x, CAPTURE_ENV (env)),
396 val);
b7742c6b
AW
397 return SCM_UNSPECIFIED;
398 }
399 }
910b5125 400
b7742c6b
AW
401 case SCM_M_MODULE_REF:
402 if (SCM_VARIABLEP (mx))
403 return SCM_VARIABLE_REF (mx);
910b5125 404 else
3149a5b6
AW
405 return SCM_VARIABLE_REF
406 (scm_memoize_variable_access_x (x, SCM_BOOL_F));
910b5125 407
b7742c6b
AW
408 case SCM_M_MODULE_SET:
409 if (SCM_VARIABLEP (CDR (mx)))
910b5125 410 {
b7742c6b
AW
411 SCM_VARIABLE_SET (CDR (mx), eval (CAR (mx), env));
412 return SCM_UNSPECIFIED;
413 }
414 else
415 {
3149a5b6
AW
416 SCM_VARIABLE_SET
417 (scm_memoize_variable_access_x (x, SCM_BOOL_F),
418 eval (CAR (mx), env));
b7742c6b 419 return SCM_UNSPECIFIED;
910b5125 420 }
910b5125 421
747022e4
AW
422 case SCM_M_PROMPT:
423 {
d2964315 424 SCM vm, prompt, handler, res;
747022e4 425
d2964315
AW
426 vm = scm_the_vm ();
427 prompt = scm_c_make_prompt (eval (CAR (mx), env), SCM_VM_DATA (vm)->fp,
428 SCM_VM_DATA (vm)->sp, SCM_VM_DATA (vm)->ip,
adbdfd6d 429 0, -1, scm_i_dynwinds ());
747022e4
AW
430 handler = eval (CDDR (mx), env);
431 scm_i_set_dynwinds (scm_cons (prompt, scm_i_dynwinds ()));
432
433 if (SCM_PROMPT_SETJMP (prompt))
434 {
b8af64db 435 /* The prompt exited nonlocally. */
747022e4 436 proc = handler;
b8af64db 437 args = scm_i_prompt_pop_abort_args_x (prompt);
747022e4
AW
438 goto apply_proc;
439 }
440
441 res = eval (CADR (mx), env);
442 scm_i_set_dynwinds (CDR (scm_i_dynwinds ()));
443 return res;
444 }
445
b7742c6b
AW
446 default:
447 abort ();
448 }
910b5125
DH
449}
450
b7742c6b
AW
451scm_t_option scm_eval_opts[] = {
452 { SCM_OPTION_INTEGER, "stack", 22000, "Size of thread stacks (in machine words)." },
453 { 0 }
454};
e6729603 455
b7742c6b
AW
456scm_t_option scm_debug_opts[] = {
457 { SCM_OPTION_BOOLEAN, "cheap", 1,
458 "*This option is now obsolete. Setting it has no effect." },
459 { SCM_OPTION_BOOLEAN, "breakpoints", 0, "*Check for breakpoints." },
460 { SCM_OPTION_BOOLEAN, "trace", 0, "*Trace mode." },
461 { SCM_OPTION_BOOLEAN, "procnames", 1,
462 "Record procedure names at definition." },
463 { SCM_OPTION_BOOLEAN, "backwards", 0,
464 "Display backtrace in anti-chronological order." },
465 { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
466 { SCM_OPTION_INTEGER, "indent", 10, "Maximal indentation in backtrace." },
467 { SCM_OPTION_INTEGER, "frames", 3,
468 "Maximum number of tail-recursive frames in backtrace." },
469 { SCM_OPTION_INTEGER, "maxdepth", 1000,
470 "Maximal number of stored backtrace frames." },
471 { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
472 { SCM_OPTION_BOOLEAN, "backtrace", 0, "Show backtrace on error." },
473 { SCM_OPTION_BOOLEAN, "debug", 0, "Use the debugging evaluator." },
474 /* This default stack limit will be overridden by debug.c:init_stack_limit(),
475 if we have getrlimit() and the stack limit is not INFINITY. But it is still
476 important, as some systems have both the soft and the hard limits set to
477 INFINITY; in that case we fall back to this value.
e6729603 478
b7742c6b
AW
479 The situation is aggravated by certain compilers, which can consume
480 "beaucoup de stack", as they say in France.
0f2d19dd 481
b7742c6b
AW
482 See http://thread.gmane.org/gmane.lisp.guile.devel/8599/focus=8662 for
483 more discussion. This setting is 640 KB on 32-bit arches (should be enough
484 for anyone!) or a whoppin' 1280 KB on 64-bit arches.
485 */
486 { SCM_OPTION_INTEGER, "stack", 160000, "Stack size limit (measured in words; 0 = no check)." },
487 { SCM_OPTION_SCM, "show-file-name", (unsigned long)SCM_BOOL_T,
488 "Show file names and line numbers "
489 "in backtraces when not `#f'. A value of `base' "
490 "displays only base names, while `#t' displays full names."},
491 { SCM_OPTION_BOOLEAN, "warn-deprecated", 0,
492 "Warn when deprecated features are used." },
493 { 0 },
494};
212e58ed 495
1cc91f1b 496
b7742c6b
AW
497/*
498 * this ordering is awkward and illogical, but we maintain it for
499 * compatibility. --hwn
500 */
501scm_t_option scm_evaluator_trap_table[] = {
502 { SCM_OPTION_BOOLEAN, "traps", 0, "Enable evaluator traps." },
503 { SCM_OPTION_BOOLEAN, "enter-frame", 0, "Trap when eval enters new frame." },
504 { SCM_OPTION_BOOLEAN, "apply-frame", 0, "Trap when entering apply." },
505 { SCM_OPTION_BOOLEAN, "exit-frame", 0, "Trap when exiting eval or apply." },
506 { SCM_OPTION_SCM, "enter-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for enter-frame traps." },
507 { SCM_OPTION_SCM, "apply-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for apply-frame traps." },
508 { SCM_OPTION_SCM, "exit-frame-handler", (unsigned long)SCM_BOOL_F, "Handler for exit-frame traps." },
509 { SCM_OPTION_BOOLEAN, "memoize-symbol", 0, "Trap when memoizing a symbol." },
510 { SCM_OPTION_SCM, "memoize-symbol-handler", (unsigned long)SCM_BOOL_F, "The handler for memoization." },
511 { 0 }
512};
8ea46249 513
0f2d19dd 514
b7742c6b
AW
515SCM_DEFINE (scm_eval_options_interface, "eval-options-interface", 0, 1, 0,
516 (SCM setting),
517 "Option interface for the evaluation options. Instead of using\n"
518 "this procedure directly, use the procedures @code{eval-enable},\n"
519 "@code{eval-disable}, @code{eval-set!} and @code{eval-options}.")
520#define FUNC_NAME s_scm_eval_options_interface
212e58ed 521{
b7742c6b
AW
522 SCM ans;
523
524 scm_dynwind_begin (0);
525 scm_dynwind_critical_section (SCM_BOOL_F);
526 ans = scm_options (setting,
527 scm_eval_opts,
528 FUNC_NAME);
529 scm_dynwind_end ();
212e58ed 530
b7742c6b
AW
531 return ans;
532}
533#undef FUNC_NAME
0f2d19dd 534
1cc91f1b 535
b7742c6b
AW
536SCM_DEFINE (scm_evaluator_traps, "evaluator-traps-interface", 0, 1, 0,
537 (SCM setting),
538 "Option interface for the evaluator trap options.")
539#define FUNC_NAME s_scm_evaluator_traps
0f2d19dd 540{
b7742c6b 541 SCM ans;
2a6f7afe 542
b7742c6b
AW
543
544 scm_options_try (setting,
545 scm_evaluator_trap_table,
546 FUNC_NAME, 1);
547 SCM_CRITICAL_SECTION_START;
548 ans = scm_options (setting,
549 scm_evaluator_trap_table,
550 FUNC_NAME);
2a6f7afe 551
b7742c6b
AW
552 /* njrev: same again. */
553 SCM_CRITICAL_SECTION_END;
554 return ans;
555}
556#undef FUNC_NAME
2a6f7afe 557
2a6f7afe 558
2a6f7afe 559
b7742c6b 560\f
2a6f7afe 561
b7742c6b
AW
562/* Simple procedure calls
563 */
2a6f7afe 564
b7742c6b
AW
565SCM
566scm_call_0 (SCM proc)
567{
bf5a05f2 568 return scm_c_vm_run (scm_the_vm (), proc, NULL, 0);
0f2d19dd
JB
569}
570
b7742c6b
AW
571SCM
572scm_call_1 (SCM proc, SCM arg1)
212e58ed 573{
bf5a05f2 574 return scm_c_vm_run (scm_the_vm (), proc, &arg1, 1);
b7742c6b 575}
212e58ed 576
b7742c6b
AW
577SCM
578scm_call_2 (SCM proc, SCM arg1, SCM arg2)
579{
bf5a05f2
AW
580 SCM args[] = { arg1, arg2 };
581 return scm_c_vm_run (scm_the_vm (), proc, args, 2);
212e58ed
DH
582}
583
b7742c6b
AW
584SCM
585scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
0f2d19dd 586{
bf5a05f2
AW
587 SCM args[] = { arg1, arg2, arg3 };
588 return scm_c_vm_run (scm_the_vm (), proc, args, 3);
0f2d19dd
JB
589}
590
b7742c6b
AW
591SCM
592scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
212e58ed 593{
bf5a05f2
AW
594 SCM args[] = { arg1, arg2, arg3, arg4 };
595 return scm_c_vm_run (scm_the_vm (), proc, args, 4);
212e58ed
DH
596}
597
86fd6dff
AW
598SCM
599scm_call_n (SCM proc, SCM *argv, size_t nargs)
600{
601 return scm_c_vm_run (scm_the_vm (), proc, argv, nargs);
602}
603
b7742c6b 604/* Simple procedure applies
9fbee57e 605 */
cc56ba80 606
b7742c6b
AW
607SCM
608scm_apply_0 (SCM proc, SCM args)
609{
610 return scm_apply (proc, args, SCM_EOL);
0f572ba7
DH
611}
612
b7742c6b
AW
613SCM
614scm_apply_1 (SCM proc, SCM arg1, SCM args)
0f572ba7 615{
b7742c6b 616 return scm_apply (proc, scm_cons (arg1, args), SCM_EOL);
8ae95199
DH
617}
618
b7742c6b
AW
619SCM
620scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
0f2d19dd 621{
b7742c6b 622 return scm_apply (proc, scm_cons2 (arg1, arg2, args), SCM_EOL);
0f2d19dd
JB
623}
624
b7742c6b
AW
625SCM
626scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
212e58ed 627{
b7742c6b
AW
628 return scm_apply (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)),
629 SCM_EOL);
212e58ed
DH
630}
631
b7742c6b 632/* This code processes the arguments to apply:
8ea46249 633
b7742c6b 634 (apply PROC ARG1 ... ARGS)
302c12b4 635
b7742c6b
AW
636 Given a list (ARG1 ... ARGS), this function conses the ARG1
637 ... arguments onto the front of ARGS, and returns the resulting
638 list. Note that ARGS is a list; thus, the argument to this
639 function is a list whose last element is a list.
302c12b4 640
b7742c6b
AW
641 Apply calls this function, and applies PROC to the elements of the
642 result. apply:nconc2last takes care of building the list of
643 arguments, given (ARG1 ... ARGS).
a954ce1d 644
b7742c6b
AW
645 Rather than do new consing, apply:nconc2last destroys its argument.
646 On that topic, this code came into my care with the following
647 beautifully cryptic comment on that topic: "This will only screw
648 you if you do (scm_apply scm_apply '( ... ))" If you know what
649 they're referring to, send me a patch to this comment. */
0f2d19dd 650
b7742c6b
AW
651SCM_DEFINE (scm_nconc2last, "apply:nconc2last", 1, 0, 0,
652 (SCM lst),
653 "Given a list (@var{arg1} @dots{} @var{args}), this function\n"
654 "conses the @var{arg1} @dots{} arguments onto the front of\n"
655 "@var{args}, and returns the resulting list. Note that\n"
656 "@var{args} is a list; thus, the argument to this function is\n"
657 "a list whose last element is a list.\n"
658 "Note: Rather than do new consing, @code{apply:nconc2last}\n"
659 "destroys its argument, so use with care.")
660#define FUNC_NAME s_scm_nconc2last
212e58ed 661{
b7742c6b
AW
662 SCM *lloc;
663 SCM_VALIDATE_NONEMPTYLIST (1, lst);
664 lloc = &lst;
665 while (!scm_is_null (SCM_CDR (*lloc))) /* Perhaps should be
666 SCM_NULL_OR_NIL_P, but not
667 needed in 99.99% of cases,
668 and it could seriously hurt
669 performance. - Neil */
670 lloc = SCM_CDRLOC (*lloc);
671 SCM_ASSERT (scm_ilength (SCM_CAR (*lloc)) >= 0, lst, SCM_ARG1, FUNC_NAME);
672 *lloc = SCM_CAR (*lloc);
673 return lst;
212e58ed 674}
b7742c6b 675#undef FUNC_NAME
212e58ed 676
b8229a3b
MS
677
678
b7742c6b 679/* Typechecking for multi-argument MAP and FOR-EACH.
0f2d19dd 680
b7742c6b
AW
681 Verify that each element of the vector ARGV, except for the first,
682 is a proper list whose length is LEN. Attribute errors to WHO,
683 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
684static inline void
685check_map_args (SCM argv,
686 long len,
687 SCM gf,
688 SCM proc,
689 SCM args,
690 const char *who)
212e58ed 691{
b7742c6b 692 long i;
0f2d19dd 693
b7742c6b 694 for (i = SCM_SIMPLE_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
9fbee57e 695 {
b7742c6b
AW
696 SCM elt = SCM_SIMPLE_VECTOR_REF (argv, i);
697 long elt_len = scm_ilength (elt);
5cb22e96 698
b7742c6b
AW
699 if (elt_len < 0)
700 {
701 if (gf)
702 scm_apply_generic (gf, scm_cons (proc, args));
703 else
704 scm_wrong_type_arg (who, i + 2, elt);
705 }
1cc91f1b 706
b7742c6b
AW
707 if (elt_len != len)
708 scm_out_of_range_pos (who, elt, scm_from_long (i + 2));
0f2d19dd 709 }
0f2d19dd 710}
6dbd0af5 711
212e58ed 712
b7742c6b 713SCM_GPROC (s_map, "map", 2, 0, 1, scm_map, g_map);
212e58ed 714
b7742c6b
AW
715/* Note: Currently, scm_map applies PROC to the argument list(s)
716 sequentially, starting with the first element(s). This is used in
717 evalext.c where the Scheme procedure `map-in-order', which guarantees
718 sequential behaviour, is implemented using scm_map. If the
719 behaviour changes, we need to update `map-in-order'.
720*/
0f2d19dd 721
b7742c6b
AW
722SCM
723scm_map (SCM proc, SCM arg1, SCM args)
724#define FUNC_NAME s_map
0f2d19dd 725{
b7742c6b
AW
726 long i, len;
727 SCM res = SCM_EOL;
728 SCM *pres = &res;
0f2d19dd 729
b7742c6b
AW
730 len = scm_ilength (arg1);
731 SCM_GASSERTn (len >= 0,
732 g_map, scm_cons2 (proc, arg1, args), SCM_ARG2, s_map);
733 SCM_VALIDATE_REST_ARGUMENT (args);
734 if (scm_is_null (args))
0f2d19dd 735 {
b7742c6b
AW
736 SCM_GASSERT2 (scm_is_true (scm_procedure_p (proc)), g_map, proc, arg1, SCM_ARG1, s_map);
737 while (SCM_NIMP (arg1))
738 {
739 *pres = scm_list_1 (scm_call_1 (proc, SCM_CAR (arg1)));
740 pres = SCM_CDRLOC (*pres);
741 arg1 = SCM_CDR (arg1);
742 }
743 return res;
0f2d19dd 744 }
b7742c6b
AW
745 if (scm_is_null (SCM_CDR (args)))
746 {
747 SCM arg2 = SCM_CAR (args);
748 int len2 = scm_ilength (arg2);
749 SCM_GASSERTn (scm_is_true (scm_procedure_p (proc)), g_map,
750 scm_cons2 (proc, arg1, args), SCM_ARG1, s_map);
751 SCM_GASSERTn (len2 >= 0,
752 g_map, scm_cons2 (proc, arg1, args), SCM_ARG3, s_map);
753 if (len2 != len)
754 SCM_OUT_OF_RANGE (3, arg2);
755 while (SCM_NIMP (arg1))
756 {
757 *pres = scm_list_1 (scm_call_2 (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
758 pres = SCM_CDRLOC (*pres);
759 arg1 = SCM_CDR (arg1);
760 arg2 = SCM_CDR (arg2);
761 }
762 return res;
763 }
764 arg1 = scm_cons (arg1, args);
765 args = scm_vector (arg1);
766 check_map_args (args, len, g_map, proc, arg1, s_map);
767 while (1)
d6754c23 768 {
b7742c6b
AW
769 arg1 = SCM_EOL;
770 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
771 {
772 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
773 if (SCM_IMP (elt))
774 return res;
775 arg1 = scm_cons (SCM_CAR (elt), arg1);
776 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
777 }
778 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
779 pres = SCM_CDRLOC (*pres);
d6754c23 780 }
0f2d19dd 781}
b7742c6b 782#undef FUNC_NAME
0f2d19dd 783
302c12b4 784
b7742c6b 785SCM_GPROC (s_for_each, "for-each", 2, 0, 1, scm_for_each, g_for_each);
d6754c23 786
b7742c6b
AW
787SCM
788scm_for_each (SCM proc, SCM arg1, SCM args)
789#define FUNC_NAME s_for_each
0f2d19dd 790{
b7742c6b
AW
791 long i, len;
792 len = scm_ilength (arg1);
793 SCM_GASSERTn (len >= 0, g_for_each, scm_cons2 (proc, arg1, args),
794 SCM_ARG2, s_for_each);
795 SCM_VALIDATE_REST_ARGUMENT (args);
796 if (scm_is_null (args))
26d5b9b4 797 {
b7742c6b
AW
798 SCM_GASSERT2 (scm_is_true (scm_procedure_p (proc)), g_for_each,
799 proc, arg1, SCM_ARG1, s_for_each);
800 while (SCM_NIMP (arg1))
801 {
802 scm_call_1 (proc, SCM_CAR (arg1));
803 arg1 = SCM_CDR (arg1);
804 }
805 return SCM_UNSPECIFIED;
26d5b9b4 806 }
b7742c6b 807 if (scm_is_null (SCM_CDR (args)))
26d5b9b4 808 {
b7742c6b
AW
809 SCM arg2 = SCM_CAR (args);
810 int len2 = scm_ilength (arg2);
811 SCM_GASSERTn (scm_is_true (scm_procedure_p (proc)), g_for_each,
812 scm_cons2 (proc, arg1, args), SCM_ARG1, s_for_each);
813 SCM_GASSERTn (len2 >= 0, g_for_each,
814 scm_cons2 (proc, arg1, args), SCM_ARG3, s_for_each);
815 if (len2 != len)
816 SCM_OUT_OF_RANGE (3, arg2);
817 while (SCM_NIMP (arg1))
818 {
819 scm_call_2 (proc, SCM_CAR (arg1), SCM_CAR (arg2));
820 arg1 = SCM_CDR (arg1);
821 arg2 = SCM_CDR (arg2);
822 }
823 return SCM_UNSPECIFIED;
26d5b9b4 824 }
b7742c6b
AW
825 arg1 = scm_cons (arg1, args);
826 args = scm_vector (arg1);
827 check_map_args (args, len, g_for_each, proc, arg1, s_for_each);
828 while (1)
302c12b4 829 {
b7742c6b
AW
830 arg1 = SCM_EOL;
831 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
71560395 832 {
b7742c6b
AW
833 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
834 if (SCM_IMP (elt))
835 return SCM_UNSPECIFIED;
836 arg1 = scm_cons (SCM_CAR (elt), arg1);
837 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
838 }
839 scm_apply (proc, arg1, SCM_EOL);
840 }
841}
842#undef FUNC_NAME
71560395 843
71560395 844
5f161164
AW
845static SCM
846scm_c_primitive_eval (SCM exp)
b7742c6b
AW
847{
848 SCM transformer = scm_current_module_transformer ();
849 if (scm_is_true (transformer))
850 exp = scm_call_1 (transformer, exp);
851 exp = scm_memoize_expression (exp);
852 return eval (exp, SCM_EOL);
853}
5f161164
AW
854
855static SCM var_primitive_eval;
856SCM
857scm_primitive_eval (SCM exp)
858{
859 return scm_c_vm_run (scm_the_vm (), scm_variable_ref (var_primitive_eval),
860 &exp, 1);
861}
71560395 862
b7742c6b
AW
863
864/* Eval does not take the second arg optionally. This is intentional
865 * in order to be R5RS compatible, and to prepare for the new module
866 * system, where we would like to make the choice of evaluation
867 * environment explicit. */
868
869SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
870 (SCM exp, SCM module_or_state),
871 "Evaluate @var{exp}, a list representing a Scheme expression,\n"
872 "in the top-level environment specified by\n"
873 "@var{module_or_state}.\n"
874 "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
875 "@var{module_or_state} is made the current module when\n"
876 "it is a module, or the current dynamic state when it is\n"
877 "a dynamic state."
878 "Example: (eval '(+ 1 2) (interaction-environment))")
879#define FUNC_NAME s_scm_eval
880{
881 SCM res;
882
883 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
884 if (scm_is_dynamic_state (module_or_state))
885 scm_dynwind_current_dynamic_state (module_or_state);
886 else if (scm_module_system_booted_p)
887 {
888 SCM_VALIDATE_MODULE (2, module_or_state);
889 scm_dynwind_current_module (module_or_state);
71560395 890 }
b7742c6b 891 /* otherwise if the module system isn't booted, ignore the module arg */
71560395 892
b7742c6b
AW
893 res = scm_primitive_eval (exp);
894
895 scm_dynwind_end ();
896 return res;
897}
898#undef FUNC_NAME
71560395
AW
899
900
b7742c6b 901static SCM f_apply;
71560395
AW
902
903/* Apply a function to a list of arguments.
904
905 This function is exported to the Scheme level as taking two
906 required arguments and a tail argument, as if it were:
907 (lambda (proc arg1 . args) ...)
908 Thus, if you just have a list of arguments to pass to a procedure,
909 pass the list as ARG1, and '() for ARGS. If you have some fixed
910 args, pass the first as ARG1, then cons any remaining fixed args
911 onto the front of your argument list, and pass that as ARGS. */
912
913SCM
914scm_apply (SCM proc, SCM arg1, SCM args)
915{
b7742c6b 916 /* Fix things up so that args contains all args. */
71560395 917 if (scm_is_null (args))
b7742c6b 918 args = arg1;
71560395 919 else
b7742c6b 920 args = scm_cons_star (arg1, args);
71560395 921
67e2d80a 922 return scm_vm_apply (scm_the_vm (), proc, args);
b7742c6b 923}
434f2f7a
DH
924
925
314b8716
AW
926static SCM
927boot_closure_apply (SCM closure, SCM args)
928{
929 int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (closure);
930 SCM new_env = BOOT_CLOSURE_ENV (closure);
931 if (BOOT_CLOSURE_HAS_REST_ARGS (closure))
932 {
933 if (SCM_UNLIKELY (scm_ilength (args) < nreq))
934 scm_wrong_num_args (closure);
935 for (; nreq; nreq--, args = CDR (args))
936 new_env = scm_cons (CAR (args), new_env);
937 new_env = scm_cons (args, new_env);
938 }
939 else
940 {
941 if (SCM_UNLIKELY (scm_ilength (args) != nreq))
942 scm_wrong_num_args (closure);
943 for (; scm_is_pair (args); args = CDR (args))
944 new_env = scm_cons (CAR (args), new_env);
945 }
946 return eval (BOOT_CLOSURE_BODY (closure), new_env);
947}
948
949static int
950boot_closure_print (SCM closure, SCM port, scm_print_state *pstate)
951{
952 SCM args;
953 scm_puts ("#<boot-closure ", port);
954 scm_uintprint ((unsigned long)SCM2PTR (closure), 16, port);
955 scm_putc (' ', port);
956 args = scm_make_list (scm_from_int (BOOT_CLOSURE_NUM_REQUIRED_ARGS (closure)),
957 scm_from_locale_symbol ("_"));
958 if (BOOT_CLOSURE_HAS_REST_ARGS (closure))
959 args = scm_cons_star (scm_from_locale_symbol ("_"), args);
960 scm_display (args, port);
961 scm_putc ('>', port);
962 return 1;
963}
964
0f2d19dd
JB
965void
966scm_init_eval ()
0f2d19dd 967{
5f161164
AW
968 SCM primitive_eval;
969
33b97402 970 scm_init_opts (scm_evaluator_traps,
62560650 971 scm_evaluator_trap_table);
33b97402 972 scm_init_opts (scm_eval_options_interface,
62560650 973 scm_eval_opts);
33b97402 974
df9ca8d8 975 f_apply = scm_c_define_gsubr ("apply", 2, 0, 1, scm_apply);
86d31dfe 976
314b8716
AW
977 scm_tc16_boot_closure = scm_make_smob_type ("boot-closure", 0);
978 scm_set_smob_apply (scm_tc16_boot_closure, boot_closure_apply, 0, 0, 1);
979 scm_set_smob_print (scm_tc16_boot_closure, boot_closure_print);
980
5f161164
AW
981 primitive_eval = scm_c_make_gsubr ("primitive-eval", 1, 0, 0,
982 scm_c_primitive_eval);
983 var_primitive_eval = scm_define (SCM_SUBR_NAME (primitive_eval),
984 primitive_eval);
985
a0599745 986#include "libguile/eval.x"
0f2d19dd 987}
0f2d19dd 988
89e00824
ML
989/*
990 Local Variables:
991 c-file-style: "gnu"
992 End:
993*/
62560650 994