macros.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */
  23. #include "libguile/eval.h"
  24. #include "libguile/ports.h"
  25. #include "libguile/print.h"
  26. #include "libguile/root.h"
  27. #include "libguile/smob.h"
  28. #include "libguile/deprecation.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/programs.h"
  31. #include "libguile/macros.h"
  32. #include "libguile/private-options.h"
  33. scm_t_bits scm_tc16_macro;
  34. static int
  35. macro_print (SCM macro, SCM port, scm_print_state *pstate)
  36. {
  37. SCM code = SCM_MACRO_CODE (macro);
  38. if (!SCM_CLOSUREP (code)
  39. || scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE))
  40. || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE,
  41. macro, port, pstate)))
  42. {
  43. scm_puts ("#<", port);
  44. if (SCM_MACRO_TYPE (macro) < 4 && SCM_MACRO_IS_EXTENDED (macro))
  45. scm_puts ("extended-", port);
  46. if (!SCM_CLOSUREP (code) && !SCM_PROGRAM_P (code))
  47. scm_puts ("primitive-", port);
  48. if (SCM_MACRO_TYPE (macro) == 0)
  49. scm_puts ("syntax", port);
  50. #if SCM_ENABLE_DEPRECATED == 1
  51. if (SCM_MACRO_TYPE (macro) == 1)
  52. scm_puts ("macro", port);
  53. #endif
  54. if (SCM_MACRO_TYPE (macro) == 2)
  55. scm_puts ("macro!", port);
  56. if (SCM_MACRO_TYPE (macro) == 3)
  57. scm_puts ("builtin-macro!", port);
  58. if (SCM_MACRO_TYPE (macro) == 4)
  59. scm_puts ("syncase-macro", port);
  60. scm_putc (' ', port);
  61. scm_iprin1 (scm_macro_name (macro), port, pstate);
  62. if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P)
  63. {
  64. SCM formals = SCM_CLOSURE_FORMALS (code);
  65. SCM env = SCM_ENV (code);
  66. SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
  67. SCM src = scm_i_unmemocopy_body (SCM_CODE (code), xenv);
  68. scm_putc (' ', port);
  69. scm_iprin1 (src, port, pstate);
  70. }
  71. if (SCM_MACRO_IS_EXTENDED (macro))
  72. {
  73. scm_putc (' ', port);
  74. scm_write (SCM_SMOB_OBJECT_2 (macro), port);
  75. scm_putc (' ', port);
  76. scm_write (SCM_SMOB_OBJECT_3 (macro), port);
  77. }
  78. scm_putc ('>', port);
  79. }
  80. return 1;
  81. }
  82. static SCM
  83. macro_mark (SCM macro)
  84. {
  85. if (SCM_MACRO_IS_EXTENDED (macro))
  86. { scm_gc_mark (SCM_SMOB_OBJECT_2 (macro));
  87. scm_gc_mark (SCM_SMOB_OBJECT_3 (macro));
  88. }
  89. return SCM_SMOB_OBJECT (macro);
  90. }
  91. static SCM
  92. makmac (SCM code, scm_t_bits flags)
  93. {
  94. SCM z;
  95. SCM_NEWSMOB (z, scm_tc16_macro, SCM_UNPACK (code));
  96. SCM_SET_SMOB_FLAGS (z, flags);
  97. return z;
  98. }
  99. /* Return a mmacro that is known to be one of guile's built in macros. */
  100. SCM
  101. scm_i_makbimacro (SCM code)
  102. #define FUNC_NAME "scm_i_makbimacro"
  103. {
  104. SCM_VALIDATE_PROC (1, code);
  105. return makmac (code, 3);
  106. }
  107. #undef FUNC_NAME
  108. SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0,
  109. (SCM code),
  110. "Return a @dfn{macro} which, when a symbol defined to this value\n"
  111. "appears as the first symbol in an expression, evaluates the\n"
  112. "result of applying @var{code} to the expression and the\n"
  113. "environment.\n\n"
  114. "@code{procedure->memoizing-macro} is the same as\n"
  115. "@code{procedure->macro}, except that the expression returned by\n"
  116. "@var{code} replaces the original macro expression in the memoized\n"
  117. "form of the containing code.")
  118. #define FUNC_NAME s_scm_makmmacro
  119. {
  120. SCM_VALIDATE_PROC (1, code);
  121. return makmac (code, 2);
  122. }
  123. #undef FUNC_NAME
  124. SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0,
  125. (SCM code),
  126. "Return a @dfn{macro} which, when a symbol defined to this value\n"
  127. "appears as the first symbol in an expression, returns the\n"
  128. "result of applying @var{code} to the expression and the\n"
  129. "environment.")
  130. #define FUNC_NAME s_scm_makacro
  131. {
  132. SCM_VALIDATE_PROC (1, code);
  133. return makmac (code, 0);
  134. }
  135. #undef FUNC_NAME
  136. #if SCM_ENABLE_DEPRECATED == 1
  137. SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0,
  138. (SCM code),
  139. "Return a @dfn{macro} which, when a symbol defined to this value\n"
  140. "appears as the first symbol in an expression, evaluates the\n"
  141. "result of applying @var{code} to the expression and the\n"
  142. "environment. For example:\n"
  143. "\n"
  144. "@lisp\n"
  145. "(define trace\n"
  146. " (procedure->macro\n"
  147. " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n"
  148. "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n"
  149. "@end lisp")
  150. #define FUNC_NAME s_scm_makmacro
  151. {
  152. scm_c_issue_deprecation_warning
  153. ("The function procedure->macro is deprecated, and so are"
  154. " non-memoizing macros in general. Use memoizing macros"
  155. " or r5rs macros instead.");
  156. SCM_VALIDATE_PROC (1, code);
  157. return makmac (code, 1);
  158. }
  159. #undef FUNC_NAME
  160. #endif
  161. SCM_DEFINE (scm_make_syncase_macro, "make-syncase-macro", 2, 0, 0,
  162. (SCM type, SCM binding),
  163. "Return a @dfn{macro} that requires expansion by syntax-case.\n"
  164. "While users should not call this function, it is useful to know\n"
  165. "that syntax-case macros are represented as Guile primitive macros.")
  166. #define FUNC_NAME s_scm_make_syncase_macro
  167. {
  168. SCM z;
  169. SCM_VALIDATE_SYMBOL (1, type);
  170. SCM_NEWSMOB3 (z, scm_tc16_macro, SCM_UNPACK (binding), SCM_UNPACK (type),
  171. SCM_UNPACK (binding));
  172. SCM_SET_SMOB_FLAGS (z, 4 | SCM_F_MACRO_EXTENDED);
  173. return z;
  174. }
  175. #undef FUNC_NAME
  176. SCM_DEFINE (scm_make_extended_syncase_macro, "make-extended-syncase-macro", 3, 0, 0,
  177. (SCM m, SCM type, SCM binding),
  178. "Extend a core macro @var{m} with a syntax-case binding.")
  179. #define FUNC_NAME s_scm_make_extended_syncase_macro
  180. {
  181. SCM z;
  182. SCM_VALIDATE_SMOB (1, m, macro);
  183. SCM_VALIDATE_SYMBOL (2, type);
  184. SCM_NEWSMOB3 (z, scm_tc16_macro, SCM_SMOB_DATA (m), SCM_UNPACK (type),
  185. SCM_UNPACK (binding));
  186. SCM_SET_SMOB_FLAGS (z, SCM_SMOB_FLAGS (m) | SCM_F_MACRO_EXTENDED);
  187. return z;
  188. }
  189. #undef FUNC_NAME
  190. SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
  191. (SCM obj),
  192. "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro, a\n"
  193. "syntax transformer, or a syntax-case macro.")
  194. #define FUNC_NAME s_scm_macro_p
  195. {
  196. return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_macro, obj));
  197. }
  198. #undef FUNC_NAME
  199. SCM_SYMBOL (scm_sym_syntax, "syntax");
  200. #if SCM_ENABLE_DEPRECATED == 1
  201. SCM_SYMBOL (scm_sym_macro, "macro");
  202. #endif
  203. SCM_SYMBOL (scm_sym_mmacro, "macro!");
  204. SCM_SYMBOL (scm_sym_bimacro, "builtin-macro!");
  205. SCM_SYMBOL (scm_sym_syncase_macro, "syncase-macro");
  206. SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
  207. (SCM m),
  208. "Return one of the symbols @code{syntax}, @code{macro},\n"
  209. "@code{macro!}, or @code{syntax-case}, depending on whether\n"
  210. "@var{m} is a syntax transformer, a regular macro, a memoizing\n"
  211. "macro, or a syntax-case macro, respectively. If @var{m} is\n"
  212. "not a macro, @code{#f} is returned.")
  213. #define FUNC_NAME s_scm_macro_type
  214. {
  215. if (!SCM_SMOB_PREDICATE (scm_tc16_macro, m))
  216. return SCM_BOOL_F;
  217. switch (SCM_MACRO_TYPE (m))
  218. {
  219. case 0: return scm_sym_syntax;
  220. #if SCM_ENABLE_DEPRECATED == 1
  221. case 1: return scm_sym_macro;
  222. #endif
  223. case 2: return scm_sym_mmacro;
  224. case 3: return scm_sym_bimacro;
  225. case 4: return scm_sym_syncase_macro;
  226. default: scm_wrong_type_arg (FUNC_NAME, 1, m);
  227. }
  228. }
  229. #undef FUNC_NAME
  230. SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
  231. (SCM m),
  232. "Return the name of the macro @var{m}.")
  233. #define FUNC_NAME s_scm_macro_name
  234. {
  235. SCM_VALIDATE_SMOB (1, m, macro);
  236. if (scm_is_true (scm_procedure_p (SCM_SMOB_OBJECT (m))))
  237. return scm_procedure_name (SCM_SMOB_OBJECT (m));
  238. return SCM_BOOL_F;
  239. }
  240. #undef FUNC_NAME
  241. SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
  242. (SCM m),
  243. "Return the transformer of the macro @var{m}.")
  244. #define FUNC_NAME s_scm_macro_transformer
  245. {
  246. SCM data;
  247. SCM_VALIDATE_SMOB (1, m, macro);
  248. data = SCM_PACK (SCM_SMOB_DATA (m));
  249. if (SCM_CLOSUREP (data) || SCM_PROGRAM_P (data))
  250. return data;
  251. else
  252. return SCM_BOOL_F;
  253. }
  254. #undef FUNC_NAME
  255. SCM_DEFINE (scm_syncase_macro_type, "syncase-macro-type", 1, 0, 0,
  256. (SCM m),
  257. "Return the type of the macro @var{m}.")
  258. #define FUNC_NAME s_scm_syncase_macro_type
  259. {
  260. SCM_VALIDATE_SMOB (1, m, macro);
  261. if (SCM_MACRO_IS_EXTENDED (m))
  262. return SCM_SMOB_OBJECT_2 (m);
  263. else
  264. return SCM_BOOL_F;
  265. }
  266. #undef FUNC_NAME
  267. SCM_DEFINE (scm_syncase_macro_binding, "syncase-macro-binding", 1, 0, 0,
  268. (SCM m),
  269. "Return the binding of the macro @var{m}.")
  270. #define FUNC_NAME s_scm_syncase_macro_binding
  271. {
  272. SCM_VALIDATE_SMOB (1, m, macro);
  273. if (SCM_MACRO_IS_EXTENDED (m))
  274. return SCM_SMOB_OBJECT_3 (m);
  275. else
  276. return SCM_BOOL_F;
  277. }
  278. #undef FUNC_NAME
  279. SCM
  280. scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() )
  281. {
  282. SCM var = scm_c_define (name, SCM_UNDEFINED);
  283. SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
  284. SCM_VARIABLE_SET (var, macroizer (transformer));
  285. return SCM_UNSPECIFIED;
  286. }
  287. void
  288. scm_init_macros ()
  289. {
  290. scm_tc16_macro = scm_make_smob_type ("macro", 0);
  291. scm_set_smob_mark (scm_tc16_macro, macro_mark);
  292. scm_set_smob_print (scm_tc16_macro, macro_print);
  293. #include "libguile/macros.x"
  294. }
  295. /*
  296. Local Variables:
  297. c-file-style: "gnu"
  298. End:
  299. */