(SCM_CELL_WORD_LOC): Moved to "deprecated.h".
[bpt/guile.git] / libguile / macros.c
CommitLineData
3b88ed2a 1/* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003 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
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
1bbd0b84 17
1bbd0b84 18
99027e3c
MD
19\f
20
a0599745 21#include "libguile/_scm.h"
726d810a
DH
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"
a0599745
MD
26#include "libguile/root.h"
27#include "libguile/smob.h"
1d1559ce 28#include "libguile/deprecation.h"
99027e3c 29
a0599745
MD
30#include "libguile/validate.h"
31#include "libguile/macros.h"
99027e3c 32
92c2555f 33scm_t_bits scm_tc16_macro;
99027e3c 34
726d810a
DH
35
36static int
37macro_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);
3063e30a
DH
52#if SCM_ENABLE_DEPRECATED == 1
53 if (SCM_MACRO_TYPE (macro) == 1)
726d810a 54 scm_puts ("macro", port);
3063e30a 55#endif
726d810a
DH
56 if (SCM_MACRO_TYPE (macro) == 2)
57 scm_puts ("macro!", port);
3b88ed2a
DH
58 if (SCM_MACRO_TYPE (macro) == 3)
59 scm_puts ("builtin-macro!", port);
60
726d810a
DH
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
3b88ed2a
DH
81/* Return a mmacro that is known to be one of guile's built in macros. */
82SCM
83scm_i_makbimacro (SCM code)
84#define FUNC_NAME "scm_i_makbimacro"
85{
86 SCM_VALIDATE_PROC (1, code);
87 SCM_RETURN_NEWSMOB (scm_tc16_macro | (3L << 16), SCM_UNPACK (code));
88}
89#undef FUNC_NAME
90
91
92SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0,
93 (SCM code),
94 "Return a @dfn{macro} which, when a symbol defined to this value\n"
95 "appears as the first symbol in an expression, evaluates the\n"
96 "result of applying @var{code} to the expression and the\n"
97 "environment.\n\n"
98 "@code{procedure->memoizing-macro} is the same as\n"
99 "@code{procedure->macro}, except that the expression returned by\n"
100 "@var{code} replaces the original macro expression in the memoized\n"
101 "form of the containing code.")
102#define FUNC_NAME s_scm_makmmacro
103{
104 SCM_VALIDATE_PROC (1, code);
105 SCM_RETURN_NEWSMOB (scm_tc16_macro | (2L << 16), SCM_UNPACK (code));
106}
107#undef FUNC_NAME
108
109
a1ec6916 110SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0,
1bbd0b84 111 (SCM code),
1e6808ea
MG
112 "Return a @dfn{macro} which, when a symbol defined to this value\n"
113 "appears as the first symbol in an expression, returns the\n"
114 "result of applying @var{code} to the expression and the\n"
115 "environment.")
1bbd0b84 116#define FUNC_NAME s_scm_makacro
99027e3c 117{
34d19ef6 118 SCM_VALIDATE_PROC (1, code);
54778cd3 119 SCM_RETURN_NEWSMOB (scm_tc16_macro, SCM_UNPACK (code));
99027e3c 120}
1bbd0b84 121#undef FUNC_NAME
99027e3c
MD
122
123
3063e30a
DH
124#if SCM_ENABLE_DEPRECATED == 1
125
3b3b36dd 126SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0,
1bbd0b84 127 (SCM code),
1e6808ea
MG
128 "Return a @dfn{macro} which, when a symbol defined to this value\n"
129 "appears as the first symbol in an expression, evaluates the\n"
130 "result of applying @var{code} to the expression and the\n"
bb2c02f2 131 "environment. For example:\n"
1e6808ea
MG
132 "\n"
133 "@lisp\n"
872e0c72
NJ
134 "(define trace\n"
135 " (procedure->macro\n"
136 " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n"
137 "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n"
1e6808ea 138 "@end lisp")
1bbd0b84 139#define FUNC_NAME s_scm_makmacro
99027e3c 140{
3063e30a
DH
141 scm_c_issue_deprecation_warning
142 ("The function procedure->macro is deprecated, and so are"
143 " non-memoizing macros in general. Use memoizing macros"
144 " or r5rs macros instead.");
145
34d19ef6 146 SCM_VALIDATE_PROC (1, code);
54778cd3 147 SCM_RETURN_NEWSMOB (scm_tc16_macro | (1L << 16), SCM_UNPACK (code));
99027e3c 148}
1bbd0b84 149#undef FUNC_NAME
99027e3c 150
3063e30a
DH
151#endif
152
99027e3c 153
a1ec6916 154SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0,
1bbd0b84 155 (SCM obj),
b380b885
MD
156 "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n"
157 "syntax transformer.")
1bbd0b84 158#define FUNC_NAME s_scm_macro_p
99027e3c 159{
e841c3e0 160 return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_macro, obj));
99027e3c 161}
1bbd0b84 162#undef FUNC_NAME
99027e3c
MD
163
164
165SCM_SYMBOL (scm_sym_syntax, "syntax");
3063e30a 166#if SCM_ENABLE_DEPRECATED == 1
99027e3c 167SCM_SYMBOL (scm_sym_macro, "macro");
3063e30a 168#endif
99027e3c 169SCM_SYMBOL (scm_sym_mmacro, "macro!");
3b88ed2a 170SCM_SYMBOL (scm_sym_bimacro, "builtin-macro!");
99027e3c 171
a1ec6916 172SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0,
1bbd0b84 173 (SCM m),
1e6808ea
MG
174 "Return one of the symbols @code{syntax}, @code{macro} or\n"
175 "@code{macro!}, depending on whether @var{m} is a syntax\n"
8f85c0c6 176 "transformer, a regular macro, or a memoizing macro,\n"
1e6808ea
MG
177 "respectively. If @var{m} is not a macro, @code{#f} is\n"
178 "returned.")
1bbd0b84 179#define FUNC_NAME s_scm_macro_type
99027e3c 180{
e841c3e0 181 if (!SCM_TYP16_PREDICATE (scm_tc16_macro, m))
99027e3c 182 return SCM_BOOL_F;
726d810a 183 switch (SCM_MACRO_TYPE (m))
99027e3c
MD
184 {
185 case 0: return scm_sym_syntax;
3063e30a 186#if SCM_ENABLE_DEPRECATED == 1
99027e3c 187 case 1: return scm_sym_macro;
3063e30a 188#endif
99027e3c 189 case 2: return scm_sym_mmacro;
3b88ed2a 190 case 3: return scm_sym_bimacro;
1bbd0b84 191 default: scm_wrong_type_arg (FUNC_NAME, 1, m);
99027e3c
MD
192 }
193}
1bbd0b84 194#undef FUNC_NAME
99027e3c
MD
195
196
a1ec6916 197SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0,
1bbd0b84 198 (SCM m),
88c927e9 199 "Return the name of the macro @var{m}.")
1bbd0b84 200#define FUNC_NAME s_scm_macro_name
99027e3c 201{
34d19ef6 202 SCM_VALIDATE_SMOB (1, m, macro);
88c927e9 203 return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m)));
99027e3c 204}
1bbd0b84 205#undef FUNC_NAME
99027e3c
MD
206
207
a1ec6916 208SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0,
1bbd0b84 209 (SCM m),
88c927e9 210 "Return the transformer of the macro @var{m}.")
1bbd0b84 211#define FUNC_NAME s_scm_macro_transformer
99027e3c 212{
34d19ef6 213 SCM_VALIDATE_SMOB (1, m, macro);
88c927e9
MV
214 return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ?
215 SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F);
99027e3c 216}
1bbd0b84 217#undef FUNC_NAME
99027e3c
MD
218
219SCM
1bbd0b84 220scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() )
99027e3c 221{
86d31dfe 222 SCM var = scm_c_define (name, SCM_UNDEFINED);
9a441ddb 223 SCM transformer = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
86d31dfe
MV
224 SCM_VARIABLE_SET (var, macroizer (transformer));
225 return SCM_UNSPECIFIED;
99027e3c
MD
226}
227
228void
229scm_init_macros ()
230{
e841c3e0
KN
231 scm_tc16_macro = scm_make_smob_type ("macro", 0);
232 scm_set_smob_mark (scm_tc16_macro, scm_markcdr);
726d810a 233 scm_set_smob_print (scm_tc16_macro, macro_print);
a0599745 234#include "libguile/macros.x"
99027e3c 235}
89e00824
ML
236
237/*
238 Local Variables:
239 c-file-style: "gnu"
240 End:
241*/