annotate macro.c @ 18:c08a947d8f30

deal with macro parameters
author David A. Holland
date Mon, 20 Dec 2010 01:51:47 -0500
parents 76da41da923f
children f9792a9ec704
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"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
8
76da41da923f added macro table
David A. Holland
parents:
diff changeset
9 struct macro {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
10 struct place defplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
11 struct place expansionplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
12 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
13 char *name;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
14 bool hasparams;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
15 struct stringarray params;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
16 char *expansion;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
17 };
76da41da923f added macro table
David A. Holland
parents:
diff changeset
18 DECLARRAY(macro);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
19 DEFARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
20 DECLARRAY(macroarray);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
21 DEFARRAY(macroarray, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
22
76da41da923f added macro table
David A. Holland
parents:
diff changeset
23 static struct macroarrayarray macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
24 static unsigned total_macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
25 static unsigned hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
26
76da41da923f added macro table
David A. Holland
parents:
diff changeset
27 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
28 // macro structure ops
76da41da923f added macro table
David A. Holland
parents:
diff changeset
29
76da41da923f added macro table
David A. Holland
parents:
diff changeset
30 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
31 struct macro *
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
32 macro_create(struct place *p1, const char *name, unsigned hash,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
33 struct place *p2, const char *expansion)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
34 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
35 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
36
76da41da923f added macro table
David A. Holland
parents:
diff changeset
37 m = domalloc(sizeof(*m));
76da41da923f added macro table
David A. Holland
parents:
diff changeset
38 m->defplace = *p1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
39 m->expansionplace = *p2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
40 m->hash = hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
41 m->name = dostrdup(name);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
42 m->hasparams = false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
43 stringarray_init(&m->params);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
44 m->expansion = dostrdup(expansion);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
45 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
46 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
47
76da41da923f added macro table
David A. Holland
parents:
diff changeset
48 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
49 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
50 macro_destroy(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
51 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
52 free(m->name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
53 free(m->expansion);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
54 free(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
55 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
56
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
57 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
58 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
59 macro_eq(const struct macro *m1, const struct macro *m2)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
60 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
61 unsigned num1, num2, i;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
62 const char *p1, *p2;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
63
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
64 if (strcmp(m1->name, m2->name) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
65 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
66 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
67
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
68 if (m1->hasparams != m2->hasparams) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
69 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
70 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
71
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
72 if (strcmp(m1->expansion, m2->expansion) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
73 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
74 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
75
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
76 num1 = stringarray_num(&m1->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
77 num2 = stringarray_num(&m2->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
78 if (num1 != num2) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
79 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
80 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
81
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
82 for (i=0; i<num1; i++) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
83 p1 = stringarray_get(&m1->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
84 p2 = stringarray_get(&m2->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
85 if (strcmp(p1, p2) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
86 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
87 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
88 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
89 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
90 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
91
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
92 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
93 // macro table
76da41da923f added macro table
David A. Holland
parents:
diff changeset
94
76da41da923f added macro table
David A. Holland
parents:
diff changeset
95 /*
76da41da923f added macro table
David A. Holland
parents:
diff changeset
96 * Unless I've screwed up, this is something called Fletcher's Checksum
76da41da923f added macro table
David A. Holland
parents:
diff changeset
97 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The
76da41da923f added macro table
David A. Holland
parents:
diff changeset
98 * implementation is new.
76da41da923f added macro table
David A. Holland
parents:
diff changeset
99 */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
100 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
101 unsigned
76da41da923f added macro table
David A. Holland
parents:
diff changeset
102 hashfunc(const char *s)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
103 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
104 uint16_t x1, x2, a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
105 size_t i, len;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
106
76da41da923f added macro table
David A. Holland
parents:
diff changeset
107 len = strlen(s);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
108
76da41da923f added macro table
David A. Holland
parents:
diff changeset
109 x1 = (uint16_t) (len >> 16);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
110 x2 = (uint16_t) (len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
111 if (x1==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
112 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
113 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
114 if (x2==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
115 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
116 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
117
76da41da923f added macro table
David A. Holland
parents:
diff changeset
118 for (i=0; i<len; i+=2) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
119 if (i==len-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
120 a = (unsigned char)s[i];
76da41da923f added macro table
David A. Holland
parents:
diff changeset
121 /* don't run off the end of the array */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
122 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
123 else {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
124 a = (unsigned char)s[i] +
76da41da923f added macro table
David A. Holland
parents:
diff changeset
125 ((uint16_t)(unsigned char)s[i+1] << 8);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
126 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
127 x1 += a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
128 if (x1 < a) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
129 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
130 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
131 x2 += x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
132 if (x2 < x1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
133 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
134 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
135 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
136
76da41da923f added macro table
David A. Holland
parents:
diff changeset
137 x1 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
138 x2 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
139 return ((uint32_t)x2)*65535U + x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
140 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
141
76da41da923f added macro table
David A. Holland
parents:
diff changeset
142 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
143 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
144 macrotable_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
145 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
146 unsigned i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
147
76da41da923f added macro table
David A. Holland
parents:
diff changeset
148 macroarrayarray_init(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
149 macroarrayarray_setsize(&macros, 4);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
150 for (i=0; i<4; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
151 macroarrayarray_set(&macros, i, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
152 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
153 total_macros = 0;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
154 hashmask = 0x3;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
155 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
156
76da41da923f added macro table
David A. Holland
parents:
diff changeset
157 DESTROYALL_ARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
158
76da41da923f added macro table
David A. Holland
parents:
diff changeset
159 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
160 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
161 macrotable_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
162 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
163 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
164 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
165
76da41da923f added macro table
David A. Holland
parents:
diff changeset
166 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
167 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
168 bucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
169 macroarray_destroyall(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
170 macroarray_destroy(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
171 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
172 macroarrayarray_setsize(&macros, 0);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
173 macroarrayarray_cleanup(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
174 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
175
76da41da923f added macro table
David A. Holland
parents:
diff changeset
176 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
177 struct macro *
76da41da923f added macro table
David A. Holland
parents:
diff changeset
178 macrotable_find(const char *name, bool remove)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
179 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
180 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
181 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
182 struct macro *m, *m2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
183 unsigned i, num;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
184
76da41da923f added macro table
David A. Holland
parents:
diff changeset
185 hash = hashfunc(name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
186 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
187 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
188 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
189 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
190 num = macroarray_num(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
191 for (i=0; i<num; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
192 m = macroarray_get(bucket, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
193 if (hash != m->hash) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
194 continue;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
195 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
196 if (!strcmp(name, m->name)) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
197 if (remove) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
198 if (i < num-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
199 m2 = macroarray_get(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
200 macroarray_set(bucket, i, m2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
201 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
202 macroarray_setsize(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
203 total_macros--;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
204 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
205 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
206 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
207 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
208 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
209 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
210
76da41da923f added macro table
David A. Holland
parents:
diff changeset
211 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
212 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
213 macrotable_rehash(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
214 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
215 struct macroarray *newbucket, *oldbucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
216 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
217 unsigned newmask, tossbit;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
218 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
219 unsigned oldnum, j, k;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
220
76da41da923f added macro table
David A. Holland
parents:
diff changeset
221 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
222 macroarrayarray_setsize(&macros, numbuckets*2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
223
76da41da923f added macro table
David A. Holland
parents:
diff changeset
224 assert(hashmask == numbuckets - 1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
225 newmask = (hashmask << 1) | 1U;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
226 tossbit = newmask && ~hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
227 hashmask = newmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
228
76da41da923f added macro table
David A. Holland
parents:
diff changeset
229 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
230 newbucket = NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
231 oldbucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
232 oldnum = macroarray_num(oldbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
233 for (j=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
234 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
235 if (m->hash & tossbit) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
236 if (newbucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
237 newbucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
238 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
239 macroarray_set(oldbucket, j, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
240 macroarray_add(newbucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
241 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
242 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
243 for (j=k=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
244 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
245 if (m != NULL && k < j) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
246 macroarray_set(oldbucket, k++, m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
247 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
248 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
249 macroarray_setsize(oldbucket, k);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
250 macroarrayarray_set(&macros, numbuckets + i, newbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
251 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
252 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
253
76da41da923f added macro table
David A. Holland
parents:
diff changeset
254 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
255 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
256 macrotable_add(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
257 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
258 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
259 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
260 unsigned numbuckets;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
261
76da41da923f added macro table
David A. Holland
parents:
diff changeset
262 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
263 if (total_macros > 0 && total_macros / numbuckets > 9) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
264 macrotable_rehash();
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 hash = hashfunc(m->name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
268 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
269 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
270 bucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
271 macroarrayarray_set(&macros, hash & hashmask, bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
272 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
273 macroarray_add(bucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
274 total_macros++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
275 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
276
76da41da923f added macro table
David A. Holland
parents:
diff changeset
277 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
278 // external macro definition interface
76da41da923f added macro table
David A. Holland
parents:
diff changeset
279
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
280 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
281 struct macro *
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
282 macro_define_common_start(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
283 struct place *p2, const char *expansion)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
284 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
285 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
286
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
287 if (!is_identifier(macro)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
288 complain(p1, "Invalid macro name %s", macro);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
289 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
290 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
291
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
292 m = macro_create(p1, macro, hashfunc(macro), p2, expansion);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
293 return m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
294 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
295
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
296 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
297 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
298 macro_define_common_end(struct macro *m)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
299 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
300 struct macro *oldm;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
301 bool ok;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
302
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
303 oldm = macrotable_find(m->name, false);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
304 if (oldm != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
305 ok = macro_eq(m, oldm);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
306 if (ok) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
307 complain(&m->defplace,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
308 "Warning: redefinition of %s", m->name);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
309 if (mode.werror) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
310 complain_fail();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
311 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
312 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
313 complain(&m->defplace,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
314 "Redefinition of %s is not identical",
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
315 m->name);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
316 complain_fail();
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
317 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
318 complain(&oldm->defplace, "Previous definition was here");
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
319 macro_destroy(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
320 return;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
321 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
322 macrotable_add(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
323 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
324
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
325 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
326 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
327 macro_parse_parameters(struct macro *m, struct place *p, const char *params)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
328 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
329 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
330 const char *s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
331 char *param;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
332
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
333 while (params != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
334 len = strspn(params, ws);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
335 params += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
336 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
337 s = strchr(params, ',');
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
338 if (s) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
339 len = s-params;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
340 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
341 s++;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
342 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
343 len = strlen(params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
344 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
345 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
346 notrailingws(param, strlen(param));
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
347 if (!is_identifier(param)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
348 complain(p, "Invalid macro parameter name %s", param);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
349 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
350 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
351 stringarray_add(&m->params, param, NULL);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
352 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
353 params = s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
354 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
355 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
356 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
357
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
358 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
359 macro_define_plain(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
360 struct place *p2, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
361 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
362 struct macro *m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
363
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
364 m = macro_define_common_start(p1, macro, p2, expansion);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
365 macro_define_common_end(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
366 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
367
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
368 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
369 macro_define_params(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
370 struct place *p2, const char *params,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
371 struct place *p3, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
372 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
373 struct macro *m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
374
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
375 m = macro_define_common_start(p1, macro, p3, expansion);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
376 macro_parse_parameters(m, p2, params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
377 macro_define_common_end(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
378 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
379
76da41da923f added macro table
David A. Holland
parents:
diff changeset
380 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
381 macro_undef(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
382 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
383 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
384
76da41da923f added macro table
David A. Holland
parents:
diff changeset
385 m = macrotable_find(macro, true);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
386 if (m) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
387 macro_destroy(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
388 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
389 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
390
76da41da923f added macro table
David A. Holland
parents:
diff changeset
391 bool
76da41da923f added macro table
David A. Holland
parents:
diff changeset
392 macro_isdefined(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
393 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
394 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
395
76da41da923f added macro table
David A. Holland
parents:
diff changeset
396 m = macrotable_find(macro, false);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
397 return m != NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
398 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
399
76da41da923f added macro table
David A. Holland
parents:
diff changeset
400 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
401 // macro expansion
76da41da923f added macro table
David A. Holland
parents:
diff changeset
402
76da41da923f added macro table
David A. Holland
parents:
diff changeset
403 char *macroexpand(struct place *, char *buf, size_t len, bool honordefined);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
404
76da41da923f added macro table
David A. Holland
parents:
diff changeset
405 void macro_sendline(struct place *, char *buf, size_t len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
406 void macro_sendeof(struct place *);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
407
76da41da923f added macro table
David A. Holland
parents:
diff changeset
408 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
409 // module initialization
76da41da923f added macro table
David A. Holland
parents:
diff changeset
410
76da41da923f added macro table
David A. Holland
parents:
diff changeset
411 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
412 macros_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
413 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
414 macrotable_init();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
415 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
416
76da41da923f added macro table
David A. Holland
parents:
diff changeset
417 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
418 macros_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
419 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
420 macrotable_cleanup();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
421 }