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