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