macros.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003, 2006, 2008, 2009, 2010, 2011 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/ports.h"
  23. #include "libguile/print.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/validate.h"
  26. #include "libguile/macros.h"
  27. #include "libguile/private-options.h"
  28. static scm_t_bits scm_tc16_macro;
  29. #define SCM_MACROP(x) SCM_SMOB_PREDICATE (scm_tc16_macro, (x))
  30. #define SCM_MACRO_PRIMITIVE(m) ((scm_t_macro_primitive)SCM_SMOB_DATA (m))
  31. #define SCM_MACRO_NAME(m) (SCM_SMOB_OBJECT_2 (m))
  32. #define SCM_MACRO_TYPE(m) (SCM_SMOB_OBJECT_3 (m))
  33. #define SCM_MACRO_BINDING(m) (SCM_CELL_OBJECT ((m), 4))
  34. #define SCM_VALIDATE_MACRO(p,v) SCM_MAKE_VALIDATE ((p), (v), MACROP)
  35. SCM_API scm_t_bits scm_tc16_macro;
  36. static int
  37. macro_print (SCM macro, SCM port, scm_print_state *pstate)
  38. {
  39. if (scm_is_false (SCM_MACRO_TYPE (macro)))
  40. scm_puts ("#<primitive-syntax-transformer ", port);
  41. else
  42. scm_puts ("#<syntax-transformer ", port);
  43. scm_iprin1 (scm_macro_name (macro), port, pstate);
  44. scm_putc ('>', port);
  45. return 1;
  46. }
  47. /* Return a mmacro that is known to be one of guile's built in macros. */
  48. SCM
  49. scm_i_make_primitive_macro (const char *name, scm_t_macro_primitive fn)
  50. {
  51. SCM z = scm_words (scm_tc16_macro, 5);
  52. SCM_SET_SMOB_DATA_N (z, 1, (scm_t_bits)fn);
  53. SCM_SET_SMOB_OBJECT_N (z, 2, scm_from_locale_symbol (name));
  54. SCM_SET_SMOB_OBJECT_N (z, 3, SCM_BOOL_F);
  55. SCM_SET_SMOB_OBJECT_N (z, 4, SCM_BOOL_F);
  56. return z;
  57. }
  58. scm_t_macro_primitive
  59. scm_i_macro_primitive (SCM macro)
  60. {
  61. return SCM_MACRO_PRIMITIVE (macro);
  62. }
  63. SCM_DEFINE (scm_make_syntax_transformer, "make-syntax-transformer", 3, 0, 0,
  64. (SCM name, SCM type, SCM binding),
  65. "Construct a @dfn{syntax transformer}.\n\n"
  66. "This function is part of Guile's low-level support for the psyntax\n"
  67. "syntax expander. Users should not call this function.")
  68. #define FUNC_NAME s_scm_make_syntax_transformer
  69. {
  70. SCM z;
  71. SCM (*prim)(SCM,SCM) = NULL;
  72. if (scm_is_true (name))
  73. {
  74. SCM existing_var;
  75. SCM_VALIDATE_SYMBOL (1, name);
  76. existing_var = scm_sym2var (name, scm_current_module_lookup_closure (),
  77. SCM_BOOL_F);
  78. if (scm_is_true (existing_var)
  79. && scm_is_true (scm_variable_bound_p (existing_var))
  80. && SCM_MACROP (SCM_VARIABLE_REF (existing_var)))
  81. prim = SCM_MACRO_PRIMITIVE (SCM_VARIABLE_REF (existing_var));
  82. }
  83. SCM_VALIDATE_SYMBOL (2, type);
  84. z = scm_words (scm_tc16_macro, 5);
  85. SCM_SET_SMOB_DATA_N (z, 1, (scm_t_bits)prim);
  86. SCM_SET_SMOB_OBJECT_N (z, 2, name);
  87. SCM_SET_SMOB_OBJECT_N (z, 3, type);
  88. SCM_SET_SMOB_OBJECT_N (z, 4, binding);
  89. return z;
  90. }
  91. #undef FUNC_NAME
  92. SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
  93. (SCM obj),
  94. "Return @code{#t} if @var{obj} is a syntax transformer (an object that "
  95. "transforms Scheme expressions at expansion-time).\n\n"
  96. "Macros are actually just one kind of syntax transformer; this\n"
  97. "procedure has its name due to historical reasons.")
  98. #define FUNC_NAME s_scm_macro_p
  99. {
  100. return scm_from_bool (SCM_MACROP (obj));
  101. }
  102. #undef FUNC_NAME
  103. SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
  104. (SCM m),
  105. "Return the type of the syntax transformer @var{m}, as passed to\n"
  106. "@code{make-syntax-transformer}. If @var{m} is a primitive syntax\n"
  107. "transformer, @code{#f} will be returned.")
  108. #define FUNC_NAME s_scm_macro_type
  109. {
  110. SCM_VALIDATE_MACRO (1, m);
  111. return SCM_MACRO_TYPE (m);
  112. }
  113. #undef FUNC_NAME
  114. SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
  115. (SCM m),
  116. "Return the name of the syntax transformer @var{m}.")
  117. #define FUNC_NAME s_scm_macro_name
  118. {
  119. SCM_VALIDATE_MACRO (1, m);
  120. return SCM_MACRO_NAME (m);
  121. }
  122. #undef FUNC_NAME
  123. SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
  124. (SCM m),
  125. "Return the transformer procedure of the macro @var{m}.\n\n"
  126. "If @var{m} is a syntax transformer but not a macro, @code{#f}\n"
  127. "will be returned. (This can happen, for example, with primitive\n"
  128. "syntax transformers).")
  129. #define FUNC_NAME s_scm_macro_transformer
  130. {
  131. SCM_VALIDATE_MACRO (1, m);
  132. /* here we rely on knowledge of how psyntax represents macro bindings, but
  133. hey, there is code out there that calls this function, and expects to get
  134. a procedure in return... */
  135. if (scm_is_true (scm_procedure_p (SCM_MACRO_BINDING (m))))
  136. return SCM_MACRO_BINDING (m);
  137. else
  138. return SCM_BOOL_F;
  139. }
  140. #undef FUNC_NAME
  141. SCM_DEFINE (scm_macro_binding, "macro-binding", 1, 0, 0,
  142. (SCM m),
  143. "Return the binding of the syntax transformer @var{m}, as passed to\n"
  144. "@code{make-syntax-transformer}. If @var{m} is a primitive syntax\n"
  145. "transformer, @code{#f} will be returned.")
  146. #define FUNC_NAME s_scm_macro_binding
  147. {
  148. SCM_VALIDATE_MACRO (1, m);
  149. return SCM_MACRO_BINDING (m);
  150. }
  151. #undef FUNC_NAME
  152. void
  153. scm_init_macros ()
  154. {
  155. scm_tc16_macro = scm_make_smob_type ("macro", 0);
  156. scm_set_smob_print (scm_tc16_macro, macro_print);
  157. #include "libguile/macros.x"
  158. }
  159. /*
  160. Local Variables:
  161. c-file-style: "gnu"
  162. End:
  163. */