annotate macro.c @ 25:18681e5ae6e4

fix some more bugs
author David A. Holland
date Mon, 20 Dec 2010 04:38:43 -0500
parents e3fab8f1b52c
children 01c3a2088ab4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1 #include <stdlib.h>
76da41da923f added macro table
David A. Holland
parents:
diff changeset
2 #include <string.h>
76da41da923f added macro table
David A. Holland
parents:
diff changeset
3
76da41da923f added macro table
David A. Holland
parents:
diff changeset
4 #include "array.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
5 #include "mode.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
6 #include "place.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
7 #include "macro.h"
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
8 #include "output.h"
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
9
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
10 struct expansionitem {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
11 bool isstring;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
12 union {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
13 char *string;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
14 unsigned param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
15 };
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
16 };
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
17 DECLARRAY(expansionitem);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
18 DEFARRAY(expansionitem, );
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
19
76da41da923f added macro table
David A. Holland
parents:
diff changeset
20 struct macro {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
21 struct place defplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
22 struct place expansionplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
23 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
24 char *name;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
25 bool hasparams;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
26 struct stringarray params;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
27 struct expansionitemarray expansion;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
28 bool inuse;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
29 };
76da41da923f added macro table
David A. Holland
parents:
diff changeset
30 DECLARRAY(macro);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
31 DEFARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
32 DECLARRAY(macroarray);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
33 DEFARRAY(macroarray, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
34
76da41da923f added macro table
David A. Holland
parents:
diff changeset
35 static struct macroarrayarray macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
36 static unsigned total_macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
37 static unsigned hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
38
76da41da923f added macro table
David A. Holland
parents:
diff changeset
39 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
40 // macro structure ops
76da41da923f added macro table
David A. Holland
parents:
diff changeset
41
76da41da923f added macro table
David A. Holland
parents:
diff changeset
42 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
43 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
44 expansionitem_create_string(const char *string)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
45 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
46 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
47
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
48 ei = domalloc(sizeof(*ei));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
49 ei->isstring = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
50 ei->string = dostrdup(string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
51 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
52 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
53
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
54 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
55 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
56 expansionitem_create_stringlen(const char *string, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
57 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
58 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
59
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
60 ei = domalloc(sizeof(*ei));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
61 ei->isstring = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
62 ei->string = dostrndup(string, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
63 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
64 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
65
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
66 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
67 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
68 expansionitem_create_param(unsigned param)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
69 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
70 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
71
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
72 ei = domalloc(sizeof(*ei));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
73 ei->isstring = false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
74 ei->param = param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
75 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
76 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
77
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
78 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
79 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
80 expansionitem_destroy(struct expansionitem *ei)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
81 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
82 if (ei->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
83 free(ei->string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
84 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
85 free(ei);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
86 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
87
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
88 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
89 bool
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
90 expansionitem_eq(const struct expansionitem *ei1,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
91 const struct expansionitem *ei2)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
92 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
93 if (ei1->isstring != ei2->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
94 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
95 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
96 if (ei1->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
97 if (strcmp(ei1->string, ei2->string) != 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
98 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
99 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
100 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
101 if (ei1->param != ei2->param) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
102 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
103 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
104 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
105 return true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
106 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
107
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
108 static
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
109 struct macro *
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
110 macro_create(struct place *p1, const char *name, unsigned hash,
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
111 struct place *p2)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
112 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
113 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
114
76da41da923f added macro table
David A. Holland
parents:
diff changeset
115 m = domalloc(sizeof(*m));
76da41da923f added macro table
David A. Holland
parents:
diff changeset
116 m->defplace = *p1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
117 m->expansionplace = *p2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
118 m->hash = hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
119 m->name = dostrdup(name);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
120 m->hasparams = false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
121 stringarray_init(&m->params);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
122 expansionitemarray_init(&m->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
123 m->inuse = false;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
124 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
125 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
126
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
127 DESTROYALL_ARRAY(expansionitem, );
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
128
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
129 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
130 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
131 macro_destroy(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
132 {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
133 expansionitemarray_destroyall(&m->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
134 expansionitemarray_cleanup(&m->expansion);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
135 free(m->name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
136 free(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
137 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
138
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
139 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
140 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
141 macro_eq(const struct macro *m1, const struct macro *m2)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
142 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
143 unsigned num1, num2, i;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
144 struct expansionitem *ei1, *ei2;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
145 const char *p1, *p2;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
146
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
147 if (strcmp(m1->name, m2->name) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
148 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
149 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
150
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
151 if (m1->hasparams != m2->hasparams) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
152 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
153 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
154
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
155 num1 = expansionitemarray_num(&m1->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
156 num2 = expansionitemarray_num(&m2->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
157 if (num1 != num2) {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
158 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
159 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
160
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
161 for (i=0; i<num1; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
162 ei1 = expansionitemarray_get(&m1->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
163 ei2 = expansionitemarray_get(&m2->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
164 if (!expansionitem_eq(ei1, ei2)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
165 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
166 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
167 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
168
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
169 num1 = stringarray_num(&m1->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
170 num2 = stringarray_num(&m2->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
171 if (num1 != num2) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
172 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
173 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
174
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
175 for (i=0; i<num1; i++) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
176 p1 = stringarray_get(&m1->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
177 p2 = stringarray_get(&m2->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
178 if (strcmp(p1, p2) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
179 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
180 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
181 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
182 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
183 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
184
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
185 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
186 // macro table
76da41da923f added macro table
David A. Holland
parents:
diff changeset
187
76da41da923f added macro table
David A. Holland
parents:
diff changeset
188 /*
76da41da923f added macro table
David A. Holland
parents:
diff changeset
189 * Unless I've screwed up, this is something called Fletcher's Checksum
76da41da923f added macro table
David A. Holland
parents:
diff changeset
190 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The
76da41da923f added macro table
David A. Holland
parents:
diff changeset
191 * implementation is new.
76da41da923f added macro table
David A. Holland
parents:
diff changeset
192 */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
193 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
194 unsigned
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
195 hashfunc(const char *s, size_t len)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
196 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
197 uint16_t x1, x2, a;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
198 size_t i;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
199
76da41da923f added macro table
David A. Holland
parents:
diff changeset
200 x1 = (uint16_t) (len >> 16);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
201 x2 = (uint16_t) (len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
202 if (x1==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
203 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
204 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
205 if (x2==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
206 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
207 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
208
76da41da923f added macro table
David A. Holland
parents:
diff changeset
209 for (i=0; i<len; i+=2) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
210 if (i==len-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
211 a = (unsigned char)s[i];
76da41da923f added macro table
David A. Holland
parents:
diff changeset
212 /* don't run off the end of the array */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
213 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
214 else {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
215 a = (unsigned char)s[i] +
76da41da923f added macro table
David A. Holland
parents:
diff changeset
216 ((uint16_t)(unsigned char)s[i+1] << 8);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
217 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
218 x1 += a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
219 if (x1 < a) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
220 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
221 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
222 x2 += x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
223 if (x2 < x1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
224 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
225 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
226 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
227
76da41da923f added macro table
David A. Holland
parents:
diff changeset
228 x1 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
229 x2 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
230 return ((uint32_t)x2)*65535U + x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
231 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
232
76da41da923f added macro table
David A. Holland
parents:
diff changeset
233 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
234 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
235 macrotable_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
236 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
237 unsigned i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
238
76da41da923f added macro table
David A. Holland
parents:
diff changeset
239 macroarrayarray_init(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
240 macroarrayarray_setsize(&macros, 4);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
241 for (i=0; i<4; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
242 macroarrayarray_set(&macros, i, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
243 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
244 total_macros = 0;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
245 hashmask = 0x3;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
246 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
247
76da41da923f added macro table
David A. Holland
parents:
diff changeset
248 DESTROYALL_ARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
249
76da41da923f added macro table
David A. Holland
parents:
diff changeset
250 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
251 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
252 macrotable_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
253 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
254 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
255 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
256
76da41da923f added macro table
David A. Holland
parents:
diff changeset
257 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
258 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
259 bucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
260 macroarray_destroyall(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
261 macroarray_destroy(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
262 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
263 macroarrayarray_setsize(&macros, 0);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
264 macroarrayarray_cleanup(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
265 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
266
76da41da923f added macro table
David A. Holland
parents:
diff changeset
267 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
268 struct macro *
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
269 macrotable_findlen(const char *name, size_t len, bool remove)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
270 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
271 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
272 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
273 struct macro *m, *m2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
274 unsigned i, num;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
275 size_t mlen;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
276
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
277 hash = hashfunc(name, len);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
278 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
279 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
280 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
281 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
282 num = macroarray_num(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
283 for (i=0; i<num; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
284 m = macroarray_get(bucket, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
285 if (hash != m->hash) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
286 continue;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
287 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
288 mlen = strlen(m->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
289 if (len == mlen && !memcmp(name, m->name, len)) {
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
290 if (remove) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
291 if (i < num-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
292 m2 = macroarray_get(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
293 macroarray_set(bucket, i, m2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
294 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
295 macroarray_setsize(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
296 total_macros--;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
297 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
298 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
299 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
300 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
301 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
302 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
303
76da41da923f added macro table
David A. Holland
parents:
diff changeset
304 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
305 struct macro *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
306 macrotable_find(const char *name, bool remove)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
307 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
308 return macrotable_findlen(name, strlen(name), remove);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
309 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
310
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
311 static
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
312 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
313 macrotable_rehash(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
314 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
315 struct macroarray *newbucket, *oldbucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
316 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
317 unsigned newmask, tossbit;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
318 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
319 unsigned oldnum, j, k;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
320
76da41da923f added macro table
David A. Holland
parents:
diff changeset
321 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
322 macroarrayarray_setsize(&macros, numbuckets*2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
323
76da41da923f added macro table
David A. Holland
parents:
diff changeset
324 assert(hashmask == numbuckets - 1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
325 newmask = (hashmask << 1) | 1U;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
326 tossbit = newmask && ~hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
327 hashmask = newmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
328
76da41da923f added macro table
David A. Holland
parents:
diff changeset
329 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
330 newbucket = NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
331 oldbucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
332 oldnum = macroarray_num(oldbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
333 for (j=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
334 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
335 if (m->hash & tossbit) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
336 if (newbucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
337 newbucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
338 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
339 macroarray_set(oldbucket, j, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
340 macroarray_add(newbucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
341 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
342 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
343 for (j=k=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
344 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
345 if (m != NULL && k < j) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
346 macroarray_set(oldbucket, k++, m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
347 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
348 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
349 macroarray_setsize(oldbucket, k);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
350 macroarrayarray_set(&macros, numbuckets + i, newbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
351 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
352 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
353
76da41da923f added macro table
David A. Holland
parents:
diff changeset
354 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
355 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
356 macrotable_add(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
357 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
358 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
359 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
360 unsigned numbuckets;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
361
76da41da923f added macro table
David A. Holland
parents:
diff changeset
362 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
363 if (total_macros > 0 && total_macros / numbuckets > 9) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
364 macrotable_rehash();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
365 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
366
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
367 hash = hashfunc(m->name, strlen(m->name));
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
368 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
369 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
370 bucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
371 macroarrayarray_set(&macros, hash & hashmask, bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
372 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
373 macroarray_add(bucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
374 total_macros++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
375 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
376
76da41da923f added macro table
David A. Holland
parents:
diff changeset
377 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
378 // external macro definition interface
76da41da923f added macro table
David A. Holland
parents:
diff changeset
379
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
380 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
381 struct macro *
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
382 macro_define_common_start(struct place *p1, const char *macro,
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
383 struct place *p2)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
384 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
385 struct macro *m;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
386 unsigned hash;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
387
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
388 if (!is_identifier(macro)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
389 complain(p1, "Invalid macro name %s", macro);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
390 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
391 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
392
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
393 hash = hashfunc(macro, strlen(macro));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
394 m = macro_create(p1, macro, hash, p2);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
395 return m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
396 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
397
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
398 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
399 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
400 macro_define_common_end(struct macro *m)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
401 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
402 struct macro *oldm;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
403 bool ok;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
404
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
405 oldm = macrotable_find(m->name, false);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
406 if (oldm != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
407 ok = macro_eq(m, oldm);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
408 if (ok) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
409 complain(&m->defplace,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
410 "Warning: redefinition of %s", m->name);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
411 if (mode.werror) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
412 complain_fail();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
413 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
414 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
415 complain(&m->defplace,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
416 "Redefinition of %s is not identical",
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
417 m->name);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
418 complain_fail();
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
419 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
420 complain(&oldm->defplace, "Previous definition was here");
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
421 macro_destroy(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
422 return;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
423 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
424 macrotable_add(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
425 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
426
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
427 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
428 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
429 macro_parse_parameters(struct macro *m, struct place *p, const char *params)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
430 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
431 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
432 const char *s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
433 char *param;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
434
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
435 while (params != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
436 len = strspn(params, ws);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
437 params += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
438 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
439 s = strchr(params, ',');
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
440 if (s) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
441 len = s-params;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
442 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
443 s++;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
444 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
445 len = strlen(params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
446 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
447 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
448 notrailingws(param, strlen(param));
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
449 if (!is_identifier(param)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
450 complain(p, "Invalid macro parameter name %s", param);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
451 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
452 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
453 stringarray_add(&m->params, param, NULL);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
454 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
455 params = s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
456 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
457 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
458 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
459
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
460 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
461 bool
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
462 isparam(struct macro *m, const char *name, size_t len, unsigned *num_ret)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
463 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
464 unsigned num, i;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
465 const char *param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
466
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
467 num = stringarray_num(&m->params);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
468 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
469 param = stringarray_get(&m->params, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
470 if (strlen(param) == len && !strcmp(name, param)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
471 *num_ret = i;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
472 return true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
473 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
474 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
475 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
476 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
477
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
478 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
479 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
480 macro_parse_expansion(struct macro *m, const char *buf)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
481 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
482 size_t blockstart, wordstart, pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
483 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
484 unsigned param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
485
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
486 pos = blockstart = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
487 while (buf[pos] != '\0') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
488 pos += strspn(buf+pos, ws);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
489 if (strchr(alnum, buf[pos])) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
490 wordstart = pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
491 pos += strspn(buf+pos, alnum);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
492 if (isparam(m, buf+wordstart, pos-wordstart, &param)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
493 if (pos > blockstart) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
494 ei = expansionitem_create_stringlen(
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
495 buf + blockstart,
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
496 pos - blockstart);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
497 expansionitemarray_add(&m->expansion,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
498 ei, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
499 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
500 ei = expansionitem_create_param(param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
501 expansionitemarray_add(&m->expansion, ei,NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
502 blockstart = pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
503 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
504 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
505 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
506 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
507 pos++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
508 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
509 if (pos > blockstart) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
510 ei = expansionitem_create_stringlen(buf + blockstart,
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
511 pos - blockstart);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
512 expansionitemarray_add(&m->expansion, ei, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
513 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
514 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
515
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
516 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
517 macro_define_plain(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
518 struct place *p2, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
519 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
520 struct macro *m;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
521 struct expansionitem *ei;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
522
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
523 m = macro_define_common_start(p1, macro, p2);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
524 ei = expansionitem_create_string(expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
525 expansionitemarray_add(&m->expansion, ei, NULL);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
526 macro_define_common_end(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
527 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
528
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
529 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
530 macro_define_params(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
531 struct place *p2, const char *params,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
532 struct place *p3, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
533 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
534 struct macro *m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
535
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
536 m = macro_define_common_start(p1, macro, p3);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
537 m->hasparams = true;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
538 macro_parse_parameters(m, p2, params);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
539 macro_parse_expansion(m, expansion);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
540 macro_define_common_end(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
541 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
542
76da41da923f added macro table
David A. Holland
parents:
diff changeset
543 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
544 macro_undef(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
545 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
546 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
547
76da41da923f added macro table
David A. Holland
parents:
diff changeset
548 m = macrotable_find(macro, true);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
549 if (m) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
550 macro_destroy(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
551 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
552 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
553
76da41da923f added macro table
David A. Holland
parents:
diff changeset
554 bool
76da41da923f added macro table
David A. Holland
parents:
diff changeset
555 macro_isdefined(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
556 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
557 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
558
76da41da923f added macro table
David A. Holland
parents:
diff changeset
559 m = macrotable_find(macro, false);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
560 return m != NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
561 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
562
76da41da923f added macro table
David A. Holland
parents:
diff changeset
563 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
564 // macro expansion
76da41da923f added macro table
David A. Holland
parents:
diff changeset
565
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
566 struct expstate {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
567 bool honordefined;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
568 enum { ES_NORMAL, ES_WANTLPAREN, ES_NOARG, ES_HAVEARG } state;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
569 struct macro *curmacro;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
570 struct stringarray args;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
571 unsigned argparens;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
572
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
573 bool tobuf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
574 char *buf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
575 size_t bufpos, bufmax;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
576 };
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
577
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
578 static struct expstate mainstate;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
579
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
580 static void doexpand(struct expstate *es, struct place *p,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
581 char *buf, size_t len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
582
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
583 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
584 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
585 expstate_init(struct expstate *es, bool tobuf, bool honordefined)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
586 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
587 es->honordefined = honordefined;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
588 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
589 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
590 stringarray_init(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
591 es->argparens = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
592 es->tobuf = tobuf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
593 es->buf = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
594 es->bufpos = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
595 es->bufmax = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
596 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
597
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
598 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
599 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
600 expstate_cleanup(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
601 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
602 assert(es->state == ES_NORMAL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
603 stringarray_cleanup(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
604 if (es->buf) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
605 free(es->buf);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
606 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
607 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
608
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
609 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
610 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
611 expstate_destroyargs(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
612 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
613 unsigned i, num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
614
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
615 num = stringarray_num(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
616 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
617 free(stringarray_get(&es->args, i));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
618 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
619 stringarray_setsize(&es->args, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
620 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
621
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
622 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
623 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
624 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
625 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
626 if (es->tobuf) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
627 if (es->bufpos + len > es->bufmax) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
628 if (es->bufmax == 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
629 es->bufmax = 64;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
630 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
631 while (es->bufpos + len > es->bufmax) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
632 es->bufmax *= 2;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
633 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
634 es->buf = dorealloc(es->buf, es->bufmax);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
635 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
636 memcpy(es->buf + es->bufpos, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
637 es->bufpos += len;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
638 } else {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
639 output(p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
640 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
641 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
642
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
643 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
644 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
645 expand_send_eof(struct expstate *es, struct place *p)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
646 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
647 if (es->tobuf) {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
648 expand_send(es, p, "", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
649 es->bufpos--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
650 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
651 output_eof();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
652 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
653 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
654
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
655 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
656 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
657 expand_newarg(struct expstate *es, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
658 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
659 char *text;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
660
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
661 text = dostrndup(buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
662 stringarray_add(&es->args, text, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
663 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
664
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
665 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
666 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
667 expand_appendarg(struct expstate *es, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
668 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
669 unsigned num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
670 char *text;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
671 size_t oldlen;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
672
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
673 num = stringarray_num(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
674 assert(num > 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
675
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
676 text = stringarray_get(&es->args, num - 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
677 oldlen = strlen(text);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
678 text = dorealloc(text, oldlen + len + 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
679 memcpy(text + oldlen, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
680 text[oldlen+len] = '\0';
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
681 stringarray_set(&es->args, num - 1, text);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
682 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
683
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
684 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
685 char *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
686 expand_substitute(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
687 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
688 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
689 unsigned i, num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
690 size_t len;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
691 char *arg;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
692 char *ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
693
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
694 len = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
695 num = expansionitemarray_num(&es->curmacro->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
696 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
697 ei = expansionitemarray_get(&es->curmacro->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
698 if (ei->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
699 len += strlen(ei->string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
700 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
701 arg = stringarray_get(&es->args, ei->param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
702 len += strlen(arg);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
703 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
704 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
705
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
706 ret = domalloc(len+1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
707 *ret = '\0';
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
708 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
709 ei = expansionitemarray_get(&es->curmacro->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
710 if (ei->isstring) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
711 strcat(ret, ei->string);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
712 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
713 arg = stringarray_get(&es->args, ei->param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
714 strcat(ret, arg);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
715 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
716 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
717
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
718 return ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
719 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
720
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
721 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
722 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
723 expand_domacro(struct expstate *es, struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
724 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
725 struct macro *m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
726 char *newbuf, *newbuf2;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
727
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
728 if (es->curmacro == NULL) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
729 /* defined() */
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
730 if (stringarray_num(&es->args) != 1) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
731 complain(p, "Too many arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
732 complain_fail();
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
733 expand_send(es, p, "0", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
734 return;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
735 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
736 m = macrotable_find(stringarray_get(&es->args, 0), false);
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
737 expand_send(es, p, (m != NULL) ? "1" : "0", 1);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
738 expstate_destroyargs(es);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
739 return;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
740 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
741
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
742 assert(es->curmacro->inuse == false);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
743 es->curmacro->inuse = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
744
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
745 newbuf = expand_substitute(es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
746 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
747 free(newbuf);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
748 expstate_destroyargs(es);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
749 doexpand(es, p, newbuf2, strlen(newbuf2));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
750 free(newbuf2);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
751
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
752 es->curmacro->inuse = false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
753 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
754
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
755 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
756 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
757 expand_got_ws(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
758 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
759 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
760 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
761 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
762 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
763 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
764 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
765 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
766 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
767 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
768 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
769 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
770 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
771 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
772
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
773 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
774 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
775 expand_got_word(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
776 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
777 struct macro *m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
778 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
779 char *newbuf;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
780
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
781 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
782 case ES_NORMAL:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
783 if (es->honordefined &&
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
784 len == 7 && !memcmp(buf, "defined", 7)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
785 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
786 es->state = ES_WANTLPAREN;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
787 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
788 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
789 m = macrotable_findlen(buf, len, false);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
790 if (m == NULL) {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
791 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
792 } else if (!m->hasparams) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
793 m->inuse = true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
794 assert(expansionitemarray_num(&m->expansion) == 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
795 ei = expansionitemarray_get(&m->expansion, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
796 assert(ei->isstring);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
797 newbuf = macroexpand(p, ei->string,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
798 strlen(ei->string), false);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
799 doexpand(es, p, newbuf, strlen(newbuf));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
800 free(newbuf);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
801 m->inuse = false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
802 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
803 es->curmacro = m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
804 es->state = ES_WANTLPAREN;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
805 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
806 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
807 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
808 if (es->curmacro != NULL) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
809 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
810 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
811 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
812 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
813 /* "defined foo" means "defined(foo)" */
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
814 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
815 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
816 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
817 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
818 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
819 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
820 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
821 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
822 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
823 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
824 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
825 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
826 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
827 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
828 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
829
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
830 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
831 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
832 expand_got_lparen(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
833 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
834 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
835 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
836 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
837 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
838 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
839 es->state = ES_NOARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
840 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
841 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
842 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
843 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
844 es->argparens++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
845 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
846 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
847 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
848 es->argparens++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
849 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
850 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
851 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
852
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
853 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
854 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
855 expand_got_rparen(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
856 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
857 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
858 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
859 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
860 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
861 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
862 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
863 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
864 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
865 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
866 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
867 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
868 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
869 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
870 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
871 assert(es->argparens == 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
872 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
873 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
874 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
875 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
876 if (es->argparens > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
877 es->argparens--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
878 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
879 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
880 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
881 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
882 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
883 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
884 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
885 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
886
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
887 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
888 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
889 expand_got_comma(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
890 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
891 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
892 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
893 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
894 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
895 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
896 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
897 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
898 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
899 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
900 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
901 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
902 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
903 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
904 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
905 assert(es->argparens == 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
906 expand_newarg(es, buf, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
907 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
908 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
909 if (es->argparens > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
910 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
911 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
912 es->state = ES_NOARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
913 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
914 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
915 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
916 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
917
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
918 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
919 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
920 expand_got_other(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
921 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
922 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
923 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
924 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
925 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
926 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
927 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
928 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
929 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
930 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
931 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
932 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
933 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
934 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
935 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
936 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
937 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
938 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
939 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
940 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
941 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
942 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
943 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
944
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
945 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
946 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
947 expand_got_eof(struct expstate *es, struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
948 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
949 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
950 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
951 expand_send_eof(es, p);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
952 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
953 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
954 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
955 complain(p, "Expected arguments for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
956 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
957 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
958 complain(p, "Expected arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
959 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
960 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
961 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
962 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
963 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
964 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
965 complain(p, "Unclosed argument list for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
966 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
967 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
968 complain(p, "Unclosed argument list for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
969 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
970 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
971 expstate_destroyargs(es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
972 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
973 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
974 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
975 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
976 es->argparens = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
977 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
978
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
979 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
980 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
981 doexpand(struct expstate *es, struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
982 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
983 size_t x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
984
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
985 while (len > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
986 x = strspn(buf, ws);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
987 if (x > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
988 expand_got_ws(es, p, buf, x);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
989 buf += x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
990 len -= x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
991 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
992
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
993 x = strspn(buf, alnum);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
994 if (x > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
995 expand_got_word(es, p, buf, x);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
996 buf += x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
997 len -= x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
998 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
999 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1000
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1001 if (buf[0] == '(') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1002 expand_got_lparen(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1003 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1004 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1005 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1006 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1007
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1008 if (buf[0] == ')') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1009 expand_got_rparen(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1010 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1011 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1012 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1013 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1014
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1015 if (buf[0] == ',') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1016 expand_got_comma(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1017 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1018 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1019 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1020 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1021
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1022 expand_got_other(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1023 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1024 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1025 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1026 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1027
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1028 char *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1029 macroexpand(struct place *p, char *buf, size_t len, bool honordefined)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1030 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1031 struct expstate es;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1032 char *ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1033
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1034 expstate_init(&es, true, honordefined);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1035 doexpand(&es, p, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1036 expand_got_eof(&es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1037 ret = es.buf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1038 es.buf = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1039 expstate_cleanup(&es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1040
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1041 return ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1042 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1043
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1044 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1045 macro_sendline(struct place *p, char *buf, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1046 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1047 doexpand(&mainstate, p, buf, len);
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1048 output(p, "\n", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1049 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1050
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1051 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1052 macro_sendeof(struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1053 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1054 expand_got_eof(&mainstate, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1055 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1056
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1057 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1058 // module initialization
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1059
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1060 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1061 macros_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1062 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1063 macrotable_init();
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1064 expstate_init(&mainstate, false, false);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1065 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1066
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1067 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1068 macros_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1069 {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1070 expstate_cleanup(&mainstate);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1071 macrotable_cleanup();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1072 }