PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/guile-1.8.8/libguile/macros.c

#
C | 252 lines | 195 code | 35 blank | 22 comment | 20 complexity | 212aad205c66c33e24c43a0404d2fb81 MD5 | raw file
Possible License(s): LGPL-2.1
  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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */
  22. #include "libguile/eval.h"
  23. #include "libguile/ports.h"
  24. #include "libguile/print.h"
  25. #include "libguile/root.h"
  26. #include "libguile/smob.h"
  27. #include "libguile/deprecation.h"
  28. #include "libguile/validate.h"
  29. #include "libguile/macros.h"
  30. scm_t_bits scm_tc16_macro;
  31. static int
  32. macro_print (SCM macro, SCM port, scm_print_state *pstate)
  33. {
  34. SCM code = SCM_MACRO_CODE (macro);
  35. if (!SCM_CLOSUREP (code)
  36. || scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE))
  37. || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE,
  38. macro, port, pstate)))
  39. {
  40. if (!SCM_CLOSUREP (code))
  41. scm_puts ("#<primitive-", port);
  42. else
  43. scm_puts ("#<", port);
  44. if (SCM_MACRO_TYPE (macro) == 0)
  45. scm_puts ("syntax", port);
  46. #if SCM_ENABLE_DEPRECATED == 1
  47. if (SCM_MACRO_TYPE (macro) == 1)
  48. scm_puts ("macro", port);
  49. #endif
  50. if (SCM_MACRO_TYPE (macro) == 2)
  51. scm_puts ("macro!", port);
  52. if (SCM_MACRO_TYPE (macro) == 3)
  53. scm_puts ("builtin-macro!", port);
  54. scm_putc (' ', port);
  55. scm_iprin1 (scm_macro_name (macro), port, pstate);
  56. if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P)
  57. {
  58. SCM formals = SCM_CLOSURE_FORMALS (code);
  59. SCM env = SCM_ENV (code);
  60. SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
  61. SCM src = scm_i_unmemocopy_body (SCM_CODE (code), xenv);
  62. scm_putc (' ', port);
  63. scm_iprin1 (src, port, pstate);
  64. }
  65. scm_putc ('>', port);
  66. }
  67. return 1;
  68. }
  69. static SCM
  70. makmac (SCM code, scm_t_bits flags)
  71. {
  72. SCM z;
  73. SCM_NEWSMOB (z, scm_tc16_macro, SCM_UNPACK (code));
  74. SCM_SET_SMOB_FLAGS (z, flags);
  75. return z;
  76. }
  77. /* Return a mmacro that is known to be one of guile's built in macros. */
  78. SCM
  79. scm_i_makbimacro (SCM code)
  80. #define FUNC_NAME "scm_i_makbimacro"
  81. {
  82. SCM_VALIDATE_PROC (1, code);
  83. return makmac (code, 3);
  84. }
  85. #undef FUNC_NAME
  86. SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0,
  87. (SCM code),
  88. "Return a @dfn{macro} which, when a symbol defined to this value\n"
  89. "appears as the first symbol in an expression, evaluates the\n"
  90. "result of applying @var{code} to the expression and the\n"
  91. "environment.\n\n"
  92. "@code{procedure->memoizing-macro} is the same as\n"
  93. "@code{procedure->macro}, except that the expression returned by\n"
  94. "@var{code} replaces the original macro expression in the memoized\n"
  95. "form of the containing code.")
  96. #define FUNC_NAME s_scm_makmmacro
  97. {
  98. SCM_VALIDATE_PROC (1, code);
  99. return makmac (code, 2);
  100. }
  101. #undef FUNC_NAME
  102. SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0,
  103. (SCM code),
  104. "Return a @dfn{macro} which, when a symbol defined to this value\n"
  105. "appears as the first symbol in an expression, returns the\n"
  106. "result of applying @var{code} to the expression and the\n"
  107. "environment.")
  108. #define FUNC_NAME s_scm_makacro
  109. {
  110. SCM_VALIDATE_PROC (1, code);
  111. return makmac (code, 0);
  112. }
  113. #undef FUNC_NAME
  114. #if SCM_ENABLE_DEPRECATED == 1
  115. SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0,
  116. (SCM code),
  117. "Return a @dfn{macro} which, when a symbol defined to this value\n"
  118. "appears as the first symbol in an expression, evaluates the\n"
  119. "result of applying @var{code} to the expression and the\n"
  120. "environment. For example:\n"
  121. "\n"
  122. "@lisp\n"
  123. "(define trace\n"
  124. " (procedure->macro\n"
  125. " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n"
  126. "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n"
  127. "@end lisp")
  128. #define FUNC_NAME s_scm_makmacro
  129. {
  130. scm_c_issue_deprecation_warning
  131. ("The function procedure->macro is deprecated, and so are"
  132. " non-memoizing macros in general. Use memoizing macros"
  133. " or r5rs macros instead.");
  134. SCM_VALIDATE_PROC (1, code);
  135. return makmac (code, 1);
  136. }
  137. #undef FUNC_NAME
  138. #endif
  139. SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
  140. (SCM obj),
  141. "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n"
  142. "syntax transformer.")
  143. #define FUNC_NAME s_scm_macro_p
  144. {
  145. return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_macro, obj));
  146. }
  147. #undef FUNC_NAME
  148. SCM_SYMBOL (scm_sym_syntax, "syntax");
  149. #if SCM_ENABLE_DEPRECATED == 1
  150. SCM_SYMBOL (scm_sym_macro, "macro");
  151. #endif
  152. SCM_SYMBOL (scm_sym_mmacro, "macro!");
  153. SCM_SYMBOL (scm_sym_bimacro, "builtin-macro!");
  154. SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
  155. (SCM m),
  156. "Return one of the symbols @code{syntax}, @code{macro} or\n"
  157. "@code{macro!}, depending on whether @var{m} is a syntax\n"
  158. "transformer, a regular macro, or a memoizing macro,\n"
  159. "respectively. If @var{m} is not a macro, @code{#f} is\n"
  160. "returned.")
  161. #define FUNC_NAME s_scm_macro_type
  162. {
  163. if (!SCM_SMOB_PREDICATE (scm_tc16_macro, m))
  164. return SCM_BOOL_F;
  165. switch (SCM_MACRO_TYPE (m))
  166. {
  167. case 0: return scm_sym_syntax;
  168. #if SCM_ENABLE_DEPRECATED == 1
  169. case 1: return scm_sym_macro;
  170. #endif
  171. case 2: return scm_sym_mmacro;
  172. case 3: return scm_sym_bimacro;
  173. default: scm_wrong_type_arg (FUNC_NAME, 1, m);
  174. }
  175. }
  176. #undef FUNC_NAME
  177. SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
  178. (SCM m),
  179. "Return the name of the macro @var{m}.")
  180. #define FUNC_NAME s_scm_macro_name
  181. {
  182. SCM_VALIDATE_SMOB (1, m, macro);
  183. return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m)));
  184. }
  185. #undef FUNC_NAME
  186. SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
  187. (SCM m),
  188. "Return the transformer of the macro @var{m}.")
  189. #define FUNC_NAME s_scm_macro_transformer
  190. {
  191. SCM_VALIDATE_SMOB (1, m, macro);
  192. return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ?
  193. SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F);
  194. }
  195. #undef FUNC_NAME
  196. SCM
  197. scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() )
  198. {
  199. SCM var = scm_c_define (name, SCM_UNDEFINED);
  200. SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
  201. SCM_VARIABLE_SET (var, macroizer (transformer));
  202. return SCM_UNSPECIFIED;
  203. }
  204. void
  205. scm_init_macros ()
  206. {
  207. scm_tc16_macro = scm_make_smob_type ("macro", 0);
  208. scm_set_smob_mark (scm_tc16_macro, scm_markcdr);
  209. scm_set_smob_print (scm_tc16_macro, macro_print);
  210. #include "libguile/macros.x"
  211. }
  212. /*
  213. Local Variables:
  214. c-file-style: "gnu"
  215. End:
  216. */