Debian Codegraph @

src/c/macros.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * macros.d -- macros
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <ecl/internal.h>
17
18 /******************************* ------- ******************************/
19 /*
20 * The are two kinds of lisp environments. One of them is by the interpreter
21 * when executing bytecodes and it contains local variable and function
22 * definitions.
23 *
24 * The other environment is shared by the bytecodes compiler and by the C
25 * compiler and it contains information for the compiler, including local
26 * variable definitions, and local function and macro definitions. The
27 * structure is as follows:
28 *
29 * env -> ( var-list . fun-list )
30 * fun-list -> ( { definition | atomic-marker }* )
31 * definition -> ( macro-name SI::MACRO { extra-data }* )
32 * | ( function-name FUNCTION { extra-data }* )
33 * | ( a-symbol anything { extra-data }* )
34 * atomic-marker -> CB | LB
35 *
36 * The main difference between the bytecode and C compilers is on the extra
37 * information. On the other hand, both environments are similar enough that
38 * the functions MACROEXPAND-1, MACROEXPAND and MACRO-FUNCTION can find the
39 * required information.
40 */
41
42 static cl_object
43 search_symbol_macro(cl_object name, cl_object env)
44 {
45 for (env = CAR(env); env != ECL_NIL; env = CDR(env)) {
46 cl_object record = CAR(env);
47 if (CONSP(record) && CAR(record) == name) {
48 if (CADR(record) == @'si::symbol-macro')
49 return CADDR(record);
50 return ECL_NIL;
51 }
52 }
53 return si_get_sysprop(name, @'si::symbol-macro');
54 }
55
56 static cl_object
57 search_macro_function(cl_object name, cl_object env)
58 {
59 int type = ecl_symbol_type(name);
60 if (env != ECL_NIL) {
61 /* When the environment has been produced by the compiler, there might be
62 atoms/symbols signaling function and unwind-protect boundaries. */
63 while (!Null(env = CDR(env))) {
64 cl_object record = CAR(env);
65 if (CONSP(record) && CAR(record) == name) {
66 cl_object tag = CADR(record);
67 if (tag == @'si::macro')
68 return CADDR(record);
69 if (tag == @'function')
70 return ECL_NIL;
71 }
72 }
73 }
74 if (type & ecl_stp_macro) {
75 return name->symbol.macfun;
76 } else {
77 return ECL_NIL;
78 }
79 }
80
81 @(defun macro_function (sym &optional env)
82 @
83 @(return (search_macro_function(sym, env)));
84 @)
85
86 /*
87 Analyze a form and expand it once if it is a macro form.
88 VALUES(0) contains either the expansion or the original form.
89 VALUES(1) is true when there was a macroexpansion.
90 */
91
92 @(defun macroexpand_1 (form &optional (env ECL_NIL))
93 cl_object exp_fun = ECL_NIL;
94 @
95 if (ECL_ATOM(form)) {
96 if (ECL_SYMBOLP(form))
97 exp_fun = search_symbol_macro(form, env);
98 } else {
99 cl_object head = CAR(form);
100 if (ECL_SYMBOLP(head))
101 exp_fun = search_macro_function(head, env);
102 }
103 if (!Null(exp_fun)) {
104 cl_object hook = ecl_cmp_symbol_value(the_env, @'*macroexpand-hook*');
105 if (hook == @'funcall')
106 form = _ecl_funcall3(exp_fun, form, env);
107 else
108 form = _ecl_funcall4(hook, exp_fun, form, env);
109 }
110 @(return form exp_fun);
111 @)
112
113 /*
114 Expands a form as many times as possible and returns the
115 finally expanded form.
116 */
117 @(defun macroexpand (form &optional env)
118 cl_object done, old_form;
119 @
120 done = ECL_NIL;
121 do {
122 form = cl_macroexpand_1(2, old_form = form, env);
123 if (ecl_nth_value(the_env, 1) == ECL_NIL) {
124 break;
125 } else if (old_form == form) {
126 FEerror("Infinite loop when expanding macro form ~A", 1, old_form);
127 } else {
128 done = ECL_T;
129 }
130 } while (1);
131 @(return form done);
132 @)
133
134 static cl_object
135 or_macro(cl_object whole, cl_object env)
136 {
137 cl_object output = ECL_NIL;
138 whole = CDR(whole);
139 if (Null(whole)) /* (OR) => NIL */
140 @(return ECL_NIL);
141 while (!Null(CDR(whole))) {
142 output = CONS(CONS(CAR(whole), ECL_NIL), output);
143 whole = CDR(whole);
144 }
145 if (Null(output)) { /* (OR form1) => form1 */
146 @(return CAR(whole));
147 }
148 /* (OR form1 ... formn forml) => (COND (form1) ... (formn) (t forml)) */
149 output = CONS(cl_list(2, ECL_T, CAR(whole)), output);
150 @(return CONS(@'cond', cl_nreverse(output)));
151 }
152
153 static cl_object
154 expand_and(cl_object whole)
155 {
156 if (Null(whole))
157 return ECL_T;
158 if (Null(CDR(whole)))
159 return CAR(whole);
160 return cl_list(3, @'if', CAR(whole), expand_and(CDR(whole)));
161 }
162
163 static cl_object
164 and_macro(cl_object whole, cl_object env)
165 {
166 @(return expand_and(CDR(whole)));
167 }
168
169 static cl_object
170 when_macro(cl_object whole, cl_object env)
171 {
172 cl_object args = CDR(whole);
173 if (ecl_unlikely(ecl_endp(args)))
174 FEprogram_error("Syntax error: ~S.", 1, whole);
175 return cl_list(3, @'if', CAR(args), CONS(@'progn', CDR(args)));
176 }
177
178 void
179 init_macros(void)
180 {
181 ECL_SET(@'*macroexpand-hook*', @'funcall');
182 ecl_def_c_macro(@'or', (cl_objectfn_fixed)or_macro, 2);
183 ecl_def_c_macro(@'and', (cl_objectfn_fixed)and_macro, 2);
184 ecl_def_c_macro(@'when', (cl_objectfn_fixed)when_macro, 2);
185 }