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