Commit | Line | Data |
---|---|---|
3063e30a | 1 | /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002 Free Software Foundation, Inc. |
99027e3c MD |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
16 | * Boston, MA 02111-1307 USA | |
17 | * | |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
40 | * If you do not wish that, delete this exception notice. */ | |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
99027e3c MD |
43 | \f |
44 | ||
a0599745 | 45 | #include "libguile/_scm.h" |
726d810a DH |
46 | #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */ |
47 | #include "libguile/eval.h" | |
48 | #include "libguile/ports.h" | |
49 | #include "libguile/print.h" | |
a0599745 MD |
50 | #include "libguile/root.h" |
51 | #include "libguile/smob.h" | |
1d1559ce | 52 | #include "libguile/deprecation.h" |
99027e3c | 53 | |
a0599745 MD |
54 | #include "libguile/validate.h" |
55 | #include "libguile/macros.h" | |
99027e3c | 56 | |
92c2555f | 57 | scm_t_bits scm_tc16_macro; |
99027e3c | 58 | |
726d810a DH |
59 | |
60 | static int | |
61 | macro_print (SCM macro, SCM port, scm_print_state *pstate) | |
62 | { | |
63 | SCM code = SCM_MACRO_CODE (macro); | |
64 | if (!SCM_CLOSUREP (code) | |
65 | || SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE)) | |
66 | || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE, | |
67 | macro, port, pstate))) | |
68 | { | |
69 | if (!SCM_CLOSUREP (code)) | |
70 | scm_puts ("#<primitive-", port); | |
71 | else | |
72 | scm_puts ("#<", port); | |
73 | ||
74 | if (SCM_MACRO_TYPE (macro) == 0) | |
75 | scm_puts ("syntax", port); | |
3063e30a DH |
76 | #if SCM_ENABLE_DEPRECATED == 1 |
77 | if (SCM_MACRO_TYPE (macro) == 1) | |
726d810a | 78 | scm_puts ("macro", port); |
3063e30a | 79 | #endif |
726d810a DH |
80 | if (SCM_MACRO_TYPE (macro) == 2) |
81 | scm_puts ("macro!", port); | |
82 | scm_putc (' ', port); | |
83 | scm_iprin1 (scm_macro_name (macro), port, pstate); | |
84 | ||
85 | if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P) | |
86 | { | |
87 | SCM formals = SCM_CLOSURE_FORMALS (code); | |
88 | SCM env = SCM_ENV (code); | |
89 | SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env); | |
90 | SCM src = scm_unmemocopy (SCM_CODE (code), xenv); | |
91 | scm_putc (' ', port); | |
92 | scm_iprin1 (src, port, pstate); | |
93 | } | |
94 | ||
95 | scm_putc ('>', port); | |
96 | } | |
97 | ||
98 | return 1; | |
99 | } | |
100 | ||
101 | ||
a1ec6916 | 102 | SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0, |
1bbd0b84 | 103 | (SCM code), |
1e6808ea MG |
104 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
105 | "appears as the first symbol in an expression, returns the\n" | |
106 | "result of applying @var{code} to the expression and the\n" | |
107 | "environment.") | |
1bbd0b84 | 108 | #define FUNC_NAME s_scm_makacro |
99027e3c | 109 | { |
34d19ef6 | 110 | SCM_VALIDATE_PROC (1, code); |
54778cd3 | 111 | SCM_RETURN_NEWSMOB (scm_tc16_macro, SCM_UNPACK (code)); |
99027e3c | 112 | } |
1bbd0b84 | 113 | #undef FUNC_NAME |
99027e3c MD |
114 | |
115 | ||
3063e30a DH |
116 | #if SCM_ENABLE_DEPRECATED == 1 |
117 | ||
3b3b36dd | 118 | SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0, |
1bbd0b84 | 119 | (SCM code), |
1e6808ea MG |
120 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
121 | "appears as the first symbol in an expression, evaluates the\n" | |
122 | "result of applying @var{code} to the expression and the\n" | |
bb2c02f2 | 123 | "environment. For example:\n" |
1e6808ea MG |
124 | "\n" |
125 | "@lisp\n" | |
872e0c72 NJ |
126 | "(define trace\n" |
127 | " (procedure->macro\n" | |
128 | " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n" | |
129 | "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n" | |
1e6808ea | 130 | "@end lisp") |
1bbd0b84 | 131 | #define FUNC_NAME s_scm_makmacro |
99027e3c | 132 | { |
3063e30a DH |
133 | scm_c_issue_deprecation_warning |
134 | ("The function procedure->macro is deprecated, and so are" | |
135 | " non-memoizing macros in general. Use memoizing macros" | |
136 | " or r5rs macros instead."); | |
137 | ||
34d19ef6 | 138 | SCM_VALIDATE_PROC (1, code); |
54778cd3 | 139 | SCM_RETURN_NEWSMOB (scm_tc16_macro | (1L << 16), SCM_UNPACK (code)); |
99027e3c | 140 | } |
1bbd0b84 | 141 | #undef FUNC_NAME |
99027e3c | 142 | |
3063e30a DH |
143 | #endif |
144 | ||
99027e3c | 145 | |
3b3b36dd | 146 | SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0, |
1bbd0b84 | 147 | (SCM code), |
1e6808ea MG |
148 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
149 | "appears as the first symbol in an expression, evaluates the\n" | |
bb2c02f2 NJ |
150 | "result of applying @var{code} to the expression and the\n" |
151 | "environment.\n\n" | |
152 | "@code{procedure->memoizing-macro} is the same as\n" | |
153 | "@code{procedure->macro}, except that the expression returned by\n" | |
154 | "@var{code} replaces the original macro expression in the memoized\n" | |
155 | "form of the containing code.") | |
1bbd0b84 | 156 | #define FUNC_NAME s_scm_makmmacro |
99027e3c | 157 | { |
34d19ef6 | 158 | SCM_VALIDATE_PROC (1, code); |
54778cd3 | 159 | SCM_RETURN_NEWSMOB (scm_tc16_macro | (2L << 16), SCM_UNPACK (code)); |
99027e3c | 160 | } |
1bbd0b84 | 161 | #undef FUNC_NAME |
99027e3c MD |
162 | |
163 | ||
a1ec6916 | 164 | SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0, |
1bbd0b84 | 165 | (SCM obj), |
b380b885 MD |
166 | "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n" |
167 | "syntax transformer.") | |
1bbd0b84 | 168 | #define FUNC_NAME s_scm_macro_p |
99027e3c | 169 | { |
e841c3e0 | 170 | return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_macro, obj)); |
99027e3c | 171 | } |
1bbd0b84 | 172 | #undef FUNC_NAME |
99027e3c MD |
173 | |
174 | ||
175 | SCM_SYMBOL (scm_sym_syntax, "syntax"); | |
3063e30a | 176 | #if SCM_ENABLE_DEPRECATED == 1 |
99027e3c | 177 | SCM_SYMBOL (scm_sym_macro, "macro"); |
3063e30a | 178 | #endif |
99027e3c MD |
179 | SCM_SYMBOL (scm_sym_mmacro, "macro!"); |
180 | ||
a1ec6916 | 181 | SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0, |
1bbd0b84 | 182 | (SCM m), |
1e6808ea MG |
183 | "Return one of the symbols @code{syntax}, @code{macro} or\n" |
184 | "@code{macro!}, depending on whether @var{m} is a syntax\n" | |
8f85c0c6 | 185 | "transformer, a regular macro, or a memoizing macro,\n" |
1e6808ea MG |
186 | "respectively. If @var{m} is not a macro, @code{#f} is\n" |
187 | "returned.") | |
1bbd0b84 | 188 | #define FUNC_NAME s_scm_macro_type |
99027e3c | 189 | { |
e841c3e0 | 190 | if (!SCM_TYP16_PREDICATE (scm_tc16_macro, m)) |
99027e3c | 191 | return SCM_BOOL_F; |
726d810a | 192 | switch (SCM_MACRO_TYPE (m)) |
99027e3c MD |
193 | { |
194 | case 0: return scm_sym_syntax; | |
3063e30a | 195 | #if SCM_ENABLE_DEPRECATED == 1 |
99027e3c | 196 | case 1: return scm_sym_macro; |
3063e30a | 197 | #endif |
99027e3c | 198 | case 2: return scm_sym_mmacro; |
1bbd0b84 | 199 | default: scm_wrong_type_arg (FUNC_NAME, 1, m); |
99027e3c MD |
200 | } |
201 | } | |
1bbd0b84 | 202 | #undef FUNC_NAME |
99027e3c MD |
203 | |
204 | ||
a1ec6916 | 205 | SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0, |
1bbd0b84 | 206 | (SCM m), |
88c927e9 | 207 | "Return the name of the macro @var{m}.") |
1bbd0b84 | 208 | #define FUNC_NAME s_scm_macro_name |
99027e3c | 209 | { |
34d19ef6 | 210 | SCM_VALIDATE_SMOB (1, m, macro); |
88c927e9 | 211 | return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m))); |
99027e3c | 212 | } |
1bbd0b84 | 213 | #undef FUNC_NAME |
99027e3c MD |
214 | |
215 | ||
a1ec6916 | 216 | SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0, |
1bbd0b84 | 217 | (SCM m), |
88c927e9 | 218 | "Return the transformer of the macro @var{m}.") |
1bbd0b84 | 219 | #define FUNC_NAME s_scm_macro_transformer |
99027e3c | 220 | { |
34d19ef6 | 221 | SCM_VALIDATE_SMOB (1, m, macro); |
88c927e9 MV |
222 | return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ? |
223 | SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F); | |
99027e3c | 224 | } |
1bbd0b84 | 225 | #undef FUNC_NAME |
99027e3c MD |
226 | |
227 | SCM | |
1bbd0b84 | 228 | scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() ) |
99027e3c | 229 | { |
86d31dfe | 230 | SCM var = scm_c_define (name, SCM_UNDEFINED); |
9a441ddb | 231 | SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn); |
86d31dfe MV |
232 | SCM_VARIABLE_SET (var, macroizer (transformer)); |
233 | return SCM_UNSPECIFIED; | |
99027e3c MD |
234 | } |
235 | ||
236 | void | |
237 | scm_init_macros () | |
238 | { | |
e841c3e0 KN |
239 | scm_tc16_macro = scm_make_smob_type ("macro", 0); |
240 | scm_set_smob_mark (scm_tc16_macro, scm_markcdr); | |
726d810a | 241 | scm_set_smob_print (scm_tc16_macro, macro_print); |
a0599745 | 242 | #include "libguile/macros.x" |
99027e3c | 243 | } |
89e00824 ML |
244 | |
245 | /* | |
246 | Local Variables: | |
247 | c-file-style: "gnu" | |
248 | End: | |
249 | */ |