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