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