macros.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003, 2006, 2008, 2009, 2010, 2011, 2012 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_unlocked ("#<primitive-syntax-transformer ", port);
  41. else
  42. scm_puts_unlocked ("#<syntax-transformer ", port);
  43. scm_iprin1 (scm_macro_name (macro), port, pstate);
  44. scm_putc_unlocked ('>', 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_utf8_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_module_variable (scm_current_module (), name);
  77. if (scm_is_true (existing_var)
  78. && scm_is_true (scm_variable_bound_p (existing_var))
  79. && SCM_MACROP (SCM_VARIABLE_REF (existing_var)))
  80. prim = SCM_MACRO_PRIMITIVE (SCM_VARIABLE_REF (existing_var));
  81. }
  82. SCM_VALIDATE_SYMBOL (2, type);
  83. z = scm_words (scm_tc16_macro, 5);
  84. SCM_SET_SMOB_DATA_N (z, 1, prim);
  85. SCM_SET_SMOB_OBJECT_N (z, 2, name);
  86. SCM_SET_SMOB_OBJECT_N (z, 3, type);
  87. SCM_SET_SMOB_OBJECT_N (z, 4, binding);
  88. return z;
  89. }
  90. #undef FUNC_NAME
  91. SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
  92. (SCM obj),
  93. "Return @code{#t} if @var{obj} is a syntax transformer (an object that "
  94. "transforms Scheme expressions at expansion-time).\n\n"
  95. "Macros are actually just one kind of syntax transformer; this\n"
  96. "procedure has its name due to historical reasons.")
  97. #define FUNC_NAME s_scm_macro_p
  98. {
  99. return scm_from_bool (SCM_MACROP (obj));
  100. }
  101. #undef FUNC_NAME
  102. SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
  103. (SCM m),
  104. "Return the type of the syntax transformer @var{m}, as passed to\n"
  105. "@code{make-syntax-transformer}. If @var{m} is a primitive syntax\n"
  106. "transformer, @code{#f} will be returned.")
  107. #define FUNC_NAME s_scm_macro_type
  108. {
  109. SCM_VALIDATE_MACRO (1, m);
  110. return SCM_MACRO_TYPE (m);
  111. }
  112. #undef FUNC_NAME
  113. SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
  114. (SCM m),
  115. "Return the name of the syntax transformer @var{m}.")
  116. #define FUNC_NAME s_scm_macro_name
  117. {
  118. SCM_VALIDATE_MACRO (1, m);
  119. return SCM_MACRO_NAME (m);
  120. }
  121. #undef FUNC_NAME
  122. SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
  123. (SCM m),
  124. "Return the transformer procedure of the macro @var{m}.\n\n"
  125. "If @var{m} is a syntax transformer but not a macro, @code{#f}\n"
  126. "will be returned. (This can happen, for example, with primitive\n"
  127. "syntax transformers).")
  128. #define FUNC_NAME s_scm_macro_transformer
  129. {
  130. SCM_VALIDATE_MACRO (1, m);
  131. /* here we rely on knowledge of how psyntax represents macro bindings, but
  132. hey, there is code out there that calls this function, and expects to get
  133. a procedure in return... */
  134. if (scm_is_true (scm_procedure_p (SCM_MACRO_BINDING (m))))
  135. return SCM_MACRO_BINDING (m);
  136. else
  137. return SCM_BOOL_F;
  138. }
  139. #undef FUNC_NAME
  140. SCM_DEFINE (scm_macro_binding, "macro-binding", 1, 0, 0,
  141. (SCM m),
  142. "Return the binding of the syntax transformer @var{m}, as passed to\n"
  143. "@code{make-syntax-transformer}. If @var{m} is a primitive syntax\n"
  144. "transformer, @code{#f} will be returned.")
  145. #define FUNC_NAME s_scm_macro_binding
  146. {
  147. SCM_VALIDATE_MACRO (1, m);
  148. return SCM_MACRO_BINDING (m);
  149. }
  150. #undef FUNC_NAME
  151. static SCM syntax_session_id;
  152. #define SESSION_ID_LENGTH 22 /* bytes */
  153. #define BASE64_RADIX_BITS 6
  154. #define BASE64_RADIX (1 << (BASE64_RADIX_BITS))
  155. #define BASE64_MASK (BASE64_RADIX - 1)
  156. static SCM
  157. fresh_syntax_session_id (void)
  158. {
  159. static const char base64[BASE64_RADIX] =
  160. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$@";
  161. unsigned char digit_buf[SESSION_ID_LENGTH];
  162. char char_buf[SESSION_ID_LENGTH];
  163. size_t i;
  164. scm_i_random_bytes_from_platform (digit_buf, SESSION_ID_LENGTH);
  165. for (i = 0; i < SESSION_ID_LENGTH; ++i)
  166. char_buf[i] = base64[digit_buf[i] & BASE64_MASK];
  167. return scm_from_latin1_stringn (char_buf, SESSION_ID_LENGTH);
  168. }
  169. static SCM
  170. scm_syntax_session_id (void)
  171. {
  172. return syntax_session_id;
  173. }
  174. void
  175. scm_init_macros ()
  176. {
  177. scm_tc16_macro = scm_make_smob_type ("macro", 0);
  178. scm_set_smob_print (scm_tc16_macro, macro_print);
  179. #include "libguile/macros.x"
  180. syntax_session_id = fresh_syntax_session_id();
  181. scm_c_define_gsubr ("syntax-session-id", 0, 0, 0, scm_syntax_session_id);
  182. }
  183. /*
  184. Local Variables:
  185. c-file-style: "gnu"
  186. End:
  187. */