eval2.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* eval2.c Copyright (C) 1989-2002 Codemist Ltd */
  2. /*
  3. * Interpreter (part 2). apply & some special forms
  4. */
  5. /*
  6. * This code may be used and modified, and redistributed in binary
  7. * or source form, subject to the "CCL Public License", which should
  8. * accompany it. This license is a variant on the BSD license, and thus
  9. * permits use of code derived from this in either open and commercial
  10. * projects: but it does require that updates to this code be made
  11. * available back to the originators of the package.
  12. * Before merging other code in with this or linking this code
  13. * with other packages or libraries please check that the license terms
  14. * of the other material are compatible with those of this.
  15. */
  16. /* Signature: 274a0849 08-Apr-2002 */
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include "machine.h"
  21. #include "tags.h"
  22. #include "cslerror.h"
  23. #include "externs.h"
  24. #include "entries.h"
  25. #ifdef TIMEOUT
  26. #include "timeout.h"
  27. #endif
  28. static Lisp_Object apply_lots(int nargs, n_args *f, Lisp_Object def)
  29. /*
  30. * Cases with 8 or more args are lifted out here into a subroutine
  31. * to make APPLY a bit shorter and because these cases should be
  32. * uncommon & not worth optimising much. The code that Microsoft C 6.00A
  33. * produced for this was utterly DREADFUL - maybe other C compilers will
  34. * make a mess of it too. Anyway I hope it will not be called very often.
  35. */
  36. {
  37. switch(nargs)
  38. {
  39. case 9:
  40. return (*f)(def, 9, stack[-9], stack[-8], stack[-7],
  41. stack[-6], stack[-5], stack[-4], stack[-3],
  42. stack[-2], stack[-1]);
  43. case 10:
  44. return (*f)(def, 10, stack[-10], stack[-9], stack[-8],
  45. stack[-7], stack[-6], stack[-5], stack[-4],
  46. stack[-3], stack[-2], stack[-1]);
  47. case 11:
  48. return (*f)(def, 11, stack[-11], stack[-10],
  49. stack[-9], stack[-8], stack[-7], stack[-6],
  50. stack[-5], stack[-4], stack[-3], stack[-2],
  51. stack[-1]);
  52. case 12:
  53. return (*f)(def, 12, stack[-12], stack[-11],
  54. stack[-10], stack[-9], stack[-8], stack[-7],
  55. stack[-6], stack[-5], stack[-4], stack[-3],
  56. stack[-2], stack[-1]);
  57. case 13:
  58. return (*f)(def, 13, stack[-13], stack[-12],
  59. stack[-11], stack[-10], stack[-9], stack[-8],
  60. stack[-7], stack[-6], stack[-5], stack[-4],
  61. stack[-3], stack[-2], stack[-1]);
  62. case 14:
  63. return (*f)(def, 14, stack[-14], stack[-13],
  64. stack[-12], stack[-11], stack[-10], stack[-9],
  65. stack[-8], stack[-7], stack[-6], stack[-5],
  66. stack[-4], stack[-3], stack[-2], stack[-1]);
  67. case 15:
  68. return (*f)(def, 15, stack[-15], stack[-14],
  69. stack[-13], stack[-12], stack[-11], stack[-10],
  70. stack[-9], stack[-8], stack[-7], stack[-6],
  71. stack[-5], stack[-4], stack[-3], stack[-2],
  72. stack[-1]);
  73. case 16:
  74. return (*f)(def, 16, stack[-16], stack[-15],
  75. stack[-14], stack[-13], stack[-12], stack[-11],
  76. stack[-10], stack[-9], stack[-8], stack[-7],
  77. stack[-6], stack[-5], stack[-4], stack[-3],
  78. stack[-2], stack[-1]);
  79. case 17:
  80. return (*f)(def, 17, stack[-17], stack[-16],
  81. stack[-15], stack[-14], stack[-13], stack[-12],
  82. stack[-11], stack[-10], stack[-9], stack[-8],
  83. stack[-7], stack[-6], stack[-5], stack[-4],
  84. stack[-3], stack[-2], stack[-1]);
  85. case 18:
  86. return (*f)(def, 18, stack[-18], stack[-17],
  87. stack[-16], stack[-15], stack[-14], stack[-13],
  88. stack[-12], stack[-11], stack[-10], stack[-9],
  89. stack[-8], stack[-7], stack[-6], stack[-5],
  90. stack[-4], stack[-3], stack[-2], stack[-1]);
  91. case 19:
  92. return (*f)(def, 19, stack[-19], stack[-18],
  93. stack[-17], stack[-16], stack[-15], stack[-14],
  94. stack[-13], stack[-12], stack[-11], stack[-10],
  95. stack[-9], stack[-8], stack[-7], stack[-6],
  96. stack[-5], stack[-4], stack[-3], stack[-2],
  97. stack[-1]);
  98. case 20:
  99. return (*f)(def, 20, stack[-20], stack[-19],
  100. stack[-18], stack[-17], stack[-16], stack[-15],
  101. stack[-14], stack[-13], stack[-12], stack[-11],
  102. stack[-10], stack[-9], stack[-8], stack[-7],
  103. stack[-6], stack[-5], stack[-4], stack[-3],
  104. stack[-2], stack[-1]);
  105. case 21:
  106. return (*f)(def, 21, stack[-21], stack[-20],
  107. stack[-19], stack[-18], stack[-17], stack[-16],
  108. stack[-15], stack[-14], stack[-13], stack[-12],
  109. stack[-11], stack[-10], stack[-9], stack[-8],
  110. stack[-7], stack[-6], stack[-5], stack[-4],
  111. stack[-3], stack[-2], stack[-1]);
  112. case 22:
  113. return (*f)(def, 22, stack[-22], stack[-21],
  114. stack[-20], stack[-19], stack[-18], stack[-17],
  115. stack[-16], stack[-15], stack[-14], stack[-13],
  116. stack[-12], stack[-11], stack[-10], stack[-9],
  117. stack[-8], stack[-7], stack[-6], stack[-5],
  118. stack[-4], stack[-3], stack[-2], stack[-1]);
  119. case 23:
  120. return (*f)(def, 23, stack[-23], stack[-22],
  121. stack[-21], stack[-20], stack[-19], stack[-18],
  122. stack[-17], stack[-16], stack[-15], stack[-14],
  123. stack[-13], stack[-12], stack[-11], stack[-10],
  124. stack[-9], stack[-8], stack[-7], stack[-6],
  125. stack[-5], stack[-4], stack[-3], stack[-2],
  126. stack[-1]);
  127. case 24:
  128. return (*f)(def, 24, stack[-24], stack[-23],
  129. stack[-22], stack[-21], stack[-20], stack[-19],
  130. stack[-18], stack[-17], stack[-16], stack[-15],
  131. stack[-14], stack[-13], stack[-12], stack[-11],
  132. stack[-10], stack[-9], stack[-8], stack[-7],
  133. stack[-6], stack[-5], stack[-4], stack[-3],
  134. stack[-2], stack[-1]);
  135. case 25:
  136. return (*f)(def, 25, stack[-25], stack[-24], stack[-23],
  137. stack[-22], stack[-21], stack[-20], stack[-19],
  138. stack[-18], stack[-17], stack[-16], stack[-15],
  139. stack[-14], stack[-13], stack[-12], stack[-11],
  140. stack[-10], stack[-9], stack[-8], stack[-7],
  141. stack[-6], stack[-5], stack[-4], stack[-3],
  142. stack[-2], stack[-1]);
  143. default:
  144. /*
  145. * If more than 25 args are going to be passed I will arrange that the
  146. * final ones are built into a list - as if the 25th arg was specified
  147. * as a "&rest" one. Why? Because passing VERY large numbers of arguments
  148. * in C is not a good idea - ANSI C compilers are only obliged to support
  149. * up to 31 args, and one some machines this limit seems to really matter.
  150. * But Common Lisp can need more args than that. I will ignore the fact that
  151. * what I do here is slow. I will HOPE that calls with 25 or more args
  152. * are very uncommon.
  153. */
  154. { int n = nargs;
  155. Lisp_Object w, *tsp = stack, nil = C_nil;
  156. #if (ARG_CUT_OFF != 25)
  157. if (ARG_CUT_OFF != 25)
  158. { fprintf(stderr, "\n+++ ARG_CUT_OFF incorrectly configured\n");
  159. my_exit(EXIT_FAILURE);
  160. }
  161. #endif
  162. w = ncons(tsp[-1]);
  163. errexit();
  164. tsp[-1] = w;
  165. while (n > ARG_CUT_OFF)
  166. { w = cons(tsp[-2], tsp[-1]);
  167. errexit();
  168. tsp[-2] = w;
  169. tsp[-1] = tsp[0];
  170. tsp--;
  171. n--;
  172. }
  173. return (*f)(def, nargs, tsp[-25], tsp[-24], tsp[-23],
  174. tsp[-22], tsp[-21], tsp[-20], tsp[-19],
  175. tsp[-18], tsp[-17], tsp[-16], tsp[-15],
  176. tsp[-14], tsp[-13], tsp[-12], tsp[-11],
  177. tsp[-10], tsp[-9], tsp[-8], tsp[-7],
  178. tsp[-6], tsp[-5], tsp[-4], tsp[-3],
  179. tsp[-2], tsp[-1]);
  180. }
  181. }
  182. }
  183. void push_args(va_list a, int nargs)
  184. /*
  185. * The unpacking here must match "apply_lots" as above. For up to
  186. * (and including) ARG_CUT_OFF (=25) args things are passed normally.
  187. * beyond that the first ARG_CUT_OFF-1 args are passed normally, and the
  188. * rest are in a list as a final actual arg. Note that this list will
  189. * have at least two elements.
  190. */
  191. {
  192. int i;
  193. if (nargs <= ARG_CUT_OFF)
  194. { for (i = 0; i<nargs; i++)
  195. { Lisp_Object w = va_arg(a, Lisp_Object);
  196. push(w);
  197. }
  198. }
  199. else
  200. { Lisp_Object x;
  201. for (i = 0; i<(ARG_CUT_OFF-1); i++)
  202. { Lisp_Object w = va_arg(a, Lisp_Object);
  203. push(w);
  204. }
  205. x = va_arg(a, Lisp_Object);
  206. /*
  207. * Internal consistency should ensure that the list passed here is long
  208. * enough for the following unpacking operation. But if (as a result of
  209. * internal system muddles it is not maybe the fact that qcar(nil) =
  210. * qcdr(nil) = nil will tend to reduce the damage?
  211. */
  212. for (; i<nargs; i++)
  213. { push(qcar(x));
  214. x = qcdr(x);
  215. }
  216. }
  217. va_end(a);
  218. }
  219. void push_args_1(va_list a, int nargs)
  220. /*
  221. * This is very much like push_args(), but is for the (rather small number
  222. * of) cases where the first argument to a function must NOT be pushed on the
  223. * stack. See, for instance, "funcall" as an example.
  224. */
  225. {
  226. int i;
  227. if (nargs <= ARG_CUT_OFF)
  228. { for (i = 1; i<nargs; i++)
  229. { Lisp_Object w = va_arg(a, Lisp_Object);
  230. push(w);
  231. }
  232. }
  233. else
  234. { Lisp_Object x;
  235. for (i = 1; i<(ARG_CUT_OFF-1); i++)
  236. { Lisp_Object w = va_arg(a, Lisp_Object);
  237. push(w);
  238. }
  239. x = va_arg(a, Lisp_Object);
  240. for (; i<nargs; i++)
  241. { push(qcar(x));
  242. x = qcdr(x);
  243. }
  244. }
  245. va_end(a);
  246. }
  247. Lisp_Object apply(Lisp_Object fn, int nargs, Lisp_Object env, Lisp_Object name)
  248. /*
  249. * There are (nargs) arguments on the Lisp stack, and apply() must use them
  250. * then pop them off. They were pushed in the order push(arg1); push(arg2),
  251. * and so on, and the stack grows upwards.
  252. * If I return with an error I will hand back the value name rather than the
  253. * junk value normally used in such cases.
  254. */
  255. {
  256. Lisp_Object def, nil = C_nil;
  257. for (;;)
  258. { if (symbolp(fn))
  259. {
  260. def = qenv(fn); /* this is passed as arg1 to the called code */
  261. /*
  262. * apply_lambda() will find arguments on the stack and is responsible for
  263. * popping them before it exits.
  264. */
  265. {
  266. /*
  267. * Because there are nargs values pushed on the (upwards growing) stack,
  268. * &stack[1-nargs] points at the first value pushed, i.e. arg-1. At one stage
  269. * I had a machine-specific bit of code (called "ncall") to do the following,
  270. * arguing that maybe in assembly code it would be possible to do much better
  271. * than the really ugly switch statement shown now. My belief now (especially
  272. * given that ncall was used in just one place - here) is that the switch will
  273. * cost no more than the procedure call did, and that in-line code will help
  274. * speed up the common and critical cases of 0, 1, 2 and 3 args. Also apply
  275. * is otherwise a reasonably short function, so if this switch is needed
  276. * anywhere here is not too bad.
  277. */
  278. push(name);
  279. switch (nargs)
  280. {
  281. /*
  282. * The Standard Lisp Report (Marti et al, Utah UUCS-78-101) only
  283. * requires support for 15 args. Common Lisp requires at least 50.
  284. * I deal with up to 8 args in-line here (I expect more than that to be
  285. * amazingly uncommon) so that this function is kept under contol.
  286. * Calls with more than 8 args go over to apply_lots, and within that
  287. * function calls with over 25 args have an even more clumsy treatment.
  288. */
  289. case 0:
  290. #ifdef DEBUG
  291. if (qfnn(fn) == NULL)
  292. { term_printf("Illegal APPLY\n");
  293. my_exit(EXIT_FAILURE);
  294. }
  295. #endif
  296. def = (*qfnn(fn))(def, 0);
  297. break;
  298. case 1:
  299. #ifdef DEBUG
  300. if (qfn1(fn) == NULL)
  301. { term_printf("Illegal APPLY\n");
  302. my_exit(EXIT_FAILURE);
  303. }
  304. #endif
  305. def = (*qfn1(fn))(def, stack[-1]);
  306. break;
  307. case 2:
  308. #ifdef DEBUG
  309. if (qfn2(fn) == NULL)
  310. { term_printf("Illegal APPLY\n");
  311. my_exit(EXIT_FAILURE);
  312. }
  313. #endif
  314. def = (*qfn2(fn))(def, stack[-2], stack[-1]);
  315. break;
  316. case 3:
  317. #ifdef DEBUG
  318. if (qfnn(fn) == NULL)
  319. { term_printf("Illegal APPLY\n");
  320. my_exit(EXIT_FAILURE);
  321. }
  322. #endif
  323. def = (*qfnn(fn))(def, 3, stack[-3], stack[-2], stack[-1]);
  324. break;
  325. case 4:
  326. #ifdef DEBUG
  327. if (qfnn(fn) == NULL)
  328. { term_printf("Illegal APPLY\n");
  329. my_exit(EXIT_FAILURE);
  330. }
  331. #endif
  332. def = (*qfnn(fn))(def, 4, stack[-4], stack[-3], stack[-2],
  333. stack[-1]);
  334. break;
  335. case 5:
  336. #ifdef DEBUG
  337. if (qfnn(fn) == NULL)
  338. { term_printf("Illegal APPLY\n");
  339. my_exit(EXIT_FAILURE);
  340. }
  341. #endif
  342. def = (*qfnn(fn))(def, 5, stack[-5], stack[-4], stack[-3],
  343. stack[-2], stack[-1]);
  344. break;
  345. case 6:
  346. #ifdef DEBUG
  347. if (qfnn(fn) == NULL)
  348. { term_printf("Illegal APPLY\n");
  349. my_exit(EXIT_FAILURE);
  350. }
  351. #endif
  352. def = (*qfnn(fn))(def, 6, stack[-6], stack[-5], stack[-4],
  353. stack[-3], stack[-2], stack[-1]);
  354. break;
  355. case 7:
  356. #ifdef DEBUG
  357. if (qfnn(fn) == NULL)
  358. { term_printf("Illegal APPLY\n");
  359. my_exit(EXIT_FAILURE);
  360. }
  361. #endif
  362. def = (*qfnn(fn))(def, 7, stack[-7], stack[-6], stack[-5],
  363. stack[-4], stack[-3], stack[-2], stack[-1]);
  364. break;
  365. case 8:
  366. #ifdef DEBUG
  367. if (qfnn(fn) == NULL)
  368. { term_printf("Illegal APPLY\n");
  369. my_exit(EXIT_FAILURE);
  370. }
  371. #endif
  372. def = (*qfnn(fn))(def, 8, stack[-8], stack[-7], stack[-6],
  373. stack[-5], stack[-4], stack[-3], stack[-2],
  374. stack[-1]);
  375. break;
  376. default:
  377. #ifdef DEBUG
  378. if (qfnn(fn) == NULL)
  379. { term_printf("Illegal APPLY\n");
  380. my_exit(EXIT_FAILURE);
  381. }
  382. #endif
  383. def = apply_lots(nargs, qfnn(fn), def);
  384. break;
  385. }
  386. /*
  387. * here I have to pop the stack by hand - note that popv does not
  388. * corrupt exit_count, which tells me how many results were being handed
  389. * back.
  390. */
  391. pop(name);
  392. popv(nargs);
  393. nil = C_nil;
  394. if (exception_pending()) return name;
  395. else return def;
  396. }
  397. }
  398. else if (!is_cons(fn))
  399. { popv(nargs);
  400. push(name);
  401. error(1, err_bad_fn, fn);
  402. pop(name);
  403. return name;
  404. }
  405. /* apply_lambda() will pop the args from the stack when it is done */
  406. if ((def = qcar(fn)) == lambda)
  407. return apply_lambda(qcdr(fn), nargs, env, name);
  408. /*
  409. * A bytecoded funarg is stored as (cfunarg <actual fn> <env>) and any call
  410. * to it behaves as if the actual function was called with the environment
  411. * passed as a forced-in first argument.
  412. */
  413. else if (def == cfunarg)
  414. { int i;
  415. push(nil);
  416. def = qcdr(fn);
  417. fn = qcar(def);
  418. for (i=0; i<nargs; i++) stack[-i] = stack[-i-1];
  419. stack[-nargs] = qcdr(def);
  420. nargs++;
  421. continue;
  422. }
  423. else if (def == funarg)
  424. { def = qcdr(fn);
  425. if (consp(def))
  426. return apply_lambda(qcdr(def), nargs, qcar(def), name);
  427. }
  428. break;
  429. }
  430. /*
  431. * Other cases are all errors.
  432. */
  433. popv(nargs);
  434. push(name);
  435. error(1, err_bad_apply, fn);
  436. pop(name);
  437. return name;
  438. }
  439. /*
  440. * Now for implementation of all the special forms...
  441. */
  442. static Lisp_Object and_fn(Lisp_Object args, Lisp_Object env)
  443. /* also needs to be a macro for Common Lisp */
  444. {
  445. Lisp_Object nil = C_nil;
  446. stackcheck2(0, args, env);
  447. if (!consp(args)) return onevalue(lisp_true);
  448. for (;;)
  449. { Lisp_Object v = qcar(args);
  450. args = qcdr(args);
  451. if (!consp(args)) return eval(v, env);
  452. push2(args, env);
  453. v = eval(v, env);
  454. pop2(env, args);
  455. errexit();
  456. if (v == nil) return onevalue(nil);
  457. }
  458. }
  459. /*
  460. * This is not used at present, but may be wanted sometime so I will
  461. * leave it here for now...
  462. *
  463. Lisp_Object append(Lisp_Object a, Lisp_Object b)
  464. {
  465. Lisp_Object nil = C_nil;
  466. if (!consp(a)) return b;
  467. else
  468. { stackcheck2(0, a, b);
  469. push(a);
  470. b = append(qcdr(a), b);
  471. pop(a);
  472. errexit();
  473. return cons(qcar(a), b);
  474. }
  475. }
  476. */
  477. static Lisp_Object block_fn(Lisp_Object args, Lisp_Object env)
  478. {
  479. Lisp_Object p, nil = C_nil;
  480. if (!consp(args)) return onevalue(nil);
  481. stackcheck2(0, args, env);
  482. push3(qcar(args), /* my_tag */
  483. qcdr(args), /* args */
  484. env);
  485. #define env stack[0]
  486. #define args stack[-1]
  487. #define my_tag stack[-2]
  488. /*
  489. * I need to augment the (lexical) environment with the name of my
  490. * tag in such a way that return-from can throw out to exactly the
  491. * correct matching level. This is done by pushing (0 . tag) onto
  492. * the environment - the 0 marks this as a block name.
  493. */
  494. my_tag = cons(fixnum_of_int(0), my_tag);
  495. errexitn(3);
  496. env = cons(my_tag, env);
  497. errexitn(3);
  498. p = nil;
  499. while (consp(args))
  500. { p = qcar(args);
  501. p = eval(p, env);
  502. /*
  503. * one of the sorts of exit that may be activated by marking nil is
  504. * a return_from. Here I need to check to see if that is what
  505. * is going on.
  506. */
  507. nil = C_nil;
  508. if (exception_pending())
  509. { flip_exception(); /* Temp restore it */
  510. qcar(my_tag) = fixnum_of_int(2); /* Invalidate */
  511. if (exit_reason == UNWIND_RETURN && exit_tag == my_tag)
  512. { exit_reason = UNWIND_NULL; /* not strictly needed - but tidy */
  513. popv(3);
  514. return nvalues(exit_value, exit_count);
  515. }
  516. if ((exit_reason & UNWIND_ERROR) != 0)
  517. { err_printf("\nEvaluating: ");
  518. loop_print_error(qcar(args));
  519. ignore_exception();
  520. }
  521. flip_exception(); /* re-instate exit condition */
  522. popv(3);
  523. return nil;
  524. }
  525. args = qcdr(args);
  526. }
  527. popv(3);
  528. return p;
  529. #undef env
  530. #undef args
  531. #undef my_tag
  532. }
  533. static Lisp_Object catch_fn(Lisp_Object args, Lisp_Object env)
  534. {
  535. Lisp_Object tag, nil = C_nil;
  536. if (!consp(args)) return onevalue(nil);
  537. stackcheck2(0, args, env);
  538. push2(args, env);
  539. tag = qcar(args);
  540. tag = eval(tag, env);
  541. errexit();
  542. tag = catch_tags = cons(tag, catch_tags);
  543. pop2(env, args);
  544. errexit();
  545. push(tag);
  546. {
  547. Lisp_Object v = progn_fn(qcdr(args), env);
  548. pop(tag);
  549. nil = C_nil;
  550. if (exception_pending())
  551. { flip_exception();
  552. catch_tags = qcdr(tag);
  553. qcar(tag) = tag;
  554. qcdr(tag) = nil; /* Invalidate the catch frame */
  555. if (exit_reason == UNWIND_THROW && exit_tag == tag)
  556. { exit_reason = UNWIND_NULL;
  557. return nvalues(exit_value, exit_count);
  558. }
  559. flip_exception();
  560. return nil;
  561. }
  562. catch_tags = qcdr(tag);
  563. qcar(tag) = tag;
  564. qcdr(tag) = nil; /* Invalidate the catch frame */
  565. return v;
  566. }
  567. }
  568. #define BODY_LET 0
  569. #define BODY_COMPILER_LET 1
  570. #define BODY_PROG 2
  571. Lisp_Object let_fn_1(Lisp_Object bvl, Lisp_Object body,
  572. Lisp_Object env, int compilerp)
  573. /*
  574. * This will have to look for (declare (special ...)).
  575. * compiler-let forces all of its bindings to be locally special. In
  576. * CSL mode I do not support local declarations, which simplifies and
  577. * speeds things up here.
  578. */
  579. {
  580. Lisp_Object nil = C_nil;
  581. stackcheck3(0, bvl, body, env);
  582. push3(bvl, body, env);
  583. nil = C_nil;
  584. push5(nil, nil, env, nil, nil);
  585. #ifdef COMMON
  586. /*
  587. * I lose the name (for security) but leave the junk stack location
  588. * (because doing otherwise seems unduly complicated.
  589. */
  590. #define local_decs stack[0]
  591. #endif
  592. #define specenv stack[-1]
  593. #define env1 stack[-2]
  594. #define p stack[-3]
  595. #define q stack[-4]
  596. #define env stack[-5]
  597. #define body stack[-6]
  598. #define bvl stack[-7]
  599. #define Return(v) { popv(8); return (v); }
  600. #ifdef COMMON
  601. /*
  602. * Find local declarations - it is necessary to macro-expand
  603. * items in the body to see if they turn into declarations.
  604. */
  605. for (;;)
  606. { if (exception_pending() || !consp(body)) break;
  607. p = macroexpand(qcar(body), env);
  608. errexitn(8);
  609. body = qcdr(body);
  610. if (!consp(p))
  611. { if (stringp(p) && consp(body)) continue;
  612. body = cons(p, body);
  613. nil = C_nil;
  614. break;
  615. }
  616. if (qcar(p) != declare_symbol)
  617. { body = cons(p, body);
  618. nil = C_nil;
  619. break;
  620. }
  621. for (p = qcdr(p); consp(p); p = qcdr(p))
  622. { q = qcar(p);
  623. if (!consp(q) || qcar(q) != special_symbol) continue;
  624. /* here q says (special ...) */
  625. for (q=qcdr(q); consp(q); q = qcdr(q))
  626. { local_decs = cons(qcar(q), local_decs);
  627. nil = C_nil;
  628. if (exception_pending()) break;
  629. }
  630. if (exception_pending()) break;
  631. }
  632. }
  633. if (exception_pending()) Return(nil);
  634. #endif
  635. for (; consp(bvl); bvl=qcdr(bvl))
  636. { Lisp_Object z;
  637. q = qcar(bvl);
  638. if (consp(q))
  639. { z = qcdr(q);
  640. q = qcar(q);
  641. if (consp(z)) z = qcar(z); else z = nil;
  642. }
  643. else z = nil;
  644. if (!is_symbol(q))
  645. { Lisp_Object qq = q;
  646. Return(error(1, err_bad_bvl, qq));
  647. }
  648. else
  649. {
  650. #ifdef COMMON
  651. Header h = qheader(q);
  652. #endif
  653. if (z != nil)
  654. { z = eval(z, env);
  655. errexitn(8);
  656. }
  657. z = cons(q, z);
  658. errexitn(8);
  659. #ifdef COMMON
  660. if (compilerp == BODY_COMPILER_LET)
  661. { specenv = cons(z, specenv);
  662. errexitn(8);
  663. q = acons(q, work_symbol, env1);
  664. errexitn(8);
  665. env1 = q; /* Locally special */
  666. }
  667. else
  668. #endif
  669. #ifndef COMMON
  670. specenv = cons(z, specenv);
  671. #else
  672. if (h & SYM_SPECIAL_VAR) specenv = cons(z, specenv);
  673. else
  674. {
  675. Lisp_Object w;
  676. for (w = local_decs; w!=nil; w = qcdr(w))
  677. { if (q != qcar(w)) continue;
  678. qcar(w) = fixnum_of_int(0);
  679. /* The next few calls to cons() maybe lose w, but that is OK! */
  680. specenv = cons(z, specenv);
  681. errexitn(8);
  682. q = acons(q, work_symbol, env1);
  683. errexitn(8);
  684. env1 = q;
  685. goto bound;
  686. }
  687. env1 = cons(z, env1);
  688. bound: ;
  689. }
  690. #endif
  691. errexitn(8);
  692. }
  693. }
  694. #ifdef COMMON
  695. while (local_decs!=nil) /* Pervasive special declarations */
  696. { Lisp_Object q1 = qcar(local_decs);
  697. local_decs=qcdr(local_decs);
  698. if (!is_symbol(q1)) continue;
  699. q1 = acons(q1, work_symbol, env1);
  700. errexitn(8);
  701. env1 = q1;
  702. }
  703. #endif
  704. if (specenv == nil)
  705. { Lisp_Object bodyx = body, env1x = env1;
  706. /*
  707. * See expansion of Return() for an explanation of why body and env1 have
  708. * been moved into new local variables before the call..
  709. */
  710. if (compilerp == BODY_PROG)
  711. { Return(tagbody_fn(bodyx, env1x));
  712. }
  713. else
  714. { Return(progn_fn(bodyx, env1x));
  715. }
  716. }
  717. /*
  718. * I instate the special bindings after all values to bind have been collected
  719. */
  720. for (p = specenv; p != nil; p = qcdr(p))
  721. { Lisp_Object w = qcar(p), v = qcar(w), z = qcdr(w);
  722. Lisp_Object old = qvalue(v);
  723. qvalue(v) = z;
  724. qcdr(w) = old;
  725. }
  726. {
  727. if (compilerp == BODY_PROG)
  728. body = tagbody_fn(body, env1);
  729. else body = progn_fn(body, env1);
  730. nil = C_nil;
  731. if (exception_pending())
  732. { flip_exception();
  733. for (p = specenv; p != nil; p = qcdr(p))
  734. { Lisp_Object w = qcar(p), v = qcar(w), z = qcdr(w);
  735. qvalue(v) = z;
  736. }
  737. flip_exception();
  738. Return(nil);
  739. }
  740. else
  741. { for (p = specenv; p != nil; p = qcdr(p))
  742. { Lisp_Object w = qcar(p), v = qcar(w), z = qcdr(w);
  743. qvalue(v) = z;
  744. }
  745. { Lisp_Object bodyx = body;
  746. Return(bodyx);
  747. }
  748. }
  749. }
  750. #ifdef COMMON
  751. #undef local_decs
  752. #endif
  753. #undef specenv
  754. #undef env1
  755. #undef p
  756. #undef q
  757. #undef env
  758. #undef body
  759. #undef bvl
  760. #undef Return
  761. }
  762. #ifdef COMMON
  763. static Lisp_Object compiler_let_fn(Lisp_Object args, Lisp_Object env)
  764. {
  765. Lisp_Object nil = C_nil;
  766. if (!consp(args)) return onevalue(nil);
  767. return let_fn_1(qcar(args), qcdr(args), env, BODY_COMPILER_LET);
  768. }
  769. #endif
  770. static Lisp_Object cond_fn(Lisp_Object args, Lisp_Object env)
  771. {
  772. Lisp_Object nil = C_nil;
  773. stackcheck2(0, args, env);
  774. while (consp(args))
  775. {
  776. Lisp_Object p = qcar(args);
  777. if (consp(p))
  778. { Lisp_Object p1;
  779. push2(args, env);
  780. p1 = qcar(p);
  781. p1 = eval(p1, env);
  782. pop2(env, args);
  783. errexit();
  784. if (p1 != nil)
  785. { args = qcdr(qcar(args));
  786. /* Here I support the case "(cond (predicate) ...)" with no consequents */
  787. if (!consp(args)) return onevalue(p1);
  788. else return progn_fn(args, env);
  789. }
  790. }
  791. args = qcdr(args);
  792. }
  793. return onevalue(nil);
  794. }
  795. #ifdef COMMON
  796. Lisp_Object declare_fn(Lisp_Object args, Lisp_Object env)
  797. /*
  798. * declarations can only properly occur at the heads of various
  799. * special forms, and so may NOT be evaluated in an ordinary manner.
  800. * Thus I am entitled (just about) to make this a no-op. It would
  801. * probably be better to arrange that (declare ...) never got evaluated
  802. * and then I could raise an error if this bit of code got activated.
  803. * Indeed (declare ...) probably does not ever get evaluated - still
  804. * a no-op here seems the safest bet.
  805. */
  806. {
  807. Lisp_Object nil = C_nil;
  808. CSL_IGNORE(env);
  809. CSL_IGNORE(args);
  810. return onevalue(nil);
  811. }
  812. #endif
  813. #define flagged_lose(v) \
  814. ((fv = qfastgets(v)) != nil && elt(fv, 1) != SPID_NOPROP)
  815. static Lisp_Object defun_fn(Lisp_Object args, Lisp_Object env)
  816. {
  817. /*
  818. * defun is eventually expected (required!) to be a macro rather than (maybe
  819. * as well as?) a special form. For bootstrap purposes it seems useful to
  820. * build it in as a special form. Also this special form is quite good enough
  821. * in CSL mode
  822. */
  823. Lisp_Object fname, nil = C_nil;
  824. CSL_IGNORE(env);
  825. if (consp(args))
  826. { fname = qcar(args);
  827. args = qcdr(args);
  828. if (is_symbol(fname))
  829. { Lisp_Object fv;
  830. if (qheader(fname) & SYM_SPECIAL_FORM)
  831. return error(1, err_redef_special, fname);
  832. if ((qheader(fname) & (SYM_C_DEF | SYM_CODEPTR)) ==
  833. (SYM_C_DEF | SYM_CODEPTR)) return onevalue(fname);
  834. if (flagged_lose(fname))
  835. { debug_printf("\n+++ ");
  836. loop_print_debug(fname);
  837. debug_printf(" not defined because of LOSE flag\n");
  838. return onevalue(nil);
  839. }
  840. qheader(fname) = qheader(fname) & ~SYM_MACRO;
  841. if ((qheader(fname) & SYM_C_DEF) != 0) lose_C_def(fname);
  842. if (qfn1(fname) != undefined1)
  843. { if (qvalue(redef_msg) != nil)
  844. { debug_printf("\n+++ ");
  845. loop_print_debug(fname);
  846. debug_printf(" redefined\n");
  847. }
  848. errexit();
  849. set_fns(fname, undefined1, undefined2, undefinedn);
  850. qenv(fname) = fname;
  851. }
  852. /*
  853. * qfn() can contain 'interpreted' for a function defined wrt the null
  854. * environment, or 'funarged' for one with an environment - in the latter
  855. * case the definition (in qenv()) is a pair (<def> . <env>)
  856. */
  857. qenv(fname) = args; /* Sort of notional lambda present */
  858. set_fns(fname, interpreted1, interpreted2, interpretedn);
  859. if (qvalue(comp_symbol) != nil &&
  860. qfn1(compiler_symbol) != undefined1)
  861. { push(fname);
  862. args = ncons(fname);
  863. nil = C_nil;
  864. if (!exception_pending())
  865. (qfn1(compiler_symbol))(qenv(compiler_symbol), args);
  866. pop(fname);
  867. }
  868. return onevalue(fname);
  869. }
  870. }
  871. return aerror("defun");
  872. }
  873. static Lisp_Object defmacro_fn(Lisp_Object args, Lisp_Object env)
  874. {
  875. /*
  876. * defmacro is eventually expected (required!) to be a macro rather than (maybe
  877. * as well as?) a special form. For bootstrap purposes it seems useful to
  878. * build it in as a special form.
  879. */
  880. Lisp_Object fname, nil = C_nil;
  881. CSL_IGNORE(env);
  882. if (consp(args))
  883. { fname = qcar(args);
  884. args = qcdr(args);
  885. if (is_symbol(fname))
  886. {
  887. if ((qheader(fname) & (SYM_C_DEF | SYM_CODEPTR)) ==
  888. (SYM_C_DEF | SYM_CODEPTR)) return onevalue(fname);
  889. qheader(fname) |= SYM_MACRO;
  890. /*
  891. * Note that a name can have a definition as a macro and as a special form,
  892. * and in that case the qfn() cell gives the special form and the qenv()
  893. * cell the macro definition. Otherwise at present I put 'undefined'
  894. * in the qfn() cell, but in due course I will want something else as better
  895. * protection against compiled code improperly attempting to call a macro.
  896. * Note also that if the symbol was a special form before I do not want
  897. * to clear the C_DEF flag, since the special form must be re-instated when
  898. * I reload the system.
  899. */
  900. if ((qheader(fname) & SYM_SPECIAL_FORM) == 0)
  901. { qheader(fname) &= ~SYM_C_DEF;
  902. if (qfn1(fname) != undefined1 &&
  903. qvalue(redef_msg) != nil)
  904. { debug_printf("\n+++ ");
  905. loop_print_debug(fname);
  906. debug_printf(" redefined as a macro\n");
  907. errexit();
  908. }
  909. set_fns(fname, undefined1, undefined2, undefinedn);
  910. }
  911. qenv(fname) = args; /* Sort of notional lambda present */
  912. if (qvalue(comp_symbol) != nil &&
  913. qfn1(compiler_symbol) != undefined1)
  914. { Lisp_Object t1, t2;
  915. push(fname);
  916. if (!(consp(args) &&
  917. consp(qcdr(args)) &&
  918. qcdr(qcdr(args)) == nil &&
  919. (t1 = qcar(args),
  920. t2 = qcdr(qcar(qcdr(args))),
  921. equal(t1, t2))))
  922. { errexitn(1);
  923. fname = stack[0];
  924. args = ncons(fname);
  925. nil = C_nil;
  926. if (!exception_pending())
  927. (qfn1(compiler_symbol))(qenv(compiler_symbol), args);
  928. }
  929. pop(fname);
  930. errexit();
  931. }
  932. return onevalue(fname);
  933. }
  934. }
  935. return aerror("defmacro");
  936. }
  937. static Lisp_Object eval_when_fn(Lisp_Object args, Lisp_Object env)
  938. /*
  939. * When interpreted, eval-when just looks for the situation EVAL.
  940. */
  941. {
  942. Lisp_Object situations, nil = C_nil;
  943. if (!consp(args)) return onevalue(nil);
  944. situations = qcar(args);
  945. args = qcdr(args);
  946. while (consp(situations))
  947. { if (qcar(situations) == eval_symbol) return progn_fn(args, env);
  948. situations = qcdr(situations);
  949. }
  950. return onevalue(nil);
  951. }
  952. #ifdef COMMON
  953. static Lisp_Object flet_fn(Lisp_Object args, Lisp_Object env)
  954. {
  955. Lisp_Object my_env, d, nil = C_nil;
  956. if (!consp(args)) return onevalue(nil);
  957. stackcheck2(0, args, env);
  958. my_env = env;
  959. d = qcar(args); /* The bunch of definitions */
  960. args = qcdr(args);
  961. nil = C_nil;
  962. while (consp(d))
  963. { Lisp_Object w = qcar(d);
  964. if (consp(w) && consp(qcdr(w)))
  965. { Lisp_Object w1;
  966. push4(args, d, env, w);
  967. w1 = list2star(funarg, my_env, qcdr(w));
  968. pop(w);
  969. nil = C_nil;
  970. if (!exception_pending()) w1 = cons(w1, qcar(w));
  971. pop(env);
  972. nil = C_nil;
  973. if (!exception_pending()) env = cons(w1, env);
  974. pop2(d, args);
  975. errexit();
  976. }
  977. d = qcdr(d);
  978. }
  979. /*
  980. * Treat body as (let nil ...) to get (declare ...) recognized.
  981. */
  982. return let_fn_1(nil, args, env, BODY_LET);
  983. }
  984. #endif
  985. Lisp_Object function_fn(Lisp_Object args, Lisp_Object env)
  986. {
  987. /*
  988. * For most things this behaves just like (quote xxx), but
  989. * (function (lambda (x) y)) gets converted to
  990. * (funarg env (x) y).
  991. */
  992. Lisp_Object nil = C_nil;
  993. if (consp(args) && qcdr(args) == nil)
  994. { args = qcar(args);
  995. if (consp(args) && qcar(args) == lambda)
  996. args = list2star(funarg, env, qcdr(args));
  997. return onevalue(args);
  998. }
  999. return aerror("function");
  1000. }
  1001. static Lisp_Object go_fn(Lisp_Object args, Lisp_Object env)
  1002. {
  1003. Lisp_Object p, tag, nil = C_nil;
  1004. CSL_IGNORE(env);
  1005. if (!consp(args)) return aerror("go");
  1006. else tag = qcar(args);
  1007. for(p=env; consp(p); p=qcdr(p))
  1008. { Lisp_Object w = qcar(p), z;
  1009. if (!consp(w)) continue;
  1010. if (qcar(w) == fixnum_of_int(1) &&
  1011. (z = qcar(qcdr(w)), eql(z, tag)))
  1012. { p = w;
  1013. goto tag_found;
  1014. }
  1015. }
  1016. return error(1, err_go_tag, tag);
  1017. tag_found:
  1018. exit_tag = p;
  1019. exit_count = 0;
  1020. exit_reason = UNWIND_GO;
  1021. flip_exception(); /* Exceptional exit active */
  1022. return nil;
  1023. }
  1024. static Lisp_Object if_fn(Lisp_Object args, Lisp_Object env)
  1025. {
  1026. Lisp_Object nil = C_nil;
  1027. Lisp_Object p=nil, tr=nil, fs=nil;
  1028. if (!consp(args)) return aerror("if");
  1029. p = qcar(args);
  1030. args = qcdr(args);
  1031. if (!consp(args)) return aerror("if");
  1032. tr = qcar(args);
  1033. args = qcdr(args);
  1034. if (!consp(args)) fs = nil;
  1035. else
  1036. { fs = qcar(args);
  1037. args = qcdr(args);
  1038. if (args != nil) return aerror("if");
  1039. }
  1040. stackcheck4(0, p, env, tr, fs);
  1041. push3(fs, tr, env);
  1042. p = eval(p, env);
  1043. pop3(env, tr, fs);
  1044. errexit();
  1045. if (p == nil)
  1046. return eval(fs, env); /* tail call on result */
  1047. else return eval(tr, env); /* ... passing back values */
  1048. }
  1049. #ifdef COMMON
  1050. static Lisp_Object labels_fn(Lisp_Object args, Lisp_Object env)
  1051. {
  1052. Lisp_Object my_env, d, nil = C_nil;
  1053. if (!consp(args)) return onevalue(nil);
  1054. stackcheck2(0, args, env);
  1055. my_env = env;
  1056. d = qcar(args); /* The bunch of definitions */
  1057. while (consp(d))
  1058. { Lisp_Object w = qcar(d);
  1059. if (consp(w) && consp(qcdr(w)))
  1060. { Lisp_Object w1;
  1061. push4(args, d, env, w);
  1062. w1 = list2star(funarg, nil, qcdr(w));
  1063. pop(w);
  1064. nil = C_nil;
  1065. if (!exception_pending()) w1 = cons(w1, qcar(w));
  1066. pop(env);
  1067. nil = C_nil;
  1068. if (!exception_pending()) env = cons(w1, env);
  1069. pop2(d, args);
  1070. errexit();
  1071. }
  1072. d = qcdr(d);
  1073. }
  1074. /*
  1075. * Now patch up the environments stored with the local defs so as to
  1076. * permit mutual recursion between them all.
  1077. */
  1078. for (d=env; d!=my_env; d=qcdr(d))
  1079. qcar(qcdr(qcar(qcar(d)))) = env;
  1080. return let_fn_1(nil, qcdr(args), env, BODY_LET);
  1081. }
  1082. #endif
  1083. static Lisp_Object let_fn(Lisp_Object args, Lisp_Object env)
  1084. {
  1085. Lisp_Object nil = C_nil;
  1086. if (!consp(args)) return onevalue(nil);
  1087. return let_fn_1(qcar(args), qcdr(args), env, BODY_LET);
  1088. }
  1089. static Lisp_Object letstar_fn(Lisp_Object args, Lisp_Object env)
  1090. /*
  1091. * This will have to look for (declare (special ...)), unless
  1092. * I am in CSL mode.
  1093. */
  1094. {
  1095. Lisp_Object nil = C_nil;
  1096. if (!consp(args)) return onevalue(nil);
  1097. stackcheck2(0, args, env);
  1098. push3(qcar(args), qcdr(args), env);
  1099. nil = C_nil;
  1100. push5(nil, nil, /* p, q */
  1101. env, nil, nil); /* env1, specenv, local_decs */
  1102. #ifdef COMMON
  1103. #define local_decs stack[0]
  1104. #endif
  1105. #define specenv stack[-1]
  1106. #define env1 stack[-2]
  1107. #define p stack[-3]
  1108. #define q stack[-4]
  1109. #define env stack[-5]
  1110. #define body stack[-6]
  1111. #define bvl stack[-7]
  1112. #define Return(v) { popv(8); return (v); }
  1113. #ifdef COMMON
  1114. for (;;)
  1115. { if (exception_pending() || !consp(body)) break;
  1116. p = macroexpand(qcar(body), env);
  1117. errexitn(8);
  1118. body = qcdr(body);
  1119. if (!consp(p))
  1120. { if (stringp(p) && consp(body)) continue;
  1121. body = cons(p, body);
  1122. nil = C_nil;
  1123. break;
  1124. }
  1125. if (qcar(p) != declare_symbol)
  1126. { body = cons(p, body);
  1127. nil = C_nil;
  1128. break;
  1129. }
  1130. for (p = qcdr(p); consp(p); p = qcdr(p))
  1131. { q = qcar(p);
  1132. if (!consp(q) || qcar(q) != special_symbol) continue;
  1133. /* here q says (special ...) */
  1134. for (q=qcdr(q); consp(q); q = qcdr(q))
  1135. { local_decs = cons(qcar(q), local_decs);
  1136. nil = C_nil;
  1137. if (exception_pending()) break;
  1138. }
  1139. if (exception_pending()) break;
  1140. }
  1141. }
  1142. if (exception_pending()) Return(nil);
  1143. #endif
  1144. for (; consp(bvl); bvl=qcdr(bvl))
  1145. { Lisp_Object z;
  1146. q = qcar(bvl);
  1147. if (consp(q))
  1148. { z = qcdr(q);
  1149. q = qcar(q);
  1150. if (consp(z)) z = qcar(z); else z = nil;
  1151. }
  1152. else z = nil;
  1153. if (!is_symbol(q))
  1154. { error(1, err_bad_bvl, q);
  1155. goto unwind_special_bindings;
  1156. }
  1157. else
  1158. {
  1159. #ifdef COMMON
  1160. Header h = qheader(q);
  1161. #endif
  1162. if (z != nil)
  1163. { z = eval(z, env);
  1164. nil = C_nil;
  1165. if (exception_pending()) goto unwind_special_bindings;
  1166. }
  1167. #ifndef COMMON
  1168. p = z;
  1169. z = acons(q, qvalue(q), specenv);
  1170. nil = C_nil;
  1171. if (!exception_pending()) specenv = z;
  1172. qvalue(q) = p;
  1173. #else
  1174. if (h & SYM_SPECIAL_VAR)
  1175. {
  1176. p = z;
  1177. z = acons(q, qvalue(q), specenv);
  1178. nil = C_nil;
  1179. if (!exception_pending()) specenv = z;
  1180. qvalue(q) = p;
  1181. }
  1182. else
  1183. {
  1184. for (p = local_decs; p!=nil; p = qcdr(p))
  1185. { Lisp_Object w;
  1186. if (q != qcar(p)) continue;
  1187. qcar(p) = fixnum_of_int(0);
  1188. w = acons(q, qvalue(q), specenv);
  1189. nil = C_nil;
  1190. if (exception_pending()) goto unwind_special_bindings;
  1191. specenv = w;
  1192. w = acons(q, work_symbol, env);
  1193. nil = C_nil;
  1194. if (exception_pending()) goto unwind_special_bindings;
  1195. env = w;
  1196. qvalue(q) = z;
  1197. goto bound;
  1198. }
  1199. q = acons(q, z, env);
  1200. nil = C_nil;
  1201. if (exception_pending()) goto unwind_special_bindings;
  1202. env = q;
  1203. bound: ;
  1204. }
  1205. #endif
  1206. nil = C_nil;
  1207. if (exception_pending()) goto unwind_special_bindings;
  1208. }
  1209. }
  1210. #ifdef COMMON
  1211. while (local_decs!=nil) /* Pervasive special declarations */
  1212. { q = qcar(local_decs);
  1213. local_decs=qcdr(local_decs);
  1214. if (!is_symbol(q)) continue;
  1215. q = acons(q, work_symbol, env);
  1216. nil = C_nil;
  1217. if (!exception_pending()) env = q;
  1218. else goto unwind_special_bindings;
  1219. }
  1220. #endif
  1221. if (specenv == nil)
  1222. { Lisp_Object bodyx = body, envx = env;
  1223. Return(progn_fn(bodyx, envx)); /* beware Return macro! */
  1224. }
  1225. {
  1226. body = progn_fn(body, env);
  1227. nil = C_nil;
  1228. if (exception_pending()) goto unwind_special_bindings;
  1229. for (bvl = specenv; bvl != nil; bvl = qcdr(bvl))
  1230. { Lisp_Object w = qcar(bvl), v = qcar(w), z = qcdr(w);
  1231. qvalue(v) = z;
  1232. }
  1233. { Lisp_Object bodyx = body;
  1234. Return(bodyx);
  1235. }
  1236. }
  1237. unwind_special_bindings:
  1238. flip_exception();
  1239. for (bvl = specenv; bvl != nil; bvl = qcdr(bvl))
  1240. { Lisp_Object w = qcar(bvl), v = qcar(w), z = qcdr(w);
  1241. qvalue(v) = z;
  1242. }
  1243. flip_exception();
  1244. popv(8);
  1245. return nil;
  1246. #ifdef COMMON
  1247. #undef local_decs
  1248. #endif
  1249. #undef specenv
  1250. #undef env1
  1251. #undef p
  1252. #undef q
  1253. #undef env
  1254. #undef body
  1255. #undef bvl
  1256. #undef Return
  1257. }
  1258. setup_type const eval2_setup[] =
  1259. /*
  1260. * A jolly curiosity - "function" and "declare" are ALSO set up in
  1261. * restart.c (because handles are needed on the symbols). I leave
  1262. * the redundant initialisation here too since I find it clearer that
  1263. * way.
  1264. */
  1265. {
  1266. {"and", and_fn, bad_special2, bad_specialn},
  1267. {"catch", catch_fn, bad_special2, bad_specialn},
  1268. {"cond", cond_fn, bad_special2, bad_specialn},
  1269. /*
  1270. * I am not over-enthusiastic about supporting eval-when in CSL, but
  1271. * something of that sort seems needed by some bits of code that I have
  1272. * come across...
  1273. */
  1274. {"eval-when", eval_when_fn, bad_special2, bad_specialn},
  1275. {"function", function_fn, bad_special2, bad_specialn},
  1276. {"go", go_fn, bad_special2, bad_specialn},
  1277. {"if", if_fn, bad_special2, bad_specialn},
  1278. {"let*", letstar_fn, bad_special2, bad_specialn},
  1279. /* DE and DM are used as low level primitives in the Common Lisp bootstrap */
  1280. {"de", defun_fn, bad_special2, bad_specialn},
  1281. {"dm", defmacro_fn, bad_special2, bad_specialn},
  1282. #ifdef COMMON
  1283. {"block", block_fn, bad_special2, bad_specialn},
  1284. {"compiler-let", compiler_let_fn, bad_special2, bad_specialn},
  1285. {"declare", declare_fn, bad_special2, bad_specialn},
  1286. {"flet", flet_fn, bad_special2, bad_specialn},
  1287. {"labels", labels_fn, bad_special2, bad_specialn},
  1288. {"let", let_fn, bad_special2, bad_specialn},
  1289. #else
  1290. {"~block", block_fn, bad_special2, bad_specialn},
  1291. {"~let", let_fn, bad_special2, bad_specialn},
  1292. #endif
  1293. {NULL, 0, 0, 0}};
  1294. /* end of eval2.c */