-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro.c
338 lines (311 loc) · 8.18 KB
/
macro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* macros are done in a unified way with include file processing:
*
* I think this implementation is a little more restrictive than normal
* cpp, in that the values to substitute need to be single tokens
* this can probably be relaxed at some point, but I need to get a good
* handle on what the actual standard says
*
* these are the canonical examples:
* #define k(a,b) a##b
* #define k(a,b) if (a) b=0
* #define k(a,b) if (a) s=#b
*/
#include "ccc.h"
#define MAXPARMS 10
char *macbuffer;
struct macro *macros;
/*
* add a definition from the command line args
*/
void
add_define(char *s)
{
}
/*
* look up a macro in the macro table
*/
struct macro *
maclookup(char *name)
{
struct macro *m;
for (m = macros; m; m = m->next) {
if (strcmp(m->name, name) == 0) {
return m;
}
}
return 0;
}
/*
* remove a name from the macro list
*/
void
macundefine(char *s)
{
int i;
struct macro *m, *p;
p = 0;
for (m = macros; m; m = m->next) {
if (strcmp(m->name, s) == 0) {
break;
}
p = m;
}
if (m) {
if (!p) {
macros = m->next;
} else {
p->next = m->next;
}
for (i = 0; i < m->parmcount; i++) {
free(m->parms[i]);
}
free(m->parms);
free(m->name);
free(m);
}
}
/*
* read the macro definition and parse out the parameter names
*/
void
macdefine(char *s)
{
int i;
struct macro *m = malloc(sizeof(*m));
char *parms[MAXPARMS];
if (!macbuffer) {
macbuffer = malloc(1024);
}
m->name = strdup(s);
m->parmcount = 0;
skipwhite1();
/*
* get the parameter names from the (<a>,<b>,...) list
*/
if (curchar == '(') {
advance();
while (1) {
skipwhite1();
if (issym()) {
advance();
parms[m->parmcount++] = strdup(s);
skipwhite1();
if (curchar == ',') {
advance();
continue;
}
}
if (curchar == ')') {
break;
}
err(ER_C_DP);
}
if (m->parmcount) {
m->parms = malloc(sizeof(char *) * m->parmcount);
}
for (i = 0; i < m->parmcount; i++) {
m->parms[i] = parms[i];
}
advance();
skipwhite1();
}
s = macbuffer;
/* we copy to the macbuffer the entire logical line,
* spaces and tabs included */
while (curchar != '\n') {
if ((curchar == '\\') && (nextchar == '\n')) {
advance();
curchar = ' ';
}
*s++ = curchar;
advance();
}
*s = 0;
advance(); /* eat the newline */
m->mactext = strdup(macbuffer);
m->next = macros;
macros = m;
// printf("macro %s defined\n", m->name);
}
/*
* if our symbol is a macro, expand it
* the arguments are processed as follows:
* we have attached to the macro structure the array of formal
* parameters, and when in our invocation, we have the values to
* substitute for them. these formal parameters look like c symbol names,
* and the substitution values are single comma delimited tokens.
* so, when we hit, we'll consume the macro call of foo(x,y) in the input
* stream, build the expansion into a buffer, and insert that buffer into
* the input stream. when that input stream is in turn processed, we'll
* check that for macros just like file content. in this way, macro calls
* inside of macro calls just work, and we process them outside-in.
* there are some cases that will be kinda bizarre, given this architecture.
* also, there are two operators that only work inside a macro expansion:
* stringify (#) and glom(##).
* stringify must immediately precede a formal parameter name, and simply
* will stringify the actual parameter.
* #define baz(a) #a
* glom, when encountered in a macro expansion, simply turns into nothing,
* while allowing keyword expansion of any adjacent identifiers
* #define foo(a,b) a##b
* #define bar(c,d) c##d
* foo(b,ar(xy,zzy)) generates xyzzy
*
* when we are called, curchar is the last character of the macro name
*/
int
macexpand(char *s) /* the symbol we are looking up as a macro */
{
struct macro *m;
char plevel;
char *d;
char args;
char *parms[MAXPARMS];
char c;
char *n;
int i;
int stringify = 0;
if (!macbuffer) {
macbuffer = malloc(1024);
}
m = maclookup(s);
if (!m) {
return 0;
}
// printf("macro %s called\n", m->name);
args = 0;
d = macbuffer;
/* this will stop after nextchar is not white space */
while (iswhite(nextchar)) {
advance();
}
plevel = 0;
/*
* read the arguments from the invocation
*/
if (nextchar == '(') {
advance();
plevel = 1;
advance();
skipwhite();
while (1) {
/*
* copy literals literally
*/
if (curchar == '\'' || curchar == '\"') {
c = curchar;
advance();
*d++ = c;
while (curchar != c) {
*d++ = curchar;
if (curchar == '\\') {
advance();
*d++ = curchar;
}
advance();
}
}
if (curchar == '(') {
plevel++;
}
if (curchar == ')') {
plevel--;
}
/*
* only advance when we have a non-parenthesized comma
*/
if (((plevel == 1) && (curchar == ',')) ||
((plevel == 0) && (curchar == ')'))) {
*d++ = 0;
parms[args++] = strdup(macbuffer);
if (curchar == ')') {
break;
}
d = macbuffer;
advance();
skipwhite();
continue;
}
*d++ = curchar;
*d = 0;
advance();
}
} // curchar should be ')'
if (args != m->parmcount) {
err(ER_C_DP);
}
/*
* now we copy the macro text to the macbuffer, expanding
* the parameters where ever we find them
*/
d = macbuffer;
*d = '\0';
s = m->mactext;
while (*s) {
c = *s;
/* literals go straight across */
if ((c == '\'') || (c == '\"')) {
*d++ = *s++;
while (*s != c) {
/* don't notice literal next quote */
if (*s == '\\' && s[1] == c) {
*d++ = *s++;
}
*d++ = *s++;
}
*d++ = *s++;
continue;
}
/* is it a glom or stringify */
stringify = 0;
if (c == '#') {
c = *++s;
if (c == '#') {
c = *++s;
} else {
stringify = 1;
}
}
/* if macro text has something that looks like an arg */
if (((c >= 'A') && (c <= 'Z')) ||
((c >= 'a') && (c <= 'z')) ||
(c == '_')) {
n = strbuf;
while ((c = *s) &&
(((c >= 'A') && (c <= 'Z')) ||
((c >= 'a') && (c <= 'z')) ||
((c >= '0') && (c <= '9')) ||
(c == '_'))) {
*n++ = *s++;
}
*n++ = 0;
n = strbuf;
/* if it matches our declared arg name */
for (i = 0; i < args; i++) {
if (strcmp(m->parms[i], strbuf) == 0) {
n = parms[i];
break;
}
}
if (stringify) {
*d++ = '\"';
}
while (*n) {
*d++ = *n++;
}
if (stringify) {
*d++ = '\"';
}
continue;
}
*d++ = *s++;
}
*d = 0;
// printf("insertmacro: %s %s\n", m->name, macbuffer);
insertmacro(m->name, macbuffer);
return 1;
}
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/