Commit | Line | Data |
---|---|---|
dbb605f5 | 1 | /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003, 2006, 2008 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 | |
a0599745 | 25 | #include "libguile/_scm.h" |
726d810a DH |
26 | #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */ |
27 | #include "libguile/eval.h" | |
28 | #include "libguile/ports.h" | |
29 | #include "libguile/print.h" | |
a0599745 MD |
30 | #include "libguile/root.h" |
31 | #include "libguile/smob.h" | |
1d1559ce | 32 | #include "libguile/deprecation.h" |
99027e3c | 33 | |
a0599745 | 34 | #include "libguile/validate.h" |
3633ff4e | 35 | #include "libguile/programs.h" |
a0599745 | 36 | #include "libguile/macros.h" |
99027e3c | 37 | |
22fc179a HWN |
38 | #include "libguile/private-options.h" |
39 | ||
92c2555f | 40 | scm_t_bits scm_tc16_macro; |
99027e3c | 41 | |
726d810a DH |
42 | |
43 | static int | |
44 | macro_print (SCM macro, SCM port, scm_print_state *pstate) | |
45 | { | |
46 | SCM code = SCM_MACRO_CODE (macro); | |
47 | if (!SCM_CLOSUREP (code) | |
7888309b MV |
48 | || scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE)) |
49 | || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE, | |
726d810a DH |
50 | macro, port, pstate))) |
51 | { | |
5a0132b3 AW |
52 | scm_puts ("#<", port); |
53 | ||
54 | if (SCM_MACRO_TYPE (macro) < 4 && SCM_MACRO_IS_EXTENDED (macro)) | |
55 | scm_puts ("extended-", port); | |
56 | ||
3633ff4e | 57 | if (!SCM_CLOSUREP (code) && !SCM_PROGRAM_P (code)) |
5a0132b3 | 58 | scm_puts ("primitive-", port); |
726d810a DH |
59 | |
60 | if (SCM_MACRO_TYPE (macro) == 0) | |
61 | scm_puts ("syntax", port); | |
3063e30a DH |
62 | #if SCM_ENABLE_DEPRECATED == 1 |
63 | if (SCM_MACRO_TYPE (macro) == 1) | |
726d810a | 64 | scm_puts ("macro", port); |
3063e30a | 65 | #endif |
726d810a DH |
66 | if (SCM_MACRO_TYPE (macro) == 2) |
67 | scm_puts ("macro!", port); | |
3b88ed2a DH |
68 | if (SCM_MACRO_TYPE (macro) == 3) |
69 | scm_puts ("builtin-macro!", port); | |
5a0132b3 AW |
70 | if (SCM_MACRO_TYPE (macro) == 4) |
71 | scm_puts ("syncase-macro", port); | |
3b88ed2a | 72 | |
726d810a DH |
73 | scm_putc (' ', port); |
74 | scm_iprin1 (scm_macro_name (macro), port, pstate); | |
75 | ||
76 | if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P) | |
77 | { | |
78 | SCM formals = SCM_CLOSURE_FORMALS (code); | |
79 | SCM env = SCM_ENV (code); | |
80 | SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env); | |
9fcf3cbb | 81 | SCM src = scm_i_unmemocopy_body (SCM_CODE (code), xenv); |
726d810a DH |
82 | scm_putc (' ', port); |
83 | scm_iprin1 (src, port, pstate); | |
84 | } | |
85 | ||
5a0132b3 AW |
86 | if (SCM_MACRO_IS_EXTENDED (macro)) |
87 | { | |
88 | scm_putc (' ', port); | |
89 | scm_write (SCM_SMOB_OBJECT_2 (macro), port); | |
90 | scm_putc (' ', port); | |
91 | scm_write (SCM_SMOB_OBJECT_3 (macro), port); | |
92 | } | |
93 | ||
726d810a DH |
94 | scm_putc ('>', port); |
95 | } | |
96 | ||
97 | return 1; | |
98 | } | |
99 | ||
5a0132b3 AW |
100 | static SCM |
101 | macro_mark (SCM macro) | |
102 | { | |
103 | if (SCM_MACRO_IS_EXTENDED (macro)) | |
104 | { scm_gc_mark (SCM_SMOB_OBJECT_2 (macro)); | |
105 | scm_gc_mark (SCM_SMOB_OBJECT_3 (macro)); | |
106 | } | |
107 | return SCM_SMOB_OBJECT (macro); | |
108 | } | |
109 | ||
f5710d53 MV |
110 | static SCM |
111 | makmac (SCM code, scm_t_bits flags) | |
112 | { | |
113 | SCM z; | |
114 | SCM_NEWSMOB (z, scm_tc16_macro, SCM_UNPACK (code)); | |
115 | SCM_SET_SMOB_FLAGS (z, flags); | |
116 | return z; | |
117 | } | |
726d810a | 118 | |
3b88ed2a DH |
119 | /* Return a mmacro that is known to be one of guile's built in macros. */ |
120 | SCM | |
121 | scm_i_makbimacro (SCM code) | |
122 | #define FUNC_NAME "scm_i_makbimacro" | |
123 | { | |
124 | SCM_VALIDATE_PROC (1, code); | |
f5710d53 | 125 | return makmac (code, 3); |
3b88ed2a DH |
126 | } |
127 | #undef FUNC_NAME | |
128 | ||
129 | ||
130 | SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0, | |
131 | (SCM code), | |
132 | "Return a @dfn{macro} which, when a symbol defined to this value\n" | |
133 | "appears as the first symbol in an expression, evaluates the\n" | |
134 | "result of applying @var{code} to the expression and the\n" | |
135 | "environment.\n\n" | |
136 | "@code{procedure->memoizing-macro} is the same as\n" | |
137 | "@code{procedure->macro}, except that the expression returned by\n" | |
138 | "@var{code} replaces the original macro expression in the memoized\n" | |
139 | "form of the containing code.") | |
140 | #define FUNC_NAME s_scm_makmmacro | |
141 | { | |
142 | SCM_VALIDATE_PROC (1, code); | |
f5710d53 | 143 | return makmac (code, 2); |
3b88ed2a DH |
144 | } |
145 | #undef FUNC_NAME | |
146 | ||
147 | ||
a1ec6916 | 148 | SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0, |
1bbd0b84 | 149 | (SCM code), |
1e6808ea MG |
150 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
151 | "appears as the first symbol in an expression, returns the\n" | |
152 | "result of applying @var{code} to the expression and the\n" | |
153 | "environment.") | |
1bbd0b84 | 154 | #define FUNC_NAME s_scm_makacro |
99027e3c | 155 | { |
34d19ef6 | 156 | SCM_VALIDATE_PROC (1, code); |
f5710d53 | 157 | return makmac (code, 0); |
99027e3c | 158 | } |
1bbd0b84 | 159 | #undef FUNC_NAME |
99027e3c MD |
160 | |
161 | ||
3063e30a DH |
162 | #if SCM_ENABLE_DEPRECATED == 1 |
163 | ||
3b3b36dd | 164 | SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0, |
1bbd0b84 | 165 | (SCM code), |
1e6808ea MG |
166 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
167 | "appears as the first symbol in an expression, evaluates the\n" | |
168 | "result of applying @var{code} to the expression and the\n" | |
bb2c02f2 | 169 | "environment. For example:\n" |
1e6808ea MG |
170 | "\n" |
171 | "@lisp\n" | |
872e0c72 NJ |
172 | "(define trace\n" |
173 | " (procedure->macro\n" | |
174 | " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n" | |
175 | "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n" | |
1e6808ea | 176 | "@end lisp") |
1bbd0b84 | 177 | #define FUNC_NAME s_scm_makmacro |
99027e3c | 178 | { |
3063e30a DH |
179 | scm_c_issue_deprecation_warning |
180 | ("The function procedure->macro is deprecated, and so are" | |
181 | " non-memoizing macros in general. Use memoizing macros" | |
182 | " or r5rs macros instead."); | |
183 | ||
34d19ef6 | 184 | SCM_VALIDATE_PROC (1, code); |
f5710d53 | 185 | return makmac (code, 1); |
99027e3c | 186 | } |
1bbd0b84 | 187 | #undef FUNC_NAME |
99027e3c | 188 | |
3063e30a DH |
189 | #endif |
190 | ||
5a0132b3 AW |
191 | SCM_DEFINE (scm_make_syncase_macro, "make-syncase-macro", 2, 0, 0, |
192 | (SCM type, SCM binding), | |
193 | "Return a @dfn{macro} that requires expansion by syntax-case.\n" | |
194 | "While users should not call this function, it is useful to know\n" | |
195 | "that syntax-case macros are represented as Guile primitive macros.") | |
196 | #define FUNC_NAME s_scm_make_syncase_macro | |
197 | { | |
198 | SCM z; | |
199 | SCM_VALIDATE_SYMBOL (1, type); | |
200 | ||
201 | SCM_NEWSMOB3 (z, scm_tc16_macro, SCM_UNPACK (binding), SCM_UNPACK (type), | |
202 | SCM_UNPACK (binding)); | |
203 | SCM_SET_SMOB_FLAGS (z, 4 | SCM_F_MACRO_EXTENDED); | |
204 | return z; | |
205 | } | |
206 | #undef FUNC_NAME | |
207 | ||
208 | SCM_DEFINE (scm_make_extended_syncase_macro, "make-extended-syncase-macro", 3, 0, 0, | |
209 | (SCM m, SCM type, SCM binding), | |
210 | "Extend a core macro @var{m} with a syntax-case binding.") | |
211 | #define FUNC_NAME s_scm_make_extended_syncase_macro | |
212 | { | |
213 | SCM z; | |
214 | SCM_VALIDATE_SMOB (1, m, macro); | |
215 | SCM_VALIDATE_SYMBOL (2, type); | |
216 | ||
217 | SCM_NEWSMOB3 (z, scm_tc16_macro, SCM_SMOB_DATA (m), SCM_UNPACK (type), | |
218 | SCM_UNPACK (binding)); | |
219 | SCM_SET_SMOB_FLAGS (z, SCM_SMOB_FLAGS (m) | SCM_F_MACRO_EXTENDED); | |
220 | return z; | |
221 | } | |
222 | #undef FUNC_NAME | |
223 | ||
224 | ||
99027e3c | 225 | |
a1ec6916 | 226 | SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0, |
1bbd0b84 | 227 | (SCM obj), |
3d5f3091 AW |
228 | "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro, a\n" |
229 | "syntax transformer, or a syntax-case macro.") | |
1bbd0b84 | 230 | #define FUNC_NAME s_scm_macro_p |
99027e3c | 231 | { |
7888309b | 232 | return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_macro, obj)); |
99027e3c | 233 | } |
1bbd0b84 | 234 | #undef FUNC_NAME |
99027e3c MD |
235 | |
236 | ||
237 | SCM_SYMBOL (scm_sym_syntax, "syntax"); | |
3063e30a | 238 | #if SCM_ENABLE_DEPRECATED == 1 |
99027e3c | 239 | SCM_SYMBOL (scm_sym_macro, "macro"); |
3063e30a | 240 | #endif |
99027e3c | 241 | SCM_SYMBOL (scm_sym_mmacro, "macro!"); |
3b88ed2a | 242 | SCM_SYMBOL (scm_sym_bimacro, "builtin-macro!"); |
5a0132b3 | 243 | SCM_SYMBOL (scm_sym_syncase_macro, "syncase-macro"); |
99027e3c | 244 | |
a1ec6916 | 245 | SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0, |
1bbd0b84 | 246 | (SCM m), |
5a0132b3 AW |
247 | "Return one of the symbols @code{syntax}, @code{macro},\n" |
248 | "@code{macro!}, or @code{syntax-case}, depending on whether\n" | |
249 | "@var{m} is a syntax transformer, a regular macro, a memoizing\n" | |
250 | "macro, or a syntax-case macro, respectively. If @var{m} is\n" | |
251 | "not a macro, @code{#f} is returned.") | |
1bbd0b84 | 252 | #define FUNC_NAME s_scm_macro_type |
99027e3c | 253 | { |
f5710d53 | 254 | if (!SCM_SMOB_PREDICATE (scm_tc16_macro, m)) |
99027e3c | 255 | return SCM_BOOL_F; |
726d810a | 256 | switch (SCM_MACRO_TYPE (m)) |
99027e3c MD |
257 | { |
258 | case 0: return scm_sym_syntax; | |
3063e30a | 259 | #if SCM_ENABLE_DEPRECATED == 1 |
99027e3c | 260 | case 1: return scm_sym_macro; |
3063e30a | 261 | #endif |
99027e3c | 262 | case 2: return scm_sym_mmacro; |
3b88ed2a | 263 | case 3: return scm_sym_bimacro; |
5a0132b3 | 264 | case 4: return scm_sym_syncase_macro; |
1bbd0b84 | 265 | default: scm_wrong_type_arg (FUNC_NAME, 1, m); |
99027e3c MD |
266 | } |
267 | } | |
1bbd0b84 | 268 | #undef FUNC_NAME |
99027e3c MD |
269 | |
270 | ||
a1ec6916 | 271 | SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0, |
1bbd0b84 | 272 | (SCM m), |
88c927e9 | 273 | "Return the name of the macro @var{m}.") |
1bbd0b84 | 274 | #define FUNC_NAME s_scm_macro_name |
99027e3c | 275 | { |
34d19ef6 | 276 | SCM_VALIDATE_SMOB (1, m, macro); |
5a0132b3 AW |
277 | if (scm_is_true (scm_procedure_p (SCM_SMOB_OBJECT (m)))) |
278 | return scm_procedure_name (SCM_SMOB_OBJECT (m)); | |
279 | return SCM_BOOL_F; | |
99027e3c | 280 | } |
1bbd0b84 | 281 | #undef FUNC_NAME |
99027e3c MD |
282 | |
283 | ||
a1ec6916 | 284 | SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0, |
1bbd0b84 | 285 | (SCM m), |
88c927e9 | 286 | "Return the transformer of the macro @var{m}.") |
1bbd0b84 | 287 | #define FUNC_NAME s_scm_macro_transformer |
99027e3c | 288 | { |
6c289afe AW |
289 | SCM data; |
290 | ||
34d19ef6 | 291 | SCM_VALIDATE_SMOB (1, m, macro); |
6c289afe AW |
292 | data = SCM_PACK (SCM_SMOB_DATA (m)); |
293 | ||
294 | if (SCM_CLOSUREP (data) || SCM_PROGRAM_P (data)) | |
295 | return data; | |
296 | else | |
297 | return SCM_BOOL_F; | |
99027e3c | 298 | } |
1bbd0b84 | 299 | #undef FUNC_NAME |
99027e3c | 300 | |
5a0132b3 AW |
301 | SCM_DEFINE (scm_syncase_macro_type, "syncase-macro-type", 1, 0, 0, |
302 | (SCM m), | |
303 | "Return the type of the macro @var{m}.") | |
304 | #define FUNC_NAME s_scm_syncase_macro_type | |
305 | { | |
306 | SCM_VALIDATE_SMOB (1, m, macro); | |
307 | ||
308 | if (SCM_MACRO_IS_EXTENDED (m)) | |
309 | return SCM_SMOB_OBJECT_2 (m); | |
310 | else | |
311 | return SCM_BOOL_F; | |
312 | } | |
313 | #undef FUNC_NAME | |
314 | ||
315 | SCM_DEFINE (scm_syncase_macro_binding, "syncase-macro-binding", 1, 0, 0, | |
316 | (SCM m), | |
317 | "Return the binding of the macro @var{m}.") | |
318 | #define FUNC_NAME s_scm_syncase_macro_binding | |
319 | { | |
320 | SCM_VALIDATE_SMOB (1, m, macro); | |
321 | ||
322 | if (SCM_MACRO_IS_EXTENDED (m)) | |
323 | return SCM_SMOB_OBJECT_3 (m); | |
324 | else | |
325 | return SCM_BOOL_F; | |
326 | } | |
327 | #undef FUNC_NAME | |
328 | ||
99027e3c | 329 | SCM |
1bbd0b84 | 330 | scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() ) |
99027e3c | 331 | { |
86d31dfe | 332 | SCM var = scm_c_define (name, SCM_UNDEFINED); |
9a441ddb | 333 | SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn); |
86d31dfe MV |
334 | SCM_VARIABLE_SET (var, macroizer (transformer)); |
335 | return SCM_UNSPECIFIED; | |
99027e3c MD |
336 | } |
337 | ||
338 | void | |
339 | scm_init_macros () | |
340 | { | |
e841c3e0 | 341 | scm_tc16_macro = scm_make_smob_type ("macro", 0); |
5a0132b3 | 342 | scm_set_smob_mark (scm_tc16_macro, macro_mark); |
726d810a | 343 | scm_set_smob_print (scm_tc16_macro, macro_print); |
a0599745 | 344 | #include "libguile/macros.x" |
99027e3c | 345 | } |
89e00824 ML |
346 | |
347 | /* | |
348 | Local Variables: | |
349 | c-file-style: "gnu" | |
350 | End: | |
351 | */ |