2ddf7a4d6942d7d5f1a6d5cab261aceada623785
[bpt/guile.git] / libguile / macros.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002 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 scm_putc (' ', port);
59 scm_iprin1 (scm_macro_name (macro), port, pstate);
60
61 if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P)
62 {
63 SCM formals = SCM_CLOSURE_FORMALS (code);
64 SCM env = SCM_ENV (code);
65 SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
66 SCM src = scm_unmemocopy (SCM_CODE (code), xenv);
67 scm_putc (' ', port);
68 scm_iprin1 (src, port, pstate);
69 }
70
71 scm_putc ('>', port);
72 }
73
74 return 1;
75 }
76
77
78 SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0,
79 (SCM code),
80 "Return a @dfn{macro} which, when a symbol defined to this value\n"
81 "appears as the first symbol in an expression, returns the\n"
82 "result of applying @var{code} to the expression and the\n"
83 "environment.")
84 #define FUNC_NAME s_scm_makacro
85 {
86 SCM_VALIDATE_PROC (1, code);
87 SCM_RETURN_NEWSMOB (scm_tc16_macro, SCM_UNPACK (code));
88 }
89 #undef FUNC_NAME
90
91
92 #if SCM_ENABLE_DEPRECATED == 1
93
94 SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0,
95 (SCM code),
96 "Return a @dfn{macro} which, when a symbol defined to this value\n"
97 "appears as the first symbol in an expression, evaluates the\n"
98 "result of applying @var{code} to the expression and the\n"
99 "environment. For example:\n"
100 "\n"
101 "@lisp\n"
102 "(define trace\n"
103 " (procedure->macro\n"
104 " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n"
105 "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n"
106 "@end lisp")
107 #define FUNC_NAME s_scm_makmacro
108 {
109 scm_c_issue_deprecation_warning
110 ("The function procedure->macro is deprecated, and so are"
111 " non-memoizing macros in general. Use memoizing macros"
112 " or r5rs macros instead.");
113
114 SCM_VALIDATE_PROC (1, code);
115 SCM_RETURN_NEWSMOB (scm_tc16_macro | (1L << 16), SCM_UNPACK (code));
116 }
117 #undef FUNC_NAME
118
119 #endif
120
121
122 SCM_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);
135 SCM_RETURN_NEWSMOB (scm_tc16_macro | (2L << 16), SCM_UNPACK (code));
136 }
137 #undef FUNC_NAME
138
139
140 SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
141 (SCM obj),
142 "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n"
143 "syntax transformer.")
144 #define FUNC_NAME s_scm_macro_p
145 {
146 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_macro, obj));
147 }
148 #undef FUNC_NAME
149
150
151 SCM_SYMBOL (scm_sym_syntax, "syntax");
152 #if SCM_ENABLE_DEPRECATED == 1
153 SCM_SYMBOL (scm_sym_macro, "macro");
154 #endif
155 SCM_SYMBOL (scm_sym_mmacro, "macro!");
156
157 SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
158 (SCM m),
159 "Return one of the symbols @code{syntax}, @code{macro} or\n"
160 "@code{macro!}, depending on whether @var{m} is a syntax\n"
161 "transformer, a regular macro, or a memoizing macro,\n"
162 "respectively. If @var{m} is not a macro, @code{#f} is\n"
163 "returned.")
164 #define FUNC_NAME s_scm_macro_type
165 {
166 if (!SCM_TYP16_PREDICATE (scm_tc16_macro, m))
167 return SCM_BOOL_F;
168 switch (SCM_MACRO_TYPE (m))
169 {
170 case 0: return scm_sym_syntax;
171 #if SCM_ENABLE_DEPRECATED == 1
172 case 1: return scm_sym_macro;
173 #endif
174 case 2: return scm_sym_mmacro;
175 default: scm_wrong_type_arg (FUNC_NAME, 1, m);
176 }
177 }
178 #undef FUNC_NAME
179
180
181 SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
182 (SCM m),
183 "Return the name of the macro @var{m}.")
184 #define FUNC_NAME s_scm_macro_name
185 {
186 SCM_VALIDATE_SMOB (1, m, macro);
187 return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m)));
188 }
189 #undef FUNC_NAME
190
191
192 SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
193 (SCM m),
194 "Return the transformer of the macro @var{m}.")
195 #define FUNC_NAME s_scm_macro_transformer
196 {
197 SCM_VALIDATE_SMOB (1, m, macro);
198 return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ?
199 SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F);
200 }
201 #undef FUNC_NAME
202
203 SCM
204 scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() )
205 {
206 SCM var = scm_c_define (name, SCM_UNDEFINED);
207 SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
208 SCM_VARIABLE_SET (var, macroizer (transformer));
209 return SCM_UNSPECIFIED;
210 }
211
212 void
213 scm_init_macros ()
214 {
215 scm_tc16_macro = scm_make_smob_type ("macro", 0);
216 scm_set_smob_mark (scm_tc16_macro, scm_markcdr);
217 scm_set_smob_print (scm_tc16_macro, macro_print);
218 #include "libguile/macros.x"
219 }
220
221 /*
222 Local Variables:
223 c-file-style: "gnu"
224 End:
225 */