expand.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/__scm.h"
  23. #include "libguile/_scm.h"
  24. #include "libguile/continuations.h"
  25. #include "libguile/eq.h"
  26. #include "libguile/list.h"
  27. #include "libguile/macros.h"
  28. #include "libguile/expand.h"
  29. #include "libguile/modules.h"
  30. #include "libguile/srcprop.h"
  31. #include "libguile/ports.h"
  32. #include "libguile/print.h"
  33. #include "libguile/strings.h"
  34. #include "libguile/throw.h"
  35. #include "libguile/validate.h"
  36. SCM scm_exp_vtable_vtable;
  37. static SCM exp_vtables[SCM_NUM_EXPANDED_TYPES];
  38. static size_t exp_nfields[SCM_NUM_EXPANDED_TYPES];
  39. static SCM const_unbound;
  40. static const char* exp_names[SCM_NUM_EXPANDED_TYPES];
  41. static const char** exp_field_names[SCM_NUM_EXPANDED_TYPES];
  42. /* The trailing underscores on these first to are to avoid spurious
  43. conflicts with macros defined on MinGW. */
  44. #define VOID_(src) \
  45. SCM_MAKE_EXPANDED_VOID(src)
  46. #define CONST_(src, exp) \
  47. SCM_MAKE_EXPANDED_CONST(src, exp)
  48. #define PRIMITIVE_REF(src, name) \
  49. SCM_MAKE_EXPANDED_PRIMITIVE_REF(src, name)
  50. #define LEXICAL_REF(src, name, gensym) \
  51. SCM_MAKE_EXPANDED_LEXICAL_REF(src, name, gensym)
  52. #define LEXICAL_SET(src, name, gensym, exp) \
  53. SCM_MAKE_EXPANDED_LEXICAL_SET(src, name, gensym, exp)
  54. #define MODULE_REF(src, mod, name, public) \
  55. SCM_MAKE_EXPANDED_MODULE_REF(src, mod, name, public)
  56. #define MODULE_SET(src, mod, name, public, exp) \
  57. SCM_MAKE_EXPANDED_MODULE_SET(src, mod, name, public, exp)
  58. #define TOPLEVEL_REF(src, name) \
  59. SCM_MAKE_EXPANDED_TOPLEVEL_REF(src, name)
  60. #define TOPLEVEL_SET(src, name, exp) \
  61. SCM_MAKE_EXPANDED_TOPLEVEL_SET(src, name, exp)
  62. #define TOPLEVEL_DEFINE(src, name, exp) \
  63. SCM_MAKE_EXPANDED_TOPLEVEL_DEFINE(src, name, exp)
  64. #define CONDITIONAL(src, test, consequent, alternate) \
  65. SCM_MAKE_EXPANDED_CONDITIONAL(src, test, consequent, alternate)
  66. #define PRIMCALL(src, name, exps) \
  67. SCM_MAKE_EXPANDED_PRIMCALL(src, name, exps)
  68. #define CALL(src, proc, exps) \
  69. SCM_MAKE_EXPANDED_CALL(src, proc, exps)
  70. #define SEQ(src, head, tail) \
  71. SCM_MAKE_EXPANDED_SEQ(src, head, tail)
  72. #define LAMBDA(src, meta, body) \
  73. SCM_MAKE_EXPANDED_LAMBDA(src, meta, body)
  74. #define LAMBDA_CASE(src, req, opt, rest, kw, inits, gensyms, body, alternate) \
  75. SCM_MAKE_EXPANDED_LAMBDA_CASE(src, req, opt, rest, kw, inits, gensyms, body, alternate)
  76. #define LET(src, names, gensyms, vals, body) \
  77. SCM_MAKE_EXPANDED_LET(src, names, gensyms, vals, body)
  78. #define LETREC(src, in_order_p, names, gensyms, vals, body) \
  79. SCM_MAKE_EXPANDED_LETREC(src, in_order_p, names, gensyms, vals, body)
  80. #define CAR(x) SCM_CAR(x)
  81. #define CDR(x) SCM_CDR(x)
  82. #define CAAR(x) SCM_CAAR(x)
  83. #define CADR(x) SCM_CADR(x)
  84. #define CDAR(x) SCM_CDAR(x)
  85. #define CDDR(x) SCM_CDDR(x)
  86. #define CADDR(x) SCM_CADDR(x)
  87. #define CDDDR(x) SCM_CDDDR(x)
  88. #define CADDDR(x) SCM_CADDDR(x)
  89. /* Abbreviate SCM_EXPANDED_REF. */
  90. #define REF(x,type,field) \
  91. (scm_struct_ref (x, SCM_I_MAKINUM (SCM_EXPANDED_##type##_##field)))
  92. static const char s_bad_expression[] = "Bad expression";
  93. static const char s_expression[] = "Missing or extra expression in";
  94. static const char s_missing_expression[] = "Missing expression in";
  95. static const char s_extra_expression[] = "Extra expression in";
  96. static const char s_bad_define[] = "Bad define placement";
  97. static const char s_missing_clauses[] = "Missing clauses";
  98. static const char s_misplaced_else_clause[] = "Misplaced else clause";
  99. static const char s_bad_cond_clause[] = "Bad cond clause";
  100. static const char s_missing_recipient[] = "Missing recipient in";
  101. static const char s_bad_variable[] = "Bad variable";
  102. static const char s_bad_bindings[] = "Bad bindings";
  103. static const char s_bad_binding[] = "Bad binding";
  104. static const char s_duplicate_binding[] = "Duplicate binding";
  105. static const char s_bad_formals[] = "Bad formals";
  106. static const char s_bad_formal[] = "Bad formal";
  107. static const char s_duplicate_formal[] = "Duplicate formal";
  108. static void syntax_error (const char* const, const SCM, const SCM) SCM_NORETURN;
  109. SCM_SYMBOL (syntax_error_key, "syntax-error");
  110. /* Shortcut macros to simplify syntax error handling. */
  111. #define ASSERT_SYNTAX(cond, message, form) \
  112. { if (SCM_UNLIKELY (!(cond))) \
  113. syntax_error (message, form, SCM_UNDEFINED); }
  114. #define ASSERT_SYNTAX_2(cond, message, form, expr) \
  115. { if (SCM_UNLIKELY (!(cond))) \
  116. syntax_error (message, form, expr); }
  117. /* Primitive syntax. */
  118. #define SCM_SYNTAX(STR, CFN) \
  119. SCM_SNARF_HERE(static SCM CFN (SCM xorig, SCM env)) \
  120. SCM_SNARF_INIT(scm_c_define (STR, scm_i_make_primitive_macro (STR, CFN)))
  121. /* True primitive syntax */
  122. SCM_SYNTAX ("@", expand_at);
  123. SCM_SYNTAX ("@@", expand_atat);
  124. SCM_SYNTAX ("begin", expand_begin);
  125. SCM_SYNTAX ("define", expand_define);
  126. SCM_SYNTAX ("eval-when", expand_eval_when);
  127. SCM_SYNTAX ("if", expand_if);
  128. SCM_SYNTAX ("lambda", expand_lambda);
  129. SCM_SYNTAX ("let", expand_let);
  130. SCM_SYNTAX ("quote", expand_quote);
  131. SCM_SYNTAX ("set!", expand_set_x);
  132. /* Convenient syntax during boot, expands to primitive syntax. Replaced after
  133. psyntax boots. */
  134. SCM_SYNTAX ("and", expand_and);
  135. SCM_SYNTAX ("cond", expand_cond);
  136. SCM_SYNTAX ("letrec", expand_letrec);
  137. SCM_SYNTAX ("letrec*", expand_letrec_star);
  138. SCM_SYNTAX ("let*", expand_letstar);
  139. SCM_SYNTAX ("or", expand_or);
  140. SCM_SYNTAX ("lambda*", expand_lambda_star);
  141. SCM_SYNTAX ("case-lambda", expand_case_lambda);
  142. SCM_SYNTAX ("case-lambda*", expand_case_lambda_star);
  143. SCM_GLOBAL_SYMBOL (scm_sym_arrow, "=>");
  144. SCM_GLOBAL_SYMBOL (scm_sym_at, "@");
  145. SCM_GLOBAL_SYMBOL (scm_sym_atat, "@@");
  146. SCM_GLOBAL_SYMBOL (scm_sym_begin, "begin");
  147. SCM_GLOBAL_SYMBOL (scm_sym_case, "case");
  148. SCM_GLOBAL_SYMBOL (scm_sym_cond, "cond");
  149. SCM_GLOBAL_SYMBOL (scm_sym_define, "define");
  150. SCM_GLOBAL_SYMBOL (scm_sym_else, "else");
  151. SCM_GLOBAL_SYMBOL (scm_sym_eval_when, "eval-when");
  152. SCM_GLOBAL_SYMBOL (scm_sym_if, "if");
  153. SCM_GLOBAL_SYMBOL (scm_sym_lambda, "lambda");
  154. SCM_GLOBAL_SYMBOL (scm_sym_let, "let");
  155. SCM_GLOBAL_SYMBOL (scm_sym_letrec, "letrec");
  156. SCM_GLOBAL_SYMBOL (scm_sym_letstar, "let*");
  157. SCM_GLOBAL_SYMBOL (scm_sym_or, "or");
  158. SCM_SYMBOL (sym_call_with_prompt, "call-with-prompt");
  159. SCM_GLOBAL_SYMBOL (scm_sym_quote, "quote");
  160. SCM_GLOBAL_SYMBOL (scm_sym_set_x, "set!");
  161. SCM_SYMBOL (sym_lambda_star, "lambda*");
  162. SCM_SYMBOL (sym_eval, "eval");
  163. SCM_SYMBOL (sym_load, "load");
  164. SCM_SYMBOL (sym_primitive, "primitive");
  165. SCM_GLOBAL_SYMBOL (scm_sym_unquote, "unquote");
  166. SCM_GLOBAL_SYMBOL (scm_sym_quasiquote, "quasiquote");
  167. SCM_GLOBAL_SYMBOL (scm_sym_uq_splicing, "unquote-splicing");
  168. SCM_KEYWORD (kw_allow_other_keys, "allow-other-keys");
  169. SCM_KEYWORD (kw_optional, "optional");
  170. SCM_KEYWORD (kw_key, "key");
  171. SCM_KEYWORD (kw_rest, "rest");
  172. /* Signal a syntax error. We distinguish between the form that caused the
  173. * error and the enclosing expression. The error message will print out as
  174. * shown in the following pattern. The file name and line number are only
  175. * given when they can be determined from the erroneous form or from the
  176. * enclosing expression.
  177. *
  178. * <filename>: In procedure memoization:
  179. * <filename>: In file <name>, line <nr>: <error-message> in <expression>. */
  180. static void
  181. syntax_error (const char* const msg, const SCM form, const SCM expr)
  182. {
  183. SCM msg_string = scm_from_locale_string (msg);
  184. SCM filename = SCM_BOOL_F;
  185. SCM linenr = SCM_BOOL_F;
  186. const char *format;
  187. SCM args;
  188. if (scm_is_pair (form))
  189. {
  190. filename = scm_source_property (form, scm_sym_filename);
  191. linenr = scm_source_property (form, scm_sym_line);
  192. }
  193. if (scm_is_false (filename) && scm_is_false (linenr) && scm_is_pair (expr))
  194. {
  195. filename = scm_source_property (expr, scm_sym_filename);
  196. linenr = scm_source_property (expr, scm_sym_line);
  197. }
  198. if (!SCM_UNBNDP (expr))
  199. {
  200. if (scm_is_true (filename))
  201. {
  202. format = "In file ~S, line ~S: ~A ~S in expression ~S.";
  203. args = scm_list_5 (filename, linenr, msg_string, form, expr);
  204. }
  205. else if (scm_is_true (linenr))
  206. {
  207. format = "In line ~S: ~A ~S in expression ~S.";
  208. args = scm_list_4 (linenr, msg_string, form, expr);
  209. }
  210. else
  211. {
  212. format = "~A ~S in expression ~S.";
  213. args = scm_list_3 (msg_string, form, expr);
  214. }
  215. }
  216. else
  217. {
  218. if (scm_is_true (filename))
  219. {
  220. format = "In file ~S, line ~S: ~A ~S.";
  221. args = scm_list_4 (filename, linenr, msg_string, form);
  222. }
  223. else if (scm_is_true (linenr))
  224. {
  225. format = "In line ~S: ~A ~S.";
  226. args = scm_list_3 (linenr, msg_string, form);
  227. }
  228. else
  229. {
  230. format = "~A ~S.";
  231. args = scm_list_2 (msg_string, form);
  232. }
  233. }
  234. scm_error (syntax_error_key, "memoization", format, args, SCM_BOOL_F);
  235. }
  236. static int
  237. expand_env_var_is_free (SCM env, SCM x)
  238. {
  239. for (; scm_is_pair (env); env = CDR (env))
  240. if (scm_is_eq (x, CAAR (env)))
  241. return 0; /* bound */
  242. return 1; /* free */
  243. }
  244. static SCM
  245. expand_env_ref_macro (SCM env, SCM x)
  246. {
  247. SCM var;
  248. if (!expand_env_var_is_free (env, x))
  249. return SCM_BOOL_F; /* lexical */
  250. var = scm_module_variable (scm_current_module (), x);
  251. if (scm_is_true (var) && scm_is_true (scm_variable_bound_p (var))
  252. && scm_is_true (scm_macro_p (scm_variable_ref (var))))
  253. return scm_variable_ref (var);
  254. else
  255. return SCM_BOOL_F; /* anything else */
  256. }
  257. static SCM
  258. expand_env_lexical_gensym (SCM env, SCM name)
  259. {
  260. for (; scm_is_pair (env); env = CDR (env))
  261. if (scm_is_eq (name, CAAR (env)))
  262. return CDAR (env); /* bound */
  263. return SCM_BOOL_F; /* free */
  264. }
  265. static SCM
  266. expand_env_extend (SCM env, SCM names, SCM vars)
  267. {
  268. while (scm_is_pair (names))
  269. {
  270. env = scm_acons (CAR (names), CAR (vars), env);
  271. names = CDR (names);
  272. vars = CDR (vars);
  273. }
  274. return env;
  275. }
  276. static SCM
  277. expand (SCM exp, SCM env)
  278. {
  279. if (scm_is_pair (exp))
  280. {
  281. SCM car;
  282. scm_t_macro_primitive trans = NULL;
  283. SCM macro = SCM_BOOL_F;
  284. car = CAR (exp);
  285. if (scm_is_symbol (car))
  286. macro = expand_env_ref_macro (env, car);
  287. if (scm_is_true (macro))
  288. trans = scm_i_macro_primitive (macro);
  289. if (trans)
  290. return trans (exp, env);
  291. else
  292. {
  293. SCM arg_exps = SCM_EOL;
  294. SCM args = SCM_EOL;
  295. SCM proc = expand (CAR (exp), env);
  296. for (arg_exps = CDR (exp); scm_is_pair (arg_exps);
  297. arg_exps = CDR (arg_exps))
  298. args = scm_cons (expand (CAR (arg_exps), env), args);
  299. args = scm_reverse_x (args, SCM_UNDEFINED);
  300. if (!scm_is_null (arg_exps))
  301. syntax_error ("expected a proper list", exp, SCM_UNDEFINED);
  302. if (SCM_EXPANDED_TYPE (proc) == SCM_EXPANDED_PRIMITIVE_REF)
  303. return PRIMCALL (scm_source_properties (exp),
  304. SCM_EXPANDED_REF (proc, PRIMITIVE_REF, NAME),
  305. args);
  306. else
  307. return CALL (scm_source_properties (exp), proc, args);
  308. }
  309. }
  310. else if (scm_is_symbol (exp))
  311. {
  312. SCM gensym = expand_env_lexical_gensym (env, exp);
  313. if (scm_is_true (gensym))
  314. return LEXICAL_REF (SCM_BOOL_F, exp, gensym);
  315. else
  316. return TOPLEVEL_REF (SCM_BOOL_F, exp);
  317. }
  318. else
  319. return CONST_ (SCM_BOOL_F, exp);
  320. }
  321. static SCM
  322. expand_exprs (SCM forms, const SCM env)
  323. {
  324. SCM ret = SCM_EOL;
  325. for (; !scm_is_null (forms); forms = CDR (forms))
  326. ret = scm_cons (expand (CAR (forms), env), ret);
  327. return scm_reverse_x (ret, SCM_UNDEFINED);
  328. }
  329. static SCM
  330. expand_sequence (const SCM forms, const SCM env)
  331. {
  332. ASSERT_SYNTAX (scm_ilength (forms) >= 1, s_bad_expression,
  333. scm_cons (scm_sym_begin, forms));
  334. if (scm_is_null (CDR (forms)))
  335. return expand (CAR (forms), env);
  336. else
  337. return SEQ (scm_source_properties (forms),
  338. expand (CAR (forms), env),
  339. expand_sequence (CDR (forms), env));
  340. }
  341. static SCM
  342. expand_at (SCM expr, SCM env SCM_UNUSED)
  343. {
  344. ASSERT_SYNTAX (scm_ilength (expr) == 3, s_bad_expression, expr);
  345. ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr);
  346. ASSERT_SYNTAX (scm_is_symbol (CADDR (expr)), s_bad_expression, expr);
  347. return MODULE_REF (scm_source_properties (expr),
  348. CADR (expr), CADDR (expr), SCM_BOOL_T);
  349. }
  350. static SCM
  351. expand_atat (SCM expr, SCM env SCM_UNUSED)
  352. {
  353. ASSERT_SYNTAX (scm_ilength (expr) == 3, s_bad_expression, expr);
  354. ASSERT_SYNTAX (scm_is_symbol (CADDR (expr)), s_bad_expression, expr);
  355. if (scm_is_eq (CADR (expr), sym_primitive))
  356. return PRIMITIVE_REF (scm_source_properties (expr), CADDR (expr));
  357. ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr);
  358. return MODULE_REF (scm_source_properties (expr),
  359. CADR (expr), CADDR (expr), SCM_BOOL_F);
  360. }
  361. static SCM
  362. expand_and (SCM expr, SCM env)
  363. {
  364. const SCM cdr_expr = CDR (expr);
  365. if (scm_is_null (cdr_expr))
  366. return CONST_ (SCM_BOOL_F, SCM_BOOL_T);
  367. ASSERT_SYNTAX (scm_is_pair (cdr_expr), s_bad_expression, expr);
  368. if (scm_is_null (CDR (cdr_expr)))
  369. return expand (CAR (cdr_expr), env);
  370. else
  371. return CONDITIONAL (scm_source_properties (expr),
  372. expand (CAR (cdr_expr), env),
  373. expand_and (cdr_expr, env),
  374. CONST_ (SCM_BOOL_F, SCM_BOOL_F));
  375. }
  376. static SCM
  377. expand_begin (SCM expr, SCM env)
  378. {
  379. const SCM cdr_expr = CDR (expr);
  380. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 1, s_bad_expression, expr);
  381. return expand_sequence (cdr_expr, env);
  382. }
  383. static SCM
  384. expand_cond_clauses (SCM clause, SCM rest, int elp, int alp, SCM env)
  385. {
  386. SCM test;
  387. const long length = scm_ilength (clause);
  388. ASSERT_SYNTAX (length >= 1, s_bad_cond_clause, clause);
  389. test = CAR (clause);
  390. if (scm_is_eq (test, scm_sym_else) && elp)
  391. {
  392. const int last_clause_p = scm_is_null (rest);
  393. ASSERT_SYNTAX (length >= 2, s_bad_cond_clause, clause);
  394. ASSERT_SYNTAX (last_clause_p, s_misplaced_else_clause, clause);
  395. return expand_sequence (CDR (clause), env);
  396. }
  397. if (scm_is_null (rest))
  398. rest = VOID_ (SCM_BOOL_F);
  399. else
  400. rest = expand_cond_clauses (CAR (rest), CDR (rest), elp, alp, env);
  401. if (length >= 2
  402. && scm_is_eq (CADR (clause), scm_sym_arrow)
  403. && alp)
  404. {
  405. SCM tmp = scm_gensym (scm_from_locale_string ("cond "));
  406. SCM new_env = scm_acons (tmp, tmp, env);
  407. ASSERT_SYNTAX (length > 2, s_missing_recipient, clause);
  408. ASSERT_SYNTAX (length == 3, s_extra_expression, clause);
  409. return LET (SCM_BOOL_F,
  410. scm_list_1 (tmp),
  411. scm_list_1 (tmp),
  412. scm_list_1 (expand (test, env)),
  413. CONDITIONAL (SCM_BOOL_F,
  414. LEXICAL_REF (SCM_BOOL_F, tmp, tmp),
  415. CALL (SCM_BOOL_F,
  416. expand (CADDR (clause), new_env),
  417. scm_list_1 (LEXICAL_REF (SCM_BOOL_F,
  418. tmp, tmp))),
  419. rest));
  420. }
  421. /* FIXME length == 1 case */
  422. else
  423. return CONDITIONAL (SCM_BOOL_F,
  424. expand (test, env),
  425. expand_sequence (CDR (clause), env),
  426. rest);
  427. }
  428. static SCM
  429. expand_cond (SCM expr, SCM env)
  430. {
  431. const int else_literal_p = expand_env_var_is_free (env, scm_sym_else);
  432. const int arrow_literal_p = expand_env_var_is_free (env, scm_sym_arrow);
  433. const SCM clauses = CDR (expr);
  434. ASSERT_SYNTAX (scm_ilength (clauses) >= 0, s_bad_expression, expr);
  435. ASSERT_SYNTAX (scm_ilength (clauses) >= 1, s_missing_clauses, expr);
  436. return expand_cond_clauses (CAR (clauses), CDR (clauses),
  437. else_literal_p, arrow_literal_p, env);
  438. }
  439. /* lone forward decl */
  440. static SCM expand_lambda (SCM expr, SCM env);
  441. /* According to Section 5.2.1 of R5RS we first have to make sure that the
  442. variable is bound, and then perform the `(set! variable expression)'
  443. operation. However, EXPRESSION _can_ be evaluated before VARIABLE is
  444. bound. This means that EXPRESSION won't necessarily be able to assign
  445. values to VARIABLE as in `(define foo (begin (set! foo 1) (+ foo 1)))'. */
  446. static SCM
  447. expand_define (SCM expr, SCM env)
  448. {
  449. const SCM cdr_expr = CDR (expr);
  450. SCM body;
  451. SCM variable;
  452. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  453. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  454. ASSERT_SYNTAX (!scm_is_pair (env), s_bad_define, expr);
  455. body = CDR (cdr_expr);
  456. variable = CAR (cdr_expr);
  457. if (scm_is_pair (variable))
  458. {
  459. ASSERT_SYNTAX_2 (scm_is_symbol (CAR (variable)), s_bad_variable, variable, expr);
  460. return TOPLEVEL_DEFINE
  461. (scm_source_properties (expr),
  462. CAR (variable),
  463. expand_lambda (scm_cons (scm_sym_lambda, scm_cons (CDR (variable), body)),
  464. env));
  465. }
  466. ASSERT_SYNTAX_2 (scm_is_symbol (variable), s_bad_variable, variable, expr);
  467. ASSERT_SYNTAX (scm_ilength (body) == 1, s_expression, expr);
  468. return TOPLEVEL_DEFINE (scm_source_properties (expr), variable,
  469. expand (CAR (body), env));
  470. }
  471. static SCM
  472. expand_eval_when (SCM expr, SCM env)
  473. {
  474. ASSERT_SYNTAX (scm_ilength (expr) >= 3, s_bad_expression, expr);
  475. ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr);
  476. if (scm_is_true (scm_memq (sym_eval, CADR (expr)))
  477. || scm_is_true (scm_memq (sym_load, CADR (expr))))
  478. return expand_sequence (CDDR (expr), env);
  479. else
  480. return VOID_ (scm_source_properties (expr));
  481. }
  482. static SCM
  483. expand_if (SCM expr, SCM env SCM_UNUSED)
  484. {
  485. const SCM cdr_expr = CDR (expr);
  486. const long length = scm_ilength (cdr_expr);
  487. ASSERT_SYNTAX (length == 2 || length == 3, s_expression, expr);
  488. return CONDITIONAL (scm_source_properties (expr),
  489. expand (CADR (expr), env),
  490. expand (CADDR (expr), env),
  491. ((length == 3)
  492. ? expand (CADDDR (expr), env)
  493. : VOID_ (SCM_BOOL_F)));
  494. }
  495. /* A helper function for expand_lambda to support checking for duplicate
  496. * formal arguments: Return true if OBJ is `eq?' to one of the elements of
  497. * LIST or to the CDR of the last cons. Therefore, LIST may have any of the
  498. * forms that a formal argument can have:
  499. * <rest>, (<arg1> ...), (<arg1> ... . <rest>) */
  500. static int
  501. c_improper_memq (SCM obj, SCM list)
  502. {
  503. for (; scm_is_pair (list); list = CDR (list))
  504. {
  505. if (scm_is_eq (CAR (list), obj))
  506. return 1;
  507. }
  508. return scm_is_eq (list, obj);
  509. }
  510. static SCM
  511. expand_lambda_case (SCM clause, SCM alternate, SCM env)
  512. {
  513. SCM formals;
  514. SCM rest;
  515. SCM req = SCM_EOL;
  516. SCM vars = SCM_EOL;
  517. SCM body;
  518. int nreq = 0;
  519. ASSERT_SYNTAX (scm_is_pair (clause) && scm_is_pair (CDR (clause)),
  520. s_bad_expression, scm_cons (scm_sym_lambda, clause));
  521. /* Before iterating the list of formal arguments, make sure the formals
  522. * actually are given as either a symbol or a non-cyclic list. */
  523. formals = CAR (clause);
  524. if (scm_is_pair (formals))
  525. {
  526. /* Dirk:FIXME:: We should check for a cyclic list of formals, and if
  527. * detected, report a 'Bad formals' error. */
  528. }
  529. else
  530. ASSERT_SYNTAX_2 (scm_is_symbol (formals) || scm_is_null (formals),
  531. s_bad_formals, formals, scm_cons (scm_sym_lambda, clause));
  532. /* Now iterate the list of formal arguments to check if all formals are
  533. * symbols, and that there are no duplicates. */
  534. while (scm_is_pair (formals))
  535. {
  536. const SCM formal = CAR (formals);
  537. formals = CDR (formals);
  538. ASSERT_SYNTAX_2 (scm_is_symbol (formal), s_bad_formal, formal,
  539. scm_cons (scm_sym_lambda, clause));
  540. ASSERT_SYNTAX_2 (!c_improper_memq (formal, formals), s_duplicate_formal,
  541. formal, scm_cons (scm_sym_lambda, clause));
  542. nreq++;
  543. req = scm_cons (formal, req);
  544. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  545. env = scm_acons (formal, CAR (vars), env);
  546. }
  547. ASSERT_SYNTAX_2 (scm_is_null (formals) || scm_is_symbol (formals),
  548. s_bad_formal, formals, scm_cons (scm_sym_lambda, clause));
  549. if (scm_is_symbol (formals))
  550. {
  551. rest = formals;
  552. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  553. env = scm_acons (rest, CAR (vars), env);
  554. }
  555. else
  556. rest = SCM_BOOL_F;
  557. body = expand_sequence (CDR (clause), env);
  558. req = scm_reverse_x (req, SCM_UNDEFINED);
  559. vars = scm_reverse_x (vars, SCM_UNDEFINED);
  560. if (scm_is_true (alternate) && !(SCM_EXPANDED_P (alternate) && SCM_EXPANDED_TYPE (alternate) == SCM_EXPANDED_LAMBDA_CASE))
  561. abort ();
  562. return LAMBDA_CASE (SCM_BOOL_F, req, SCM_BOOL_F, rest, SCM_BOOL_F,
  563. SCM_EOL, vars, body, alternate);
  564. }
  565. static SCM
  566. expand_lambda (SCM expr, SCM env)
  567. {
  568. return LAMBDA (scm_source_properties (expr),
  569. SCM_EOL,
  570. expand_lambda_case (CDR (expr), SCM_BOOL_F, env));
  571. }
  572. static SCM
  573. expand_lambda_star_case (SCM clause, SCM alternate, SCM env)
  574. {
  575. SCM req, opt, kw, allow_other_keys, rest, formals, vars, body, tmp;
  576. SCM inits;
  577. int nreq, nopt;
  578. const long length = scm_ilength (clause);
  579. ASSERT_SYNTAX (length >= 1, s_bad_expression,
  580. scm_cons (sym_lambda_star, clause));
  581. ASSERT_SYNTAX (length >= 2, s_missing_expression,
  582. scm_cons (sym_lambda_star, clause));
  583. formals = CAR (clause);
  584. body = CDR (clause);
  585. nreq = nopt = 0;
  586. req = opt = kw = SCM_EOL;
  587. rest = allow_other_keys = SCM_BOOL_F;
  588. while (scm_is_pair (formals) && scm_is_symbol (CAR (formals)))
  589. {
  590. nreq++;
  591. req = scm_cons (CAR (formals), req);
  592. formals = scm_cdr (formals);
  593. }
  594. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_optional))
  595. {
  596. formals = CDR (formals);
  597. while (scm_is_pair (formals)
  598. && (scm_is_symbol (CAR (formals)) || scm_is_pair (CAR (formals))))
  599. {
  600. nopt++;
  601. opt = scm_cons (CAR (formals), opt);
  602. formals = scm_cdr (formals);
  603. }
  604. }
  605. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_key))
  606. {
  607. formals = CDR (formals);
  608. while (scm_is_pair (formals)
  609. && (scm_is_symbol (CAR (formals)) || scm_is_pair (CAR (formals))))
  610. {
  611. kw = scm_cons (CAR (formals), kw);
  612. formals = scm_cdr (formals);
  613. }
  614. }
  615. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_allow_other_keys))
  616. {
  617. formals = CDR (formals);
  618. allow_other_keys = SCM_BOOL_T;
  619. }
  620. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_rest))
  621. {
  622. ASSERT_SYNTAX (scm_ilength (formals) == 2, s_bad_formals,
  623. CAR (clause));
  624. rest = CADR (formals);
  625. }
  626. else if (scm_is_symbol (formals))
  627. rest = formals;
  628. else
  629. {
  630. ASSERT_SYNTAX (scm_is_null (formals), s_bad_formals, CAR (clause));
  631. rest = SCM_BOOL_F;
  632. }
  633. /* Now, iterate through them a second time, building up an expansion-time
  634. environment, checking, expanding and canonicalizing the opt/kw init forms,
  635. and eventually memoizing the body as well. Note that the rest argument, if
  636. any, is expanded before keyword args, thus necessitating the second
  637. pass.
  638. Also note that the specific environment during expansion of init
  639. expressions here needs to coincide with the environment when psyntax
  640. expands. A lot of effort for something that is only used in the bootstrap
  641. expandr, you say? Yes. Yes it is.
  642. */
  643. vars = SCM_EOL;
  644. req = scm_reverse_x (req, SCM_EOL);
  645. for (tmp = req; scm_is_pair (tmp); tmp = scm_cdr (tmp))
  646. {
  647. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  648. env = scm_acons (CAR (tmp), CAR (vars), env);
  649. }
  650. /* Build up opt inits and env */
  651. inits = SCM_EOL;
  652. opt = scm_reverse_x (opt, SCM_EOL);
  653. for (tmp = opt; scm_is_pair (tmp); tmp = scm_cdr (tmp))
  654. {
  655. SCM x = CAR (tmp);
  656. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  657. env = scm_acons (x, CAR (vars), env);
  658. if (scm_is_symbol (x))
  659. inits = scm_cons (CONST_ (SCM_BOOL_F, SCM_BOOL_F), inits);
  660. else
  661. {
  662. ASSERT_SYNTAX (scm_ilength (x) == 2 && scm_is_symbol (CAR (x)),
  663. s_bad_formals, CAR (clause));
  664. inits = scm_cons (expand (CADR (x), env), inits);
  665. }
  666. env = scm_acons (scm_is_symbol (x) ? x : CAR (x), CAR (vars), env);
  667. }
  668. if (scm_is_null (opt))
  669. opt = SCM_BOOL_F;
  670. /* Process rest before keyword args */
  671. if (scm_is_true (rest))
  672. {
  673. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  674. env = scm_acons (rest, CAR (vars), env);
  675. }
  676. /* Build up kw inits, env, and kw-canon list */
  677. if (scm_is_null (kw))
  678. kw = SCM_BOOL_F;
  679. else
  680. {
  681. SCM kw_canon = SCM_EOL;
  682. kw = scm_reverse_x (kw, SCM_UNDEFINED);
  683. for (tmp = kw; scm_is_pair (tmp); tmp = scm_cdr (tmp))
  684. {
  685. SCM x, sym, k, init;
  686. x = CAR (tmp);
  687. if (scm_is_symbol (x))
  688. {
  689. sym = x;
  690. init = SCM_BOOL_F;
  691. k = scm_symbol_to_keyword (sym);
  692. }
  693. else if (scm_ilength (x) == 2 && scm_is_symbol (CAR (x)))
  694. {
  695. sym = CAR (x);
  696. init = CADR (x);
  697. k = scm_symbol_to_keyword (sym);
  698. }
  699. else if (scm_ilength (x) == 3 && scm_is_symbol (CAR (x))
  700. && scm_is_keyword (CADDR (x)))
  701. {
  702. sym = CAR (x);
  703. init = CADR (x);
  704. k = CADDR (x);
  705. }
  706. else
  707. syntax_error (s_bad_formals, CAR (clause), SCM_UNDEFINED);
  708. inits = scm_cons (expand (init, env), inits);
  709. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  710. kw_canon = scm_cons (scm_list_3 (k, sym, CAR (vars)), kw_canon);
  711. env = scm_acons (sym, CAR (vars), env);
  712. }
  713. kw_canon = scm_reverse_x (kw_canon, SCM_UNDEFINED);
  714. kw = scm_cons (allow_other_keys, kw_canon);
  715. }
  716. /* We should check for no duplicates, but given that psyntax does this
  717. already, we can punt on it here... */
  718. vars = scm_reverse_x (vars, SCM_UNDEFINED);
  719. inits = scm_reverse_x (inits, SCM_UNDEFINED);
  720. body = expand_sequence (body, env);
  721. return LAMBDA_CASE (SCM_BOOL_F, req, opt, rest, kw, inits, vars, body,
  722. alternate);
  723. }
  724. static SCM
  725. expand_lambda_star (SCM expr, SCM env)
  726. {
  727. return LAMBDA (scm_source_properties (expr),
  728. SCM_EOL,
  729. expand_lambda_star_case (CDR (expr), SCM_BOOL_F, env));
  730. }
  731. static SCM
  732. expand_case_lambda_clauses (SCM expr, SCM rest, SCM env)
  733. {
  734. SCM alt;
  735. if (scm_is_pair (rest))
  736. alt = expand_case_lambda_clauses (CAR (rest), CDR (rest), env);
  737. else
  738. alt = SCM_BOOL_F;
  739. return expand_lambda_case (expr, alt, env);
  740. }
  741. static SCM
  742. expand_case_lambda (SCM expr, SCM env)
  743. {
  744. ASSERT_SYNTAX (scm_is_pair (CDR (expr)), s_missing_expression, expr);
  745. return LAMBDA (scm_source_properties (expr),
  746. SCM_EOL,
  747. expand_case_lambda_clauses (CADR (expr), CDDR (expr), env));
  748. }
  749. static SCM
  750. expand_case_lambda_star_clauses (SCM expr, SCM rest, SCM env)
  751. {
  752. SCM alt;
  753. if (scm_is_pair (rest))
  754. alt = expand_case_lambda_star_clauses (CAR (rest), CDR (rest), env);
  755. else
  756. alt = SCM_BOOL_F;
  757. return expand_lambda_star_case (expr, alt, env);
  758. }
  759. static SCM
  760. expand_case_lambda_star (SCM expr, SCM env)
  761. {
  762. ASSERT_SYNTAX (scm_is_pair (CDR (expr)), s_missing_expression, expr);
  763. return LAMBDA (scm_source_properties (expr),
  764. SCM_EOL,
  765. expand_case_lambda_star_clauses (CADR (expr), CDDR (expr), env));
  766. }
  767. /* Check if the format of the bindings is ((<symbol> <init-form>) ...). */
  768. static void
  769. check_bindings (const SCM bindings, const SCM expr)
  770. {
  771. SCM binding_idx;
  772. ASSERT_SYNTAX_2 (scm_ilength (bindings) >= 0,
  773. s_bad_bindings, bindings, expr);
  774. binding_idx = bindings;
  775. for (; !scm_is_null (binding_idx); binding_idx = CDR (binding_idx))
  776. {
  777. SCM name; /* const */
  778. const SCM binding = CAR (binding_idx);
  779. ASSERT_SYNTAX_2 (scm_ilength (binding) == 2,
  780. s_bad_binding, binding, expr);
  781. name = CAR (binding);
  782. ASSERT_SYNTAX_2 (scm_is_symbol (name), s_bad_variable, name, expr);
  783. }
  784. }
  785. /* The bindings, which must have the format ((v1 i1) (v2 i2) ... (vn in)), are
  786. * transformed to the lists (vn .. v2 v1) and (i1 i2 ... in). If a duplicate
  787. * variable name is detected, an error is signalled. */
  788. static void
  789. transform_bindings (const SCM bindings, const SCM expr,
  790. SCM *const names, SCM *const vars, SCM *const initptr)
  791. {
  792. SCM rnames = SCM_EOL;
  793. SCM rvars = SCM_EOL;
  794. SCM rinits = SCM_EOL;
  795. SCM binding_idx = bindings;
  796. for (; !scm_is_null (binding_idx); binding_idx = CDR (binding_idx))
  797. {
  798. const SCM binding = CAR (binding_idx);
  799. const SCM CDR_binding = CDR (binding);
  800. const SCM name = CAR (binding);
  801. ASSERT_SYNTAX_2 (scm_is_false (scm_c_memq (name, rnames)),
  802. s_duplicate_binding, name, expr);
  803. rnames = scm_cons (name, rnames);
  804. rvars = scm_cons (scm_gensym (SCM_UNDEFINED), rvars);
  805. rinits = scm_cons (CAR (CDR_binding), rinits);
  806. }
  807. *names = scm_reverse_x (rnames, SCM_UNDEFINED);
  808. *vars = scm_reverse_x (rvars, SCM_UNDEFINED);
  809. *initptr = scm_reverse_x (rinits, SCM_UNDEFINED);
  810. }
  811. /* FIXME: Remove named let in this boot expander. */
  812. static SCM
  813. expand_named_let (const SCM expr, SCM env)
  814. {
  815. SCM var_names, var_syms, inits;
  816. SCM inner_env;
  817. SCM name_sym;
  818. const SCM cdr_expr = CDR (expr);
  819. const SCM name = CAR (cdr_expr);
  820. const SCM cddr_expr = CDR (cdr_expr);
  821. const SCM bindings = CAR (cddr_expr);
  822. check_bindings (bindings, expr);
  823. transform_bindings (bindings, expr, &var_names, &var_syms, &inits);
  824. name_sym = scm_gensym (SCM_UNDEFINED);
  825. inner_env = scm_acons (name, name_sym, env);
  826. inner_env = expand_env_extend (inner_env, var_names, var_syms);
  827. return LETREC
  828. (scm_source_properties (expr), SCM_BOOL_F,
  829. scm_list_1 (name), scm_list_1 (name_sym),
  830. scm_list_1 (LAMBDA (SCM_BOOL_F,
  831. SCM_EOL,
  832. LAMBDA_CASE (SCM_BOOL_F, var_names, SCM_EOL, SCM_BOOL_F,
  833. SCM_BOOL_F, SCM_EOL, var_syms,
  834. expand_sequence (CDDDR (expr), inner_env),
  835. SCM_BOOL_F))),
  836. CALL (SCM_BOOL_F,
  837. LEXICAL_REF (SCM_BOOL_F, name, name_sym),
  838. expand_exprs (inits, env)));
  839. }
  840. static SCM
  841. expand_let (SCM expr, SCM env)
  842. {
  843. SCM bindings;
  844. const SCM cdr_expr = CDR (expr);
  845. const long length = scm_ilength (cdr_expr);
  846. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  847. ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
  848. bindings = CAR (cdr_expr);
  849. if (scm_is_symbol (bindings))
  850. {
  851. ASSERT_SYNTAX (length >= 3, s_missing_expression, expr);
  852. return expand_named_let (expr, env);
  853. }
  854. check_bindings (bindings, expr);
  855. if (scm_is_null (bindings))
  856. return expand_sequence (CDDR (expr), env);
  857. else
  858. {
  859. SCM var_names, var_syms, inits;
  860. transform_bindings (bindings, expr, &var_names, &var_syms, &inits);
  861. return LET (SCM_BOOL_F,
  862. var_names, var_syms, expand_exprs (inits, env),
  863. expand_sequence (CDDR (expr),
  864. expand_env_extend (env, var_names,
  865. var_syms)));
  866. }
  867. }
  868. static SCM
  869. expand_letrec_helper (SCM expr, SCM env, SCM in_order_p)
  870. {
  871. SCM bindings;
  872. const SCM cdr_expr = CDR (expr);
  873. const long length = scm_ilength (cdr_expr);
  874. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  875. ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
  876. bindings = CAR (cdr_expr);
  877. check_bindings (bindings, expr);
  878. if (scm_is_null (bindings))
  879. return expand_sequence (CDDR (expr), env);
  880. else
  881. {
  882. SCM var_names, var_syms, inits;
  883. transform_bindings (bindings, expr, &var_names, &var_syms, &inits);
  884. env = expand_env_extend (env, var_names, var_syms);
  885. return LETREC (SCM_BOOL_F, in_order_p,
  886. var_names, var_syms, expand_exprs (inits, env),
  887. expand_sequence (CDDR (expr), env));
  888. }
  889. }
  890. static SCM
  891. expand_letrec (SCM expr, SCM env)
  892. {
  893. return expand_letrec_helper (expr, env, SCM_BOOL_F);
  894. }
  895. static SCM
  896. expand_letrec_star (SCM expr, SCM env)
  897. {
  898. return expand_letrec_helper (expr, env, SCM_BOOL_T);
  899. }
  900. static SCM
  901. expand_letstar_clause (SCM bindings, SCM body, SCM env SCM_UNUSED)
  902. {
  903. if (scm_is_null (bindings))
  904. return expand_sequence (body, env);
  905. else
  906. {
  907. SCM bind, name, sym, init;
  908. ASSERT_SYNTAX (scm_is_pair (bindings), s_bad_expression, bindings);
  909. bind = CAR (bindings);
  910. ASSERT_SYNTAX (scm_ilength (bind) == 2, s_bad_binding, bind);
  911. name = CAR (bind);
  912. sym = scm_gensym (SCM_UNDEFINED);
  913. init = CADR (bind);
  914. return LET (SCM_BOOL_F, scm_list_1 (name), scm_list_1 (sym),
  915. scm_list_1 (expand (init, env)),
  916. expand_letstar_clause (CDR (bindings), body,
  917. scm_acons (name, sym, env)));
  918. }
  919. }
  920. static SCM
  921. expand_letstar (SCM expr, SCM env SCM_UNUSED)
  922. {
  923. const SCM cdr_expr = CDR (expr);
  924. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  925. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  926. return expand_letstar_clause (CADR (expr), CDDR (expr), env);
  927. }
  928. static SCM
  929. expand_or (SCM expr, SCM env SCM_UNUSED)
  930. {
  931. SCM tail = CDR (expr);
  932. const long length = scm_ilength (tail);
  933. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  934. if (scm_is_null (CDR (expr)))
  935. return CONST_ (SCM_BOOL_F, SCM_BOOL_F);
  936. else
  937. {
  938. SCM tmp = scm_gensym (SCM_UNDEFINED);
  939. return LET (SCM_BOOL_F,
  940. scm_list_1 (tmp), scm_list_1 (tmp),
  941. scm_list_1 (expand (CADR (expr), env)),
  942. CONDITIONAL (SCM_BOOL_F,
  943. LEXICAL_REF (SCM_BOOL_F, tmp, tmp),
  944. LEXICAL_REF (SCM_BOOL_F, tmp, tmp),
  945. expand_or (CDR (expr),
  946. scm_acons (tmp, tmp, env))));
  947. }
  948. }
  949. static SCM
  950. expand_quote (SCM expr, SCM env SCM_UNUSED)
  951. {
  952. SCM quotee;
  953. const SCM cdr_expr = CDR (expr);
  954. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  955. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  956. quotee = CAR (cdr_expr);
  957. return CONST_ (scm_source_properties (expr), quotee);
  958. }
  959. static SCM
  960. expand_set_x (SCM expr, SCM env)
  961. {
  962. SCM variable;
  963. SCM vmem;
  964. const SCM cdr_expr = CDR (expr);
  965. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  966. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
  967. variable = CAR (cdr_expr);
  968. vmem = expand (variable, env);
  969. switch (SCM_EXPANDED_TYPE (vmem))
  970. {
  971. case SCM_EXPANDED_LEXICAL_REF:
  972. return LEXICAL_SET (scm_source_properties (expr),
  973. SCM_EXPANDED_REF (vmem, LEXICAL_REF, NAME),
  974. SCM_EXPANDED_REF (vmem, LEXICAL_REF, GENSYM),
  975. expand (CADDR (expr), env));
  976. case SCM_EXPANDED_TOPLEVEL_REF:
  977. return TOPLEVEL_SET (scm_source_properties (expr),
  978. SCM_EXPANDED_REF (vmem, TOPLEVEL_REF, NAME),
  979. expand (CADDR (expr), env));
  980. case SCM_EXPANDED_MODULE_REF:
  981. return MODULE_SET (scm_source_properties (expr),
  982. SCM_EXPANDED_REF (vmem, MODULE_REF, MOD),
  983. SCM_EXPANDED_REF (vmem, MODULE_REF, NAME),
  984. SCM_EXPANDED_REF (vmem, MODULE_REF, PUBLIC),
  985. expand (CADDR (expr), env));
  986. default:
  987. syntax_error (s_bad_variable, variable, expr);
  988. }
  989. }
  990. /* This is the boot expander. It is later replaced with psyntax's sc-expand. */
  991. SCM_DEFINE (scm_macroexpand, "macroexpand", 1, 0, 0,
  992. (SCM exp),
  993. "Expand the expression @var{exp}.")
  994. #define FUNC_NAME s_scm_macroexpand
  995. {
  996. return expand (exp, scm_current_module ());
  997. }
  998. #undef FUNC_NAME
  999. SCM_DEFINE (scm_macroexpanded_p, "macroexpanded?", 1, 0, 0,
  1000. (SCM exp),
  1001. "Return @code{#t} if @var{exp} is an expanded expression.")
  1002. #define FUNC_NAME s_scm_macroexpanded_p
  1003. {
  1004. return scm_from_bool (SCM_EXPANDED_P (exp));
  1005. }
  1006. #undef FUNC_NAME
  1007. static void
  1008. compute_assigned (SCM exp, SCM assigned)
  1009. {
  1010. if (scm_is_null (exp) || scm_is_false (exp))
  1011. return;
  1012. if (scm_is_pair (exp))
  1013. {
  1014. compute_assigned (CAR (exp), assigned);
  1015. compute_assigned (CDR (exp), assigned);
  1016. return;
  1017. }
  1018. if (!SCM_EXPANDED_P (exp))
  1019. abort ();
  1020. switch (SCM_EXPANDED_TYPE (exp))
  1021. {
  1022. case SCM_EXPANDED_VOID:
  1023. case SCM_EXPANDED_CONST:
  1024. case SCM_EXPANDED_PRIMITIVE_REF:
  1025. case SCM_EXPANDED_LEXICAL_REF:
  1026. case SCM_EXPANDED_MODULE_REF:
  1027. case SCM_EXPANDED_TOPLEVEL_REF:
  1028. return;
  1029. case SCM_EXPANDED_LEXICAL_SET:
  1030. scm_hashq_set_x (assigned, REF (exp, LEXICAL_SET, GENSYM), SCM_BOOL_T);
  1031. compute_assigned (REF (exp, LEXICAL_SET, EXP), assigned);
  1032. return;
  1033. case SCM_EXPANDED_MODULE_SET:
  1034. compute_assigned (REF (exp, MODULE_SET, EXP), assigned);
  1035. return;
  1036. case SCM_EXPANDED_TOPLEVEL_SET:
  1037. compute_assigned (REF (exp, TOPLEVEL_SET, EXP), assigned);
  1038. return;
  1039. case SCM_EXPANDED_TOPLEVEL_DEFINE:
  1040. compute_assigned (REF (exp, TOPLEVEL_DEFINE, EXP), assigned);
  1041. return;
  1042. case SCM_EXPANDED_CONDITIONAL:
  1043. compute_assigned (REF (exp, CONDITIONAL, TEST), assigned);
  1044. compute_assigned (REF (exp, CONDITIONAL, CONSEQUENT), assigned);
  1045. compute_assigned (REF (exp, CONDITIONAL, ALTERNATE), assigned);
  1046. return;
  1047. case SCM_EXPANDED_CALL:
  1048. compute_assigned (REF (exp, CALL, PROC), assigned);
  1049. compute_assigned (REF (exp, CALL, ARGS), assigned);
  1050. return;
  1051. case SCM_EXPANDED_PRIMCALL:
  1052. compute_assigned (REF (exp, PRIMCALL, ARGS), assigned);
  1053. return;
  1054. case SCM_EXPANDED_SEQ:
  1055. compute_assigned (REF (exp, SEQ, HEAD), assigned);
  1056. compute_assigned (REF (exp, SEQ, TAIL), assigned);
  1057. return;
  1058. case SCM_EXPANDED_LAMBDA:
  1059. compute_assigned (REF (exp, LAMBDA, BODY), assigned);
  1060. return;
  1061. case SCM_EXPANDED_LAMBDA_CASE:
  1062. compute_assigned (REF (exp, LAMBDA_CASE, INITS), assigned);
  1063. compute_assigned (REF (exp, LAMBDA_CASE, BODY), assigned);
  1064. compute_assigned (REF (exp, LAMBDA_CASE, ALTERNATE), assigned);
  1065. return;
  1066. case SCM_EXPANDED_LET:
  1067. compute_assigned (REF (exp, LET, VALS), assigned);
  1068. compute_assigned (REF (exp, LET, BODY), assigned);
  1069. return;
  1070. case SCM_EXPANDED_LETREC:
  1071. {
  1072. SCM syms = REF (exp, LETREC, GENSYMS);
  1073. /* We lower letrec in this same pass, so mark these variables as
  1074. assigned. */
  1075. for (; scm_is_pair (syms); syms = CDR (syms))
  1076. scm_hashq_set_x (assigned, CAR (syms), SCM_BOOL_T);
  1077. }
  1078. compute_assigned (REF (exp, LETREC, VALS), assigned);
  1079. compute_assigned (REF (exp, LETREC, BODY), assigned);
  1080. return;
  1081. default:
  1082. abort ();
  1083. }
  1084. }
  1085. static SCM
  1086. box_value (SCM exp)
  1087. {
  1088. return PRIMCALL (SCM_BOOL_F, scm_from_latin1_symbol ("make-variable"),
  1089. scm_list_1 (exp));
  1090. }
  1091. static SCM
  1092. box_lexical (SCM name, SCM sym)
  1093. {
  1094. return LEXICAL_SET (SCM_BOOL_F, name, sym,
  1095. box_value (LEXICAL_REF (SCM_BOOL_F, name, sym)));
  1096. }
  1097. static SCM
  1098. init_if_unbound (SCM src, SCM name, SCM sym, SCM init)
  1099. {
  1100. return CONDITIONAL (src,
  1101. PRIMCALL (src,
  1102. scm_from_latin1_symbol ("eq?"),
  1103. scm_list_2 (LEXICAL_REF (src, name, sym),
  1104. const_unbound)),
  1105. LEXICAL_SET (src, name, sym, init),
  1106. VOID_ (src));
  1107. }
  1108. static SCM
  1109. init_boxes (SCM names, SCM syms, SCM vals, SCM body)
  1110. {
  1111. if (scm_is_null (names)) return body;
  1112. return SEQ (SCM_BOOL_F,
  1113. PRIMCALL
  1114. (SCM_BOOL_F,
  1115. scm_from_latin1_symbol ("variable-set!"),
  1116. scm_list_2 (LEXICAL_REF (SCM_BOOL_F, CAR (names), CAR (syms)),
  1117. CAR (vals))),
  1118. init_boxes (CDR (names), CDR (syms), CDR (vals), body));
  1119. }
  1120. static SCM
  1121. convert_assignment (SCM exp, SCM assigned)
  1122. {
  1123. if (scm_is_null (exp) || scm_is_false (exp))
  1124. return exp;
  1125. if (scm_is_pair (exp))
  1126. return scm_cons (convert_assignment (CAR (exp), assigned),
  1127. convert_assignment (CDR (exp), assigned));
  1128. if (!SCM_EXPANDED_P (exp))
  1129. abort ();
  1130. switch (SCM_EXPANDED_TYPE (exp))
  1131. {
  1132. case SCM_EXPANDED_VOID:
  1133. case SCM_EXPANDED_CONST:
  1134. case SCM_EXPANDED_PRIMITIVE_REF:
  1135. case SCM_EXPANDED_MODULE_REF:
  1136. case SCM_EXPANDED_TOPLEVEL_REF:
  1137. return exp;
  1138. case SCM_EXPANDED_LEXICAL_REF:
  1139. {
  1140. SCM sym = REF (exp, LEXICAL_REF, GENSYM);
  1141. if (scm_is_true (scm_hashq_ref (assigned, sym, SCM_BOOL_F)))
  1142. return PRIMCALL
  1143. (REF (exp, LEXICAL_REF, SRC),
  1144. scm_from_latin1_symbol ("variable-ref"),
  1145. scm_list_1 (exp));
  1146. return exp;
  1147. }
  1148. case SCM_EXPANDED_LEXICAL_SET:
  1149. return PRIMCALL
  1150. (REF (exp, LEXICAL_SET, SRC),
  1151. scm_from_latin1_symbol ("variable-set!"),
  1152. scm_list_2 (LEXICAL_REF (REF (exp, LEXICAL_SET, SRC),
  1153. REF (exp, LEXICAL_SET, NAME),
  1154. REF (exp, LEXICAL_SET, GENSYM)),
  1155. convert_assignment (REF (exp, LEXICAL_SET, EXP),
  1156. assigned)));
  1157. case SCM_EXPANDED_MODULE_SET:
  1158. return MODULE_SET
  1159. (REF (exp, MODULE_SET, SRC),
  1160. REF (exp, MODULE_SET, MOD),
  1161. REF (exp, MODULE_SET, NAME),
  1162. REF (exp, MODULE_SET, PUBLIC),
  1163. convert_assignment (REF (exp, MODULE_SET, EXP), assigned));
  1164. case SCM_EXPANDED_TOPLEVEL_SET:
  1165. return TOPLEVEL_SET
  1166. (REF (exp, TOPLEVEL_SET, SRC),
  1167. REF (exp, TOPLEVEL_SET, NAME),
  1168. convert_assignment (REF (exp, TOPLEVEL_SET, EXP), assigned));
  1169. case SCM_EXPANDED_TOPLEVEL_DEFINE:
  1170. return TOPLEVEL_DEFINE
  1171. (REF (exp, TOPLEVEL_DEFINE, SRC),
  1172. REF (exp, TOPLEVEL_DEFINE, NAME),
  1173. convert_assignment (REF (exp, TOPLEVEL_DEFINE, EXP),
  1174. assigned));
  1175. case SCM_EXPANDED_CONDITIONAL:
  1176. return CONDITIONAL
  1177. (REF (exp, CONDITIONAL, SRC),
  1178. convert_assignment (REF (exp, CONDITIONAL, TEST), assigned),
  1179. convert_assignment (REF (exp, CONDITIONAL, CONSEQUENT), assigned),
  1180. convert_assignment (REF (exp, CONDITIONAL, ALTERNATE), assigned));
  1181. case SCM_EXPANDED_CALL:
  1182. return CALL
  1183. (REF (exp, CALL, SRC),
  1184. convert_assignment (REF (exp, CALL, PROC), assigned),
  1185. convert_assignment (REF (exp, CALL, ARGS), assigned));
  1186. case SCM_EXPANDED_PRIMCALL:
  1187. return PRIMCALL
  1188. (REF (exp, PRIMCALL, SRC),
  1189. REF (exp, PRIMCALL, NAME),
  1190. convert_assignment (REF (exp, PRIMCALL, ARGS), assigned));
  1191. case SCM_EXPANDED_SEQ:
  1192. return SEQ
  1193. (REF (exp, SEQ, SRC),
  1194. convert_assignment (REF (exp, SEQ, HEAD), assigned),
  1195. convert_assignment (REF (exp, SEQ, TAIL), assigned));
  1196. case SCM_EXPANDED_LAMBDA:
  1197. return LAMBDA
  1198. (REF (exp, LAMBDA, SRC),
  1199. REF (exp, LAMBDA, META),
  1200. scm_is_false (REF (exp, LAMBDA, BODY))
  1201. /* Give a body to case-lambda with no clauses. */
  1202. ? LAMBDA_CASE (SCM_BOOL_F, SCM_EOL, SCM_EOL, SCM_BOOL_F, SCM_BOOL_F,
  1203. SCM_EOL, SCM_EOL,
  1204. PRIMCALL
  1205. (SCM_BOOL_F,
  1206. scm_from_latin1_symbol ("throw"),
  1207. scm_list_5 (CONST_ (SCM_BOOL_F, scm_args_number_key),
  1208. CONST_ (SCM_BOOL_F, SCM_BOOL_F),
  1209. CONST_ (SCM_BOOL_F, scm_from_latin1_string
  1210. ("Wrong number of arguments")),
  1211. CONST_ (SCM_BOOL_F, SCM_EOL),
  1212. CONST_ (SCM_BOOL_F, SCM_BOOL_F))),
  1213. SCM_BOOL_F)
  1214. : convert_assignment (REF (exp, LAMBDA, BODY), assigned));
  1215. case SCM_EXPANDED_LAMBDA_CASE:
  1216. {
  1217. SCM src, req, opt, rest, kw, inits, syms, body, alt;
  1218. SCM namewalk, symwalk, new_inits, seq;
  1219. /* Box assigned formals. Since initializers can capture
  1220. previous formals, we convert initializers to be in the body
  1221. instead of in the "header". */
  1222. src = REF (exp, LAMBDA_CASE, SRC);
  1223. req = REF (exp, LAMBDA_CASE, REQ);
  1224. opt = REF (exp, LAMBDA_CASE, OPT);
  1225. rest = REF (exp, LAMBDA_CASE, REST);
  1226. kw = REF (exp, LAMBDA_CASE, KW);
  1227. inits = convert_assignment (REF (exp, LAMBDA_CASE, INITS), assigned);
  1228. syms = REF (exp, LAMBDA_CASE, GENSYMS);
  1229. body = convert_assignment (REF (exp, LAMBDA_CASE, BODY), assigned);
  1230. alt = convert_assignment (REF (exp, LAMBDA_CASE, ALTERNATE), assigned);
  1231. new_inits = scm_make_list (scm_length (inits), const_unbound);
  1232. seq = SCM_EOL, symwalk = syms;
  1233. /* Required arguments may need boxing. */
  1234. for (namewalk = req;
  1235. scm_is_pair (namewalk);
  1236. namewalk = CDR (namewalk), symwalk = CDR (symwalk))
  1237. {
  1238. SCM name = CAR (namewalk), sym = CAR (symwalk);
  1239. if (scm_is_true (scm_hashq_ref (assigned, sym, SCM_BOOL_F)))
  1240. seq = scm_cons (box_lexical (name, sym), seq);
  1241. }
  1242. /* Optional arguments may need initialization and/or boxing. */
  1243. for (namewalk = opt;
  1244. scm_is_pair (namewalk);
  1245. namewalk = CDR (namewalk), symwalk = CDR (symwalk),
  1246. inits = CDR (inits))
  1247. {
  1248. SCM name = CAR (namewalk), sym = CAR (symwalk), init = CAR (inits);
  1249. seq = scm_cons (init_if_unbound (src, name, sym, init), seq);
  1250. if (scm_is_true (scm_hashq_ref (assigned, sym, SCM_BOOL_F)))
  1251. seq = scm_cons (box_lexical (name, sym), seq);
  1252. }
  1253. /* Rest arguments may need boxing. */
  1254. if (scm_is_true (rest))
  1255. {
  1256. SCM sym = CAR (symwalk);
  1257. symwalk = CDR (symwalk);
  1258. if (scm_is_true (scm_hashq_ref (assigned, sym, SCM_BOOL_F)))
  1259. seq = scm_cons (box_lexical (rest, sym), seq);
  1260. }
  1261. /* The rest of the arguments, if any, are keyword arguments,
  1262. which may need initialization and/or boxing. */
  1263. for (;
  1264. scm_is_pair (symwalk);
  1265. symwalk = CDR (symwalk), inits = CDR (inits))
  1266. {
  1267. SCM sym = CAR (symwalk), init = CAR (inits);
  1268. seq = scm_cons (init_if_unbound (src, SCM_BOOL_F, sym, init), seq);
  1269. if (scm_is_true (scm_hashq_ref (assigned, sym, SCM_BOOL_F)))
  1270. seq = scm_cons (box_lexical (SCM_BOOL_F, sym), seq);
  1271. }
  1272. for (; scm_is_pair (seq); seq = CDR (seq))
  1273. body = SEQ (src, CAR (seq), body);
  1274. return LAMBDA_CASE
  1275. (src, req, opt, rest, kw, new_inits, syms, body, alt);
  1276. }
  1277. case SCM_EXPANDED_LET:
  1278. {
  1279. SCM src, names, syms, vals, body, new_vals, walk;
  1280. src = REF (exp, LET, SRC);
  1281. names = REF (exp, LET, NAMES);
  1282. syms = REF (exp, LET, GENSYMS);
  1283. vals = convert_assignment (REF (exp, LET, VALS), assigned);
  1284. body = convert_assignment (REF (exp, LET, BODY), assigned);
  1285. for (new_vals = SCM_EOL, walk = syms;
  1286. scm_is_pair (vals);
  1287. vals = CDR (vals), walk = CDR (walk))
  1288. {
  1289. SCM sym = CAR (walk), val = CAR (vals);
  1290. if (scm_is_true (scm_hashq_ref (assigned, sym, SCM_BOOL_F)))
  1291. new_vals = scm_cons (box_value (val), new_vals);
  1292. else
  1293. new_vals = scm_cons (val, new_vals);
  1294. }
  1295. new_vals = scm_reverse (new_vals);
  1296. return LET (src, names, syms, new_vals, body);
  1297. }
  1298. case SCM_EXPANDED_LETREC:
  1299. {
  1300. SCM src, names, syms, vals, empty_box, boxes, body;
  1301. src = REF (exp, LETREC, SRC);
  1302. names = REF (exp, LETREC, NAMES);
  1303. syms = REF (exp, LETREC, GENSYMS);
  1304. vals = convert_assignment (REF (exp, LETREC, VALS), assigned);
  1305. body = convert_assignment (REF (exp, LETREC, BODY), assigned);
  1306. empty_box =
  1307. PRIMCALL (SCM_BOOL_F,
  1308. scm_from_latin1_symbol ("make-undefined-variable"),
  1309. SCM_EOL);
  1310. boxes = scm_make_list (scm_length (names), empty_box);
  1311. if (scm_is_true (REF (exp, LETREC, IN_ORDER_P)))
  1312. return LET
  1313. (src, names, syms, boxes,
  1314. init_boxes (names, syms, vals, body));
  1315. else
  1316. {
  1317. SCM walk, tmps = SCM_EOL, inits = SCM_EOL;
  1318. for (walk = syms; scm_is_pair (walk); walk = CDR (walk))
  1319. {
  1320. SCM tmp = scm_gensym (SCM_UNDEFINED);
  1321. tmps = scm_cons (tmp, tmps);
  1322. inits = scm_cons (LEXICAL_REF (SCM_BOOL_F, SCM_BOOL_F, tmp),
  1323. inits);
  1324. }
  1325. tmps = scm_reverse (tmps);
  1326. inits = scm_reverse (inits);
  1327. return LET
  1328. (src, names, syms, boxes,
  1329. SEQ (src,
  1330. LET (src, names, tmps, vals,
  1331. init_boxes (names, syms, inits, VOID_ (src))),
  1332. body));
  1333. }
  1334. }
  1335. default:
  1336. abort ();
  1337. }
  1338. }
  1339. SCM
  1340. scm_convert_assignment (SCM exp)
  1341. {
  1342. SCM assigned = scm_c_make_hash_table (0);
  1343. compute_assigned (exp, assigned);
  1344. return convert_assignment (exp, assigned);
  1345. }
  1346. #define DEFINE_NAMES(type) \
  1347. { \
  1348. static const char *fields[] = SCM_EXPANDED_##type##_FIELD_NAMES; \
  1349. exp_field_names[SCM_EXPANDED_##type] = fields; \
  1350. exp_names[SCM_EXPANDED_##type] = SCM_EXPANDED_##type##_TYPE_NAME; \
  1351. exp_nfields[SCM_EXPANDED_##type] = SCM_NUM_EXPANDED_##type##_FIELDS; \
  1352. }
  1353. static SCM
  1354. make_exp_vtable (size_t n)
  1355. {
  1356. SCM layout, printer, name, code, fields;
  1357. layout = scm_string_to_symbol
  1358. (scm_string_append (scm_make_list (scm_from_size_t (exp_nfields[n]),
  1359. scm_from_locale_string ("pw"))));
  1360. printer = SCM_BOOL_F;
  1361. name = scm_from_utf8_symbol (exp_names[n]);
  1362. code = scm_from_size_t (n);
  1363. fields = SCM_EOL;
  1364. {
  1365. size_t m = exp_nfields[n];
  1366. while (m--)
  1367. fields = scm_cons (scm_from_utf8_symbol (exp_field_names[n][m]), fields);
  1368. }
  1369. return scm_c_make_struct (scm_exp_vtable_vtable, 0, 5,
  1370. SCM_UNPACK (layout), SCM_UNPACK (printer), SCM_UNPACK (name),
  1371. SCM_UNPACK (code), SCM_UNPACK (fields));
  1372. }
  1373. void
  1374. scm_init_expand ()
  1375. {
  1376. size_t n;
  1377. SCM exp_vtable_list = SCM_EOL;
  1378. DEFINE_NAMES (VOID);
  1379. DEFINE_NAMES (CONST);
  1380. DEFINE_NAMES (PRIMITIVE_REF);
  1381. DEFINE_NAMES (LEXICAL_REF);
  1382. DEFINE_NAMES (LEXICAL_SET);
  1383. DEFINE_NAMES (MODULE_REF);
  1384. DEFINE_NAMES (MODULE_SET);
  1385. DEFINE_NAMES (TOPLEVEL_REF);
  1386. DEFINE_NAMES (TOPLEVEL_SET);
  1387. DEFINE_NAMES (TOPLEVEL_DEFINE);
  1388. DEFINE_NAMES (CONDITIONAL);
  1389. DEFINE_NAMES (CALL);
  1390. DEFINE_NAMES (PRIMCALL);
  1391. DEFINE_NAMES (SEQ);
  1392. DEFINE_NAMES (LAMBDA);
  1393. DEFINE_NAMES (LAMBDA_CASE);
  1394. DEFINE_NAMES (LET);
  1395. DEFINE_NAMES (LETREC);
  1396. scm_exp_vtable_vtable =
  1397. scm_make_vtable (scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT "pwuwpw"),
  1398. SCM_BOOL_F);
  1399. for (n = 0; n < SCM_NUM_EXPANDED_TYPES; n++)
  1400. exp_vtables[n] = make_exp_vtable (n);
  1401. /* Now walk back down, consing in reverse. */
  1402. while (n--)
  1403. exp_vtable_list = scm_cons (exp_vtables[n], exp_vtable_list);
  1404. const_unbound =
  1405. CONST_ (SCM_BOOL_F, scm_list_1 (scm_from_latin1_symbol ("unbound")));
  1406. scm_c_define_gsubr ("convert-assignment", 1, 0, 0, scm_convert_assignment);
  1407. scm_c_define ("%expanded-vtables", scm_vector (exp_vtable_list));
  1408. #include "libguile/expand.x"
  1409. }
  1410. /*
  1411. Local Variables:
  1412. c-file-style: "gnu"
  1413. End:
  1414. */