expand.c 52 KB

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