expand.c 53 KB

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