eval.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,
  2. * 2005,2006,2007,2008,2009,2010,2011,2012,2013
  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,inits,alt) \
  99. do { SCM fu = fu_; \
  100. body = CAR (fu); fu = CDDR (fu); \
  101. \
  102. rest = kw = alt = SCM_BOOL_F; \
  103. inits = SCM_EOL; \
  104. nopt = 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. inits = CAR (fu); fu = CDR (fu); \
  115. alt = CAR (fu); \
  116. } \
  117. } \
  118. } while (0)
  119. static void prepare_boot_closure_env_for_apply (SCM proc, SCM args,
  120. SCM *out_body, SCM *out_env);
  121. static void prepare_boot_closure_env_for_eval (SCM proc, unsigned int argc,
  122. SCM exps, SCM *out_body,
  123. SCM *inout_env);
  124. #define CAR(x) SCM_CAR(x)
  125. #define CDR(x) SCM_CDR(x)
  126. #define CAAR(x) SCM_CAAR(x)
  127. #define CADR(x) SCM_CADR(x)
  128. #define CDAR(x) SCM_CDAR(x)
  129. #define CDDR(x) SCM_CDDR(x)
  130. #define CADDR(x) SCM_CADDR(x)
  131. #define CDDDR(x) SCM_CDDDR(x)
  132. SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable");
  133. static void error_used_before_defined (void)
  134. {
  135. scm_error (scm_unbound_variable_key, NULL,
  136. "Variable used before given a value", SCM_EOL, SCM_BOOL_F);
  137. }
  138. static void error_invalid_keyword (SCM proc, SCM obj)
  139. {
  140. scm_error_scm (scm_from_latin1_symbol ("keyword-argument-error"), proc,
  141. scm_from_locale_string ("Invalid keyword"), SCM_EOL,
  142. scm_list_1 (obj));
  143. }
  144. static void error_unrecognized_keyword (SCM proc, SCM kw)
  145. {
  146. scm_error_scm (scm_from_latin1_symbol ("keyword-argument-error"), proc,
  147. scm_from_locale_string ("Unrecognized keyword"), SCM_EOL,
  148. scm_list_1 (kw));
  149. }
  150. /* Multiple values truncation. */
  151. static SCM
  152. truncate_values (SCM x)
  153. {
  154. if (SCM_LIKELY (!SCM_VALUESP (x)))
  155. return x;
  156. else
  157. {
  158. SCM l = scm_struct_ref (x, SCM_INUM0);
  159. if (SCM_LIKELY (scm_is_pair (l)))
  160. return scm_car (l);
  161. else
  162. {
  163. scm_ithrow (scm_from_latin1_symbol ("vm-run"),
  164. scm_list_3 (scm_from_latin1_symbol ("vm-run"),
  165. scm_from_locale_string
  166. ("Too few values returned to continuation"),
  167. SCM_EOL),
  168. 1);
  169. /* Not reached. */
  170. return SCM_BOOL_F;
  171. }
  172. }
  173. }
  174. #define EVAL1(x, env) (truncate_values (eval ((x), (env))))
  175. /* the environment:
  176. (VAL ... . MOD)
  177. If MOD is #f, it means the environment was captured before modules were
  178. booted.
  179. If MOD is the literal value '(), we are evaluating at the top level, and so
  180. should track changes to the current module. You have to be careful in this
  181. case, because further lexical contours should capture the current module.
  182. */
  183. #define CAPTURE_ENV(env) \
  184. (scm_is_null (env) ? scm_current_module () : \
  185. (scm_is_false (env) ? scm_the_root_module () : env))
  186. static SCM
  187. eval (SCM x, SCM env)
  188. {
  189. SCM mx;
  190. SCM proc = SCM_UNDEFINED, args = SCM_EOL;
  191. unsigned int argc;
  192. loop:
  193. SCM_TICK;
  194. if (!SCM_MEMOIZED_P (x))
  195. abort ();
  196. mx = SCM_MEMOIZED_ARGS (x);
  197. switch (SCM_MEMOIZED_TAG (x))
  198. {
  199. case SCM_M_SEQ:
  200. eval (CAR (mx), env);
  201. x = CDR (mx);
  202. goto loop;
  203. case SCM_M_IF:
  204. if (scm_is_true (EVAL1 (CAR (mx), env)))
  205. x = CADR (mx);
  206. else
  207. x = CDDR (mx);
  208. goto loop;
  209. case SCM_M_LET:
  210. {
  211. SCM inits = CAR (mx);
  212. SCM new_env = CAPTURE_ENV (env);
  213. for (; scm_is_pair (inits); inits = CDR (inits))
  214. new_env = scm_cons (EVAL1 (CAR (inits), env),
  215. new_env);
  216. env = new_env;
  217. x = CDR (mx);
  218. goto loop;
  219. }
  220. case SCM_M_LAMBDA:
  221. RETURN_BOOT_CLOSURE (mx, CAPTURE_ENV (env));
  222. case SCM_M_QUOTE:
  223. return mx;
  224. case SCM_M_DEFINE:
  225. scm_define (CAR (mx), EVAL1 (CDR (mx), env));
  226. return SCM_UNSPECIFIED;
  227. case SCM_M_APPLY:
  228. /* Evaluate the procedure to be applied. */
  229. proc = EVAL1 (CAR (mx), env);
  230. /* Evaluate the argument holding the list of arguments */
  231. args = EVAL1 (CADR (mx), env);
  232. apply_proc:
  233. /* Go here to tail-apply a procedure. PROC is the procedure and
  234. * ARGS is the list of arguments. */
  235. if (BOOT_CLOSURE_P (proc))
  236. {
  237. prepare_boot_closure_env_for_apply (proc, args, &x, &env);
  238. goto loop;
  239. }
  240. else
  241. return scm_call_with_vm (scm_the_vm (), proc, args);
  242. case SCM_M_CALL:
  243. /* Evaluate the procedure to be applied. */
  244. proc = EVAL1 (CAR (mx), env);
  245. argc = SCM_I_INUM (CADR (mx));
  246. mx = CDDR (mx);
  247. if (BOOT_CLOSURE_P (proc))
  248. {
  249. prepare_boot_closure_env_for_eval (proc, argc, mx, &x, &env);
  250. goto loop;
  251. }
  252. else
  253. {
  254. SCM *argv;
  255. unsigned int i;
  256. argv = alloca (argc * sizeof (SCM));
  257. for (i = 0; i < argc; i++, mx = CDR (mx))
  258. argv[i] = EVAL1 (CAR (mx), env);
  259. return scm_c_vm_run (scm_the_vm (), proc, argv, argc);
  260. }
  261. case SCM_M_CONT:
  262. return scm_i_call_with_current_continuation (EVAL1 (mx, env));
  263. case SCM_M_CALL_WITH_VALUES:
  264. {
  265. SCM producer;
  266. SCM v;
  267. producer = EVAL1 (CAR (mx), env);
  268. /* `proc' is the consumer. */
  269. proc = EVAL1 (CDR (mx), env);
  270. v = scm_call_with_vm (scm_the_vm (), producer, SCM_EOL);
  271. if (SCM_VALUESP (v))
  272. args = scm_struct_ref (v, SCM_INUM0);
  273. else
  274. args = scm_list_1 (v);
  275. goto apply_proc;
  276. }
  277. case SCM_M_LEXICAL_REF:
  278. {
  279. int n;
  280. SCM ret;
  281. for (n = SCM_I_INUM (mx); n; n--)
  282. env = CDR (env);
  283. ret = CAR (env);
  284. if (SCM_UNLIKELY (SCM_UNBNDP (ret)))
  285. /* we don't know what variable, though, because we don't have its
  286. name */
  287. error_used_before_defined ();
  288. return ret;
  289. }
  290. case SCM_M_LEXICAL_SET:
  291. {
  292. int n;
  293. SCM val = EVAL1 (CDR (mx), env);
  294. for (n = SCM_I_INUM (CAR (mx)); n; n--)
  295. env = CDR (env);
  296. SCM_SETCAR (env, val);
  297. return SCM_UNSPECIFIED;
  298. }
  299. case SCM_M_TOPLEVEL_REF:
  300. if (SCM_VARIABLEP (mx))
  301. return SCM_VARIABLE_REF (mx);
  302. else
  303. {
  304. while (scm_is_pair (env))
  305. env = CDR (env);
  306. return SCM_VARIABLE_REF
  307. (scm_memoize_variable_access_x (x, CAPTURE_ENV (env)));
  308. }
  309. case SCM_M_TOPLEVEL_SET:
  310. {
  311. SCM var = CAR (mx);
  312. SCM val = EVAL1 (CDR (mx), env);
  313. if (SCM_VARIABLEP (var))
  314. {
  315. SCM_VARIABLE_SET (var, val);
  316. return SCM_UNSPECIFIED;
  317. }
  318. else
  319. {
  320. while (scm_is_pair (env))
  321. env = CDR (env);
  322. SCM_VARIABLE_SET
  323. (scm_memoize_variable_access_x (x, CAPTURE_ENV (env)),
  324. val);
  325. return SCM_UNSPECIFIED;
  326. }
  327. }
  328. case SCM_M_MODULE_REF:
  329. if (SCM_VARIABLEP (mx))
  330. return SCM_VARIABLE_REF (mx);
  331. else
  332. return SCM_VARIABLE_REF
  333. (scm_memoize_variable_access_x (x, SCM_BOOL_F));
  334. case SCM_M_MODULE_SET:
  335. if (SCM_VARIABLEP (CDR (mx)))
  336. {
  337. SCM_VARIABLE_SET (CDR (mx), EVAL1 (CAR (mx), env));
  338. return SCM_UNSPECIFIED;
  339. }
  340. else
  341. {
  342. SCM_VARIABLE_SET
  343. (scm_memoize_variable_access_x (x, SCM_BOOL_F),
  344. EVAL1 (CAR (mx), env));
  345. return SCM_UNSPECIFIED;
  346. }
  347. case SCM_M_CALL_WITH_PROMPT:
  348. {
  349. SCM vm, k, res;
  350. scm_i_jmp_buf registers;
  351. /* We need the handler after nonlocal return to the setjmp, so
  352. make sure it is volatile. */
  353. volatile SCM handler;
  354. k = EVAL1 (CAR (mx), env);
  355. handler = EVAL1 (CDDR (mx), env);
  356. vm = scm_the_vm ();
  357. /* Push the prompt onto the dynamic stack. */
  358. scm_dynstack_push_prompt (&SCM_I_CURRENT_THREAD->dynstack,
  359. SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY,
  360. k,
  361. SCM_VM_DATA (vm)->fp,
  362. SCM_VM_DATA (vm)->sp,
  363. SCM_VM_DATA (vm)->ip,
  364. &registers);
  365. if (SCM_I_SETJMP (registers))
  366. {
  367. /* The prompt exited nonlocally. */
  368. proc = handler;
  369. args = scm_i_prompt_pop_abort_args_x (scm_the_vm ());
  370. goto apply_proc;
  371. }
  372. res = scm_call_0 (eval (CADR (mx), env));
  373. scm_dynstack_pop (&SCM_I_CURRENT_THREAD->dynstack);
  374. return res;
  375. }
  376. default:
  377. abort ();
  378. }
  379. }
  380. /* Simple procedure calls
  381. */
  382. SCM
  383. scm_call_0 (SCM proc)
  384. {
  385. return scm_c_vm_run (scm_the_vm (), proc, NULL, 0);
  386. }
  387. SCM
  388. scm_call_1 (SCM proc, SCM arg1)
  389. {
  390. return scm_c_vm_run (scm_the_vm (), proc, &arg1, 1);
  391. }
  392. SCM
  393. scm_call_2 (SCM proc, SCM arg1, SCM arg2)
  394. {
  395. SCM args[] = { arg1, arg2 };
  396. return scm_c_vm_run (scm_the_vm (), proc, args, 2);
  397. }
  398. SCM
  399. scm_call_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3)
  400. {
  401. SCM args[] = { arg1, arg2, arg3 };
  402. return scm_c_vm_run (scm_the_vm (), proc, args, 3);
  403. }
  404. SCM
  405. scm_call_4 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4)
  406. {
  407. SCM args[] = { arg1, arg2, arg3, arg4 };
  408. return scm_c_vm_run (scm_the_vm (), proc, args, 4);
  409. }
  410. SCM
  411. scm_call_5 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5)
  412. {
  413. SCM args[] = { arg1, arg2, arg3, arg4, arg5 };
  414. return scm_c_vm_run (scm_the_vm (), proc, args, 5);
  415. }
  416. SCM
  417. scm_call_6 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  418. SCM arg6)
  419. {
  420. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
  421. return scm_c_vm_run (scm_the_vm (), proc, args, 6);
  422. }
  423. SCM
  424. scm_call_7 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  425. SCM arg6, SCM arg7)
  426. {
  427. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
  428. return scm_c_vm_run (scm_the_vm (), proc, args, 7);
  429. }
  430. SCM
  431. scm_call_8 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  432. SCM arg6, SCM arg7, SCM arg8)
  433. {
  434. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 };
  435. return scm_c_vm_run (scm_the_vm (), proc, args, 8);
  436. }
  437. SCM
  438. scm_call_9 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM arg4, SCM arg5,
  439. SCM arg6, SCM arg7, SCM arg8, SCM arg9)
  440. {
  441. SCM args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 };
  442. return scm_c_vm_run (scm_the_vm (), proc, args, 9);
  443. }
  444. SCM
  445. scm_call_n (SCM proc, SCM *argv, size_t nargs)
  446. {
  447. return scm_c_vm_run (scm_the_vm (), proc, argv, nargs);
  448. }
  449. SCM
  450. scm_call (SCM proc, ...)
  451. {
  452. va_list argp;
  453. SCM *argv = NULL;
  454. size_t i, nargs = 0;
  455. va_start (argp, proc);
  456. while (!SCM_UNBNDP (va_arg (argp, SCM)))
  457. nargs++;
  458. va_end (argp);
  459. argv = alloca (nargs * sizeof (SCM));
  460. va_start (argp, proc);
  461. for (i = 0; i < nargs; i++)
  462. argv[i] = va_arg (argp, SCM);
  463. va_end (argp);
  464. return scm_c_vm_run (scm_the_vm (), proc, argv, nargs);
  465. }
  466. /* Simple procedure applies
  467. */
  468. SCM
  469. scm_apply_0 (SCM proc, SCM args)
  470. {
  471. return scm_apply (proc, args, SCM_EOL);
  472. }
  473. SCM
  474. scm_apply_1 (SCM proc, SCM arg1, SCM args)
  475. {
  476. return scm_apply (proc, scm_cons (arg1, args), SCM_EOL);
  477. }
  478. SCM
  479. scm_apply_2 (SCM proc, SCM arg1, SCM arg2, SCM args)
  480. {
  481. return scm_apply (proc, scm_cons2 (arg1, arg2, args), SCM_EOL);
  482. }
  483. SCM
  484. scm_apply_3 (SCM proc, SCM arg1, SCM arg2, SCM arg3, SCM args)
  485. {
  486. return scm_apply (proc, scm_cons (arg1, scm_cons2 (arg2, arg3, args)),
  487. SCM_EOL);
  488. }
  489. SCM
  490. scm_map (SCM proc, SCM arg1, SCM args)
  491. {
  492. static SCM var = SCM_BOOL_F;
  493. if (scm_is_false (var))
  494. var = scm_private_variable (scm_the_root_module (),
  495. scm_from_latin1_symbol ("map"));
  496. return scm_apply (scm_variable_ref (var),
  497. scm_cons (proc, scm_cons (arg1, args)), SCM_EOL);
  498. }
  499. SCM
  500. scm_for_each (SCM proc, SCM arg1, SCM args)
  501. {
  502. static SCM var = SCM_BOOL_F;
  503. if (scm_is_false (var))
  504. var = scm_private_variable (scm_the_root_module (),
  505. scm_from_latin1_symbol ("for-each"));
  506. return scm_apply (scm_variable_ref (var),
  507. scm_cons (proc, scm_cons (arg1, args)), SCM_EOL);
  508. }
  509. static SCM
  510. scm_c_primitive_eval (SCM exp)
  511. {
  512. if (!SCM_EXPANDED_P (exp))
  513. exp = scm_call_1 (scm_current_module_transformer (), exp);
  514. return eval (scm_memoize_expression (exp), SCM_EOL);
  515. }
  516. static SCM var_primitive_eval;
  517. SCM
  518. scm_primitive_eval (SCM exp)
  519. {
  520. return scm_c_vm_run (scm_the_vm (), scm_variable_ref (var_primitive_eval),
  521. &exp, 1);
  522. }
  523. /* Eval does not take the second arg optionally. This is intentional
  524. * in order to be R5RS compatible, and to prepare for the new module
  525. * system, where we would like to make the choice of evaluation
  526. * environment explicit. */
  527. SCM_DEFINE (scm_eval, "eval", 2, 0, 0,
  528. (SCM exp, SCM module_or_state),
  529. "Evaluate @var{exp}, a list representing a Scheme expression,\n"
  530. "in the top-level environment specified by\n"
  531. "@var{module_or_state}.\n"
  532. "While @var{exp} is evaluated (using @code{primitive-eval}),\n"
  533. "@var{module_or_state} is made the current module when\n"
  534. "it is a module, or the current dynamic state when it is\n"
  535. "a dynamic state."
  536. "Example: (eval '(+ 1 2) (interaction-environment))")
  537. #define FUNC_NAME s_scm_eval
  538. {
  539. SCM res;
  540. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  541. if (scm_is_dynamic_state (module_or_state))
  542. scm_dynwind_current_dynamic_state (module_or_state);
  543. else if (scm_module_system_booted_p)
  544. {
  545. SCM_VALIDATE_MODULE (2, module_or_state);
  546. scm_dynwind_current_module (module_or_state);
  547. }
  548. /* otherwise if the module system isn't booted, ignore the module arg */
  549. res = scm_primitive_eval (exp);
  550. scm_dynwind_end ();
  551. return res;
  552. }
  553. #undef FUNC_NAME
  554. static SCM f_apply;
  555. /* Apply a function to a list of arguments.
  556. This function is exported to the Scheme level as taking two
  557. required arguments and a tail argument, as if it were:
  558. (lambda (proc arg1 . args) ...)
  559. Thus, if you just have a list of arguments to pass to a procedure,
  560. pass the list as ARG1, and '() for ARGS. If you have some fixed
  561. args, pass the first as ARG1, then cons any remaining fixed args
  562. onto the front of your argument list, and pass that as ARGS. */
  563. SCM
  564. scm_apply (SCM proc, SCM arg1, SCM args)
  565. {
  566. /* Fix things up so that args contains all args. */
  567. if (scm_is_null (args))
  568. args = arg1;
  569. else
  570. args = scm_cons_star (arg1, args);
  571. return scm_call_with_vm (scm_the_vm (), proc, args);
  572. }
  573. static void
  574. prepare_boot_closure_env_for_apply (SCM proc, SCM args,
  575. SCM *out_body, SCM *out_env)
  576. {
  577. int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (proc);
  578. SCM env = BOOT_CLOSURE_ENV (proc);
  579. if (BOOT_CLOSURE_IS_FIXED (proc)
  580. || (BOOT_CLOSURE_IS_REST (proc)
  581. && !BOOT_CLOSURE_HAS_REST_ARGS (proc)))
  582. {
  583. if (SCM_UNLIKELY (scm_ilength (args) != nreq))
  584. scm_wrong_num_args (proc);
  585. for (; scm_is_pair (args); args = CDR (args))
  586. env = scm_cons (CAR (args), env);
  587. *out_body = BOOT_CLOSURE_BODY (proc);
  588. *out_env = env;
  589. }
  590. else if (BOOT_CLOSURE_IS_REST (proc))
  591. {
  592. if (SCM_UNLIKELY (scm_ilength (args) < nreq))
  593. scm_wrong_num_args (proc);
  594. for (; nreq; nreq--, args = CDR (args))
  595. env = scm_cons (CAR (args), env);
  596. env = scm_cons (args, env);
  597. *out_body = BOOT_CLOSURE_BODY (proc);
  598. *out_env = env;
  599. }
  600. else
  601. {
  602. int i, argc, nreq, nopt;
  603. SCM body, rest, kw, inits, alt;
  604. SCM mx = BOOT_CLOSURE_CODE (proc);
  605. loop:
  606. BOOT_CLOSURE_PARSE_FULL (mx, body, nargs, rest, nopt, kw, inits, alt);
  607. argc = scm_ilength (args);
  608. if (argc < nreq)
  609. {
  610. if (scm_is_true (alt))
  611. {
  612. mx = alt;
  613. goto loop;
  614. }
  615. else
  616. scm_wrong_num_args (proc);
  617. }
  618. if (scm_is_false (kw) && argc > nreq + nopt && scm_is_false (rest))
  619. {
  620. if (scm_is_true (alt))
  621. {
  622. mx = alt;
  623. goto loop;
  624. }
  625. else
  626. scm_wrong_num_args (proc);
  627. }
  628. for (i = 0; i < nreq; i++, args = CDR (args))
  629. env = scm_cons (CAR (args), env);
  630. if (scm_is_false (kw))
  631. {
  632. /* Optional args (possibly), but no keyword args. */
  633. for (; i < argc && i < nreq + nopt;
  634. i++, args = CDR (args))
  635. {
  636. env = scm_cons (CAR (args), env);
  637. inits = CDR (inits);
  638. }
  639. for (; i < nreq + nopt; i++, inits = CDR (inits))
  640. env = scm_cons (EVAL1 (CAR (inits), env), env);
  641. if (scm_is_true (rest))
  642. env = scm_cons (args, env);
  643. }
  644. else
  645. {
  646. SCM aok;
  647. aok = CAR (kw);
  648. kw = CDR (kw);
  649. /* Keyword args. As before, but stop at the first keyword. */
  650. for (; i < argc && i < nreq + nopt && !scm_is_keyword (CAR (args));
  651. i++, args = CDR (args), inits = CDR (inits))
  652. env = scm_cons (CAR (args), env);
  653. for (; i < nreq + nopt; i++, inits = CDR (inits))
  654. env = scm_cons (EVAL1 (CAR (inits), env), env);
  655. if (scm_is_true (rest))
  656. {
  657. env = scm_cons (args, env);
  658. i++;
  659. }
  660. else if (scm_is_true (alt)
  661. && scm_is_pair (args) && !scm_is_keyword (CAR (args)))
  662. {
  663. /* Too many positional args, no rest arg, and we have an
  664. alternate clause. */
  665. mx = alt;
  666. goto loop;
  667. }
  668. /* Now fill in env with unbound values, limn the rest of the args for
  669. keywords, and fill in unbound values with their inits. */
  670. {
  671. int imax = i - 1;
  672. int kw_start_idx = i;
  673. SCM walk, k, v;
  674. for (walk = kw; scm_is_pair (walk); walk = CDR (walk))
  675. if (SCM_I_INUM (CDAR (walk)) > imax)
  676. imax = SCM_I_INUM (CDAR (walk));
  677. for (; i <= imax; i++)
  678. env = scm_cons (SCM_UNDEFINED, env);
  679. if (scm_is_pair (args) && scm_is_pair (CDR (args)))
  680. for (; scm_is_pair (args) && scm_is_pair (CDR (args));
  681. args = CDR (args))
  682. {
  683. k = CAR (args); v = CADR (args);
  684. if (!scm_is_keyword (k))
  685. {
  686. if (scm_is_true (rest))
  687. continue;
  688. else
  689. break;
  690. }
  691. for (walk = kw; scm_is_pair (walk); walk = CDR (walk))
  692. if (scm_is_eq (k, CAAR (walk)))
  693. {
  694. /* Well... ok, list-set! isn't the nicest interface, but
  695. hey. */
  696. int iset = imax - SCM_I_INUM (CDAR (walk));
  697. scm_list_set_x (env, SCM_I_MAKINUM (iset), v);
  698. args = CDR (args);
  699. break;
  700. }
  701. if (scm_is_null (walk) && scm_is_false (aok))
  702. error_unrecognized_keyword (proc, k);
  703. }
  704. if (scm_is_pair (args) && scm_is_false (rest))
  705. error_invalid_keyword (proc, CAR (args));
  706. /* Now fill in unbound values, evaluating init expressions in their
  707. appropriate environment. */
  708. for (i = imax - kw_start_idx; scm_is_pair (inits); i--, inits = CDR (inits))
  709. {
  710. SCM tail = scm_list_tail (env, SCM_I_MAKINUM (i));
  711. if (SCM_UNBNDP (CAR (tail)))
  712. SCM_SETCAR (tail, EVAL1 (CAR (inits), CDR (tail)));
  713. }
  714. }
  715. }
  716. *out_body = body;
  717. *out_env = env;
  718. }
  719. }
  720. static void
  721. prepare_boot_closure_env_for_eval (SCM proc, unsigned int argc,
  722. SCM exps, SCM *out_body, SCM *inout_env)
  723. {
  724. int nreq = BOOT_CLOSURE_NUM_REQUIRED_ARGS (proc);
  725. SCM new_env = BOOT_CLOSURE_ENV (proc);
  726. if (BOOT_CLOSURE_IS_FIXED (proc)
  727. || (BOOT_CLOSURE_IS_REST (proc)
  728. && !BOOT_CLOSURE_HAS_REST_ARGS (proc)))
  729. {
  730. for (; scm_is_pair (exps); exps = CDR (exps), nreq--)
  731. new_env = scm_cons (EVAL1 (CAR (exps), *inout_env),
  732. new_env);
  733. if (SCM_UNLIKELY (nreq != 0))
  734. scm_wrong_num_args (proc);
  735. *out_body = BOOT_CLOSURE_BODY (proc);
  736. *inout_env = new_env;
  737. }
  738. else if (BOOT_CLOSURE_IS_REST (proc))
  739. {
  740. if (SCM_UNLIKELY (argc < nreq))
  741. scm_wrong_num_args (proc);
  742. for (; nreq; nreq--, exps = CDR (exps))
  743. new_env = scm_cons (EVAL1 (CAR (exps), *inout_env),
  744. new_env);
  745. {
  746. SCM rest = SCM_EOL;
  747. for (; scm_is_pair (exps); exps = CDR (exps))
  748. rest = scm_cons (EVAL1 (CAR (exps), *inout_env), rest);
  749. new_env = scm_cons (scm_reverse (rest),
  750. new_env);
  751. }
  752. *out_body = BOOT_CLOSURE_BODY (proc);
  753. *inout_env = new_env;
  754. }
  755. else
  756. {
  757. SCM args = SCM_EOL;
  758. for (; scm_is_pair (exps); exps = CDR (exps))
  759. args = scm_cons (EVAL1 (CAR (exps), *inout_env), args);
  760. args = scm_reverse_x (args, SCM_UNDEFINED);
  761. prepare_boot_closure_env_for_apply (proc, args, out_body, inout_env);
  762. }
  763. }
  764. static SCM
  765. boot_closure_apply (SCM closure, SCM args)
  766. {
  767. SCM body, env;
  768. prepare_boot_closure_env_for_apply (closure, args, &body, &env);
  769. return eval (body, env);
  770. }
  771. static int
  772. boot_closure_print (SCM closure, SCM port, scm_print_state *pstate)
  773. {
  774. SCM args;
  775. scm_puts_unlocked ("#<boot-closure ", port);
  776. scm_uintprint (SCM_UNPACK (closure), 16, port);
  777. scm_putc_unlocked (' ', port);
  778. args = scm_make_list (scm_from_int (BOOT_CLOSURE_NUM_REQUIRED_ARGS (closure)),
  779. scm_from_latin1_symbol ("_"));
  780. if (!BOOT_CLOSURE_IS_FIXED (closure) && BOOT_CLOSURE_HAS_REST_ARGS (closure))
  781. args = scm_cons_star (scm_from_latin1_symbol ("_"), args);
  782. /* FIXME: optionals and rests */
  783. scm_display (args, port);
  784. scm_putc_unlocked ('>', port);
  785. return 1;
  786. }
  787. void
  788. scm_init_eval ()
  789. {
  790. SCM primitive_eval;
  791. f_apply = scm_c_define_gsubr ("apply", 2, 0, 1, scm_apply);
  792. scm_tc16_boot_closure = scm_make_smob_type ("boot-closure", 0);
  793. scm_set_smob_apply (scm_tc16_boot_closure, boot_closure_apply, 0, 0, 1);
  794. scm_set_smob_print (scm_tc16_boot_closure, boot_closure_print);
  795. primitive_eval = scm_c_make_gsubr ("primitive-eval", 1, 0, 0,
  796. scm_c_primitive_eval);
  797. var_primitive_eval = scm_define (SCM_SUBR_NAME (primitive_eval),
  798. primitive_eval);
  799. #include "libguile/eval.x"
  800. }
  801. /*
  802. Local Variables:
  803. c-file-style: "gnu"
  804. End:
  805. */