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