expand.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011
  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 const char* exp_names[SCM_NUM_EXPANDED_TYPES];
  40. static const char** exp_field_names[SCM_NUM_EXPANDED_TYPES];
  41. #define VOID(src) \
  42. SCM_MAKE_EXPANDED_VOID(src)
  43. #define CONST(src, exp) \
  44. SCM_MAKE_EXPANDED_CONST(src, exp)
  45. #define PRIMITIVE_REF_TYPE(src, name) \
  46. SCM_MAKE_EXPANDED_PRIMITIVE_REF_TYPE(src, name)
  47. #define LEXICAL_REF(src, name, gensym) \
  48. SCM_MAKE_EXPANDED_LEXICAL_REF(src, name, gensym)
  49. #define LEXICAL_SET(src, name, gensym, exp) \
  50. SCM_MAKE_EXPANDED_LEXICAL_SET(src, name, gensym, exp)
  51. #define MODULE_REF(src, mod, name, public) \
  52. SCM_MAKE_EXPANDED_MODULE_REF(src, mod, name, public)
  53. #define MODULE_SET(src, mod, name, public, exp) \
  54. SCM_MAKE_EXPANDED_MODULE_SET(src, mod, name, public, exp)
  55. #define TOPLEVEL_REF(src, name) \
  56. SCM_MAKE_EXPANDED_TOPLEVEL_REF(src, name)
  57. #define TOPLEVEL_SET(src, name, exp) \
  58. SCM_MAKE_EXPANDED_TOPLEVEL_SET(src, name, exp)
  59. #define TOPLEVEL_DEFINE(src, name, exp) \
  60. SCM_MAKE_EXPANDED_TOPLEVEL_DEFINE(src, name, exp)
  61. #define CONDITIONAL(src, test, consequent, alternate) \
  62. SCM_MAKE_EXPANDED_CONDITIONAL(src, test, consequent, alternate)
  63. #define CALL(src, proc, exps) \
  64. SCM_MAKE_EXPANDED_CALL(src, proc, exps)
  65. #define SEQ(src, head, tail) \
  66. SCM_MAKE_EXPANDED_SEQ(src, head, tail)
  67. #define LAMBDA(src, meta, body) \
  68. SCM_MAKE_EXPANDED_LAMBDA(src, meta, body)
  69. #define LAMBDA_CASE(src, req, opt, rest, kw, inits, gensyms, body, alternate) \
  70. SCM_MAKE_EXPANDED_LAMBDA_CASE(src, req, opt, rest, kw, inits, gensyms, body, alternate)
  71. #define LET(src, names, gensyms, vals, body) \
  72. SCM_MAKE_EXPANDED_LET(src, names, gensyms, vals, body)
  73. #define LETREC(src, in_order_p, names, gensyms, vals, body) \
  74. SCM_MAKE_EXPANDED_LETREC(src, in_order_p, names, gensyms, vals, body)
  75. #define DYNLET(src, fluids, vals, body) \
  76. SCM_MAKE_EXPANDED_DYNLET(src, fluids, vals, body)
  77. #define CAR(x) SCM_CAR(x)
  78. #define CDR(x) SCM_CDR(x)
  79. #define CAAR(x) SCM_CAAR(x)
  80. #define CADR(x) SCM_CADR(x)
  81. #define CDAR(x) SCM_CDAR(x)
  82. #define CDDR(x) SCM_CDDR(x)
  83. #define CADDR(x) SCM_CADDR(x)
  84. #define CDDDR(x) SCM_CDDDR(x)
  85. #define CADDDR(x) SCM_CADDDR(x)
  86. static const char s_bad_expression[] = "Bad expression";
  87. static const char s_expression[] = "Missing or extra expression in";
  88. static const char s_missing_expression[] = "Missing expression in";
  89. static const char s_extra_expression[] = "Extra expression in";
  90. static const char s_empty_combination[] = "Illegal empty combination";
  91. static const char s_missing_body_expression[] = "Missing body expression in";
  92. static const char s_mixed_body_forms[] = "Mixed definitions and expressions in";
  93. static const char s_bad_define[] = "Bad define placement";
  94. static const char s_missing_clauses[] = "Missing clauses";
  95. static const char s_misplaced_else_clause[] = "Misplaced else clause";
  96. static const char s_bad_case_clause[] = "Bad case clause";
  97. static const char s_bad_case_labels[] = "Bad case labels";
  98. static const char s_duplicate_case_label[] = "Duplicate case label";
  99. static const char s_bad_cond_clause[] = "Bad cond clause";
  100. static const char s_missing_recipient[] = "Missing recipient in";
  101. static const char s_bad_variable[] = "Bad variable";
  102. static const char s_bad_bindings[] = "Bad bindings";
  103. static const char s_bad_binding[] = "Bad binding";
  104. static const char s_duplicate_binding[] = "Duplicate binding";
  105. static const char s_bad_exit_clause[] = "Bad exit clause";
  106. static const char s_bad_formals[] = "Bad formals";
  107. static const char s_bad_formal[] = "Bad formal";
  108. static const char s_duplicate_formal[] = "Duplicate formal";
  109. static const char s_splicing[] = "Non-list result for unquote-splicing";
  110. static const char s_bad_slot_number[] = "Bad slot number";
  111. static void syntax_error (const char* const, const SCM, const SCM) SCM_NORETURN;
  112. SCM_SYMBOL (syntax_error_key, "syntax-error");
  113. /* Shortcut macros to simplify syntax error handling. */
  114. #define ASSERT_SYNTAX(cond, message, form) \
  115. { if (SCM_UNLIKELY (!(cond))) \
  116. syntax_error (message, form, SCM_UNDEFINED); }
  117. #define ASSERT_SYNTAX_2(cond, message, form, expr) \
  118. { if (SCM_UNLIKELY (!(cond))) \
  119. syntax_error (message, form, expr); }
  120. /* Primitive syntax. */
  121. #define SCM_SYNTAX(STR, CFN) \
  122. SCM_SNARF_HERE(static SCM CFN (SCM xorig, SCM env)) \
  123. SCM_SNARF_INIT(scm_c_define (STR, scm_i_make_primitive_macro (STR, CFN)))
  124. /* True primitive syntax */
  125. SCM_SYNTAX ("@", expand_at);
  126. SCM_SYNTAX ("@@", expand_atat);
  127. SCM_SYNTAX ("begin", expand_begin);
  128. SCM_SYNTAX ("define", expand_define);
  129. SCM_SYNTAX ("with-fluids", expand_with_fluids);
  130. SCM_SYNTAX ("eval-when", expand_eval_when);
  131. SCM_SYNTAX ("if", expand_if);
  132. SCM_SYNTAX ("lambda", expand_lambda);
  133. SCM_SYNTAX ("let", expand_let);
  134. SCM_SYNTAX ("quote", expand_quote);
  135. SCM_SYNTAX ("set!", expand_set_x);
  136. /* Convenient syntax during boot, expands to primitive syntax. Replaced after
  137. psyntax boots. */
  138. SCM_SYNTAX ("and", expand_and);
  139. SCM_SYNTAX ("cond", expand_cond);
  140. SCM_SYNTAX ("letrec", expand_letrec);
  141. SCM_SYNTAX ("letrec*", expand_letrec_star);
  142. SCM_SYNTAX ("let*", expand_letstar);
  143. SCM_SYNTAX ("or", expand_or);
  144. SCM_SYNTAX ("lambda*", expand_lambda_star);
  145. SCM_SYNTAX ("case-lambda", expand_case_lambda);
  146. SCM_SYNTAX ("case-lambda*", expand_case_lambda_star);
  147. SCM_GLOBAL_SYMBOL (scm_sym_apply, "apply");
  148. SCM_GLOBAL_SYMBOL (scm_sym_arrow, "=>");
  149. SCM_GLOBAL_SYMBOL (scm_sym_at, "@");
  150. SCM_GLOBAL_SYMBOL (scm_sym_atat, "@@");
  151. SCM_GLOBAL_SYMBOL (scm_sym_at_call_with_values, "@call-with-values");
  152. SCM_GLOBAL_SYMBOL (scm_sym_atapply, "@apply");
  153. SCM_GLOBAL_SYMBOL (scm_sym_atcall_cc, "@call-with-current-continuation");
  154. SCM_GLOBAL_SYMBOL (scm_sym_begin, "begin");
  155. SCM_GLOBAL_SYMBOL (scm_sym_case, "case");
  156. SCM_GLOBAL_SYMBOL (scm_sym_cond, "cond");
  157. SCM_GLOBAL_SYMBOL (scm_sym_define, "define");
  158. SCM_GLOBAL_SYMBOL (scm_sym_at_dynamic_wind, "@dynamic-wind");
  159. SCM_GLOBAL_SYMBOL (scm_sym_with_fluids, "with-fluids");
  160. SCM_GLOBAL_SYMBOL (scm_sym_else, "else");
  161. SCM_GLOBAL_SYMBOL (scm_sym_eval_when, "eval-when");
  162. SCM_GLOBAL_SYMBOL (scm_sym_if, "if");
  163. SCM_GLOBAL_SYMBOL (scm_sym_lambda, "lambda");
  164. SCM_GLOBAL_SYMBOL (scm_sym_let, "let");
  165. SCM_GLOBAL_SYMBOL (scm_sym_letrec, "letrec");
  166. SCM_GLOBAL_SYMBOL (scm_sym_letstar, "let*");
  167. SCM_GLOBAL_SYMBOL (scm_sym_or, "or");
  168. SCM_GLOBAL_SYMBOL (scm_sym_at_prompt, "@prompt");
  169. SCM_GLOBAL_SYMBOL (scm_sym_quote, "quote");
  170. SCM_GLOBAL_SYMBOL (scm_sym_set_x, "set!");
  171. SCM_SYMBOL (sym_lambda_star, "lambda*");
  172. SCM_SYMBOL (sym_eval, "eval");
  173. SCM_SYMBOL (sym_load, "load");
  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 = CAR (exp);
  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. if (scm_is_null (arg_exps))
  309. return CALL (scm_source_properties (exp),
  310. expand (proc, env),
  311. scm_reverse_x (args, SCM_UNDEFINED));
  312. else
  313. syntax_error ("expected a proper list", exp, SCM_UNDEFINED);
  314. }
  315. }
  316. else if (scm_is_symbol (exp))
  317. {
  318. SCM gensym = expand_env_lexical_gensym (env, exp);
  319. if (scm_is_true (gensym))
  320. return LEXICAL_REF (SCM_BOOL_F, exp, gensym);
  321. else
  322. return TOPLEVEL_REF (SCM_BOOL_F, exp);
  323. }
  324. else
  325. return CONST (SCM_BOOL_F, exp);
  326. }
  327. static SCM
  328. expand_exprs (SCM forms, const SCM env)
  329. {
  330. SCM ret = SCM_EOL;
  331. for (; !scm_is_null (forms); forms = CDR (forms))
  332. ret = scm_cons (expand (CAR (forms), env), ret);
  333. return scm_reverse_x (ret, SCM_UNDEFINED);
  334. }
  335. static SCM
  336. expand_sequence (const SCM forms, const SCM env)
  337. {
  338. ASSERT_SYNTAX (scm_ilength (forms) >= 1, s_bad_expression,
  339. scm_cons (scm_sym_begin, forms));
  340. if (scm_is_null (CDR (forms)))
  341. return expand (CAR (forms), env);
  342. else
  343. return SEQ (scm_source_properties (forms),
  344. expand (CAR (forms), env),
  345. expand_sequence (CDR (forms), env));
  346. }
  347. static SCM
  348. expand_at (SCM expr, SCM env SCM_UNUSED)
  349. {
  350. ASSERT_SYNTAX (scm_ilength (expr) == 3, s_bad_expression, expr);
  351. ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr);
  352. ASSERT_SYNTAX (scm_is_symbol (CADDR (expr)), s_bad_expression, expr);
  353. return MODULE_REF (scm_source_properties (expr),
  354. CADR (expr), CADDR (expr), SCM_BOOL_T);
  355. }
  356. static SCM
  357. expand_atat (SCM expr, SCM env SCM_UNUSED)
  358. {
  359. ASSERT_SYNTAX (scm_ilength (expr) == 3, s_bad_expression, expr);
  360. ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr);
  361. ASSERT_SYNTAX (scm_is_symbol (CADDR (expr)), s_bad_expression, expr);
  362. return MODULE_REF (scm_source_properties (expr),
  363. CADR (expr), CADDR (expr), SCM_BOOL_F);
  364. }
  365. static SCM
  366. expand_and (SCM expr, SCM env)
  367. {
  368. const SCM cdr_expr = CDR (expr);
  369. if (scm_is_null (cdr_expr))
  370. return CONST (SCM_BOOL_F, SCM_BOOL_T);
  371. ASSERT_SYNTAX (scm_is_pair (cdr_expr), s_bad_expression, expr);
  372. if (scm_is_null (CDR (cdr_expr)))
  373. return expand (CAR (cdr_expr), env);
  374. else
  375. return CONDITIONAL (scm_source_properties (expr),
  376. expand (CAR (cdr_expr), env),
  377. expand_and (cdr_expr, env),
  378. CONST (SCM_BOOL_F, SCM_BOOL_F));
  379. }
  380. static SCM
  381. expand_begin (SCM expr, SCM env)
  382. {
  383. const SCM cdr_expr = CDR (expr);
  384. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 1, s_bad_expression, expr);
  385. return expand_sequence (cdr_expr, env);
  386. }
  387. static SCM
  388. expand_cond_clauses (SCM clause, SCM rest, int elp, int alp, SCM env)
  389. {
  390. SCM test;
  391. const long length = scm_ilength (clause);
  392. ASSERT_SYNTAX (length >= 1, s_bad_cond_clause, clause);
  393. test = CAR (clause);
  394. if (scm_is_eq (test, scm_sym_else) && elp)
  395. {
  396. const int last_clause_p = scm_is_null (rest);
  397. ASSERT_SYNTAX (length >= 2, s_bad_cond_clause, clause);
  398. ASSERT_SYNTAX (last_clause_p, s_misplaced_else_clause, clause);
  399. return expand_sequence (CDR (clause), env);
  400. }
  401. if (scm_is_null (rest))
  402. rest = VOID (SCM_BOOL_F);
  403. else
  404. rest = expand_cond_clauses (CAR (rest), CDR (rest), elp, alp, env);
  405. if (length >= 2
  406. && scm_is_eq (CADR (clause), scm_sym_arrow)
  407. && alp)
  408. {
  409. SCM tmp = scm_gensym (scm_from_locale_string ("cond "));
  410. SCM new_env = scm_acons (tmp, tmp, env);
  411. ASSERT_SYNTAX (length > 2, s_missing_recipient, clause);
  412. ASSERT_SYNTAX (length == 3, s_extra_expression, clause);
  413. return LET (SCM_BOOL_F,
  414. scm_list_1 (tmp),
  415. scm_list_1 (tmp),
  416. scm_list_1 (expand (test, env)),
  417. CONDITIONAL (SCM_BOOL_F,
  418. LEXICAL_REF (SCM_BOOL_F, tmp, tmp),
  419. CALL (SCM_BOOL_F,
  420. expand (CADDR (clause), new_env),
  421. scm_list_1 (LEXICAL_REF (SCM_BOOL_F,
  422. tmp, tmp))),
  423. rest));
  424. }
  425. /* FIXME length == 1 case */
  426. else
  427. return CONDITIONAL (SCM_BOOL_F,
  428. expand (test, env),
  429. expand_sequence (CDR (clause), env),
  430. rest);
  431. }
  432. static SCM
  433. expand_cond (SCM expr, SCM env)
  434. {
  435. const int else_literal_p = expand_env_var_is_free (env, scm_sym_else);
  436. const int arrow_literal_p = expand_env_var_is_free (env, scm_sym_arrow);
  437. const SCM clauses = CDR (expr);
  438. ASSERT_SYNTAX (scm_ilength (clauses) >= 0, s_bad_expression, expr);
  439. ASSERT_SYNTAX (scm_ilength (clauses) >= 1, s_missing_clauses, expr);
  440. return expand_cond_clauses (CAR (clauses), CDR (clauses),
  441. else_literal_p, arrow_literal_p, env);
  442. }
  443. /* lone forward decl */
  444. static SCM expand_lambda (SCM expr, SCM env);
  445. /* According to Section 5.2.1 of R5RS we first have to make sure that the
  446. variable is bound, and then perform the `(set! variable expression)'
  447. operation. However, EXPRESSION _can_ be evaluated before VARIABLE is
  448. bound. This means that EXPRESSION won't necessarily be able to assign
  449. values to VARIABLE as in `(define foo (begin (set! foo 1) (+ foo 1)))'. */
  450. static SCM
  451. expand_define (SCM expr, SCM env)
  452. {
  453. const SCM cdr_expr = CDR (expr);
  454. SCM body;
  455. SCM variable;
  456. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  457. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  458. ASSERT_SYNTAX (!scm_is_pair (env), s_bad_define, expr);
  459. body = CDR (cdr_expr);
  460. variable = CAR (cdr_expr);
  461. if (scm_is_pair (variable))
  462. {
  463. ASSERT_SYNTAX_2 (scm_is_symbol (CAR (variable)), s_bad_variable, variable, expr);
  464. return TOPLEVEL_DEFINE
  465. (scm_source_properties (expr),
  466. CAR (variable),
  467. expand_lambda (scm_cons (scm_sym_lambda, scm_cons (CDR (variable), body)),
  468. env));
  469. }
  470. ASSERT_SYNTAX_2 (scm_is_symbol (variable), s_bad_variable, variable, expr);
  471. ASSERT_SYNTAX (scm_ilength (body) == 1, s_expression, expr);
  472. return TOPLEVEL_DEFINE (scm_source_properties (expr), variable,
  473. expand (CAR (body), env));
  474. }
  475. static SCM
  476. expand_with_fluids (SCM expr, SCM env)
  477. {
  478. SCM binds, fluids, vals;
  479. ASSERT_SYNTAX (scm_ilength (expr) >= 3, s_bad_expression, expr);
  480. binds = CADR (expr);
  481. ASSERT_SYNTAX_2 (scm_ilength (binds) >= 0, s_bad_bindings, binds, expr);
  482. for (fluids = SCM_EOL, vals = SCM_EOL;
  483. scm_is_pair (binds);
  484. binds = CDR (binds))
  485. {
  486. SCM binding = CAR (binds);
  487. ASSERT_SYNTAX_2 (scm_ilength (CAR (binds)) == 2, s_bad_binding,
  488. binding, expr);
  489. fluids = scm_cons (expand (CAR (binding), env), fluids);
  490. vals = scm_cons (expand (CADR (binding), env), vals);
  491. }
  492. return DYNLET (scm_source_properties (expr),
  493. scm_reverse_x (fluids, SCM_UNDEFINED),
  494. scm_reverse_x (vals, SCM_UNDEFINED),
  495. expand_sequence (CDDR (expr), env));
  496. }
  497. static SCM
  498. expand_eval_when (SCM expr, SCM env)
  499. {
  500. ASSERT_SYNTAX (scm_ilength (expr) >= 3, s_bad_expression, expr);
  501. ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr);
  502. if (scm_is_true (scm_memq (sym_eval, CADR (expr)))
  503. || scm_is_true (scm_memq (sym_load, CADR (expr))))
  504. return expand_sequence (CDDR (expr), env);
  505. else
  506. return VOID (scm_source_properties (expr));
  507. }
  508. static SCM
  509. expand_if (SCM expr, SCM env SCM_UNUSED)
  510. {
  511. const SCM cdr_expr = CDR (expr);
  512. const long length = scm_ilength (cdr_expr);
  513. ASSERT_SYNTAX (length == 2 || length == 3, s_expression, expr);
  514. return CONDITIONAL (scm_source_properties (expr),
  515. expand (CADR (expr), env),
  516. expand (CADDR (expr), env),
  517. ((length == 3)
  518. ? expand (CADDDR (expr), env)
  519. : VOID (SCM_BOOL_F)));
  520. }
  521. /* A helper function for expand_lambda to support checking for duplicate
  522. * formal arguments: Return true if OBJ is `eq?' to one of the elements of
  523. * LIST or to the CDR of the last cons. Therefore, LIST may have any of the
  524. * forms that a formal argument can have:
  525. * <rest>, (<arg1> ...), (<arg1> ... . <rest>) */
  526. static int
  527. c_improper_memq (SCM obj, SCM list)
  528. {
  529. for (; scm_is_pair (list); list = CDR (list))
  530. {
  531. if (scm_is_eq (CAR (list), obj))
  532. return 1;
  533. }
  534. return scm_is_eq (list, obj);
  535. }
  536. static SCM
  537. expand_lambda_case (SCM clause, SCM alternate, SCM env)
  538. {
  539. SCM formals;
  540. SCM rest;
  541. SCM req = SCM_EOL;
  542. SCM vars = SCM_EOL;
  543. SCM body;
  544. int nreq = 0;
  545. ASSERT_SYNTAX (scm_is_pair (clause) && scm_is_pair (CDR (clause)),
  546. s_bad_expression, scm_cons (scm_sym_lambda, clause));
  547. /* Before iterating the list of formal arguments, make sure the formals
  548. * actually are given as either a symbol or a non-cyclic list. */
  549. formals = CAR (clause);
  550. if (scm_is_pair (formals))
  551. {
  552. /* Dirk:FIXME:: We should check for a cyclic list of formals, and if
  553. * detected, report a 'Bad formals' error. */
  554. }
  555. else
  556. ASSERT_SYNTAX_2 (scm_is_symbol (formals) || scm_is_null (formals),
  557. s_bad_formals, formals, scm_cons (scm_sym_lambda, clause));
  558. /* Now iterate the list of formal arguments to check if all formals are
  559. * symbols, and that there are no duplicates. */
  560. while (scm_is_pair (formals))
  561. {
  562. const SCM formal = CAR (formals);
  563. formals = CDR (formals);
  564. ASSERT_SYNTAX_2 (scm_is_symbol (formal), s_bad_formal, formal,
  565. scm_cons (scm_sym_lambda, clause));
  566. ASSERT_SYNTAX_2 (!c_improper_memq (formal, formals), s_duplicate_formal,
  567. formal, scm_cons (scm_sym_lambda, clause));
  568. nreq++;
  569. req = scm_cons (formal, req);
  570. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  571. env = scm_acons (formal, CAR (vars), env);
  572. }
  573. ASSERT_SYNTAX_2 (scm_is_null (formals) || scm_is_symbol (formals),
  574. s_bad_formal, formals, scm_cons (scm_sym_lambda, clause));
  575. if (scm_is_symbol (formals))
  576. {
  577. rest = formals;
  578. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  579. env = scm_acons (rest, CAR (vars), env);
  580. }
  581. else
  582. rest = SCM_BOOL_F;
  583. body = expand_sequence (CDR (clause), env);
  584. req = scm_reverse_x (req, SCM_UNDEFINED);
  585. vars = scm_reverse_x (vars, SCM_UNDEFINED);
  586. if (scm_is_true (alternate) && !(SCM_EXPANDED_P (alternate) && SCM_EXPANDED_TYPE (alternate) == SCM_EXPANDED_LAMBDA_CASE))
  587. abort ();
  588. return LAMBDA_CASE (SCM_BOOL_F, req, SCM_BOOL_F, rest, SCM_BOOL_F,
  589. SCM_EOL, vars, body, alternate);
  590. }
  591. static SCM
  592. expand_lambda (SCM expr, SCM env)
  593. {
  594. return LAMBDA (scm_source_properties (expr),
  595. SCM_EOL,
  596. expand_lambda_case (CDR (expr), SCM_BOOL_F, env));
  597. }
  598. static SCM
  599. expand_lambda_star_case (SCM clause, SCM alternate, SCM env)
  600. {
  601. SCM req, opt, kw, allow_other_keys, rest, formals, vars, body, tmp;
  602. SCM inits;
  603. int nreq, nopt;
  604. const long length = scm_ilength (clause);
  605. ASSERT_SYNTAX (length >= 1, s_bad_expression,
  606. scm_cons (sym_lambda_star, clause));
  607. ASSERT_SYNTAX (length >= 2, s_missing_expression,
  608. scm_cons (sym_lambda_star, clause));
  609. formals = CAR (clause);
  610. body = CDR (clause);
  611. nreq = nopt = 0;
  612. req = opt = kw = SCM_EOL;
  613. rest = allow_other_keys = SCM_BOOL_F;
  614. while (scm_is_pair (formals) && scm_is_symbol (CAR (formals)))
  615. {
  616. nreq++;
  617. req = scm_cons (CAR (formals), req);
  618. formals = scm_cdr (formals);
  619. }
  620. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_optional))
  621. {
  622. formals = CDR (formals);
  623. while (scm_is_pair (formals)
  624. && (scm_is_symbol (CAR (formals)) || scm_is_pair (CAR (formals))))
  625. {
  626. nopt++;
  627. opt = scm_cons (CAR (formals), opt);
  628. formals = scm_cdr (formals);
  629. }
  630. }
  631. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_key))
  632. {
  633. formals = CDR (formals);
  634. while (scm_is_pair (formals)
  635. && (scm_is_symbol (CAR (formals)) || scm_is_pair (CAR (formals))))
  636. {
  637. kw = scm_cons (CAR (formals), kw);
  638. formals = scm_cdr (formals);
  639. }
  640. }
  641. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_allow_other_keys))
  642. {
  643. formals = CDR (formals);
  644. allow_other_keys = SCM_BOOL_T;
  645. }
  646. if (scm_is_pair (formals) && scm_is_eq (CAR (formals), kw_rest))
  647. {
  648. ASSERT_SYNTAX (scm_ilength (formals) == 2, s_bad_formals,
  649. CAR (clause));
  650. rest = CADR (formals);
  651. }
  652. else if (scm_is_symbol (formals))
  653. rest = formals;
  654. else
  655. {
  656. ASSERT_SYNTAX (scm_is_null (formals), s_bad_formals, CAR (clause));
  657. rest = SCM_BOOL_F;
  658. }
  659. /* Now, iterate through them a second time, building up an expansion-time
  660. environment, checking, expanding and canonicalizing the opt/kw init forms,
  661. and eventually memoizing the body as well. Note that the rest argument, if
  662. any, is expanded before keyword args, thus necessitating the second
  663. pass.
  664. Also note that the specific environment during expansion of init
  665. expressions here needs to coincide with the environment when psyntax
  666. expands. A lot of effort for something that is only used in the bootstrap
  667. expandr, you say? Yes. Yes it is.
  668. */
  669. vars = SCM_EOL;
  670. req = scm_reverse_x (req, SCM_EOL);
  671. for (tmp = req; scm_is_pair (tmp); tmp = scm_cdr (tmp))
  672. {
  673. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  674. env = scm_acons (CAR (tmp), CAR (vars), env);
  675. }
  676. /* Build up opt inits and env */
  677. inits = SCM_EOL;
  678. opt = scm_reverse_x (opt, SCM_EOL);
  679. for (tmp = opt; scm_is_pair (tmp); tmp = scm_cdr (tmp))
  680. {
  681. SCM x = CAR (tmp);
  682. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  683. env = scm_acons (x, CAR (vars), env);
  684. if (scm_is_symbol (x))
  685. inits = scm_cons (CONST (SCM_BOOL_F, SCM_BOOL_F), inits);
  686. else
  687. {
  688. ASSERT_SYNTAX (scm_ilength (x) == 2 && scm_is_symbol (CAR (x)),
  689. s_bad_formals, CAR (clause));
  690. inits = scm_cons (expand (CADR (x), env), inits);
  691. }
  692. env = scm_acons (scm_is_symbol (x) ? x : CAR (x), CAR (vars), env);
  693. }
  694. if (scm_is_null (opt))
  695. opt = SCM_BOOL_F;
  696. /* Process rest before keyword args */
  697. if (scm_is_true (rest))
  698. {
  699. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  700. env = scm_acons (rest, CAR (vars), env);
  701. }
  702. /* Build up kw inits, env, and kw-canon list */
  703. if (scm_is_null (kw))
  704. kw = SCM_BOOL_F;
  705. else
  706. {
  707. SCM kw_canon = SCM_EOL;
  708. kw = scm_reverse_x (kw, SCM_UNDEFINED);
  709. for (tmp = kw; scm_is_pair (tmp); tmp = scm_cdr (tmp))
  710. {
  711. SCM x, sym, k, init;
  712. x = CAR (tmp);
  713. if (scm_is_symbol (x))
  714. {
  715. sym = x;
  716. init = SCM_BOOL_F;
  717. k = scm_symbol_to_keyword (sym);
  718. }
  719. else if (scm_ilength (x) == 2 && scm_is_symbol (CAR (x)))
  720. {
  721. sym = CAR (x);
  722. init = CADR (x);
  723. k = scm_symbol_to_keyword (sym);
  724. }
  725. else if (scm_ilength (x) == 3 && scm_is_symbol (CAR (x))
  726. && scm_is_keyword (CADDR (x)))
  727. {
  728. sym = CAR (x);
  729. init = CADR (x);
  730. k = CADDR (x);
  731. }
  732. else
  733. syntax_error (s_bad_formals, CAR (clause), SCM_UNDEFINED);
  734. inits = scm_cons (expand (init, env), inits);
  735. vars = scm_cons (scm_gensym (SCM_UNDEFINED), vars);
  736. kw_canon = scm_cons (scm_list_3 (k, sym, CAR (vars)), kw_canon);
  737. env = scm_acons (sym, CAR (vars), env);
  738. }
  739. kw_canon = scm_reverse_x (kw_canon, SCM_UNDEFINED);
  740. kw = scm_cons (allow_other_keys, kw_canon);
  741. }
  742. /* We should check for no duplicates, but given that psyntax does this
  743. already, we can punt on it here... */
  744. vars = scm_reverse_x (vars, SCM_UNDEFINED);
  745. inits = scm_reverse_x (inits, SCM_UNDEFINED);
  746. body = expand_sequence (body, env);
  747. return LAMBDA_CASE (SCM_BOOL_F, req, opt, rest, kw, inits, vars, body,
  748. alternate);
  749. }
  750. static SCM
  751. expand_lambda_star (SCM expr, SCM env)
  752. {
  753. return LAMBDA (scm_source_properties (expr),
  754. SCM_EOL,
  755. expand_lambda_star_case (CDR (expr), SCM_BOOL_F, env));
  756. }
  757. static SCM
  758. expand_case_lambda_clauses (SCM expr, SCM rest, SCM env)
  759. {
  760. SCM alt;
  761. if (scm_is_pair (rest))
  762. alt = expand_case_lambda_clauses (CAR (rest), CDR (rest), env);
  763. else
  764. alt = SCM_BOOL_F;
  765. return expand_lambda_case (expr, alt, env);
  766. }
  767. static SCM
  768. expand_case_lambda (SCM expr, SCM env)
  769. {
  770. ASSERT_SYNTAX (scm_is_pair (CDR (expr)), s_missing_expression, expr);
  771. return LAMBDA (scm_source_properties (expr),
  772. SCM_EOL,
  773. expand_case_lambda_clauses (CADR (expr), CDDR (expr), env));
  774. }
  775. static SCM
  776. expand_case_lambda_star_clauses (SCM expr, SCM rest, SCM env)
  777. {
  778. SCM alt;
  779. if (scm_is_pair (rest))
  780. alt = expand_case_lambda_star_clauses (CAR (rest), CDR (rest), env);
  781. else
  782. alt = SCM_BOOL_F;
  783. return expand_lambda_star_case (expr, alt, env);
  784. }
  785. static SCM
  786. expand_case_lambda_star (SCM expr, SCM env)
  787. {
  788. ASSERT_SYNTAX (scm_is_pair (CDR (expr)), s_missing_expression, expr);
  789. return LAMBDA (scm_source_properties (expr),
  790. SCM_EOL,
  791. expand_case_lambda_star_clauses (CADR (expr), CDDR (expr), env));
  792. }
  793. /* Check if the format of the bindings is ((<symbol> <init-form>) ...). */
  794. static void
  795. check_bindings (const SCM bindings, const SCM expr)
  796. {
  797. SCM binding_idx;
  798. ASSERT_SYNTAX_2 (scm_ilength (bindings) >= 0,
  799. s_bad_bindings, bindings, expr);
  800. binding_idx = bindings;
  801. for (; !scm_is_null (binding_idx); binding_idx = CDR (binding_idx))
  802. {
  803. SCM name; /* const */
  804. const SCM binding = CAR (binding_idx);
  805. ASSERT_SYNTAX_2 (scm_ilength (binding) == 2,
  806. s_bad_binding, binding, expr);
  807. name = CAR (binding);
  808. ASSERT_SYNTAX_2 (scm_is_symbol (name), s_bad_variable, name, expr);
  809. }
  810. }
  811. /* The bindings, which must have the format ((v1 i1) (v2 i2) ... (vn in)), are
  812. * transformed to the lists (vn .. v2 v1) and (i1 i2 ... in). If a duplicate
  813. * variable name is detected, an error is signalled. */
  814. static void
  815. transform_bindings (const SCM bindings, const SCM expr,
  816. SCM *const names, SCM *const vars, SCM *const initptr)
  817. {
  818. SCM rnames = SCM_EOL;
  819. SCM rvars = SCM_EOL;
  820. SCM rinits = SCM_EOL;
  821. SCM binding_idx = bindings;
  822. for (; !scm_is_null (binding_idx); binding_idx = CDR (binding_idx))
  823. {
  824. const SCM binding = CAR (binding_idx);
  825. const SCM CDR_binding = CDR (binding);
  826. const SCM name = CAR (binding);
  827. ASSERT_SYNTAX_2 (scm_is_false (scm_c_memq (name, rnames)),
  828. s_duplicate_binding, name, expr);
  829. rnames = scm_cons (name, rnames);
  830. rvars = scm_cons (scm_gensym (SCM_UNDEFINED), rvars);
  831. rinits = scm_cons (CAR (CDR_binding), rinits);
  832. }
  833. *names = scm_reverse_x (rnames, SCM_UNDEFINED);
  834. *vars = scm_reverse_x (rvars, SCM_UNDEFINED);
  835. *initptr = scm_reverse_x (rinits, SCM_UNDEFINED);
  836. }
  837. /* FIXME: Remove named let in this boot expander. */
  838. static SCM
  839. expand_named_let (const SCM expr, SCM env)
  840. {
  841. SCM var_names, var_syms, inits;
  842. SCM inner_env;
  843. SCM name_sym;
  844. const SCM cdr_expr = CDR (expr);
  845. const SCM name = CAR (cdr_expr);
  846. const SCM cddr_expr = CDR (cdr_expr);
  847. const SCM bindings = CAR (cddr_expr);
  848. check_bindings (bindings, expr);
  849. transform_bindings (bindings, expr, &var_names, &var_syms, &inits);
  850. name_sym = scm_gensym (SCM_UNDEFINED);
  851. inner_env = scm_acons (name, name_sym, env);
  852. inner_env = expand_env_extend (inner_env, var_names, var_syms);
  853. return LETREC
  854. (scm_source_properties (expr), SCM_BOOL_F,
  855. scm_list_1 (name), scm_list_1 (name_sym),
  856. scm_list_1 (LAMBDA (SCM_BOOL_F,
  857. SCM_EOL,
  858. LAMBDA_CASE (SCM_BOOL_F, var_names, SCM_BOOL_F, SCM_BOOL_F,
  859. SCM_BOOL_F, SCM_BOOL_F, var_syms,
  860. expand_sequence (CDDDR (expr), inner_env),
  861. SCM_BOOL_F))),
  862. CALL (SCM_BOOL_F,
  863. LEXICAL_REF (SCM_BOOL_F, name, name_sym),
  864. expand_exprs (inits, env)));
  865. }
  866. static SCM
  867. expand_let (SCM expr, SCM env)
  868. {
  869. SCM bindings;
  870. const SCM cdr_expr = CDR (expr);
  871. const long length = scm_ilength (cdr_expr);
  872. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  873. ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
  874. bindings = CAR (cdr_expr);
  875. if (scm_is_symbol (bindings))
  876. {
  877. ASSERT_SYNTAX (length >= 3, s_missing_expression, expr);
  878. return expand_named_let (expr, env);
  879. }
  880. check_bindings (bindings, expr);
  881. if (scm_is_null (bindings))
  882. return expand_sequence (CDDR (expr), env);
  883. else
  884. {
  885. SCM var_names, var_syms, inits;
  886. transform_bindings (bindings, expr, &var_names, &var_syms, &inits);
  887. return LET (SCM_BOOL_F,
  888. var_names, var_syms, expand_exprs (inits, env),
  889. expand_sequence (CDDR (expr),
  890. expand_env_extend (env, var_names,
  891. var_syms)));
  892. }
  893. }
  894. static SCM
  895. expand_letrec_helper (SCM expr, SCM env, SCM in_order_p)
  896. {
  897. SCM bindings;
  898. const SCM cdr_expr = CDR (expr);
  899. const long length = scm_ilength (cdr_expr);
  900. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  901. ASSERT_SYNTAX (length >= 2, s_missing_expression, expr);
  902. bindings = CAR (cdr_expr);
  903. check_bindings (bindings, expr);
  904. if (scm_is_null (bindings))
  905. return expand_sequence (CDDR (expr), env);
  906. else
  907. {
  908. SCM var_names, var_syms, inits;
  909. transform_bindings (bindings, expr, &var_names, &var_syms, &inits);
  910. env = expand_env_extend (env, var_names, var_syms);
  911. return LETREC (SCM_BOOL_F, in_order_p,
  912. var_names, var_syms, expand_exprs (inits, env),
  913. expand_sequence (CDDR (expr), env));
  914. }
  915. }
  916. static SCM
  917. expand_letrec (SCM expr, SCM env)
  918. {
  919. return expand_letrec_helper (expr, env, SCM_BOOL_F);
  920. }
  921. static SCM
  922. expand_letrec_star (SCM expr, SCM env)
  923. {
  924. return expand_letrec_helper (expr, env, SCM_BOOL_T);
  925. }
  926. static SCM
  927. expand_letstar_clause (SCM bindings, SCM body, SCM env SCM_UNUSED)
  928. {
  929. if (scm_is_null (bindings))
  930. return expand_sequence (body, env);
  931. else
  932. {
  933. SCM bind, name, sym, init;
  934. ASSERT_SYNTAX (scm_is_pair (bindings), s_bad_expression, bindings);
  935. bind = CAR (bindings);
  936. ASSERT_SYNTAX (scm_ilength (bind) == 2, s_bad_binding, bind);
  937. name = CAR (bind);
  938. sym = scm_gensym (SCM_UNDEFINED);
  939. init = CADR (bind);
  940. return LET (SCM_BOOL_F, scm_list_1 (name), scm_list_1 (sym),
  941. scm_list_1 (expand (init, env)),
  942. expand_letstar_clause (CDR (bindings), body,
  943. scm_acons (name, sym, env)));
  944. }
  945. }
  946. static SCM
  947. expand_letstar (SCM expr, SCM env SCM_UNUSED)
  948. {
  949. const SCM cdr_expr = CDR (expr);
  950. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  951. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr);
  952. return expand_letstar_clause (CADR (expr), CDDR (expr), env);
  953. }
  954. static SCM
  955. expand_or (SCM expr, SCM env SCM_UNUSED)
  956. {
  957. SCM tail = CDR (expr);
  958. const long length = scm_ilength (tail);
  959. ASSERT_SYNTAX (length >= 0, s_bad_expression, expr);
  960. if (scm_is_null (CDR (expr)))
  961. return CONST (SCM_BOOL_F, SCM_BOOL_F);
  962. else
  963. {
  964. SCM tmp = scm_gensym (SCM_UNDEFINED);
  965. return LET (SCM_BOOL_F,
  966. scm_list_1 (tmp), scm_list_1 (tmp),
  967. scm_list_1 (expand (CADR (expr), env)),
  968. CONDITIONAL (SCM_BOOL_F,
  969. LEXICAL_REF (SCM_BOOL_F, tmp, tmp),
  970. LEXICAL_REF (SCM_BOOL_F, tmp, tmp),
  971. expand_or (CDR (expr),
  972. scm_acons (tmp, tmp, env))));
  973. }
  974. }
  975. static SCM
  976. expand_quote (SCM expr, SCM env SCM_UNUSED)
  977. {
  978. SCM quotee;
  979. const SCM cdr_expr = CDR (expr);
  980. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  981. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr);
  982. quotee = CAR (cdr_expr);
  983. return CONST (scm_source_properties (expr), quotee);
  984. }
  985. static SCM
  986. expand_set_x (SCM expr, SCM env)
  987. {
  988. SCM variable;
  989. SCM vmem;
  990. const SCM cdr_expr = CDR (expr);
  991. ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr);
  992. ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr);
  993. variable = CAR (cdr_expr);
  994. vmem = expand (variable, env);
  995. switch (SCM_EXPANDED_TYPE (vmem))
  996. {
  997. case SCM_EXPANDED_LEXICAL_REF:
  998. return LEXICAL_SET (scm_source_properties (expr),
  999. SCM_EXPANDED_REF (vmem, LEXICAL_REF, NAME),
  1000. SCM_EXPANDED_REF (vmem, LEXICAL_REF, GENSYM),
  1001. expand (CADDR (expr), env));
  1002. case SCM_EXPANDED_TOPLEVEL_REF:
  1003. return TOPLEVEL_SET (scm_source_properties (expr),
  1004. SCM_EXPANDED_REF (vmem, TOPLEVEL_REF, NAME),
  1005. expand (CADDR (expr), env));
  1006. case SCM_EXPANDED_MODULE_REF:
  1007. return MODULE_SET (scm_source_properties (expr),
  1008. SCM_EXPANDED_REF (vmem, MODULE_REF, MOD),
  1009. SCM_EXPANDED_REF (vmem, MODULE_REF, NAME),
  1010. SCM_EXPANDED_REF (vmem, MODULE_REF, PUBLIC),
  1011. expand (CADDR (expr), env));
  1012. default:
  1013. syntax_error (s_bad_variable, variable, expr);
  1014. }
  1015. }
  1016. /* This is the boot expander. It is later replaced with psyntax's sc-expand. */
  1017. SCM_DEFINE (scm_macroexpand, "macroexpand", 1, 0, 0,
  1018. (SCM exp),
  1019. "Expand the expression @var{exp}.")
  1020. #define FUNC_NAME s_scm_macroexpand
  1021. {
  1022. return expand (exp, scm_current_module ());
  1023. }
  1024. #undef FUNC_NAME
  1025. SCM_DEFINE (scm_macroexpanded_p, "macroexpanded?", 1, 0, 0,
  1026. (SCM exp),
  1027. "Return @code{#t} if @var{exp} is an expanded expression.")
  1028. #define FUNC_NAME s_scm_macroexpanded_p
  1029. {
  1030. return scm_from_bool (SCM_EXPANDED_P (exp));
  1031. }
  1032. #undef FUNC_NAME
  1033. #define DEFINE_NAMES(type) \
  1034. { \
  1035. static const char *fields[] = SCM_EXPANDED_##type##_FIELD_NAMES; \
  1036. exp_field_names[SCM_EXPANDED_##type] = fields; \
  1037. exp_names[SCM_EXPANDED_##type] = SCM_EXPANDED_##type##_TYPE_NAME; \
  1038. exp_nfields[SCM_EXPANDED_##type] = SCM_NUM_EXPANDED_##type##_FIELDS; \
  1039. }
  1040. static SCM
  1041. make_exp_vtable (size_t n)
  1042. {
  1043. SCM layout, printer, name, code, fields;
  1044. layout = scm_string_to_symbol
  1045. (scm_string_append (scm_make_list (scm_from_size_t (exp_nfields[n]),
  1046. scm_from_locale_string ("pw"))));
  1047. printer = SCM_BOOL_F;
  1048. name = scm_from_utf8_symbol (exp_names[n]);
  1049. code = scm_from_size_t (n);
  1050. fields = SCM_EOL;
  1051. {
  1052. size_t m = exp_nfields[n];
  1053. while (m--)
  1054. fields = scm_cons (scm_from_utf8_symbol (exp_field_names[n][m]), fields);
  1055. }
  1056. return scm_c_make_struct (scm_exp_vtable_vtable, 0, 5,
  1057. SCM_UNPACK (layout), SCM_UNPACK (printer), SCM_UNPACK (name),
  1058. SCM_UNPACK (code), SCM_UNPACK (fields));
  1059. }
  1060. void
  1061. scm_init_expand ()
  1062. {
  1063. size_t n;
  1064. SCM exp_vtable_list = SCM_EOL;
  1065. DEFINE_NAMES (VOID);
  1066. DEFINE_NAMES (CONST);
  1067. DEFINE_NAMES (PRIMITIVE_REF);
  1068. DEFINE_NAMES (LEXICAL_REF);
  1069. DEFINE_NAMES (LEXICAL_SET);
  1070. DEFINE_NAMES (MODULE_REF);
  1071. DEFINE_NAMES (MODULE_SET);
  1072. DEFINE_NAMES (TOPLEVEL_REF);
  1073. DEFINE_NAMES (TOPLEVEL_SET);
  1074. DEFINE_NAMES (TOPLEVEL_DEFINE);
  1075. DEFINE_NAMES (CONDITIONAL);
  1076. DEFINE_NAMES (CALL);
  1077. DEFINE_NAMES (PRIMCALL);
  1078. DEFINE_NAMES (SEQ);
  1079. DEFINE_NAMES (LAMBDA);
  1080. DEFINE_NAMES (LAMBDA_CASE);
  1081. DEFINE_NAMES (LET);
  1082. DEFINE_NAMES (LETREC);
  1083. DEFINE_NAMES (DYNLET);
  1084. scm_exp_vtable_vtable =
  1085. scm_make_vtable (scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT "pwuwpw"),
  1086. SCM_BOOL_F);
  1087. for (n = 0; n < SCM_NUM_EXPANDED_TYPES; n++)
  1088. exp_vtables[n] = make_exp_vtable (n);
  1089. /* Now walk back down, consing in reverse. */
  1090. while (n--)
  1091. exp_vtable_list = scm_cons (exp_vtables[n], exp_vtable_list);
  1092. scm_c_define ("%expanded-vtables", scm_vector (exp_vtable_list));
  1093. #include "libguile/expand.x"
  1094. }
  1095. /*
  1096. Local Variables:
  1097. c-file-style: "gnu"
  1098. End:
  1099. */