* boot-9.scm (%cond-expand-table): New hash table mapping modules
[bpt/guile.git] / libguile / macros.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
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. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include "libguile/_scm.h"
48 #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */
49 #include "libguile/eval.h"
50 #include "libguile/ports.h"
51 #include "libguile/print.h"
52 #include "libguile/root.h"
53 #include "libguile/smob.h"
54
55 #include "libguile/validate.h"
56 #include "libguile/macros.h"
57
58 scm_bits_t scm_tc16_macro;
59
60
61 static int
62 macro_print (SCM macro, SCM port, scm_print_state *pstate)
63 {
64 SCM code = SCM_MACRO_CODE (macro);
65 if (!SCM_CLOSUREP (code)
66 || SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE))
67 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE,
68 macro, port, pstate)))
69 {
70 if (!SCM_CLOSUREP (code))
71 scm_puts ("#<primitive-", port);
72 else
73 scm_puts ("#<", port);
74
75 if (SCM_MACRO_TYPE (macro) == 0)
76 scm_puts ("syntax", port);
77 else if (SCM_MACRO_TYPE (macro) == 1)
78 scm_puts ("macro", port);
79 if (SCM_MACRO_TYPE (macro) == 2)
80 scm_puts ("macro!", port);
81 scm_putc (' ', port);
82 scm_iprin1 (scm_macro_name (macro), port, pstate);
83
84 if (SCM_CLOSUREP (code) && SCM_PRINT_SOURCE_P)
85 {
86 SCM formals = SCM_CLOSURE_FORMALS (code);
87 SCM env = SCM_ENV (code);
88 SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
89 SCM src = scm_unmemocopy (SCM_CODE (code), xenv);
90 scm_putc (' ', port);
91 scm_iprin1 (src, port, pstate);
92 }
93
94 scm_putc ('>', port);
95 }
96
97 return 1;
98 }
99
100
101 SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0,
102 (SCM code),
103 "Return a @dfn{macro} which, when a symbol defined to this value\n"
104 "appears as the first symbol in an expression, returns the\n"
105 "result of applying @var{code} to the expression and the\n"
106 "environment.")
107 #define FUNC_NAME s_scm_makacro
108 {
109 SCM_VALIDATE_PROC (1,code);
110 SCM_RETURN_NEWSMOB (scm_tc16_macro, SCM_UNPACK (code));
111 }
112 #undef FUNC_NAME
113
114
115 SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0,
116 (SCM code),
117 "Return a @dfn{macro} which, when a symbol defined to this value\n"
118 "appears as the first symbol in an expression, evaluates the\n"
119 "result of applying @var{code} to the expression and the\n"
120 "environment. The value returned from @var{code} which has been\n"
121 "passed to @code{procedure->memoizing-macro} replaces the form\n"
122 "passed to @var{code}. For example:\n"
123 "\n"
124 "@lisp\n"
125 "(define trace\n"
126 " (procedure->macro\n"
127 " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n"
128 "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n"
129 "@end lisp")
130 #define FUNC_NAME s_scm_makmacro
131 {
132 SCM_VALIDATE_PROC (1,code);
133 SCM_RETURN_NEWSMOB (scm_tc16_macro | (1L << 16), SCM_UNPACK (code));
134 }
135 #undef FUNC_NAME
136
137
138 SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0,
139 (SCM code),
140 "Return a @dfn{macro} which, when a symbol defined to this value\n"
141 "appears as the first symbol in an expression, evaluates the\n"
142 "result of applying @var{proc} to the expression and the\n"
143 "environment. The value returned from @var{proc} which has been\n"
144 "passed to @code{procedure->memoizing-macro} replaces the form\n"
145 "passed to @var{proc}. For example:\n"
146 "\n"
147 "@lisp\n"
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"
152 "@end lisp")
153 #define FUNC_NAME s_scm_makmmacro
154 {
155 SCM_VALIDATE_PROC (1,code);
156 SCM_RETURN_NEWSMOB (scm_tc16_macro | (2L << 16), SCM_UNPACK (code));
157 }
158 #undef FUNC_NAME
159
160
161 SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
162 (SCM obj),
163 "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n"
164 "syntax transformer.")
165 #define FUNC_NAME s_scm_macro_p
166 {
167 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_macro, obj));
168 }
169 #undef FUNC_NAME
170
171
172 SCM_SYMBOL (scm_sym_syntax, "syntax");
173 SCM_SYMBOL (scm_sym_macro, "macro");
174 SCM_SYMBOL (scm_sym_mmacro, "macro!");
175
176 SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
177 (SCM m),
178 "Return one of the symbols @code{syntax}, @code{macro} or\n"
179 "@code{macro!}, depending on whether @var{m} is a syntax\n"
180 "tranformer, a regular macro, or a memoizing macro,\n"
181 "respectively. If @var{m} is not a macro, @code{#f} is\n"
182 "returned.")
183 #define FUNC_NAME s_scm_macro_type
184 {
185 if (!SCM_TYP16_PREDICATE (scm_tc16_macro, m))
186 return SCM_BOOL_F;
187 switch (SCM_MACRO_TYPE (m))
188 {
189 case 0: return scm_sym_syntax;
190 case 1: return scm_sym_macro;
191 case 2: return scm_sym_mmacro;
192 default: scm_wrong_type_arg (FUNC_NAME, 1, m);
193 }
194 }
195 #undef FUNC_NAME
196
197
198 SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
199 (SCM m),
200 "Return the name of the macro @var{m}.")
201 #define FUNC_NAME s_scm_macro_name
202 {
203 SCM_VALIDATE_SMOB (1,m,macro);
204 return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m)));
205 }
206 #undef FUNC_NAME
207
208
209 SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
210 (SCM m),
211 "Return the transformer of the macro @var{m}.")
212 #define FUNC_NAME s_scm_macro_transformer
213 {
214 SCM_VALIDATE_SMOB (1,m,macro);
215 return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ?
216 SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F);
217 }
218 #undef FUNC_NAME
219
220 SCM
221 scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() )
222 {
223 SCM var = scm_c_define (name, SCM_UNDEFINED);
224 SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
225 SCM_VARIABLE_SET (var, macroizer (transformer));
226 return SCM_UNSPECIFIED;
227 }
228
229 void
230 scm_init_macros ()
231 {
232 scm_tc16_macro = scm_make_smob_type ("macro", 0);
233 scm_set_smob_mark (scm_tc16_macro, scm_markcdr);
234 scm_set_smob_print (scm_tc16_macro, macro_print);
235 #ifndef SCM_MAGIC_SNARFER
236 #include "libguile/macros.x"
237 #endif
238 }
239
240 /*
241 Local Variables:
242 c-file-style: "gnu"
243 End:
244 */