eval.c 26 KB

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