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