annotate macro.c @ 17:76da41da923f

added macro table
author David A. Holland
date Mon, 20 Dec 2010 01:15:43 -0500
parents
children c08a947d8f30
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;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
14 char *expansion;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
15 };
76da41da923f added macro table
David A. Holland
parents:
diff changeset
16 DECLARRAY(macro);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
17 DEFARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
18 DECLARRAY(macroarray);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
19 DEFARRAY(macroarray, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
20
76da41da923f added macro table
David A. Holland
parents:
diff changeset
21 static struct macroarrayarray macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
22 static unsigned total_macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
23 static unsigned hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
24
76da41da923f added macro table
David A. Holland
parents:
diff changeset
25 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
26 // macro structure ops
76da41da923f added macro table
David A. Holland
parents:
diff changeset
27
76da41da923f added macro table
David A. Holland
parents:
diff changeset
28 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
29 struct macro *
76da41da923f added macro table
David A. Holland
parents:
diff changeset
30 macro_create(struct place *p1, struct place *p2, unsigned hash,
76da41da923f added macro table
David A. Holland
parents:
diff changeset
31 const char *name, const char *expansion)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
32 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
33 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
34
76da41da923f added macro table
David A. Holland
parents:
diff changeset
35 m = domalloc(sizeof(*m));
76da41da923f added macro table
David A. Holland
parents:
diff changeset
36 m->defplace = *p1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
37 m->expansionplace = *p2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
38 m->hash = hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
39 m->name = dostrdup(name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
40 m->expansion = dostrdup(expansion);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
41 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
42 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
43
76da41da923f added macro table
David A. Holland
parents:
diff changeset
44 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
45 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
46 macro_destroy(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
47 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
48 free(m->name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
49 free(m->expansion);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
50 free(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
51 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
52
76da41da923f added macro table
David A. Holland
parents:
diff changeset
53 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
54 // macro table
76da41da923f added macro table
David A. Holland
parents:
diff changeset
55
76da41da923f added macro table
David A. Holland
parents:
diff changeset
56 /*
76da41da923f added macro table
David A. Holland
parents:
diff changeset
57 * Unless I've screwed up, this is something called Fletcher's Checksum
76da41da923f added macro table
David A. Holland
parents:
diff changeset
58 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The
76da41da923f added macro table
David A. Holland
parents:
diff changeset
59 * implementation is new.
76da41da923f added macro table
David A. Holland
parents:
diff changeset
60 */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
61 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
62 unsigned
76da41da923f added macro table
David A. Holland
parents:
diff changeset
63 hashfunc(const char *s)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
64 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
65 uint16_t x1, x2, a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
66 size_t i, len;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
67
76da41da923f added macro table
David A. Holland
parents:
diff changeset
68 len = strlen(s);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
69
76da41da923f added macro table
David A. Holland
parents:
diff changeset
70 x1 = (uint16_t) (len >> 16);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
71 x2 = (uint16_t) (len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
72 if (x1==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
73 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
74 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
75 if (x2==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
76 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
77 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
78
76da41da923f added macro table
David A. Holland
parents:
diff changeset
79 for (i=0; i<len; i+=2) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
80 if (i==len-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
81 a = (unsigned char)s[i];
76da41da923f added macro table
David A. Holland
parents:
diff changeset
82 /* don't run off the end of the array */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
83 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
84 else {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
85 a = (unsigned char)s[i] +
76da41da923f added macro table
David A. Holland
parents:
diff changeset
86 ((uint16_t)(unsigned char)s[i+1] << 8);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
87 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
88 x1 += a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
89 if (x1 < a) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
90 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
91 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
92 x2 += x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
93 if (x2 < x1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
94 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
95 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
96 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
97
76da41da923f added macro table
David A. Holland
parents:
diff changeset
98 x1 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
99 x2 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
100 return ((uint32_t)x2)*65535U + x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
101 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
102
76da41da923f added macro table
David A. Holland
parents:
diff changeset
103 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
104 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
105 macrotable_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
106 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
107 unsigned i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
108
76da41da923f added macro table
David A. Holland
parents:
diff changeset
109 macroarrayarray_init(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
110 macroarrayarray_setsize(&macros, 4);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
111 for (i=0; i<4; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
112 macroarrayarray_set(&macros, i, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
113 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
114 total_macros = 0;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
115 hashmask = 0x3;
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 DESTROYALL_ARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
119
76da41da923f added macro table
David A. Holland
parents:
diff changeset
120 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
121 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
122 macrotable_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
123 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
124 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
125 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
126
76da41da923f added macro table
David A. Holland
parents:
diff changeset
127 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
128 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
129 bucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
130 macroarray_destroyall(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
131 macroarray_destroy(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
132 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
133 macroarrayarray_setsize(&macros, 0);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
134 macroarrayarray_cleanup(&macros);
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 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
138 struct macro *
76da41da923f added macro table
David A. Holland
parents:
diff changeset
139 macrotable_find(const char *name, bool remove)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
140 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
141 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
142 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
143 struct macro *m, *m2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
144 unsigned i, num;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
145
76da41da923f added macro table
David A. Holland
parents:
diff changeset
146 hash = hashfunc(name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
147 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
148 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
149 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
150 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
151 num = macroarray_num(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
152 for (i=0; i<num; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
153 m = macroarray_get(bucket, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
154 if (hash != m->hash) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
155 continue;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
156 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
157 if (!strcmp(name, m->name)) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
158 if (remove) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
159 if (i < num-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
160 m2 = macroarray_get(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
161 macroarray_set(bucket, i, m2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
162 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
163 macroarray_setsize(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
164 total_macros--;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
165 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
166 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
167 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
168 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
169 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
170 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
171
76da41da923f added macro table
David A. Holland
parents:
diff changeset
172 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
173 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
174 macrotable_rehash(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
175 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
176 struct macroarray *newbucket, *oldbucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
177 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
178 unsigned newmask, tossbit;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
179 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
180 unsigned oldnum, j, k;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
181
76da41da923f added macro table
David A. Holland
parents:
diff changeset
182 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
183 macroarrayarray_setsize(&macros, numbuckets*2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
184
76da41da923f added macro table
David A. Holland
parents:
diff changeset
185 assert(hashmask == numbuckets - 1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
186 newmask = (hashmask << 1) | 1U;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
187 tossbit = newmask && ~hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
188 hashmask = newmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
189
76da41da923f added macro table
David A. Holland
parents:
diff changeset
190 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
191 newbucket = NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
192 oldbucket = macroarrayarray_get(&macros, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
193 oldnum = macroarray_num(oldbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
194 for (j=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
195 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
196 if (m->hash & tossbit) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
197 if (newbucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
198 newbucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
199 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
200 macroarray_set(oldbucket, j, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
201 macroarray_add(newbucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
202 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
203 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
204 for (j=k=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
205 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
206 if (m != NULL && k < j) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
207 macroarray_set(oldbucket, k++, m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
208 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
209 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
210 macroarray_setsize(oldbucket, k);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
211 macroarrayarray_set(&macros, numbuckets + i, newbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
212 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
213 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
214
76da41da923f added macro table
David A. Holland
parents:
diff changeset
215 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
216 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
217 macrotable_add(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
218 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
219 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
220 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
221 unsigned numbuckets;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
222
76da41da923f added macro table
David A. Holland
parents:
diff changeset
223 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
224 if (total_macros > 0 && total_macros / numbuckets > 9) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
225 macrotable_rehash();
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 hash = hashfunc(m->name);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
229 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
230 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
231 bucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
232 macroarrayarray_set(&macros, hash & hashmask, bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
233 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
234 macroarray_add(bucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
235 total_macros++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
236 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
237
76da41da923f added macro table
David A. Holland
parents:
diff changeset
238 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
239 // external macro definition interface
76da41da923f added macro table
David A. Holland
parents:
diff changeset
240
76da41da923f added macro table
David A. Holland
parents:
diff changeset
241 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
242 macro_define(struct place *p1, const char *macro,
76da41da923f added macro table
David A. Holland
parents:
diff changeset
243 struct place *p2, const char *expansion)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
244 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
245 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
246
76da41da923f added macro table
David A. Holland
parents:
diff changeset
247 m = macrotable_find(macro, false);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
248 if (m != NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
249 if (!strcmp(expansion, m->expansion)) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
250 complain(p1, "Warning: redefinition of %s", macro);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
251 if (mode.werror) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
252 complain_fail();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
253 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
254 return;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
255 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
256 complain(p1, "Redefinition of %s is not identical", macro);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
257 complain_fail();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
258 return;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
259 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
260
76da41da923f added macro table
David A. Holland
parents:
diff changeset
261 m = macro_create(p1, p2, hashfunc(macro), macro, expansion);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
262 macrotable_add(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
263 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
264
76da41da923f added macro table
David A. Holland
parents:
diff changeset
265 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
266 macro_undef(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
267 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
268 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
269
76da41da923f added macro table
David A. Holland
parents:
diff changeset
270 m = macrotable_find(macro, true);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
271 if (m) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
272 macro_destroy(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
273 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
274 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
275
76da41da923f added macro table
David A. Holland
parents:
diff changeset
276 bool
76da41da923f added macro table
David A. Holland
parents:
diff changeset
277 macro_isdefined(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
278 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
279 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
280
76da41da923f added macro table
David A. Holland
parents:
diff changeset
281 m = macrotable_find(macro, false);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
282 return m != NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
283 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
284
76da41da923f added macro table
David A. Holland
parents:
diff changeset
285 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
286 // macro expansion
76da41da923f added macro table
David A. Holland
parents:
diff changeset
287
76da41da923f added macro table
David A. Holland
parents:
diff changeset
288 char *macroexpand(struct place *, char *buf, size_t len, bool honordefined);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
289
76da41da923f added macro table
David A. Holland
parents:
diff changeset
290 void macro_sendline(struct place *, char *buf, size_t len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
291 void macro_sendeof(struct place *);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
292
76da41da923f added macro table
David A. Holland
parents:
diff changeset
293 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
294 // module initialization
76da41da923f added macro table
David A. Holland
parents:
diff changeset
295
76da41da923f added macro table
David A. Holland
parents:
diff changeset
296 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
297 macros_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
298 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
299 macrotable_init();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
300 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
301
76da41da923f added macro table
David A. Holland
parents:
diff changeset
302 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
303 macros_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
304 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
305 macrotable_cleanup();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
306 }