Merge commit '29776e85da637ec4d44b2b2822d6934a50c0084b' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / macros.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003, 2006 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
21 #include "libguile/_scm.h"
22 #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */
23 #include "libguile/eval.h"
24 #include "libguile/ports.h"
25 #include "libguile/print.h"
26 #include "libguile/root.h"
27 #include "libguile/smob.h"
28 #include "libguile/deprecation.h"
29
30 #include "libguile/validate.h"
31 #include "libguile/macros.h"
32
33 #include "libguile/private-options.h"
34
35 scm_t_bits scm_tc16_macro;
36
37
38 static int
39 macro_print (SCM macro, SCM port, scm_print_state *pstate)
40 {
41 SCM code = SCM_MACRO_CODE (macro);
42 if (!SCM_CLOSUREP (code)
43 || scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE))
44 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE,
45 macro, port, pstate)))
46 {
47 if (!SCM_CLOSUREP (code))
48 scm_puts ("#<primitive-", port);
49 else
50 scm_puts ("#<", port);
51
52 if (SCM_MACRO_TYPE (macro) == 0)
53 scm_puts ("syntax", port);
54 #if SCM_ENABLE_DEPRECATED == 1
55 if (SCM_MACRO_TYPE (macro) == 1)
56 scm_puts ("macro", port);
57 #endif
58 if (SCM_MACRO_TYPE (macro) == 2)
59 scm_puts ("macro!", port);
60 if (SCM_MACRO_TYPE (macro) == 3)
61 scm_puts ("builtin-macro!", port);
62
63 scm_putc (' ', port);
64 scm_iprin1 (scm_macro_name (macro), port, pstate);
65
66 if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P)
67 {
68 SCM formals = SCM_CLOSURE_FORMALS (code);
69 SCM env = SCM_ENV (code);
70 SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
71 SCM src = scm_i_unmemocopy_body (SCM_CODE (code), xenv);
72 scm_putc (' ', port);
73 scm_iprin1 (src, port, pstate);
74 }
75
76 scm_putc ('>', port);
77 }
78
79 return 1;
80 }
81
82 static SCM
83 makmac (SCM code, scm_t_bits flags)
84 {
85 SCM z;
86 SCM_NEWSMOB (z, scm_tc16_macro, SCM_UNPACK (code));
87 SCM_SET_SMOB_FLAGS (z, flags);
88 return z;
89 }
90
91 /* Return a mmacro that is known to be one of guile's built in macros. */
92 SCM
93 scm_i_makbimacro (SCM code)
94 #define FUNC_NAME "scm_i_makbimacro"
95 {
96 SCM_VALIDATE_PROC (1, code);
97 return makmac (code, 3);
98 }
99 #undef FUNC_NAME
100
101
102 SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0,
103 (SCM code),
104 "Return a @dfn{macro} which, when a symbol defined to this value\n"
105 "appears as the first symbol in an expression, evaluates the\n"
106 "result of applying @var{code} to the expression and the\n"
107 "environment.\n\n"
108 "@code{procedure->memoizing-macro} is the same as\n"
109 "@code{procedure->macro}, except that the expression returned by\n"
110 "@var{code} replaces the original macro expression in the memoized\n"
111 "form of the containing code.")
112 #define FUNC_NAME s_scm_makmmacro
113 {
114 SCM_VALIDATE_PROC (1, code);
115 return makmac (code, 2);
116 }
117 #undef FUNC_NAME
118
119
120 SCM_DEFINE (scm_makacro, "procedure->syntax", 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, returns the\n"
124 "result of applying @var{code} to the expression and the\n"
125 "environment.")
126 #define FUNC_NAME s_scm_makacro
127 {
128 SCM_VALIDATE_PROC (1, code);
129 return makmac (code, 0);
130 }
131 #undef FUNC_NAME
132
133
134 #if SCM_ENABLE_DEPRECATED == 1
135
136 SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0,
137 (SCM code),
138 "Return a @dfn{macro} which, when a symbol defined to this value\n"
139 "appears as the first symbol in an expression, evaluates the\n"
140 "result of applying @var{code} to the expression and the\n"
141 "environment. For example:\n"
142 "\n"
143 "@lisp\n"
144 "(define trace\n"
145 " (procedure->macro\n"
146 " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n"
147 "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n"
148 "@end lisp")
149 #define FUNC_NAME s_scm_makmacro
150 {
151 scm_c_issue_deprecation_warning
152 ("The function procedure->macro is deprecated, and so are"
153 " non-memoizing macros in general. Use memoizing macros"
154 " or r5rs macros instead.");
155
156 SCM_VALIDATE_PROC (1, code);
157 return makmac (code, 1);
158 }
159 #undef FUNC_NAME
160
161 #endif
162
163
164 SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
165 (SCM obj),
166 "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n"
167 "syntax transformer.")
168 #define FUNC_NAME s_scm_macro_p
169 {
170 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_macro, obj));
171 }
172 #undef FUNC_NAME
173
174
175 SCM_SYMBOL (scm_sym_syntax, "syntax");
176 #if SCM_ENABLE_DEPRECATED == 1
177 SCM_SYMBOL (scm_sym_macro, "macro");
178 #endif
179 SCM_SYMBOL (scm_sym_mmacro, "macro!");
180 SCM_SYMBOL (scm_sym_bimacro, "builtin-macro!");
181
182 SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
183 (SCM m),
184 "Return one of the symbols @code{syntax}, @code{macro} or\n"
185 "@code{macro!}, depending on whether @var{m} is a syntax\n"
186 "transformer, a regular macro, or a memoizing macro,\n"
187 "respectively. If @var{m} is not a macro, @code{#f} is\n"
188 "returned.")
189 #define FUNC_NAME s_scm_macro_type
190 {
191 if (!SCM_SMOB_PREDICATE (scm_tc16_macro, m))
192 return SCM_BOOL_F;
193 switch (SCM_MACRO_TYPE (m))
194 {
195 case 0: return scm_sym_syntax;
196 #if SCM_ENABLE_DEPRECATED == 1
197 case 1: return scm_sym_macro;
198 #endif
199 case 2: return scm_sym_mmacro;
200 case 3: return scm_sym_bimacro;
201 default: scm_wrong_type_arg (FUNC_NAME, 1, m);
202 }
203 }
204 #undef FUNC_NAME
205
206
207 SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
208 (SCM m),
209 "Return the name of the macro @var{m}.")
210 #define FUNC_NAME s_scm_macro_name
211 {
212 SCM_VALIDATE_SMOB (1, m, macro);
213 return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m)));
214 }
215 #undef FUNC_NAME
216
217
218 SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
219 (SCM m),
220 "Return the transformer of the macro @var{m}.")
221 #define FUNC_NAME s_scm_macro_transformer
222 {
223 SCM_VALIDATE_SMOB (1, m, macro);
224 return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ?
225 SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F);
226 }
227 #undef FUNC_NAME
228
229 SCM
230 scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() )
231 {
232 SCM var = scm_c_define (name, SCM_UNDEFINED);
233 SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
234 SCM_VARIABLE_SET (var, macroizer (transformer));
235 return SCM_UNSPECIFIED;
236 }
237
238 void
239 scm_init_macros ()
240 {
241 scm_tc16_macro = scm_make_smob_type ("macro", 0);
242 scm_set_smob_print (scm_tc16_macro, macro_print);
243 #include "libguile/macros.x"
244 }
245
246 /*
247 Local Variables:
248 c-file-style: "gnu"
249 End:
250 */