eval1.c 53 KB

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