eval1.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /* eval1.c Copyright (C) 1989-2002 Codemist Ltd */
  2. /*
  3. * Interpreter (part 1).
  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: 7b09cda9 10-Oct-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. Lisp_Object nreverse(Lisp_Object a)
  29. {
  30. Lisp_Object nil = C_nil;
  31. Lisp_Object b = nil;
  32. while (consp(a))
  33. { Lisp_Object c = a;
  34. a = qcdr(a);
  35. qcdr(c) = b;
  36. b = c;
  37. }
  38. return b;
  39. }
  40. /*
  41. * Environments are represented as association lists, and have to cope
  42. * with several sorts of things. The items in an environment can be
  43. * in one of the following forms:
  44. *
  45. * (a) (symbol . value) normal lexical variable binding
  46. * (b) (symbol . ~magic~) given symbol is (locally) special
  47. * (c) (0 . tag) (block tag ...) marker
  48. * (d) (1 . (tag ...)) (tagbody ... tag ...) marker
  49. * (e) (2 . <anything>) case (c) or (d) but now invalidated
  50. * (f) (def . symbol) (flet ...) or (macrolet ...) binding,
  51. * where the def is non-atomic.
  52. *
  53. * Format for def in case (f)
  54. *
  55. * (1) (funarg env bvl ...) flet and labels
  56. * (2) (bvl ...) macrolet
  57. * Note that 'funarg is not valid as a bvl
  58. * and indeed in this case bvl is a list
  59. */
  60. /*
  61. * In CSL mode flet, macrolet and local declarations are not supported.
  62. */
  63. Lisp_Object Ceval(Lisp_Object u, Lisp_Object env)
  64. {
  65. Lisp_Object nil = C_nil;
  66. #ifdef COMMON
  67. int t;
  68. #ifdef CHECK_STACK
  69. if (check_stack(__FILE__,__LINE__)) return aerror("deep stack in eval");
  70. #endif
  71. restart:
  72. t = (int)u & TAG_BITS;
  73. /*
  74. * The first case considered is of symbols - lexical and special bindings
  75. * have to be sorted out.
  76. */
  77. if (t == TAG_SYMBOL)
  78. {
  79. Header h = qheader(u);
  80. if (h & SYM_SPECIAL_VAR)
  81. { Lisp_Object v = qvalue(u);
  82. if (v == unset_var) return error(1, err_unset_var, u);
  83. else return onevalue(v);
  84. }
  85. else
  86. {
  87. while (env != nil)
  88. { Lisp_Object p = qcar(env);
  89. if (qcar(p) == u)
  90. { Lisp_Object v =qcdr(p);
  91. /*
  92. * If a variable is lexically bound to the value work_symbol that means
  93. * that the symbol has been (lexically) declared to be special, so its
  94. * value cell should be inspected.
  95. */
  96. if (v == work_symbol)
  97. { v = qvalue(u);
  98. if (v == unset_var) return error(1, err_unset_var, u);
  99. }
  100. return onevalue(v);
  101. }
  102. env = qcdr(env);
  103. }
  104. #ifdef ARTHURS_ORIGINAL_INTERPRETATION
  105. return error(1, err_unbound_lexical, u);
  106. #else
  107. { Lisp_Object v = qvalue(u);
  108. if (v == unset_var) return error(1, err_unset_var, u);
  109. else return onevalue(v);
  110. }
  111. #endif
  112. }
  113. }
  114. /*
  115. * Things that are neither symbols nor lists evaluate to themselves,
  116. * e.g. numbers and vectors.
  117. */
  118. else if (t != TAG_CONS) return onevalue(u);
  119. else
  120. #endif /* COMMON */
  121. {
  122. /*
  123. * The final case is that of a list (fn ...), and one case that has to
  124. * be checked is if fn is lexically bound.
  125. */
  126. Lisp_Object fn, args;
  127. #ifdef COMMON
  128. /*
  129. * The test for nil here is because although nil is a symbol the tagging
  130. * structure tested here marks it as a list.
  131. */
  132. if (u == nil) return onevalue(nil);
  133. #endif
  134. stackcheck2(0, u, env);
  135. fn = qcar(u);
  136. args = qcdr(u);
  137. #ifdef COMMON
  138. /*
  139. * Local function bindings must be looked for first.
  140. */
  141. { Lisp_Object p;
  142. for (p=env; p!=nil; p=qcdr(p))
  143. { Lisp_Object w = qcar(p);
  144. /*
  145. * The form (<list> . sym) is used in an environment to indicate a local
  146. * binding of a function, either as a regular function or as a macro
  147. * (i.e. flet or macrolet). The structure of the list distinguishes
  148. * between these two cases.
  149. */
  150. if (qcdr(w) == fn && is_cons(w = qcar(w)) && w!=nil)
  151. {
  152. p = qcar(w);
  153. if (p == funarg) /* ordinary function */
  154. { fn = w; /* (funarg ...) is OK to apply */
  155. goto ordinary_function;
  156. }
  157. /*
  158. * Here it is a local macro. Observe that the macroexpansion is done
  159. * with respect to an empty environment. Macros that are defined at the same
  160. * time may seem to be mutually recursive but there is a sense in which they
  161. * are not (as well as a sense in which they are) - self and cross references
  162. * only happen AFTER an expansion and can not happen during one.
  163. */
  164. push2(u, env);
  165. w = cons(lambda, w);
  166. nil = C_nil;
  167. if (!exception_pending())
  168. p = Lfuncalln(nil, 4, qvalue(macroexpand_hook),
  169. w, u, nil);
  170. pop2(env, u);
  171. nil = C_nil;
  172. if (exception_pending())
  173. { flip_exception();
  174. if ((exit_reason & UNWIND_ERROR) != 0)
  175. { err_printf("\nMacroexpanding: ");
  176. loop_print_error(u);
  177. nil = C_nil;
  178. if (exception_pending()) flip_exception();
  179. }
  180. flip_exception();
  181. return nil;
  182. }
  183. u = p;
  184. goto restart;
  185. }
  186. }
  187. }
  188. #endif
  189. if (is_symbol(fn))
  190. {
  191. /*
  192. * Special forms and macros are checked for next. Special forms
  193. * take precedence over macros.
  194. */
  195. Header h = qheader(fn);
  196. if (h & SYM_SPECIAL_FORM)
  197. { Lisp_Object v;
  198. #ifdef DEBUG
  199. if (qfn1(fn) == NULL)
  200. { term_printf("Illegal special form\n");
  201. my_exit(EXIT_FAILURE);
  202. }
  203. #endif
  204. v = ((Special_Form *)qfn1(fn))(args, env);
  205. return v;
  206. }
  207. else if (h & SYM_MACRO)
  208. {
  209. push2(u, env);
  210. /*
  211. * the environment passed to macroexpand should only be needed to cope
  212. * with macrolet, I think. Since I use just one datastructure for the
  213. * whole environment I also pass along lexical bindings etc, but I hope that
  214. * they will never be accessed. I do not think that macrolet is important
  215. * enough to call for complication and slow-down in the interpreter this
  216. * way - but then I am not exactly what you would call a Common Lisp Fan!
  217. */
  218. fn = macroexpand(u, env);
  219. pop2(env, u);
  220. nil = C_nil;
  221. if (exception_pending())
  222. { flip_exception();
  223. if ((exit_reason & UNWIND_ERROR) != 0)
  224. { err_printf("\nMacroexpanding: ");
  225. loop_print_error(u);
  226. nil = C_nil;
  227. if (exception_pending()) flip_exception();
  228. }
  229. flip_exception();
  230. return nil;
  231. }
  232. return eval(fn, env);
  233. }
  234. }
  235. /*
  236. * Otherwise we have a regular function call. I prepare the args and
  237. * call APPLY.
  238. */
  239. #ifdef COMMON
  240. ordinary_function:
  241. #endif
  242. { int nargs = 0;
  243. Lisp_Object *save_stack = stack;
  244. /*
  245. * Args are built up on the stack here...
  246. */
  247. while (consp(args))
  248. { Lisp_Object w;
  249. push3(fn, args, env);
  250. w = qcar(args);
  251. w = eval(w, env);
  252. pop3(env, args, fn);
  253. /*
  254. * nil having its mark bit set indicates that a special sort of exit
  255. * is in progress. Multiple values can be ignored in this case.
  256. */
  257. nil = C_nil;
  258. if (exception_pending())
  259. { flip_exception();
  260. stack = save_stack;
  261. if ((exit_reason & UNWIND_ERROR) != 0)
  262. { err_printf("\nEvaluating: ");
  263. loop_print_error(qcar(args));
  264. nil = C_nil;
  265. if (exception_pending()) flip_exception();
  266. }
  267. flip_exception();
  268. return nil;
  269. }
  270. push(w); /* args build up on the Lisp stack */
  271. nargs++;
  272. args = qcdr(args);
  273. }
  274. /*
  275. * I pass the environment down to apply() because it will be used if the
  276. * function was a simple lambda expression. If the function is a symbol
  277. * or a closure, env will be irrelevant. The arguments are on the Lisp
  278. * stack, and it is the responsibility of apply() to pop them.
  279. */
  280. return apply(fn, nargs, env, fn);
  281. }
  282. }
  283. }
  284. #ifdef COMMON
  285. /*
  286. * Keyword arguments are not supported in CSL mode - but &optional
  287. * and &rest and &aux will be (at least for now). Removal of
  288. * support for keywords will save a little space and an even smaller
  289. * amount of time.
  290. */
  291. static CSLbool check_no_unwanted_keys(Lisp_Object restarg, Lisp_Object ok_keys)
  292. /*
  293. * verify that there were no unwanted keys in the actual arg list
  294. */
  295. {
  296. Lisp_Object nil = C_nil;
  297. CSLbool odd_key_found = NO;
  298. while (restarg!=nil)
  299. { Lisp_Object k = qcar(restarg);
  300. Lisp_Object w;
  301. for (w=ok_keys; w!=nil; w=qcdr(w))
  302. if (k == qcar(w)) goto is_ok;
  303. odd_key_found = YES;
  304. is_ok:
  305. restarg = qcdr(restarg);
  306. if (restarg==nil) return YES; /* odd length list */
  307. if (k == allow_key_key && qcar(restarg) != nil) return NO; /* OK */
  308. restarg = qcdr(restarg);
  309. }
  310. return odd_key_found;
  311. }
  312. static CSLbool check_keyargs_even(Lisp_Object restarg)
  313. /*
  314. * check that list is even length with alternate items symbols in
  315. * the keyword package.
  316. */
  317. {
  318. Lisp_Object nil = C_nil;
  319. while (restarg!=nil)
  320. { Lisp_Object q = qcar(restarg);
  321. if (!is_symbol(q) || qpackage(q) != qvalue(keyword_package)) return YES;
  322. restarg = qcdr(restarg);
  323. if (restarg==nil) return YES; /* Odd length is wrong */
  324. restarg = qcdr(restarg);
  325. }
  326. return NO; /* OK */
  327. }
  328. static Lisp_Object keywordify(Lisp_Object v)
  329. {
  330. /*
  331. * arg is a non-nil symbol. Should nil be permitted - I think not
  332. * since there seems too much chance of confusion.
  333. */
  334. Lisp_Object nil, name = get_pname(v);
  335. errexit();
  336. return Lintern_2(nil, name, qvalue(keyword_package));
  337. }
  338. static Lisp_Object key_lookup(Lisp_Object keyname, Lisp_Object args)
  339. {
  340. Lisp_Object nil = C_nil;
  341. while (args!=nil)
  342. { Lisp_Object next = qcdr(args);
  343. if (next==nil) return nil;
  344. if (qcar(args) == keyname) return next;
  345. else args = qcdr(next);
  346. }
  347. return nil;
  348. }
  349. #endif
  350. Lisp_Object apply_lambda(Lisp_Object def, int nargs,
  351. Lisp_Object env, Lisp_Object name)
  352. /*
  353. * Here def is a lambda expression (sans the initial lambda) that is to
  354. * be applied. Much horrible messing about is needed so that I can cope
  355. * with &optional and &rest args (including initialisers and supplied-p
  356. * variables, also &key, &allow-other-keys and &aux). Note the need to find
  357. * any special declarations at the head of the body of the lambda-form.
  358. * Must pop (nargs) items from the stack at exit.
  359. */
  360. {
  361. /*
  362. * lambda-lists are parsed using a finite state engine with the
  363. * following states, plus an exit state.
  364. */
  365. #define STATE_NULL 0 /* at start and during regular args */
  366. #define STATE_OPT 1 /* after &optional */
  367. #define STATE_OPT1 2 /* after &optional + at least one var */
  368. #define STATE_REST 3 /* immediately after &rest */
  369. #define STATE_REST1 4 /* after &rest vv */
  370. #ifdef COMMON
  371. #define STATE_KEY 5 /* &key with no &rest */
  372. #define STATE_ALLOW 6 /* &allow-other-keys */
  373. #endif
  374. #define STATE_AUX 7 /* &aux */
  375. Lisp_Object nil = C_nil;
  376. int opt_rest_state = STATE_NULL;
  377. Lisp_Object *next_arg;
  378. int args_left = nargs;
  379. Lisp_Object w;
  380. if (!consp(def))
  381. { popv(nargs);
  382. return onevalue(nil); /* Should never happen */
  383. }
  384. stackcheck3(0, def, env, name);
  385. w = qcar(def);
  386. next_arg = &stack[1-nargs]; /* Points to arg1 */
  387. push4(w, /* bvl */
  388. qcdr(def), /* body */
  389. env, name);
  390. /*
  391. * Here I need to macroexpand the first few items in body and
  392. * look for declare/special items. I will only bother with SPECIAL decls.
  393. * Note that args have been pushed onto the stack first to avoid corruption
  394. * while the interpreter performs macroexpansion. This is the sort of place
  395. * where I feel that Common Lisp has built in causes of inefficiency.
  396. * Well oh well!!! The Common Lisp standardisation group thought so too,
  397. * and have now indicated that DECLARE forms can not be hidden away as
  398. * the result of macros, so some of this is unnecessary.
  399. */
  400. push5(nil, nil, /* local_decs, ok_keys */
  401. nil, nil, nil); /* restarg, specenv, val1 */
  402. push5(nil, nil, /* arg, v1 */
  403. nil, nil, nil); /* v, p, w */
  404. /*
  405. * On computers which have unsigned offsets in indexed memory reference
  406. * instructions the negative indexes off the stack suggested here might
  407. * be more expensive than I would like - maybe on such machines the stack
  408. * pointer should be kept offset by 64 bytes (say). Doing so in general
  409. * would be to the disadvantage of machines with auto-index address modes
  410. * that might be used when pushing/popping single items on the stack.
  411. */
  412. #define w stack[0]
  413. #define p stack[-1]
  414. #define v stack[-2]
  415. #define v1 stack[-3]
  416. #define arg stack[-4]
  417. #define val1 stack[-5]
  418. #define specenv stack[-6]
  419. #define restarg stack[-7]
  420. #ifdef COMMON
  421. #define ok_keys stack[-8]
  422. #define local_decs stack[-9]
  423. #endif
  424. #define name stack[-10]
  425. #define env stack[-11]
  426. #define body stack[-12]
  427. #define bvl stack[-13]
  428. #define arg1 stack[-14]
  429. #define stack_used ((int)(nargs + 14))
  430. #ifdef COMMON
  431. for (;;)
  432. { if (!consp(body)) break;
  433. p = macroexpand(qcar(body), env);
  434. nil = C_nil;
  435. if (exception_pending())
  436. { Lisp_Object qname = name;
  437. popv(stack_used);
  438. return qname;
  439. }
  440. body = qcdr(body);
  441. if (!consp(p))
  442. { if (stringp(p) && consp(body)) continue;
  443. body = cons(p, body);
  444. break;
  445. }
  446. if (qcar(p) != declare_symbol)
  447. { body = cons(p, body);
  448. break;
  449. }
  450. for (v = qcdr(v); consp(v); v = qcdr(v))
  451. { v1 = qcar(v);
  452. if (!consp(v1) || qcar(v1) != special_symbol) continue;
  453. /* here v1 says (special ...) */
  454. for (v1=qcdr(v1); consp(v1); v1 = qcdr(v1))
  455. { local_decs = cons(qcar(v1), local_decs);
  456. if (exception_pending()) break;
  457. }
  458. }
  459. }
  460. nil = C_nil;
  461. if (exception_pending())
  462. { Lisp_Object qname = name;
  463. popv(stack_used);
  464. return qname;
  465. }
  466. #endif
  467. /*
  468. * Parse the BVL
  469. */
  470. for (p = bvl; consp(p); p=qcdr(p))
  471. { v = qcar(p);
  472. v1 = nil;
  473. arg = nil;
  474. val1 = nil;
  475. /*
  476. * I can break from this switch statement with v a variable to bind
  477. * and arg the value to bind to it, also v1 (if not nil) is a second
  478. * variable to be bound (a supplied-p value) and val1 the value to bind it to.
  479. * If I see &rest or &key the remaining actual args get collected into
  480. * restarg, which takes the place of arg in some respects.
  481. */
  482. switch (opt_rest_state)
  483. {
  484. case STATE_NULL:
  485. if (v == opt_key)
  486. { opt_rest_state = STATE_OPT;
  487. continue;
  488. }
  489. #define BAD1(msg) { error(0, msg); goto unwind_special_bindings; }
  490. #define BAD2(msg, a) { error(1, msg, a); goto unwind_special_bindings; }
  491. #define collect_rest_arg() \
  492. while (args_left-- != 0) \
  493. { if (!exception_pending()) \
  494. restarg = cons(next_arg[args_left], restarg); \
  495. nil = C_nil; \
  496. }
  497. if (v == rest_key)
  498. { collect_rest_arg();
  499. if (exception_pending()) goto unwind_special_bindings;
  500. opt_rest_state = STATE_REST;
  501. continue;
  502. }
  503. #ifdef COMMON
  504. if (v == key_key)
  505. { collect_rest_arg();
  506. if (exception_pending()) goto unwind_special_bindings;
  507. if (check_keyargs_even(restarg)) BAD2(err_bad_keyargs, restarg);
  508. opt_rest_state = STATE_KEY;
  509. continue;
  510. }
  511. if (v == aux_key)
  512. { if (args_left != 0) BAD1(err_excess_args);
  513. opt_rest_state = STATE_AUX;
  514. continue;
  515. }
  516. if (v == allow_other_keys) BAD2(err_bad_bvl, v);
  517. #endif
  518. if (args_left == 0) BAD1(err_insufficient_args);
  519. arg = *next_arg++;
  520. args_left--;
  521. v1 = nil; /* no suppliedp mess here, I'm glad to say */
  522. break;
  523. case STATE_OPT:
  524. if (v == opt_key
  525. || v == rest_key
  526. #ifdef COMMON
  527. || v == key_key
  528. || v == allow_other_keys
  529. || v == aux_key
  530. #endif
  531. ) BAD2(err_bad_bvl, v);
  532. /*
  533. * Here v may be a simple variable, or a list (var init suppliedp)
  534. */
  535. opt_rest_state = STATE_OPT1;
  536. process_optional_parameter:
  537. if (args_left != 0)
  538. { arg = *next_arg++;
  539. args_left--;
  540. val1 = lisp_true;
  541. }
  542. else
  543. { arg = nil;
  544. val1 = nil;
  545. }
  546. v1 = nil;
  547. if (!consp(v)) break; /* Simple case */
  548. { w = qcdr(v);
  549. v = qcar(v);
  550. if (!consp(w)) break; /* (var) */
  551. if (val1 == nil) /* use the init form */
  552. { arg = qcar(w);
  553. arg = eval(arg, env);
  554. nil = C_nil;
  555. if (exception_pending()) goto unwind_special_bindings;
  556. }
  557. w = qcdr(w);
  558. if (consp(w)) v1 = qcar(w); /* suppliedp name */
  559. break;
  560. }
  561. case STATE_OPT1:
  562. if (v == rest_key)
  563. { collect_rest_arg();
  564. if (exception_pending()) goto unwind_special_bindings;
  565. opt_rest_state = STATE_REST;
  566. continue;
  567. }
  568. #ifdef COMMON
  569. if (v == key_key)
  570. { collect_rest_arg();
  571. if (exception_pending()) goto unwind_special_bindings;
  572. if (check_keyargs_even(restarg)) BAD2(err_bad_keyargs, restarg);
  573. opt_rest_state = STATE_KEY;
  574. continue;
  575. }
  576. if (v == aux_key)
  577. { if (args_left != 0) BAD1(err_excess_args);
  578. opt_rest_state = STATE_AUX;
  579. continue;
  580. }
  581. #endif
  582. if (v == opt_key
  583. #ifdef COMMON
  584. || v == allow_other_keys
  585. #endif
  586. ) BAD2(err_bad_bvl, v);
  587. goto process_optional_parameter;
  588. case STATE_REST:
  589. if (v == opt_key
  590. || v == rest_key
  591. #ifdef COMMON
  592. || v == key_key
  593. || v == allow_other_keys
  594. || v == aux_key
  595. #endif
  596. ) BAD2(err_bad_bvl, v);
  597. opt_rest_state = STATE_REST1;
  598. arg = restarg;
  599. break;
  600. case STATE_REST1:
  601. #ifdef COMMON
  602. if (v == key_key)
  603. { if (check_keyargs_even(restarg)) BAD2(err_bad_keyargs, restarg);
  604. opt_rest_state = STATE_KEY;
  605. continue;
  606. }
  607. if (v == aux_key)
  608. {
  609. opt_rest_state = STATE_AUX;
  610. continue;
  611. }
  612. #endif
  613. BAD2(err_bad_bvl, rest_key);
  614. #ifdef COMMON
  615. case STATE_KEY:
  616. if (v == allow_other_keys)
  617. { opt_rest_state = STATE_ALLOW;
  618. continue;
  619. }
  620. if (v == aux_key)
  621. { if (check_no_unwanted_keys(restarg, ok_keys))
  622. BAD2(err_bad_keyargs, restarg);
  623. opt_rest_state = STATE_AUX;
  624. continue;
  625. }
  626. if (v == opt_key || v == rest_key || v == key_key)
  627. BAD2(err_bad_bvl, v);
  628. process_keyword_parameter:
  629. /*
  630. * v needs to expand to ((:kv v) init svar) in effect here.
  631. */
  632. { Lisp_Object keyname = nil;
  633. w = nil;
  634. if (!consp(v))
  635. { if (!is_symbol(v)) BAD2(err_bad_bvl, v);
  636. keyname = keywordify(v);
  637. }
  638. else
  639. { w = qcdr(v);
  640. v = qcar(v);
  641. if (!consp(v))
  642. { if (!is_symbol(v)) BAD2(err_bad_bvl, v);
  643. keyname = keywordify(v);
  644. nil = C_nil;
  645. if (exception_pending()) goto unwind_special_bindings;
  646. }
  647. else
  648. { keyname = qcar(v);
  649. if (!is_symbol(keyname)) BAD2(err_bad_bvl, v);
  650. keyname = keywordify(keyname);
  651. nil = C_nil;
  652. if (exception_pending()) goto unwind_special_bindings;
  653. v = qcdr(v);
  654. if (consp(v)) v = qcar(v);
  655. else BAD2(err_bad_bvl, v);
  656. }
  657. }
  658. ok_keys = cons(keyname, ok_keys);
  659. nil = C_nil;
  660. if (exception_pending()) goto unwind_special_bindings;
  661. arg = key_lookup(qcar(ok_keys), restarg);
  662. if (arg == nil) val1 = nil;
  663. else
  664. { arg = qcar(arg);
  665. val1 = lisp_true;
  666. }
  667. v1 = nil;
  668. if (!consp(w)) break; /* (var) */
  669. if (val1 == nil) /* use the init form */
  670. { arg = qcar(w);
  671. arg = eval(arg, env);
  672. nil = C_nil;
  673. if (exception_pending()) goto unwind_special_bindings;
  674. }
  675. w = qcdr(w);
  676. if (consp(w)) v1 = qcar(w); /* suppliedp name */
  677. break;
  678. }
  679. case STATE_ALLOW:
  680. if (v == aux_key)
  681. { opt_rest_state = STATE_AUX;
  682. continue;
  683. }
  684. if (v == opt_key || v == rest_key || v == key_key ||
  685. v == allow_other_keys) BAD2(err_bad_bvl, v);
  686. goto process_keyword_parameter;
  687. case STATE_AUX:
  688. if (v == opt_key || v == rest_key ||
  689. v == key_key || v == allow_other_keys ||
  690. v == aux_key) BAD2(err_bad_bvl, v);
  691. if (consp(v))
  692. { w = qcdr(v);
  693. v = qcar(v);
  694. if (consp(w))
  695. { arg = qcar(w);
  696. arg = eval(arg, env);
  697. nil = C_nil;
  698. if (exception_pending()) goto unwind_special_bindings;
  699. }
  700. }
  701. else arg = nil;
  702. v1 = nil;
  703. break;
  704. #endif
  705. }
  706. /*
  707. * This is where I get when I have one or two vars to bind.
  708. */
  709. #ifndef COMMON
  710. /*
  711. * CSL mode does not have to mess about looking for local special bindings
  712. * and so is MUCH shorter and neater. I always shallow bind
  713. */
  714. #define instate_binding(var, val, local_decs1, lab) \
  715. { if (!is_symbol(var)) BAD2(err_bad_bvl, var); \
  716. w = acons(var, qvalue(var), specenv); \
  717. nil = C_nil; \
  718. if (exception_pending()) goto unwind_special_bindings; \
  719. specenv = w; \
  720. qvalue(var) = val; \
  721. }
  722. #else
  723. #define instate_binding(var, val, local_decs1, lab) \
  724. { Header h; \
  725. if (!is_symbol(var)) BAD2(err_bad_bvl, var); \
  726. h = qheader(var); \
  727. if ((h & SYM_SPECIAL_VAR) != 0) \
  728. { w = acons(var, qvalue(var), specenv); \
  729. nil = C_nil; \
  730. if (exception_pending()) goto unwind_special_bindings; \
  731. specenv = w; \
  732. qvalue(var) = val; \
  733. } \
  734. else \
  735. { for (w = local_decs1; w!=nil; w = qcdr(w)) \
  736. { if (qcar(w) == var) \
  737. { qcar(w) = fixnum_of_int(0);/* decl is used up */\
  738. w = acons(var, work_symbol, env); \
  739. nil = C_nil; \
  740. if (exception_pending()) \
  741. goto unwind_special_bindings; \
  742. env = w; \
  743. w = acons(var, qvalue(var), specenv); \
  744. nil = C_nil; \
  745. if (exception_pending()) \
  746. goto unwind_special_bindings; \
  747. specenv = w; \
  748. qvalue(var) = val; \
  749. goto lab; \
  750. } \
  751. } \
  752. w = acons(var, val, env); \
  753. nil = C_nil; \
  754. if (exception_pending()) goto unwind_special_bindings; \
  755. env = w; \
  756. lab: ; \
  757. } \
  758. }
  759. #endif
  760. #ifdef COMMON
  761. /*
  762. * Must check about local special declarations here...
  763. */
  764. #endif
  765. instate_binding(v, arg, local_decs, label1);
  766. if (v1 != nil) instate_binding(v1, val1, local_decs, label2);
  767. } /* End of for loop that scans BVL */
  768. #ifdef COMMON
  769. /*
  770. * As well as local special declarations that have applied to bindings here
  771. * there can be some that apply just to variable references within the body.
  772. */
  773. while (local_decs!=nil)
  774. { Lisp_Object q = qcar(local_decs);
  775. local_decs=qcdr(local_decs);
  776. if (!is_symbol(q)) continue;
  777. w = acons(q, work_symbol, env);
  778. nil = C_nil;
  779. if (exception_pending()) goto unwind_special_bindings;
  780. env = w;
  781. }
  782. #endif
  783. switch (opt_rest_state)
  784. {
  785. case STATE_NULL:
  786. case STATE_OPT1: /* Ensure there had not been too many args */
  787. if (args_left != 0) BAD1(err_excess_args);
  788. break;
  789. case STATE_OPT: /* error if bvl finishes here */
  790. case STATE_REST:
  791. BAD2(err_bad_bvl, opt_rest_state == STATE_OPT ? opt_key : rest_key);
  792. #ifdef COMMON
  793. case STATE_KEY: /* ensure only valid keys were given */
  794. if (check_no_unwanted_keys(restarg, ok_keys))
  795. BAD2(err_bad_keyargs, restarg);
  796. break;
  797. #endif
  798. default:
  799. /* in the following cases all is known to be well
  800. case STATE_REST1:
  801. case STATE_ALLOW:
  802. case STATE_AUX:
  803. */
  804. break;
  805. }
  806. /*
  807. * Now all the argument bindings have been performed - it remains to
  808. * process the body of the lambda-expression.
  809. */
  810. if (specenv == nil)
  811. { Lisp_Object bodyx = body, envx = env;
  812. Lisp_Object qname = name;
  813. popv(stack_used);
  814. push(qname);
  815. bodyx = progn_fn(bodyx, envx);
  816. pop(qname);
  817. nil = C_nil;
  818. if (exception_pending()) return qname;
  819. return bodyx;
  820. }
  821. { body = progn_fn(body, env);
  822. nil = C_nil;
  823. if (exception_pending()) goto unwind_special_bindings;
  824. while (specenv != nil)
  825. {
  826. Lisp_Object bv = qcar(specenv);
  827. qvalue(qcar(bv)) = qcdr(bv);
  828. specenv = qcdr(specenv);
  829. }
  830. { Lisp_Object bodyx = body;
  831. popv(stack_used);
  832. /*
  833. * note that exit_count has not been disturbed since I called progn_fn,
  834. * so the numbert of values that will be returned remains correctly
  835. * established (in Common Lisp mode where it is needed.
  836. */
  837. return bodyx;
  838. }
  839. }
  840. unwind_special_bindings:
  841. /*
  842. * I gete here ONLY if nil has its mark bit set, which means that (for
  843. * one reason or another) I am having to unwind the stack, restoring
  844. * special bindings as I go.
  845. */
  846. nil = C_nil;
  847. flip_exception();
  848. while (specenv != nil)
  849. { Lisp_Object bv = qcar(specenv);
  850. qvalue(qcar(bv)) = qcdr(bv);
  851. specenv = qcdr(specenv);
  852. }
  853. flip_exception();
  854. { Lisp_Object qname = name;
  855. popv(stack_used);
  856. return qname;
  857. }
  858. #undef w
  859. #undef p
  860. #undef v
  861. #undef v1
  862. #undef arg
  863. #undef val1
  864. #undef specenv
  865. #undef restarg
  866. #undef ok_keys
  867. #undef local_decs
  868. #undef name
  869. #undef env
  870. #undef body
  871. #undef bvl
  872. #undef stack_used
  873. }
  874. Lisp_Object Leval(Lisp_Object nil, Lisp_Object a)
  875. {
  876. return eval(a, nil); /* Multiple values may be returned */
  877. }
  878. Lisp_Object Levlis(Lisp_Object nil, Lisp_Object a)
  879. {
  880. Lisp_Object r;
  881. stackcheck1(0, a);
  882. r = nil;
  883. while (consp(a))
  884. { push2(qcdr(a), r);
  885. a = qcar(a);
  886. a = eval(a, nil);
  887. errexitn(2);
  888. pop(r);
  889. r = cons(a, r);
  890. pop(a);
  891. errexit();
  892. }
  893. return onevalue(nreverse(r));
  894. }
  895. Lisp_Object MS_CDECL Lapply_n(Lisp_Object nil, int nargs, ...)
  896. {
  897. va_list a;
  898. int i;
  899. Lisp_Object *stack_save = stack, last, fn = nil;
  900. if (nargs == 0) return aerror("apply");
  901. else if (nargs > 1)
  902. { va_start(a, nargs);
  903. fn = va_arg(a, Lisp_Object);
  904. push_args_1(a, nargs);
  905. pop(last);
  906. i = nargs-2;
  907. while (consp(last))
  908. { push(qcar(last));
  909. last = qcdr(last);
  910. i++;
  911. }
  912. }
  913. else i = 0;
  914. stackcheck1(stack-stack_save, fn);
  915. return apply(fn, i, nil, fn);
  916. }
  917. Lisp_Object Lapply_1(Lisp_Object nil, Lisp_Object fn)
  918. {
  919. return Lapply_n(nil, 1, fn);
  920. }
  921. Lisp_Object Lapply_2(Lisp_Object nil, Lisp_Object fn, Lisp_Object a1)
  922. {
  923. return Lapply_n(nil, 2, fn, a1);
  924. }
  925. Lisp_Object Lapply0(Lisp_Object nil, Lisp_Object fn)
  926. {
  927. if (is_symbol(fn)) return (*qfnn(fn))(qenv(fn), 0);
  928. stackcheck1(0, fn);
  929. return apply(fn, 0, C_nil, fn);
  930. }
  931. Lisp_Object Lapply1(Lisp_Object nil, Lisp_Object fn, Lisp_Object a)
  932. {
  933. if (is_symbol(fn)) return (*qfn1(fn))(qenv(fn), a);
  934. push(a);
  935. stackcheck1(1, fn);
  936. return apply(fn, 1, C_nil, fn);
  937. }
  938. Lisp_Object MS_CDECL Lapply2(Lisp_Object nil, int nargs, ...)
  939. {
  940. va_list aa;
  941. Lisp_Object fn, a, b;
  942. argcheck(nargs, 3, "apply2");
  943. va_start(aa, nargs);
  944. fn = va_arg(aa, Lisp_Object);
  945. a = va_arg(aa, Lisp_Object);
  946. b = va_arg(aa, Lisp_Object);
  947. va_end(aa);
  948. if (is_symbol(fn)) return (*qfn2(fn))(qenv(fn), a, b);
  949. push2(a, b);
  950. stackcheck1(2, fn);
  951. return apply(fn, 2, C_nil, fn);
  952. }
  953. Lisp_Object MS_CDECL Lapply3(Lisp_Object nil, int nargs, ...)
  954. {
  955. va_list aa;
  956. Lisp_Object fn, a, b, c;
  957. argcheck(nargs, 4, "apply3");
  958. va_start(aa, nargs);
  959. fn = va_arg(aa, Lisp_Object);
  960. a = va_arg(aa, Lisp_Object);
  961. b = va_arg(aa, Lisp_Object);
  962. c = va_arg(aa, Lisp_Object);
  963. va_end(aa);
  964. if (is_symbol(fn)) return (*qfnn(fn))(qenv(fn), 3, a, b, c);
  965. push3(a, b, c);
  966. stackcheck1(3, fn);
  967. return apply(fn, 3, C_nil, fn);
  968. }
  969. Lisp_Object Lfuncall1(Lisp_Object nil, Lisp_Object fn)
  970. {
  971. if (is_symbol(fn)) return (*qfnn(fn))(qenv(fn), 0);
  972. stackcheck1(0, fn);
  973. return apply(fn, 0, nil, fn);
  974. }
  975. Lisp_Object Lfuncall2(Lisp_Object nil, Lisp_Object fn, Lisp_Object a1)
  976. {
  977. if (is_symbol(fn)) return (*qfn1(fn))(qenv(fn), a1);
  978. push(a1);
  979. stackcheck1(1, fn);
  980. return apply(fn, 1, nil, fn);
  981. }
  982. static Lisp_Object MS_CDECL Lfuncalln_sub(Lisp_Object nil, int nargs, va_list a)
  983. {
  984. Lisp_Object *stack_save = stack, fn;
  985. fn = va_arg(a, Lisp_Object);
  986. push_args_1(a, nargs);
  987. stackcheck1(stack-stack_save, fn);
  988. return apply(fn, nargs-1, nil, fn);
  989. }
  990. Lisp_Object MS_CDECL Lfuncalln(Lisp_Object nil, int nargs, ...)
  991. {
  992. va_list a;
  993. Lisp_Object fn, a1, a2, a3, a4;
  994. va_start(a, nargs);
  995. switch (nargs)
  996. {
  997. case 0: return aerror("funcall");
  998. case 1: /* cases 1 and 2 should go through Lfuncall1,2 not here */
  999. case 2: return aerror("funcall wrong call");
  1000. case 3: fn = va_arg(a, Lisp_Object);
  1001. a1 = va_arg(a, Lisp_Object);
  1002. a2 = va_arg(a, Lisp_Object);
  1003. if (is_symbol(fn)) return (*qfn2(fn))(qenv(fn), a1, a2);
  1004. push2(a1, a2);
  1005. return apply(fn, 2, nil, fn);
  1006. case 4: fn = va_arg(a, Lisp_Object);
  1007. a1 = va_arg(a, Lisp_Object);
  1008. a2 = va_arg(a, Lisp_Object);
  1009. a3 = va_arg(a, Lisp_Object);
  1010. if (is_symbol(fn)) return (*qfnn(fn))(qenv(fn), 3, a1, a2, a3);
  1011. push3(a1, a2, a3);
  1012. return apply(fn, 3, nil, fn);
  1013. case 5: fn = va_arg(a, Lisp_Object);
  1014. a1 = va_arg(a, Lisp_Object);
  1015. a2 = va_arg(a, Lisp_Object);
  1016. a3 = va_arg(a, Lisp_Object);
  1017. a4 = va_arg(a, Lisp_Object);
  1018. if (is_symbol(fn)) return (*qfnn(fn))(qenv(fn), 4, a1, a2, a3, a4);
  1019. push4(a1, a2, a3, a4);
  1020. return apply(fn, 4, nil, fn);
  1021. default:
  1022. return Lfuncalln_sub(nil, nargs, a);
  1023. }
  1024. }
  1025. #ifdef COMMON
  1026. Lisp_Object MS_CDECL Lvalues(Lisp_Object nil, int nargs, ...)
  1027. {
  1028. va_list a;
  1029. Lisp_Object *p = &mv_2, w;
  1030. int i;
  1031. /*
  1032. * Because multiple-values get passed back in static storage there is
  1033. * a fixed upper limit to how many I can handle - truncate here to allow
  1034. * for that.
  1035. */
  1036. if (nargs > 50) nargs = 50;
  1037. if (nargs == 0) return nvalues(nil, 0);
  1038. va_start(a, nargs);
  1039. push_args(a, nargs);
  1040. for (i=1; i<nargs; i++)
  1041. { pop(w);
  1042. p[nargs-i-1] = w;
  1043. }
  1044. pop(w);
  1045. return nvalues(w, nargs);
  1046. }
  1047. Lisp_Object Lvalues_2(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1048. {
  1049. return Lvalues(nil, 2, a, b);
  1050. }
  1051. Lisp_Object Lvalues_1(Lisp_Object nil, Lisp_Object a)
  1052. {
  1053. return Lvalues(nil, 1, a);
  1054. }
  1055. Lisp_Object mv_call_fn(Lisp_Object args, Lisp_Object env)
  1056. /*
  1057. * here with the rest of the interpreter rather than in specforms.c
  1058. */
  1059. {
  1060. Lisp_Object nil = C_nil;
  1061. Lisp_Object fn, *stack_save = stack;
  1062. int i=0, j=0;
  1063. if (!consp(args)) return nil; /* (multiple-value-call) => nil */
  1064. stackcheck2(0, args, env);
  1065. push2(args, env);
  1066. fn = qcar(args);
  1067. fn = eval(fn, env);
  1068. pop2(env, args);
  1069. errexit();
  1070. args = qcdr(args);
  1071. while (consp(args))
  1072. { Lisp_Object r1;
  1073. push2(args, env);
  1074. r1 = qcar(args);
  1075. r1 = eval(r1, env);
  1076. nil = C_nil;
  1077. if (exception_pending())
  1078. { stack = stack_save;
  1079. return nil;
  1080. }
  1081. /*
  1082. * It is critical here that push does not check for stack overflow and
  1083. * thus can not call the garbage collector, or otherwise lead to calculation
  1084. * that could possibly clobber the multiple results that I am working with
  1085. * here.
  1086. */
  1087. pop2(env, args);
  1088. push(r1);
  1089. i++;
  1090. for (j = 2; j<=exit_count; j++)
  1091. { push((&work_0)[j]);
  1092. i++;
  1093. }
  1094. args = qcdr(args);
  1095. }
  1096. stackcheck2(stack-stack_save, fn, env);
  1097. return apply(fn, i, env, fn);
  1098. }
  1099. #endif
  1100. Lisp_Object interpreted1(Lisp_Object def, Lisp_Object a1)
  1101. {
  1102. Lisp_Object nil = C_nil;
  1103. push(a1);
  1104. stackcheck1(1, def);
  1105. return apply_lambda(def, 1, nil, def);
  1106. }
  1107. Lisp_Object interpreted2(Lisp_Object def, Lisp_Object a1, Lisp_Object a2)
  1108. {
  1109. Lisp_Object nil = C_nil;
  1110. push2(a1, a2);
  1111. stackcheck1(2, def);
  1112. return apply_lambda(def, 2, nil, def);
  1113. }
  1114. Lisp_Object MS_CDECL interpretedn(Lisp_Object def, int nargs, ...)
  1115. {
  1116. /*
  1117. * The messing about here is to get the (unknown number of) args
  1118. * into a nice neat vector so that they can be indexed into. If I knew
  1119. * that the args were in consecutive locations on the stack I could
  1120. * probably save a copying operation.
  1121. */
  1122. Lisp_Object nil = C_nil;
  1123. Lisp_Object *stack_save = stack;
  1124. va_list a;
  1125. if (nargs != 0)
  1126. { va_start(a, nargs);
  1127. push_args(a, nargs);
  1128. }
  1129. stackcheck1(stack-stack_save, def);
  1130. return apply_lambda(def, nargs, nil, def);
  1131. }
  1132. Lisp_Object funarged1(Lisp_Object def, Lisp_Object a1)
  1133. {
  1134. Lisp_Object nil = C_nil;
  1135. push(a1);
  1136. stackcheck1(1, def);
  1137. return apply_lambda(qcdr(def), 1, qcar(def), qcdr(def));
  1138. }
  1139. Lisp_Object funarged2(Lisp_Object def, Lisp_Object a1, Lisp_Object a2)
  1140. {
  1141. Lisp_Object nil = C_nil;
  1142. push2(a1, a2);
  1143. stackcheck1(2, def);
  1144. return apply_lambda(qcdr(def), 2, qcar(def), qcdr(def));
  1145. }
  1146. Lisp_Object MS_CDECL funargedn(Lisp_Object def, int nargs, ...)
  1147. {
  1148. Lisp_Object nil = C_nil;
  1149. Lisp_Object *stack_save = stack;
  1150. va_list a;
  1151. if (nargs != 0)
  1152. { va_start(a, nargs);
  1153. push_args(a, nargs);
  1154. }
  1155. stackcheck1(stack-stack_save, def);
  1156. return apply_lambda(qcdr(def), nargs, qcar(def), qcdr(def));
  1157. }
  1158. /*
  1159. * Now some execution-doubling versions...
  1160. */
  1161. Lisp_Object double_interpreted1(Lisp_Object def, Lisp_Object a1)
  1162. {
  1163. Lisp_Object nil = C_nil;
  1164. push(a1);
  1165. stackcheck1(1, def);
  1166. return apply_lambda(def, 1, nil, def);
  1167. }
  1168. Lisp_Object double_interpreted2(Lisp_Object def, Lisp_Object a1, Lisp_Object a2)
  1169. {
  1170. Lisp_Object nil = C_nil;
  1171. push2(a1, a2);
  1172. stackcheck1(2, def);
  1173. return apply_lambda(def, 2, nil, def);
  1174. }
  1175. Lisp_Object MS_CDECL double_interpretedn(Lisp_Object def, int nargs, ...)
  1176. {
  1177. /*
  1178. * The messing about here is to get the (unknown number of) args
  1179. * into a nice neat vector so that they can be indexed into. If I knew
  1180. * that the args were in consecutive locations on the stack I could
  1181. * probably save a copying operation.
  1182. */
  1183. Lisp_Object nil = C_nil;
  1184. Lisp_Object *stack_save = stack;
  1185. va_list a;
  1186. if (nargs != 0)
  1187. { va_start(a, nargs);
  1188. push_args(a, nargs);
  1189. }
  1190. stackcheck1(stack-stack_save, def);
  1191. return apply_lambda(def, nargs, nil, def);
  1192. }
  1193. Lisp_Object double_funarged1(Lisp_Object def, Lisp_Object a1)
  1194. {
  1195. Lisp_Object nil = C_nil;
  1196. push(a1);
  1197. stackcheck1(1, def);
  1198. return apply_lambda(qcdr(def), 1, qcar(def), qcdr(def));
  1199. }
  1200. Lisp_Object double_funarged2(Lisp_Object def, Lisp_Object a1, Lisp_Object a2)
  1201. {
  1202. Lisp_Object nil = C_nil;
  1203. push2(a1, a2);
  1204. stackcheck1(2, def);
  1205. return apply_lambda(qcdr(def), 2, qcar(def), qcdr(def));
  1206. }
  1207. Lisp_Object MS_CDECL double_funargedn(Lisp_Object def, int nargs, ...)
  1208. {
  1209. Lisp_Object nil = C_nil;
  1210. Lisp_Object *stack_save = stack;
  1211. va_list a;
  1212. if (nargs != 0)
  1213. { va_start(a, nargs);
  1214. push_args(a, nargs);
  1215. }
  1216. stackcheck1(stack-stack_save, def);
  1217. return apply_lambda(qcdr(def), nargs, qcar(def), qcdr(def));
  1218. }
  1219. Lisp_Object traceinterpreted1(Lisp_Object def, Lisp_Object a1)
  1220. /*
  1221. * Like interpreted() but the definition has the fn name consed on the front
  1222. */
  1223. {
  1224. Lisp_Object nil = C_nil, r;
  1225. push(a1);
  1226. stackcheck1(1, def);
  1227. freshline_trace();
  1228. trace_printf("Entering ");
  1229. loop_print_trace(qcar(def));
  1230. trace_printf(" (1 arg)\n");
  1231. trace_printf("Arg1: ");
  1232. loop_print_trace(stack[0]);
  1233. trace_printf("\n");
  1234. r = apply_lambda(qcdr(def), 1, nil, def);
  1235. errexit();
  1236. push(r);
  1237. trace_printf("Value = ");
  1238. loop_print_trace(r);
  1239. trace_printf("\n");
  1240. pop(r);
  1241. return r;
  1242. }
  1243. Lisp_Object traceinterpreted2(Lisp_Object def, Lisp_Object a1, Lisp_Object a2)
  1244. /*
  1245. * Like interpreted() but the definition has the fn name consed on the front
  1246. */
  1247. {
  1248. Lisp_Object nil = C_nil, r;
  1249. int i;
  1250. push2(a1, a2);
  1251. stackcheck1(2, def);
  1252. freshline_trace();
  1253. trace_printf("Entering ");
  1254. loop_print_trace(qcar(def));
  1255. trace_printf(" (2 args)\n");
  1256. for (i=1; i<=2; i++)
  1257. { trace_printf("Arg%d: ", i);
  1258. loop_print_trace(stack[i-2]);
  1259. trace_printf("\n");
  1260. }
  1261. r = apply_lambda(qcdr(def), 2, nil, def);
  1262. errexit();
  1263. push(r);
  1264. trace_printf("Value = ");
  1265. loop_print_trace(r);
  1266. trace_printf("\n");
  1267. pop(r);
  1268. return r;
  1269. }
  1270. Lisp_Object MS_CDECL traceinterpretedn(Lisp_Object def, int nargs, ...)
  1271. /*
  1272. * Like interpreted() but the definition has the fn name consed on the front
  1273. */
  1274. {
  1275. int i;
  1276. Lisp_Object nil = C_nil, r;
  1277. Lisp_Object *stack_save = stack;
  1278. va_list a;
  1279. if (nargs != 0)
  1280. { va_start(a, nargs);
  1281. push_args(a, nargs);
  1282. }
  1283. stackcheck1(stack-stack_save, def);
  1284. freshline_trace();
  1285. trace_printf("Entering ");
  1286. loop_print_trace(qcar(def));
  1287. trace_printf(" (%d args)\n", nargs);
  1288. for (i=1; i<=nargs; i++)
  1289. { trace_printf("Arg%d: ", i);
  1290. loop_print_trace(stack[i-nargs]);
  1291. trace_printf("\n");
  1292. }
  1293. r = apply_lambda(qcdr(def), nargs, nil, def);
  1294. errexit();
  1295. push(r);
  1296. trace_printf("Value = ");
  1297. loop_print_trace(r);
  1298. trace_printf("\n");
  1299. pop(r);
  1300. return r;
  1301. }
  1302. Lisp_Object tracefunarged1(Lisp_Object def, Lisp_Object a1)
  1303. /*
  1304. * Like funarged() but with some printing
  1305. */
  1306. {
  1307. Lisp_Object nil = C_nil, r;
  1308. push(a1);
  1309. stackcheck1(1, def);
  1310. freshline_trace();
  1311. trace_printf("Entering funarg ");
  1312. loop_print_trace(qcar(def));
  1313. trace_printf(" (1 arg)\n");
  1314. def = qcdr(def);
  1315. r = apply_lambda(qcdr(def), 1, qcar(def), qcdr(def));
  1316. errexit();
  1317. push(r);
  1318. trace_printf("Value = ");
  1319. loop_print_trace(r);
  1320. trace_printf("\n");
  1321. pop(r);
  1322. return r;
  1323. }
  1324. Lisp_Object tracefunarged2(Lisp_Object def, Lisp_Object a1, Lisp_Object a2)
  1325. /*
  1326. * Like funarged() but with some printing
  1327. */
  1328. {
  1329. Lisp_Object nil = C_nil, r;
  1330. push2(a1, a2);
  1331. stackcheck1(2, def);
  1332. freshline_trace();
  1333. trace_printf("Entering funarg ");
  1334. loop_print_trace(qcar(def));
  1335. trace_printf(" (2 args)\n");
  1336. def = qcdr(def);
  1337. r = apply_lambda(qcdr(def), 2, qcar(def), qcdr(def));
  1338. errexit();
  1339. push(r);
  1340. trace_printf("Value = ");
  1341. loop_print_trace(r);
  1342. trace_printf("\n");
  1343. pop(r);
  1344. return r;
  1345. }
  1346. Lisp_Object MS_CDECL tracefunargedn(Lisp_Object def, int nargs, ...)
  1347. /*
  1348. * Like funarged() but with some printing
  1349. */
  1350. {
  1351. Lisp_Object nil = C_nil, r;
  1352. Lisp_Object *stack_save = stack;
  1353. va_list a;
  1354. if (nargs != 0)
  1355. { va_start(a, nargs);
  1356. push_args(a, nargs);
  1357. }
  1358. stackcheck1(stack-stack_save, def);
  1359. freshline_trace();
  1360. trace_printf("Entering funarg ");
  1361. loop_print_trace(qcar(def));
  1362. trace_printf(" (%d args)\n", nargs);
  1363. def = qcdr(def);
  1364. r = apply_lambda(qcdr(def), nargs, qcar(def), qcdr(def));
  1365. errexit();
  1366. push(r);
  1367. trace_printf("Value = ");
  1368. loop_print_trace(r);
  1369. trace_printf("\n");
  1370. pop(r);
  1371. return r;
  1372. }
  1373. static Lisp_Object macroexpand_1(Lisp_Object form, Lisp_Object env)
  1374. { /* The environment here seems only necessary for macrolet */
  1375. Lisp_Object done;
  1376. Lisp_Object f, nil;
  1377. nil = C_nil;
  1378. stackcheck2(0, form, env);
  1379. done = nil;
  1380. if (consp(form))
  1381. { f = qcar(form);
  1382. #ifdef COMMON
  1383. /*
  1384. * look for local macro definitions
  1385. */
  1386. { Lisp_Object p;
  1387. for (p=env; p!=nil; p=qcdr(p))
  1388. { Lisp_Object w = qcar(p);
  1389. if (qcdr(w) == f && is_cons(w = qcar(w)) && w!=nil)
  1390. {
  1391. p = qcar(w);
  1392. if (p == funarg) /* ordinary function */
  1393. { mv_2 = nil;
  1394. return nvalues(form, 2);
  1395. }
  1396. push2(form, done);
  1397. w = cons(lambda, w);
  1398. errexitn(1);
  1399. p = Lfuncalln(nil, 4, qvalue(macroexpand_hook),
  1400. w, stack[-1], nil);
  1401. pop2(done, form);
  1402. nil = C_nil;
  1403. if (exception_pending())
  1404. { flip_exception();
  1405. if ((exit_reason & UNWIND_ERROR) != 0)
  1406. { err_printf("\nMacroexpanding: ");
  1407. loop_print_error(form);
  1408. nil = C_nil;
  1409. if (exception_pending()) flip_exception();
  1410. }
  1411. flip_exception();
  1412. return nil;
  1413. }
  1414. mv_2 = lisp_true;
  1415. return nvalues(p, 2);
  1416. }
  1417. }
  1418. }
  1419. /*
  1420. * If there is no local macro definition I need to look for a global one
  1421. */
  1422. #endif
  1423. if (symbolp(f) && (qheader(f) & SYM_MACRO) != 0)
  1424. {
  1425. done = qvalue(macroexpand_hook);
  1426. if (done == unset_var)
  1427. return error(1, err_macroex_hook, macroexpand_hook);
  1428. push3(form, env, done);
  1429. f = cons(lambda, qenv(f));
  1430. pop3(done, env, form);
  1431. nil = C_nil;
  1432. if (!exception_pending())
  1433. {
  1434. #ifndef COMMON
  1435. /* CSL does not pass an environment down here, so does not demand &opt arg */
  1436. form = Lfuncalln(nil, 3, done, f, form);
  1437. #else
  1438. form = Lfuncalln(nil, 4, done, f, form, env);
  1439. #endif
  1440. nil = C_nil;
  1441. }
  1442. if (exception_pending()) return nil;
  1443. done = lisp_true;
  1444. }
  1445. }
  1446. mv_2 = done;
  1447. return nvalues(form, 2); /* Multiple values handed back */
  1448. }
  1449. Lisp_Object macroexpand(Lisp_Object form, Lisp_Object env)
  1450. { /* The environment here seems only necessary for macrolet */
  1451. Lisp_Object done, nil;
  1452. nil = C_nil;
  1453. stackcheck2(0, form, env);
  1454. done = nil;
  1455. for (;;)
  1456. { push2(env, done);
  1457. form = macroexpand_1(form, env);
  1458. pop2(done, env);
  1459. errexit();
  1460. if (mv_2 == nil) break;
  1461. done = lisp_true;
  1462. }
  1463. mv_2 = done;
  1464. return nvalues(form, 2); /* Multiple values handed back */
  1465. }
  1466. Lisp_Object Lmacroexpand(Lisp_Object nil, Lisp_Object a)
  1467. {
  1468. return macroexpand(a, nil);
  1469. }
  1470. #ifdef COMMON
  1471. Lisp_Object Lmacroexpand_2(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1472. {
  1473. CSL_IGNORE(nil);
  1474. return macroexpand(a, b);
  1475. }
  1476. #endif
  1477. Lisp_Object Lmacroexpand_1(Lisp_Object nil, Lisp_Object a)
  1478. {
  1479. return macroexpand_1(a, nil);
  1480. }
  1481. #ifdef COMMON
  1482. Lisp_Object Lmacroexpand_1_2(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1483. {
  1484. CSL_IGNORE(nil);
  1485. return macroexpand_1(a, b);
  1486. }
  1487. #endif
  1488. /*
  1489. * To make something autoloadable I should set the environment cell to
  1490. * (name-of-self module-name-1 module-name-2 ...)
  1491. * and when invoked the function will do a load-module on each of the
  1492. * modules specified and then re-attempt to call. Loading the
  1493. * modules is expected to establish a proper definition for the
  1494. * function involved.
  1495. */
  1496. Lisp_Object autoload1(Lisp_Object fname, Lisp_Object a1)
  1497. {
  1498. Lisp_Object nil = C_nil;
  1499. push2(a1, qcar(fname));
  1500. set_fns(qcar(fname), undefined1, undefined2, undefinedn);
  1501. qenv(qcar(fname)) = qcar(fname);
  1502. fname = qcdr(fname);
  1503. while (consp(fname))
  1504. { push(qcdr(fname));
  1505. Lload_module(nil, qcar(fname));
  1506. errexitn(3);
  1507. pop(fname);
  1508. }
  1509. pop(fname);
  1510. return apply(fname, 1, nil, fname);
  1511. }
  1512. Lisp_Object autoload2(Lisp_Object fname, Lisp_Object a1, Lisp_Object a2)
  1513. {
  1514. Lisp_Object nil = C_nil;
  1515. push3(a1, a2, qcar(fname));
  1516. set_fns(qcar(fname), undefined1, undefined2, undefinedn);
  1517. qenv(qcar(fname)) = qcar(fname);
  1518. fname = qcdr(fname);
  1519. while (consp(fname))
  1520. { push(qcdr(fname));
  1521. Lload_module(nil, qcar(fname));
  1522. errexitn(4);
  1523. pop(fname);
  1524. }
  1525. pop(fname);
  1526. return apply(fname, 2, nil, fname);
  1527. }
  1528. Lisp_Object MS_CDECL autoloadn(Lisp_Object fname, int nargs, ...)
  1529. {
  1530. Lisp_Object nil = C_nil;
  1531. va_list a;
  1532. va_start(a, nargs);
  1533. push_args(a, nargs);
  1534. push(qcar(fname));
  1535. set_fns(qcar(fname), undefined1, undefined2, undefinedn);
  1536. qenv(qcar(fname)) = qcar(fname);
  1537. fname = qcdr(fname);
  1538. while (consp(fname))
  1539. { push(qcdr(fname));
  1540. Lload_module(nil, qcar(fname));
  1541. errexitn(nargs+2);
  1542. pop(fname);
  1543. }
  1544. pop(fname);
  1545. return apply(fname, nargs, nil, fname);
  1546. }
  1547. Lisp_Object undefined1(Lisp_Object fname, Lisp_Object a1)
  1548. {
  1549. /*
  1550. * It would be perfectly possible to grab and save the args here, and retry
  1551. * the function call after error has patched things up. Again
  1552. * this entrypoint is for compiled code calling something that is undefined,
  1553. * and so no lexical environment is needed.
  1554. */
  1555. CSL_IGNORE(a1);
  1556. return error(1, err_undefined_function_1, fname);
  1557. }
  1558. Lisp_Object undefined2(Lisp_Object fname, Lisp_Object a1, Lisp_Object a2)
  1559. {
  1560. CSL_IGNORE(a1);
  1561. CSL_IGNORE(a2);
  1562. return error(1, err_undefined_function_2, fname);
  1563. }
  1564. Lisp_Object MS_CDECL undefinedn(Lisp_Object fname, int nargs, ...)
  1565. {
  1566. CSL_IGNORE(nargs);
  1567. return error(1, err_undefined_function_n, fname);
  1568. }
  1569. /*
  1570. * The next few functions allow me to create variants on things! The
  1571. * entrypoint fX_as_Y goes in the function cell of a symbol, and the name
  1572. * of a function with Y arguments goes in is environment cell. The result will
  1573. * be a function that accepts X arguments and discards all but the first Y of
  1574. * them, then chains to the other function. The purpose is to support goo
  1575. * compilation of things like
  1576. * (de funny_equal (a b c) (equal a b))
  1577. */
  1578. Lisp_Object MS_CDECL f0_as_0(Lisp_Object env, int nargs, ...)
  1579. {
  1580. if (nargs != 0) return aerror1("wrong number of args (0->0)", env);
  1581. return (*qfnn(env))(qenv(env), 0);
  1582. }
  1583. Lisp_Object f1_as_0(Lisp_Object env, Lisp_Object a)
  1584. {
  1585. CSL_IGNORE(a);
  1586. return (*qfnn(env))(qenv(env), 0);
  1587. }
  1588. Lisp_Object f2_as_0(Lisp_Object env, Lisp_Object a, Lisp_Object b)
  1589. {
  1590. CSL_IGNORE(a);
  1591. CSL_IGNORE(b);
  1592. return (*qfnn(env))(qenv(env), 0);
  1593. }
  1594. Lisp_Object MS_CDECL f3_as_0(Lisp_Object env, int nargs, ...)
  1595. {
  1596. if (nargs != 3) return aerror1("wrong number of args (3->0)", env);
  1597. return (*qfnn(env))(qenv(env), 0);
  1598. }
  1599. Lisp_Object f1_as_1(Lisp_Object env, Lisp_Object a)
  1600. {
  1601. return (*qfn1(env))(qenv(env), a);
  1602. }
  1603. Lisp_Object f2_as_1(Lisp_Object env, Lisp_Object a, Lisp_Object b)
  1604. {
  1605. CSL_IGNORE(b);
  1606. return (*qfn1(env))(qenv(env), a);
  1607. }
  1608. Lisp_Object MS_CDECL f3_as_1(Lisp_Object env, int nargs, ...)
  1609. {
  1610. va_list a;
  1611. Lisp_Object a1;
  1612. if (nargs != 3) return aerror1("wrong number of args (3->1)", env);
  1613. va_start(a, nargs);
  1614. a1 = va_arg(a, Lisp_Object);
  1615. va_end(a);
  1616. return (*qfn1(env))(qenv(env), a1);
  1617. }
  1618. Lisp_Object f2_as_2(Lisp_Object env, Lisp_Object a, Lisp_Object b)
  1619. {
  1620. return (*qfn2(env))(qenv(env), a, b);
  1621. }
  1622. Lisp_Object MS_CDECL f3_as_2(Lisp_Object env, int nargs, ...)
  1623. {
  1624. va_list a;
  1625. Lisp_Object a1, a2;
  1626. if (nargs != 3) return aerror1("wrong number of args (3->2)", env);
  1627. va_start(a, nargs);
  1628. a1 = va_arg(a, Lisp_Object);
  1629. a2 = va_arg(a, Lisp_Object);
  1630. va_end(a);
  1631. return (*qfn2(env))(qenv(env), a1, a2);
  1632. }
  1633. Lisp_Object MS_CDECL f3_as_3(Lisp_Object env, int nargs, ...)
  1634. {
  1635. va_list a;
  1636. Lisp_Object a1, a2, a3;
  1637. if (nargs != 3) return aerror1("wrong number of args (3->3)", env);
  1638. va_start(a, nargs);
  1639. a1 = va_arg(a, Lisp_Object);
  1640. a2 = va_arg(a, Lisp_Object);
  1641. a3 = va_arg(a, Lisp_Object);
  1642. va_end(a);
  1643. return (*qfnn(env))(qenv(env), 3, a1, a2, a3);
  1644. }
  1645. setup_type const eval1_setup[] =
  1646. {
  1647. {"bytecounts", wrong_no_na, wrong_no_nb, bytecounts},
  1648. {"apply", Lapply_1, Lapply_2, Lapply_n},
  1649. {"apply0", Lapply0, too_many_1, wrong_no_1},
  1650. {"apply1", too_few_2, Lapply1, wrong_no_2},
  1651. {"apply2", wrong_no_na, wrong_no_nb, Lapply2},
  1652. {"apply3", wrong_no_na, wrong_no_nb, Lapply3},
  1653. {"evlis", Levlis, too_many_1, wrong_no_1},
  1654. {"funcall", Lfuncall1, Lfuncall2, Lfuncalln},
  1655. {"funcall*", Lfuncall1, Lfuncall2, Lfuncalln},
  1656. #ifdef COMMON
  1657. {"values", Lvalues_1, Lvalues_2, Lvalues},
  1658. {"macroexpand", Lmacroexpand, Lmacroexpand_2, wrong_no_1},
  1659. {"macroexpand-1", Lmacroexpand_1, Lmacroexpand_1_2, wrong_no_1},
  1660. #else
  1661. {"macroexpand", Lmacroexpand, too_many_1, wrong_no_1},
  1662. {"macroexpand-1", Lmacroexpand_1, too_many_1, wrong_no_1},
  1663. #endif
  1664. {NULL, 0, 0, 0}
  1665. };
  1666. /* end of eval1.c */