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