eval.c 27 KB

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