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