Commit | Line | Data |
---|---|---|
9f09b127 | 1 | /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. |
99027e3c | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
99027e3c | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
99027e3c | 12 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
99027e3c | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
99027e3c | 24 | |
0eb934f1 LC |
25 | #define SCM_BUILDING_DEPRECATED_CODE |
26 | ||
a0599745 | 27 | #include "libguile/_scm.h" |
726d810a DH |
28 | #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */ |
29 | #include "libguile/eval.h" | |
30 | #include "libguile/ports.h" | |
31 | #include "libguile/print.h" | |
a0599745 MD |
32 | #include "libguile/root.h" |
33 | #include "libguile/smob.h" | |
1d1559ce | 34 | #include "libguile/deprecation.h" |
99027e3c | 35 | |
a0599745 | 36 | #include "libguile/validate.h" |
3633ff4e | 37 | #include "libguile/programs.h" |
a0599745 | 38 | #include "libguile/macros.h" |
99027e3c | 39 | |
22fc179a HWN |
40 | #include "libguile/private-options.h" |
41 | ||
92c2555f | 42 | scm_t_bits scm_tc16_macro; |
99027e3c | 43 | |
726d810a DH |
44 | |
45 | static int | |
46 | macro_print (SCM macro, SCM port, scm_print_state *pstate) | |
47 | { | |
48 | SCM code = SCM_MACRO_CODE (macro); | |
5a0132b3 | 49 | |
314b8716 AW |
50 | scm_puts ("#<", port); |
51 | ||
52 | if (SCM_MACRO_TYPE (macro) < 4 && SCM_MACRO_IS_EXTENDED (macro)) | |
53 | scm_puts ("extended-", port); | |
5a0132b3 | 54 | |
314b8716 AW |
55 | /* FIXME: doesn't catch boot closures; but do we care? */ |
56 | if (!SCM_PROGRAM_P (code)) | |
57 | scm_puts ("primitive-", port); | |
726d810a | 58 | |
314b8716 AW |
59 | if (SCM_MACRO_TYPE (macro) == 3) |
60 | scm_puts ("builtin-macro!", port); | |
61 | if (SCM_MACRO_TYPE (macro) == 4) | |
62 | scm_puts ("syncase-macro", port); | |
726d810a | 63 | |
314b8716 AW |
64 | scm_putc (' ', port); |
65 | scm_iprin1 (scm_macro_name (macro), port, pstate); | |
5a0132b3 | 66 | |
314b8716 AW |
67 | if (SCM_MACRO_IS_EXTENDED (macro)) |
68 | { | |
69 | scm_putc (' ', port); | |
70 | scm_write (SCM_SMOB_OBJECT_2 (macro), port); | |
71 | scm_putc (' ', port); | |
72 | scm_write (SCM_SMOB_OBJECT_3 (macro), port); | |
726d810a DH |
73 | } |
74 | ||
314b8716 AW |
75 | scm_putc ('>', port); |
76 | ||
726d810a DH |
77 | return 1; |
78 | } | |
79 | ||
f5710d53 MV |
80 | static SCM |
81 | makmac (SCM code, scm_t_bits flags) | |
82 | { | |
83 | SCM z; | |
84 | SCM_NEWSMOB (z, scm_tc16_macro, SCM_UNPACK (code)); | |
85 | SCM_SET_SMOB_FLAGS (z, flags); | |
86 | return z; | |
87 | } | |
726d810a | 88 | |
3b88ed2a DH |
89 | /* Return a mmacro that is known to be one of guile's built in macros. */ |
90 | SCM | |
bab98046 | 91 | scm_i_makbimacro (const char *name, SCM (*fn)(SCM, SCM)) |
3b88ed2a | 92 | { |
bab98046 | 93 | return makmac (scm_c_make_gsubr (name, 2, 0, 0, fn), 3); |
3b88ed2a | 94 | } |
3b88ed2a DH |
95 | |
96 | ||
5a0132b3 AW |
97 | SCM_DEFINE (scm_make_syncase_macro, "make-syncase-macro", 2, 0, 0, |
98 | (SCM type, SCM binding), | |
99 | "Return a @dfn{macro} that requires expansion by syntax-case.\n" | |
100 | "While users should not call this function, it is useful to know\n" | |
101 | "that syntax-case macros are represented as Guile primitive macros.") | |
102 | #define FUNC_NAME s_scm_make_syncase_macro | |
103 | { | |
104 | SCM z; | |
105 | SCM_VALIDATE_SYMBOL (1, type); | |
106 | ||
107 | SCM_NEWSMOB3 (z, scm_tc16_macro, SCM_UNPACK (binding), SCM_UNPACK (type), | |
108 | SCM_UNPACK (binding)); | |
109 | SCM_SET_SMOB_FLAGS (z, 4 | SCM_F_MACRO_EXTENDED); | |
110 | return z; | |
111 | } | |
112 | #undef FUNC_NAME | |
113 | ||
114 | SCM_DEFINE (scm_make_extended_syncase_macro, "make-extended-syncase-macro", 3, 0, 0, | |
115 | (SCM m, SCM type, SCM binding), | |
116 | "Extend a core macro @var{m} with a syntax-case binding.") | |
117 | #define FUNC_NAME s_scm_make_extended_syncase_macro | |
118 | { | |
119 | SCM z; | |
120 | SCM_VALIDATE_SMOB (1, m, macro); | |
121 | SCM_VALIDATE_SYMBOL (2, type); | |
122 | ||
123 | SCM_NEWSMOB3 (z, scm_tc16_macro, SCM_SMOB_DATA (m), SCM_UNPACK (type), | |
124 | SCM_UNPACK (binding)); | |
125 | SCM_SET_SMOB_FLAGS (z, SCM_SMOB_FLAGS (m) | SCM_F_MACRO_EXTENDED); | |
126 | return z; | |
127 | } | |
128 | #undef FUNC_NAME | |
129 | ||
130 | ||
99027e3c | 131 | |
a1ec6916 | 132 | SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0, |
1bbd0b84 | 133 | (SCM obj), |
3d5f3091 AW |
134 | "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro, a\n" |
135 | "syntax transformer, or a syntax-case macro.") | |
1bbd0b84 | 136 | #define FUNC_NAME s_scm_macro_p |
99027e3c | 137 | { |
7888309b | 138 | return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_macro, obj)); |
99027e3c | 139 | } |
1bbd0b84 | 140 | #undef FUNC_NAME |
99027e3c MD |
141 | |
142 | ||
3b88ed2a | 143 | SCM_SYMBOL (scm_sym_bimacro, "builtin-macro!"); |
5a0132b3 | 144 | SCM_SYMBOL (scm_sym_syncase_macro, "syncase-macro"); |
99027e3c | 145 | |
a1ec6916 | 146 | SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0, |
1bbd0b84 | 147 | (SCM m), |
5a0132b3 AW |
148 | "Return one of the symbols @code{syntax}, @code{macro},\n" |
149 | "@code{macro!}, or @code{syntax-case}, depending on whether\n" | |
150 | "@var{m} is a syntax transformer, a regular macro, a memoizing\n" | |
151 | "macro, or a syntax-case macro, respectively. If @var{m} is\n" | |
152 | "not a macro, @code{#f} is returned.") | |
1bbd0b84 | 153 | #define FUNC_NAME s_scm_macro_type |
99027e3c | 154 | { |
f5710d53 | 155 | if (!SCM_SMOB_PREDICATE (scm_tc16_macro, m)) |
99027e3c | 156 | return SCM_BOOL_F; |
726d810a | 157 | switch (SCM_MACRO_TYPE (m)) |
99027e3c | 158 | { |
3b88ed2a | 159 | case 3: return scm_sym_bimacro; |
5a0132b3 | 160 | case 4: return scm_sym_syncase_macro; |
1bbd0b84 | 161 | default: scm_wrong_type_arg (FUNC_NAME, 1, m); |
99027e3c MD |
162 | } |
163 | } | |
1bbd0b84 | 164 | #undef FUNC_NAME |
99027e3c MD |
165 | |
166 | ||
a1ec6916 | 167 | SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0, |
1bbd0b84 | 168 | (SCM m), |
88c927e9 | 169 | "Return the name of the macro @var{m}.") |
1bbd0b84 | 170 | #define FUNC_NAME s_scm_macro_name |
99027e3c | 171 | { |
34d19ef6 | 172 | SCM_VALIDATE_SMOB (1, m, macro); |
5a0132b3 AW |
173 | if (scm_is_true (scm_procedure_p (SCM_SMOB_OBJECT (m)))) |
174 | return scm_procedure_name (SCM_SMOB_OBJECT (m)); | |
175 | return SCM_BOOL_F; | |
99027e3c | 176 | } |
1bbd0b84 | 177 | #undef FUNC_NAME |
99027e3c MD |
178 | |
179 | ||
a1ec6916 | 180 | SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0, |
1bbd0b84 | 181 | (SCM m), |
88c927e9 | 182 | "Return the transformer of the macro @var{m}.") |
1bbd0b84 | 183 | #define FUNC_NAME s_scm_macro_transformer |
99027e3c | 184 | { |
6c289afe AW |
185 | SCM data; |
186 | ||
34d19ef6 | 187 | SCM_VALIDATE_SMOB (1, m, macro); |
6c289afe AW |
188 | data = SCM_PACK (SCM_SMOB_DATA (m)); |
189 | ||
314b8716 | 190 | if (scm_is_true (scm_procedure_p (data))) |
6c289afe AW |
191 | return data; |
192 | else | |
193 | return SCM_BOOL_F; | |
99027e3c | 194 | } |
1bbd0b84 | 195 | #undef FUNC_NAME |
99027e3c | 196 | |
5a0132b3 AW |
197 | SCM_DEFINE (scm_syncase_macro_type, "syncase-macro-type", 1, 0, 0, |
198 | (SCM m), | |
199 | "Return the type of the macro @var{m}.") | |
200 | #define FUNC_NAME s_scm_syncase_macro_type | |
201 | { | |
202 | SCM_VALIDATE_SMOB (1, m, macro); | |
203 | ||
204 | if (SCM_MACRO_IS_EXTENDED (m)) | |
205 | return SCM_SMOB_OBJECT_2 (m); | |
206 | else | |
207 | return SCM_BOOL_F; | |
208 | } | |
209 | #undef FUNC_NAME | |
210 | ||
211 | SCM_DEFINE (scm_syncase_macro_binding, "syncase-macro-binding", 1, 0, 0, | |
212 | (SCM m), | |
213 | "Return the binding of the macro @var{m}.") | |
214 | #define FUNC_NAME s_scm_syncase_macro_binding | |
215 | { | |
216 | SCM_VALIDATE_SMOB (1, m, macro); | |
217 | ||
218 | if (SCM_MACRO_IS_EXTENDED (m)) | |
219 | return SCM_SMOB_OBJECT_3 (m); | |
220 | else | |
221 | return SCM_BOOL_F; | |
222 | } | |
223 | #undef FUNC_NAME | |
224 | ||
99027e3c MD |
225 | void |
226 | scm_init_macros () | |
227 | { | |
e841c3e0 | 228 | scm_tc16_macro = scm_make_smob_type ("macro", 0); |
726d810a | 229 | scm_set_smob_print (scm_tc16_macro, macro_print); |
a0599745 | 230 | #include "libguile/macros.x" |
99027e3c | 231 | } |
89e00824 ML |
232 | |
233 | /* | |
234 | Local Variables: | |
235 | c-file-style: "gnu" | |
236 | End: | |
237 | */ |