Commit | Line | Data |
---|---|---|
f2c9fcb0 | 1 | /* Copyright (C) 1995,1996,1997,1998, 2000 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 GB |
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 | ||
99027e3c MD |
45 | \f |
46 | ||
a0599745 MD |
47 | #include "libguile/_scm.h" |
48 | #include "libguile/root.h" | |
49 | #include "libguile/smob.h" | |
99027e3c | 50 | |
a0599745 MD |
51 | #include "libguile/validate.h" |
52 | #include "libguile/macros.h" | |
99027e3c | 53 | |
e841c3e0 | 54 | scm_bits_t scm_tc16_macro; |
99027e3c | 55 | |
a1ec6916 | 56 | SCM_DEFINE (scm_makacro, "procedure->syntax", 1, 0, 0, |
1bbd0b84 | 57 | (SCM code), |
1e6808ea MG |
58 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
59 | "appears as the first symbol in an expression, returns the\n" | |
60 | "result of applying @var{code} to the expression and the\n" | |
61 | "environment.") | |
1bbd0b84 | 62 | #define FUNC_NAME s_scm_makacro |
99027e3c | 63 | { |
3b3b36dd | 64 | SCM_VALIDATE_PROC (1,code); |
54778cd3 | 65 | SCM_RETURN_NEWSMOB (scm_tc16_macro, SCM_UNPACK (code)); |
99027e3c | 66 | } |
1bbd0b84 | 67 | #undef FUNC_NAME |
99027e3c MD |
68 | |
69 | ||
3b3b36dd | 70 | SCM_DEFINE (scm_makmacro, "procedure->macro", 1, 0, 0, |
1bbd0b84 | 71 | (SCM code), |
1e6808ea MG |
72 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
73 | "appears as the first symbol in an expression, evaluates the\n" | |
74 | "result of applying @var{code} to the expression and the\n" | |
75 | "environment. The value returned from @var{code} which has been\n" | |
76 | "passed to @code{procedure->memoizing-macro} replaces the form\n" | |
77 | "passed to @var{code}. For example:\n" | |
78 | "\n" | |
79 | "@lisp\n" | |
872e0c72 NJ |
80 | "(define trace\n" |
81 | " (procedure->macro\n" | |
82 | " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n" | |
83 | "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n" | |
1e6808ea | 84 | "@end lisp") |
1bbd0b84 | 85 | #define FUNC_NAME s_scm_makmacro |
99027e3c | 86 | { |
3b3b36dd | 87 | SCM_VALIDATE_PROC (1,code); |
54778cd3 | 88 | SCM_RETURN_NEWSMOB (scm_tc16_macro | (1L << 16), SCM_UNPACK (code)); |
99027e3c | 89 | } |
1bbd0b84 | 90 | #undef FUNC_NAME |
99027e3c MD |
91 | |
92 | ||
3b3b36dd | 93 | SCM_DEFINE (scm_makmmacro, "procedure->memoizing-macro", 1, 0, 0, |
1bbd0b84 | 94 | (SCM code), |
1e6808ea MG |
95 | "Return a @dfn{macro} which, when a symbol defined to this value\n" |
96 | "appears as the first symbol in an expression, evaluates the\n" | |
97 | "result of applying @var{proc} to the expression and the\n" | |
98 | "environment. The value returned from @var{proc} which has been\n" | |
99 | "passed to @code{procedure->memoizing-macro} replaces the form\n" | |
100 | "passed to @var{proc}. For example:\n" | |
101 | "\n" | |
102 | "@lisp\n" | |
103 | "(define trace\n" | |
104 | " (procedure->macro\n" | |
105 | " (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))\n\n" | |
106 | "(trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).\n" | |
107 | "@end lisp") | |
1bbd0b84 | 108 | #define FUNC_NAME s_scm_makmmacro |
99027e3c | 109 | { |
3b3b36dd | 110 | SCM_VALIDATE_PROC (1,code); |
54778cd3 | 111 | SCM_RETURN_NEWSMOB (scm_tc16_macro | (2L << 16), SCM_UNPACK (code)); |
99027e3c | 112 | } |
1bbd0b84 | 113 | #undef FUNC_NAME |
99027e3c MD |
114 | |
115 | ||
a1ec6916 | 116 | SCM_DEFINE (scm_macro_p, "macro?", 1, 0, 0, |
1bbd0b84 | 117 | (SCM obj), |
b380b885 MD |
118 | "Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a\n" |
119 | "syntax transformer.") | |
1bbd0b84 | 120 | #define FUNC_NAME s_scm_macro_p |
99027e3c | 121 | { |
e841c3e0 | 122 | return SCM_BOOL (SCM_TYP16_PREDICATE (scm_tc16_macro, obj)); |
99027e3c | 123 | } |
1bbd0b84 | 124 | #undef FUNC_NAME |
99027e3c MD |
125 | |
126 | ||
127 | SCM_SYMBOL (scm_sym_syntax, "syntax"); | |
128 | SCM_SYMBOL (scm_sym_macro, "macro"); | |
129 | SCM_SYMBOL (scm_sym_mmacro, "macro!"); | |
130 | ||
a1ec6916 | 131 | SCM_DEFINE (scm_macro_type, "macro-type", 1, 0, 0, |
1bbd0b84 | 132 | (SCM m), |
1e6808ea MG |
133 | "Return one of the symbols @code{syntax}, @code{macro} or\n" |
134 | "@code{macro!}, depending on whether @var{m} is a syntax\n" | |
135 | "tranformer, a regular macro, or a memoizing macro,\n" | |
136 | "respectively. If @var{m} is not a macro, @code{#f} is\n" | |
137 | "returned.") | |
1bbd0b84 | 138 | #define FUNC_NAME s_scm_macro_type |
99027e3c | 139 | { |
e841c3e0 | 140 | if (!SCM_TYP16_PREDICATE (scm_tc16_macro, m)) |
99027e3c | 141 | return SCM_BOOL_F; |
206d3de3 | 142 | switch (SCM_CELL_WORD_0 (m) >> 16) |
99027e3c MD |
143 | { |
144 | case 0: return scm_sym_syntax; | |
145 | case 1: return scm_sym_macro; | |
146 | case 2: return scm_sym_mmacro; | |
1bbd0b84 | 147 | default: scm_wrong_type_arg (FUNC_NAME, 1, m); |
99027e3c MD |
148 | } |
149 | } | |
1bbd0b84 | 150 | #undef FUNC_NAME |
99027e3c MD |
151 | |
152 | ||
a1ec6916 | 153 | SCM_DEFINE (scm_macro_name, "macro-name", 1, 0, 0, |
1bbd0b84 | 154 | (SCM m), |
88c927e9 | 155 | "Return the name of the macro @var{m}.") |
1bbd0b84 | 156 | #define FUNC_NAME s_scm_macro_name |
99027e3c | 157 | { |
3b3b36dd | 158 | SCM_VALIDATE_SMOB (1,m,macro); |
88c927e9 | 159 | return scm_procedure_name (SCM_PACK (SCM_SMOB_DATA (m))); |
99027e3c | 160 | } |
1bbd0b84 | 161 | #undef FUNC_NAME |
99027e3c MD |
162 | |
163 | ||
a1ec6916 | 164 | SCM_DEFINE (scm_macro_transformer, "macro-transformer", 1, 0, 0, |
1bbd0b84 | 165 | (SCM m), |
88c927e9 | 166 | "Return the transformer of the macro @var{m}.") |
1bbd0b84 | 167 | #define FUNC_NAME s_scm_macro_transformer |
99027e3c | 168 | { |
3b3b36dd | 169 | SCM_VALIDATE_SMOB (1,m,macro); |
88c927e9 MV |
170 | return ((SCM_CLOSUREP (SCM_PACK (SCM_SMOB_DATA (m)))) ? |
171 | SCM_PACK(SCM_SMOB_DATA (m)) : SCM_BOOL_F); | |
99027e3c | 172 | } |
1bbd0b84 | 173 | #undef FUNC_NAME |
99027e3c MD |
174 | |
175 | SCM | |
1bbd0b84 | 176 | scm_make_synt (const char *name, SCM (*macroizer) (), SCM (*fcn)() ) |
99027e3c MD |
177 | { |
178 | SCM symcell = scm_sysintern (name, SCM_UNDEFINED); | |
26cfa3be MD |
179 | SCM transformer = scm_make_subr_opt (name, scm_tc7_subr_2, fcn, 0); |
180 | SCM_SETCDR (symcell, macroizer (transformer)); | |
99027e3c MD |
181 | return SCM_CAR (symcell); |
182 | } | |
183 | ||
184 | void | |
185 | scm_init_macros () | |
186 | { | |
e841c3e0 KN |
187 | scm_tc16_macro = scm_make_smob_type ("macro", 0); |
188 | scm_set_smob_mark (scm_tc16_macro, scm_markcdr); | |
8dc9439f | 189 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 190 | #include "libguile/macros.x" |
8dc9439f | 191 | #endif |
99027e3c | 192 | } |
89e00824 ML |
193 | |
194 | /* | |
195 | Local Variables: | |
196 | c-file-style: "gnu" | |
197 | End: | |
198 | */ |