eval.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /* Copyright 1995-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <alloca.h>
  19. #include <stdarg.h>
  20. #include "alist.h"
  21. #include "async.h"
  22. #include "boolean.h"
  23. #include "continuations.h"
  24. #include "control.h"
  25. #include "debug.h"
  26. #include "deprecation.h"
  27. #include "dynwind.h"
  28. #include "eq.h"
  29. #include "expand.h"
  30. #include "feature.h"
  31. #include "frames.h"
  32. #include "fluids.h"
  33. #include "goops.h"
  34. #include "gsubr.h"
  35. #include "hash.h"
  36. #include "hashtab.h"
  37. #include "keywords.h"
  38. #include "list.h"
  39. #include "macros.h"
  40. #include "memoize.h"
  41. #include "modules.h"
  42. #include "numbers.h"
  43. #include "pairs.h"
  44. #include "ports.h"
  45. #include "print.h"
  46. #include "private-options.h"
  47. #include "procprop.h"
  48. #include "programs.h"
  49. #include "smob.h"
  50. #include "srcprop.h"
  51. #include "stackchk.h"
  52. #include "strings.h"
  53. #include "symbols.h"
  54. #include "threads.h"
  55. #include "throw.h"
  56. #include "values.h"
  57. #include "variable.h"
  58. #include "vectors.h"
  59. #include "vm.h"
  60. #include "eval.h"
  61. /* We have three levels of EVAL here:
  62. - eval (exp, env)
  63. evaluates EXP in environment ENV. ENV is a lexical environment
  64. structure as used by the actual tree code evaluator. When ENV is
  65. a top-level environment, then changes to the current module are
  66. tracked by updating ENV so that it continues to be in sync with
  67. the current module.
  68. - scm_primitive_eval (exp)
  69. evaluates EXP in the top-level environment as determined by the
  70. current module. This is done by constructing a suitable
  71. environment and calling eval. Thus, changes to the
  72. top-level module are tracked normally.
  73. - scm_eval (exp, mod)
  74. evaluates EXP while MOD is the current module. This is done
  75. by setting the current module to MOD_OR_STATE, invoking
  76. scm_primitive_eval on EXP, and then restoring the current module
  77. to the value it had previously. That is, while EXP is evaluated,
  78. changes to the current module (or dynamic state) are tracked,
  79. but these changes do not persist when scm_eval returns.
  80. */
  81. /* Boot closures. We only see these when compiling eval.scm, because once
  82. eval.scm is in the house, closures are standard VM closures.
  83. */
  84. static scm_t_bits scm_tc16_boot_closure;
  85. #define RETURN_BOOT_CLOSURE(code, env) \
  86. SCM_RETURN_NEWSMOB2 (scm_tc16_boot_closure, SCM_UNPACK (code), SCM_UNPACK (env))
  87. #define BOOT_CLOSURE_P(obj) SCM_TYP16_PREDICATE (scm_tc16_boot_closure, (obj))
  88. #define BOOT_CLOSURE_CODE(x) SCM_SMOB_OBJECT (x)
  89. #define BOOT_CLOSURE_ENV(x) SCM_SMOB_OBJECT_2 (x)
  90. #define BOOT_CLOSURE_BODY(x) CAR (BOOT_CLOSURE_CODE (x))
  91. #define BOOT_CLOSURE_NUM_REQUIRED_ARGS(x) (SCM_I_INUM (CADDR (BOOT_CLOSURE_CODE (x))))
  92. #define BOOT_CLOSURE_IS_FIXED(x) (scm_is_null (CDDDR (BOOT_CLOSURE_CODE (x))))
  93. /* NB: One may only call the following accessors if the closure is not FIXED. */
  94. #define BOOT_CLOSURE_HAS_REST_ARGS(x) scm_is_true (CADDR (SCM_CDR (BOOT_CLOSURE_CODE (x))))
  95. #define BOOT_CLOSURE_IS_REST(x) scm_is_null (SCM_CDR (CDDDR (BOOT_CLOSURE_CODE (x))))
  96. /* NB: One may only call the following accessors if the closure is not REST. */
  97. #define BOOT_CLOSURE_IS_FULL(x) (1)
  98. #define BOOT_CLOSURE_PARSE_FULL(fu_,body,nargs,rest,nopt,kw,ninits,unbound,alt) \
  99. do { SCM fu = fu_; \
  100. body = CAR (fu); fu = CDDR (fu); \
  101. \
  102. rest = kw = alt = SCM_BOOL_F; \
  103. unbound = SCM_BOOL_F; \
  104. nopt = ninits = 0; \
  105. \
  106. nreq = SCM_I_INUM (CAR (fu)); fu = CDR (fu); \
  107. if (scm_is_pair (fu)) \
  108. { \
  109. rest = CAR (fu); fu = CDR (fu); \
  110. if (scm_is_pair (fu)) \
  111. { \
  112. nopt = SCM_I_INUM (CAR (fu)); fu = CDR (fu); \
  113. kw = CAR (fu); fu = CDR (fu); \
  114. ninits = SCM_I_INUM (CAR (fu)); fu = CDR (fu); \
  115. unbound = CAR (fu); fu = CDR (fu); \
  116. alt = CAR (fu); \
  117. } \
  118. } \
  119. } while (0)
  120. static void prepare_boot_closure_env_for_apply (SCM proc, SCM args,
  121. SCM *out_body, SCM *out_env);
  122. static void prepare_boot_closure_env_for_eval (SCM proc, unsigned int argc,
  123. SCM exps, SCM *out_body,
  124. SCM *inout_env);
  125. #define CAR(x) SCM_CAR(x)
  126. #define CDR(x) SCM_CDR(x)
  127. #define CAAR(x) SCM_CAAR(x)
  128. #define CADR(x) SCM_CADR(x)
  129. #define CDAR(x) SCM_CDAR(x)
  130. #define CDDR(x) SCM_CDDR(x)
  131. #define CADDR(x) SCM_CADDR(x)
  132. #define CDDDR(x) SCM_CDDDR(x)
  133. #define VECTOR_REF(v, i) (SCM_SIMPLE_VECTOR_REF (v, i))
  134. #define VECTOR_SET(v, i, x) (SCM_SIMPLE_VECTOR_SET (v, i, x))
  135. #define VECTOR_LENGTH(v) (SCM_SIMPLE_VECTOR_LENGTH (v))
  136. static SCM
  137. make_env (int n, SCM init, SCM next)
  138. {
  139. SCM env = scm_c_make_vector (n + 1, init);
  140. VECTOR_SET (env, 0, next);
  141. return env;
  142. }
  143. static SCM
  144. next_rib (SCM env)
  145. {
  146. return VECTOR_REF (env, 0);
  147. }
  148. static SCM
  149. env_tail (SCM env)
  150. {
  151. while (SCM_I_IS_VECTOR (env))
  152. env = next_rib (env);
  153. return env;
  154. }
  155. static SCM
  156. env_ref (SCM env, int depth, int width)
  157. {
  158. while (depth--)
  159. env = next_rib (env);
  160. return VECTOR_REF (env, width + 1);
  161. }
  162. static void
  163. env_set (SCM env, int depth, int width, SCM val)
  164. {
  165. while (depth--)
  166. env = next_rib (env);
  167. VECTOR_SET (env, width + 1, val);
  168. }
  169. static void error_missing_value (SCM proc, SCM kw)
  170. {
  171. scm_error_scm (scm_from_utf8_symbol ("keyword-argument-error"), proc,
  172. scm_from_utf8_string ("Keyword argument has no value"), SCM_EOL,
  173. scm_list_1 (kw));
  174. }
  175. static void error_invalid_keyword (SCM proc, SCM obj)
  176. {
  177. scm_error_scm (scm_from_utf8_symbol ("keyword-argument-error"), proc,
  178. scm_from_utf8_string ("Invalid keyword"), SCM_EOL,
  179. scm_list_1 (obj));
  180. }
  181. static void error_unrecognized_keyword (SCM proc, SCM kw)
  182. {
  183. scm_error_scm (scm_from_utf8_symbol ("keyword-argument-error"), proc,
  184. scm_from_utf8_string ("Unrecognized keyword"), SCM_EOL,
  185. scm_list_1 (kw));
  186. }
  187. /* Multiple values truncation. */
  188. static SCM
  189. truncate_values (SCM x)
  190. {
  191. if (SCM_LIKELY (!scm_is_values (x)))
  192. return x;
  193. else
  194. {
  195. if (SCM_LIKELY (scm_i_nvalues (x) > 0))
  196. return scm_i_value_ref (x, 0);
  197. else
  198. {
  199. scm_ithrow (scm_from_utf8_symbol ("vm-run"),
  200. scm_list_3 (scm_from_utf8_symbol ("vm-run"),
  201. scm_from_utf8_string
  202. ("Too few values returned to continuation"),
  203. SCM_EOL),
  204. 1);
  205. /* Not reached. */
  206. return SCM_BOOL_F;
  207. }
  208. }
  209. }
  210. #define EVAL1(x, env) (truncate_values (eval ((x), (env))))
  211. static SCM
  212. eval (SCM x, SCM env)
  213. {
  214. SCM mx;
  215. SCM proc = SCM_UNDEFINED, args = SCM_EOL;
  216. unsigned int argc;
  217. loop:
  218. SCM_TICK;
  219. mx = SCM_MEMOIZED_ARGS (x);
  220. switch (SCM_I_INUM (SCM_CAR (x)))
  221. {
  222. case SCM_M_SEQ:
  223. eval (CAR (mx), env);
  224. x = CDR (mx);
  225. goto loop;
  226. case SCM_M_IF:
  227. if (scm_is_true (EVAL1 (CAR (mx), env)))
  228. x = CADR (mx);
  229. else
  230. x = CDDR (mx);
  231. goto loop;
  232. case SCM_M_LET:
  233. {
  234. SCM inits = CAR (mx);
  235. SCM new_env;
  236. int i;
  237. new_env = make_env (VECTOR_LENGTH (inits), SCM_UNDEFINED, env);
  238. for (i = 0; i < VECTOR_LENGTH (inits); i++)
  239. env_set (new_env, 0, i, EVAL1 (VECTOR_REF (inits, i), env));
  240. env = new_env;
  241. x = CDR (mx);
  242. goto loop;
  243. }
  244. case SCM_M_LAMBDA:
  245. RETURN_BOOT_CLOSURE (mx, env);
  246. case SCM_M_CAPTURE_ENV:
  247. {
  248. SCM locs = CAR (mx);
  249. SCM new_env;
  250. int i;
  251. new_env = make_env (VECTOR_LENGTH (locs), SCM_BOOL_F, env);
  252. for (i = 0; i < VECTOR_LENGTH (locs); i++)
  253. {
  254. SCM loc = VECTOR_REF (locs, i);
  255. int depth, width;
  256. depth = SCM_I_INUM (CAR (loc));
  257. width = SCM_I_INUM (CDR (loc));
  258. env_set (new_env, 0, i, env_ref (env, depth, width));
  259. }
  260. env = new_env;
  261. x = CDR (mx);
  262. goto loop;
  263. }
  264. case SCM_M_QUOTE:
  265. return mx;
  266. case SCM_M_CAPTURE_MODULE:
  267. return eval (mx, scm_current_module ());
  268. case SCM_M_APPLY:
  269. /* Evaluate the procedure to be applied. */
  270. proc = EVAL1 (CAR (mx), env);
  271. /* Evaluate the argument holding the list of arguments */
  272. args = EVAL1 (CADR (mx), env);
  273. apply_proc:
  274. /* Go here to tail-apply a procedure. PROC is the procedure and
  275. * ARGS is the list of arguments. */
  276. if (BOOT_CLOSURE_P (proc))
  277. {
  278. prepare_boot_closure_env_for_apply (proc, args, &x, &env);
  279. goto loop;
  280. }
  281. else
  282. return scm_apply_0 (proc, args);
  283. case SCM_M_CALL:
  284. /* Evaluate the procedure to be applied. */
  285. proc = EVAL1 (CAR (mx), env);
  286. argc = scm_ilength (CDR (mx));
  287. mx = CDR (mx);
  288. if (BOOT_CLOSURE_P (proc))
  289. {
  290. prepare_boot_closure_env_for_eval (proc, argc, mx, &x, &env);
  291. goto loop;
  292. }
  293. else
  294. {
  295. SCM *argv;
  296. unsigned int i;
  297. argv = alloca (argc * sizeof (SCM));
  298. for (i = 0; i < argc; i++, mx = CDR (mx))
  299. argv[i] = EVAL1 (CAR (mx), env);
  300. return scm_call_n (proc, argv, argc);
  301. }
  302. case SCM_M_CONT:
  303. return scm_i_call_with_current_continuation (EVAL1 (mx, env));
  304. case SCM_M_CALL_WITH_VALUES:
  305. {
  306. SCM producer;
  307. SCM v;
  308. producer = EVAL1 (CAR (mx), env);
  309. /* `proc' is the consumer. */
  310. proc = EVAL1 (CDR (mx), env);
  311. v = scm_call_0 (producer);
  312. if (scm_is_values (v))
  313. {
  314. size_t i = scm_i_nvalues (v);
  315. args = SCM_EOL;
  316. while (i--)
  317. args = scm_cons (scm_i_value_ref (v, i), args);
  318. }
  319. else
  320. args = scm_list_1 (v);
  321. goto apply_proc;
  322. }
  323. case SCM_M_LEXICAL_REF:
  324. {
  325. SCM pos;
  326. int depth, width;
  327. pos = mx;
  328. depth = SCM_I_INUM (CAR (pos));
  329. width = SCM_I_INUM (CDR (pos));
  330. return env_ref (env, depth, width);
  331. }
  332. case SCM_M_LEXICAL_SET:
  333. {
  334. SCM pos;
  335. int depth, width;
  336. SCM val = EVAL1 (CDR (mx), env);
  337. pos = CAR (mx);
  338. depth = SCM_I_INUM (CAR (pos));
  339. width = SCM_I_INUM (CDR (pos));
  340. env_set (env, depth, width, val);
  341. return SCM_UNSPECIFIED;
  342. }
  343. case SCM_M_BOX_REF:
  344. {
  345. SCM box = mx;
  346. return scm_variable_ref (EVAL1 (box, env));
  347. }
  348. case SCM_M_BOX_SET:
  349. {
  350. SCM box = CAR (mx), val = CDR (mx);
  351. return scm_variable_set_x (EVAL1 (box, env), EVAL1 (val, env));
  352. }
  353. case SCM_M_RESOLVE:
  354. if (SCM_VARIABLEP (mx))
  355. return mx;
  356. else
  357. {
  358. SCM var;
  359. var = scm_sys_resolve_variable (mx, env_tail (env));
  360. scm_set_cdr_x (x, var);
  361. return var;
  362. }
  363. case SCM_M_CALL_WITH_PROMPT:
  364. {
  365. scm_thread *t;
  366. SCM k, handler, res;
  367. jmp_buf registers;
  368. jmp_buf *prev_registers;
  369. ptrdiff_t saved_stack_depth;
  370. uint8_t *mra = NULL;
  371. k = EVAL1 (CAR (mx), env);
  372. handler = EVAL1 (CDDR (mx), env);
  373. t = SCM_I_CURRENT_THREAD;
  374. saved_stack_depth = t->vm.stack_top - t->vm.sp;
  375. /* Push the prompt onto the dynamic stack. */
  376. scm_dynstack_push_prompt (&t->dynstack,
  377. SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY,
  378. k,
  379. t->vm.stack_top - t->vm.fp,
  380. saved_stack_depth,
  381. t->vm.ip, mra,
  382. &registers);
  383. prev_registers = t->vm.registers;
  384. if (setjmp (registers))
  385. {
  386. /* The prompt exited nonlocally. */
  387. t->vm.registers = prev_registers;
  388. scm_gc_after_nonlocal_exit ();
  389. proc = handler;
  390. args = scm_i_prompt_pop_abort_args_x (&t->vm, saved_stack_depth);
  391. goto apply_proc;
  392. }
  393. res = scm_call_0 (eval (CADR (mx), env));
  394. scm_dynstack_pop (&t->dynstack);
  395. return res;
  396. }
  397. default:
  398. abort ();
  399. }
  400. }
  401. /* Simple procedure calls
  402. */
  403. SCM
  404. scm_call_0 (SCM proc)
  405. {
  406. return scm_call_n (proc, NULL, 0);
  407. }
  408. SCM
  409. scm_call_1 (SCM proc, SCM arg1)
  410. {
  411. return scm_call_n (proc, &arg1, 1);
  412. }
  413. SCM
  414. scm_call_2 (SCM proc, SCM arg1, SCM arg2)
  415. {
  416. SCM args[] = { arg1, arg2 };
  417. return scm_call_n (proc, args, 2);
  418. }
  419. SCM
  420. scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
  421. {
  422. SCM args[] = { arg1, arg2, arg3 };
  423. return scm_call_n (proc, args, 3);
  424. }
  425. SCM
  426. scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
  427. {
  428. SCM args[] = { arg1, arg2, arg3, arg4 };
  429. return scm_call_n (proc, args, 4);
  430. }
  431. SCM
  432. scm_call_5 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5)
  433. {
  434. SCM args[] = { arg1, arg2, arg3, arg4, arg5 };
  435. return scm_call_n (proc, args, 5);
  436. }
  437. SCM
  438. scm_call_6 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  439. SCM arg6)
  440. {
  441. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
  442. return scm_call_n (proc, args, 6);
  443. }
  444. SCM
  445. scm_call_7 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  446. SCM arg6, SCM arg7)
  447. {
  448. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
  449. return scm_call_n (proc, args, 7);
  450. }
  451. SCM
  452. scm_call_8 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  453. SCM arg6, SCM arg7, SCM arg8)
  454. {
  455. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 };
  456. return scm_call_n (proc, args, 8);
  457. }
  458. SCM
  459. scm_call_9 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  460. SCM arg6, SCM arg7, SCM arg8, SCM arg9)
  461. {
  462. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 };
  463. return scm_call_n (proc, args, 9);
  464. }
  465. /* scm_call_n defined in vm.c */
  466. SCM
  467. scm_call (SCM proc, ...)
  468. {
  469. va_list argp;
  470. SCM *argv = NULL;
  471. size_t i, nargs = 0;
  472. va_start (argp, proc);
  473. while (!SCM_UNBNDP (va_arg (argp, SCM)))
  474. nargs++;
  475. va_end (argp);
  476. argv = alloca (nargs * sizeof (SCM));
  477. va_start (argp, proc);
  478. for (i = 0; i < nargs; i++)
  479. argv[i] = va_arg (argp, SCM);
  480. va_end (argp);
  481. return scm_call_n (proc, argv, nargs);
  482. }
  483. /* Simple procedure applies
  484. */
  485. SCM
  486. scm_apply_0 (SCM proc, SCM args)
  487. {
  488. SCM *argv;
  489. int i, nargs;
  490. nargs = scm_ilength (args);
  491. if (SCM_UNLIKELY (nargs < 0))
  492. scm_wrong_type_arg_msg ("apply", 2, args, "list");
  493. /* FIXME: Use vm_builtin_apply instead of alloca. */
  494. argv = alloca (nargs * sizeof(SCM));
  495. for (i = 0; i < nargs; i++)
  496. {
  497. argv[i] = SCM_CAR (args);
  498. args = SCM_CDR (args);
  499. }
  500. return scm_call_n (proc, argv, nargs);
  501. }
  502. SCM
  503. scm_apply_1 (SCM proc, SCM arg1, SCM args)
  504. {
  505. return scm_apply_0 (proc, scm_cons (arg1, args));
  506. }
  507. SCM
  508. scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
  509. {
  510. return scm_apply_0 (proc, scm_cons2 (arg1, arg2, args));
  511. }
  512. SCM
  513. scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
  514. {
  515. return scm_apply_0 (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)));
  516. }
  517. static SCM map_var, for_each_var;
  518. static void init_map_var (void)
  519. {
  520. map_var = scm_private_variable (scm_the_root_module (),
  521. scm_from_latin1_symbol ("map"));
  522. }
  523. static void init_for_each_var (void)
  524. {
  525. for_each_var = scm_private_variable (scm_the_root_module (),
  526. scm_from_latin1_symbol ("for-each"));
  527. }
  528. SCM
  529. scm_map (SCM proc, SCM arg1, SCM args)
  530. {
  531. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  532. scm_i_pthread_once (&once, init_map_var);
  533. return scm_apply_0 (scm_variable_ref (map_var),
  534. scm_cons (proc, scm_cons (arg1, args)));
  535. }
  536. SCM
  537. scm_for_each (SCM proc, SCM arg1, SCM args)
  538. {
  539. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  540. scm_i_pthread_once (&once, init_for_each_var);
  541. return scm_apply_0 (scm_variable_ref (for_each_var),
  542. scm_cons (proc, scm_cons (arg1, args)));
  543. }
  544. static SCM
  545. scm_c_primitive_eval (SCM exp)
  546. {
  547. if (!SCM_EXPANDED_P (exp))
  548. exp = scm_call_1 (scm_current_module_transformer (), exp);
  549. return eval (scm_memoize_expression (exp), SCM_BOOL_F);
  550. }
  551. static SCM var_primitive_eval;
  552. SCM
  553. scm_primitive_eval (SCM exp)
  554. {
  555. return scm_call_n (scm_variable_ref (var_primitive_eval),
  556. &exp, 1);
  557. }
  558. /* Eval does not take the second arg optionally. This is intentional
  559. * in order to be R5RS compatible, and to prepare for the new module
  560. * system, where we would like to make the choice of evaluation
  561. * environment explicit. */
  562. SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
  563. (SCM exp, SCM module_or_state),
  564. "Evaluate @var{exp}, a list representing a Scheme expression,\n"
  565. "in the top-level environment specified by\n"
  566. "@var{module_or_state}.\n"
  567. "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
  568. "@var{module_or_state} is made the current module when\n"
  569. "it is a module, or the current dynamic state when it is\n"
  570. "a dynamic state."
  571. "Example: (eval '(+ 1 2) (interaction-environment))")
  572. #define FUNC_NAME s_scm_eval
  573. {
  574. SCM res;
  575. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  576. if (scm_is_dynamic_state (module_or_state))
  577. scm_dynwind_current_dynamic_state (module_or_state);
  578. else if (scm_module_system_booted_p)
  579. {
  580. SCM_VALIDATE_MODULE (2, module_or_state);
  581. scm_dynwind_current_module (module_or_state);
  582. }
  583. /* otherwise if the module system isn't booted, ignore the module arg */
  584. res = scm_primitive_eval (exp);
  585. scm_dynwind_end ();
  586. return res;
  587. }
  588. #undef FUNC_NAME
  589. static SCM f_apply;
  590. /* Apply a function to a list of arguments.
  591. This function's interface is a bit wonly. It takes two required
  592. arguments and a tail argument, as if it were:
  593. (lambda (proc arg1 . args) ...)
  594. Usually you want to use scm_apply_0 or one of its cousins. */
  595. SCM
  596. scm_apply (SCM proc, SCM arg1, SCM args)
  597. {
  598. return scm_apply_0 (proc,
  599. scm_is_null (args) ? arg1 : scm_cons_star (arg1, args));
  600. }
  601. static void
  602. prepare_boot_closure_env_for_apply (SCM proc, SCM args,
  603. SCM *out_body, SCM *out_env)
  604. {
  605. int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (proc);
  606. SCM env = BOOT_CLOSURE_ENV (proc);
  607. int i;
  608. if (BOOT_CLOSURE_IS_FIXED (proc)
  609. || (BOOT_CLOSURE_IS_REST (proc)
  610. && !BOOT_CLOSURE_HAS_REST_ARGS (proc)))
  611. {
  612. if (SCM_UNLIKELY (scm_ilength (args) != nreq))
  613. scm_wrong_num_args (proc);
  614. env = make_env (nreq, SCM_UNDEFINED, env);
  615. for (i = 0; i < nreq; args = CDR (args), i++)
  616. env_set (env, 0, i, CAR (args));
  617. *out_body = BOOT_CLOSURE_BODY (proc);
  618. *out_env = env;
  619. }
  620. else if (BOOT_CLOSURE_IS_REST (proc))
  621. {
  622. if (SCM_UNLIKELY (scm_ilength (args) < nreq))
  623. scm_wrong_num_args (proc);
  624. env = make_env (nreq + 1, SCM_UNDEFINED, env);
  625. for (i = 0; i < nreq; args = CDR (args), i++)
  626. env_set (env, 0, i, CAR (args));
  627. env_set (env, 0, i++, args);
  628. *out_body = BOOT_CLOSURE_BODY (proc);
  629. *out_env = env;
  630. }
  631. else
  632. {
  633. int i, argc, nreq, nopt, ninits, nenv;
  634. SCM body, rest, kw, unbound, alt;
  635. SCM mx = BOOT_CLOSURE_CODE (proc);
  636. loop:
  637. BOOT_CLOSURE_PARSE_FULL (mx, body, nargs, rest, nopt, kw,
  638. ninits, unbound, alt);
  639. argc = scm_ilength (args);
  640. if (argc < nreq)
  641. {
  642. if (scm_is_true (alt))
  643. {
  644. mx = alt;
  645. goto loop;
  646. }
  647. else
  648. scm_wrong_num_args (proc);
  649. }
  650. if (scm_is_false (kw) && argc > nreq + nopt && scm_is_false (rest))
  651. {
  652. if (scm_is_true (alt))
  653. {
  654. mx = alt;
  655. goto loop;
  656. }
  657. else
  658. scm_wrong_num_args (proc);
  659. }
  660. if (scm_is_true (kw) && scm_is_false (rest))
  661. {
  662. int npos = 0;
  663. SCM walk;
  664. for (walk = args; scm_is_pair (walk); walk = CDR (walk), npos++)
  665. if (npos >= nreq && scm_is_keyword (CAR (walk)))
  666. break;
  667. if (npos > nreq + nopt)
  668. {
  669. /* Too many positional args and no rest arg. */
  670. if (scm_is_true (alt))
  671. {
  672. mx = alt;
  673. goto loop;
  674. }
  675. else
  676. scm_wrong_num_args (proc);
  677. }
  678. }
  679. /* At this point we are committed to the chosen clause. */
  680. nenv = nreq + (scm_is_true (rest) ? 1 : 0) + ninits;
  681. env = make_env (nenv, unbound, env);
  682. for (i = 0; i < nreq; i++, args = CDR (args))
  683. env_set (env, 0, i, CAR (args));
  684. if (scm_is_false (kw))
  685. {
  686. /* Optional args (possibly), but no keyword args. */
  687. for (; i < argc && i < nreq + nopt; i++, args = CDR (args))
  688. env_set (env, 0, i, CAR (args));
  689. if (scm_is_true (rest))
  690. env_set (env, 0, nreq + nopt, args);
  691. }
  692. else
  693. {
  694. SCM aok;
  695. aok = CAR (kw);
  696. kw = CDR (kw);
  697. /* Optional args. As before, but stop at the first keyword. */
  698. for (; i < argc && i < nreq + nopt && !scm_is_keyword (CAR (args));
  699. i++, args = CDR (args))
  700. env_set (env, 0, i, CAR (args));
  701. if (scm_is_true (rest))
  702. env_set (env, 0, nreq + nopt, args);
  703. /* Parse keyword args. */
  704. {
  705. SCM walk;
  706. while (scm_is_pair (args))
  707. {
  708. SCM k = CAR (args);
  709. args = CDR (args);
  710. if (!scm_is_keyword (k))
  711. {
  712. if (scm_is_true (rest))
  713. continue;
  714. else
  715. break;
  716. }
  717. for (walk = kw; scm_is_pair (walk); walk = CDR (walk))
  718. if (scm_is_eq (k, CAAR (walk)))
  719. {
  720. if (scm_is_pair (args))
  721. {
  722. SCM v = CAR (args);
  723. args = CDR (args);
  724. env_set (env, 0, SCM_I_INUM (CDAR (walk)), v);
  725. break;
  726. }
  727. else
  728. error_missing_value (proc, k);
  729. }
  730. if (scm_is_null (walk))
  731. {
  732. if (scm_is_false (aok))
  733. error_unrecognized_keyword (proc, k);
  734. else if (!scm_is_pair (args))
  735. /* Advance past argument of unrecognized
  736. keyword, if present. */
  737. args = CDR (args);
  738. }
  739. }
  740. if (scm_is_pair (args) && scm_is_false (rest))
  741. error_invalid_keyword (proc, CAR (args));
  742. }
  743. }
  744. *out_body = body;
  745. *out_env = env;
  746. }
  747. }
  748. static void
  749. prepare_boot_closure_env_for_eval (SCM proc, unsigned int argc,
  750. SCM exps, SCM *out_body, SCM *inout_env)
  751. {
  752. int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (proc);
  753. SCM new_env = BOOT_CLOSURE_ENV (proc);
  754. if ((BOOT_CLOSURE_IS_FIXED (proc)
  755. || (BOOT_CLOSURE_IS_REST (proc)
  756. && !BOOT_CLOSURE_HAS_REST_ARGS (proc)))
  757. && nreq == argc)
  758. {
  759. int i;
  760. new_env = make_env (nreq, SCM_UNDEFINED, new_env);
  761. for (i = 0; i < nreq; exps = CDR (exps), i++)
  762. env_set (new_env, 0, i, EVAL1 (CAR (exps), *inout_env));
  763. *out_body = BOOT_CLOSURE_BODY (proc);
  764. *inout_env = new_env;
  765. }
  766. else if (!BOOT_CLOSURE_IS_FIXED (proc) &&
  767. BOOT_CLOSURE_IS_REST (proc) && argc >= nreq)
  768. {
  769. SCM rest;
  770. int i;
  771. new_env = make_env (nreq + 1, SCM_UNDEFINED, new_env);
  772. for (i = 0; i < nreq; exps = CDR (exps), i++)
  773. env_set (new_env, 0, i, EVAL1 (CAR (exps), *inout_env));
  774. for (rest = SCM_EOL; scm_is_pair (exps); exps = CDR (exps))
  775. rest = scm_cons (EVAL1 (CAR (exps), *inout_env), rest);
  776. env_set (new_env, 0, i++, scm_reverse_x (rest, SCM_UNDEFINED));
  777. *out_body = BOOT_CLOSURE_BODY (proc);
  778. *inout_env = new_env;
  779. }
  780. else
  781. {
  782. SCM args = SCM_EOL;
  783. for (; scm_is_pair (exps); exps = CDR (exps))
  784. args = scm_cons (EVAL1 (CAR (exps), *inout_env), args);
  785. args = scm_reverse_x (args, SCM_UNDEFINED);
  786. prepare_boot_closure_env_for_apply (proc, args, out_body, inout_env);
  787. }
  788. }
  789. static SCM
  790. boot_closure_apply (SCM closure, SCM args)
  791. {
  792. SCM body, env;
  793. prepare_boot_closure_env_for_apply (closure, args, &body, &env);
  794. return eval (body, env);
  795. }
  796. static int
  797. boot_closure_print (SCM closure, SCM port, scm_print_state *pstate)
  798. {
  799. SCM args;
  800. scm_puts ("#<boot-closure ", port);
  801. scm_uintprint (SCM_UNPACK (closure), 16, port);
  802. scm_putc (' ', port);
  803. args = scm_make_list (scm_from_int (BOOT_CLOSURE_NUM_REQUIRED_ARGS (closure)),
  804. scm_from_latin1_symbol ("_"));
  805. if (!BOOT_CLOSURE_IS_FIXED (closure) && BOOT_CLOSURE_HAS_REST_ARGS (closure))
  806. args = scm_cons_star (scm_from_latin1_symbol ("_"), args);
  807. /* FIXME: optionals and rests */
  808. scm_display (args, port);
  809. scm_putc ('>', port);
  810. return 1;
  811. }
  812. void
  813. scm_init_eval ()
  814. {
  815. SCM primitive_eval;
  816. f_apply = scm_c_define_gsubr ("apply", 2, 0, 1, scm_apply);
  817. scm_tc16_boot_closure = scm_make_smob_type ("boot-closure", 0);
  818. scm_set_smob_apply (scm_tc16_boot_closure, boot_closure_apply, 0, 0, 1);
  819. scm_set_smob_print (scm_tc16_boot_closure, boot_closure_print);
  820. primitive_eval = scm_c_make_gsubr ("primitive-eval", 1, 0, 0,
  821. scm_c_primitive_eval);
  822. var_primitive_eval = scm_define (SCM_SUBR_NAME (primitive_eval),
  823. primitive_eval);
  824. #include "eval.x"
  825. }