-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreprocessor.c
More file actions
444 lines (342 loc) · 11.3 KB
/
Preprocessor.c
File metadata and controls
444 lines (342 loc) · 11.3 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "mocc.h"
static Token *
Token_clone_with_hidden(const Token *t, Vec(String) * hidden_set) {
assert(t);
Token *p = malloc(sizeof(Token));
p->kind = t->kind;
p->text = t->text;
p->string = t->string;
p->string_len = t->string_len;
p->is_bol = t->is_bol;
p->has_spaces = t->has_spaces;
p->hidden_set = Vec_new(String)();
for (size_t i = 0; i < Vec_len(String)(hidden_set); i = i + 1) {
Vec_push(String)(p->hidden_set, Vec_get(String)(hidden_set, i));
}
return p;
}
static bool Token_contains_in_hidden_set(const Token *t, const char *name) {
assert(t);
assert(name);
for (size_t i = 0; i < Vec_len(String)(t->hidden_set); i = i + 1) {
if (strcmp(Vec_get(String)(t->hidden_set, i), name) == 0) {
return true;
}
}
return false;
}
Macro *
Macro_new(const char *name, Vec(String) * parameters, Vec(Token) * contents) {
assert(name);
assert(contents);
Macro *m = malloc(sizeof(Macro));
m->name = name;
m->parameters = parameters;
m->contents = contents;
return m;
}
bool Macro_is_function(const Macro *m) {
assert(m);
return m->parameters != (Vec(String) *)NULL;
}
typedef struct Preprocessor {
Vec(String) * include_paths;
Vec(Macro) * macros;
Vec(Token) * output;
Vec(Token) * queue;
const char *path;
Lexer *lexer;
} Preprocessor;
static Token *
Preprocessor_read_file(Preprocessor *pp, const char *path, const char *text);
static void Preprocessor_init(Preprocessor *pp, Vec(String) * include_paths) {
assert(pp);
assert(include_paths);
pp->include_paths = include_paths;
pp->macros = Vec_new(Macro)();
pp->output = Vec_new(Token)();
pp->queue = NULL;
pp->path = NULL;
pp->lexer = NULL;
}
static const Token *Preprocessor_current(Preprocessor *pp) {
assert(pp);
if (Vec_len(Token)(pp->queue) == 0) {
Vec_push(Token)(pp->queue, Lexer_read(pp->lexer));
}
return Vec_get(Token)(pp->queue, 0);
}
static Token *Preprocessor_consume(Preprocessor *pp) {
assert(pp);
// Fill lookahead queue with a next token
Preprocessor_current(pp);
Token *front = Vec_get(Token)(pp->queue, 0);
for (size_t i = 1; i < Vec_len(Token)(pp->queue); i = i + 1) {
Token *t = Vec_get(Token)(pp->queue, i);
Vec_set(Token)(pp->queue, i - 1, t);
}
Vec_pop(Token)(pp->queue);
return front;
}
static void Preprocessor_insert_tokens(
Preprocessor *pp, const Vec(Token) * tokens, size_t at) {
assert(pp);
assert(tokens);
assert(at <= Vec_len(Token)(pp->queue));
size_t queue_len = Vec_len(Token)(pp->queue);
size_t tokens_len = Vec_len(Token)(tokens);
Vec_resize(Token)(pp->queue, queue_len + tokens_len);
for (size_t i = at; i < queue_len; i = i + 1) {
Vec_set(Token)(pp->queue, i + tokens_len, Vec_get(Token)(pp->queue, i));
}
for (size_t i = 0; i < tokens_len; i = i + 1) {
Vec_set(Token)(pp->queue, i + at, Vec_get(Token)(tokens, i));
}
}
static const Macro *
Preprocessor_find_macro(const Preprocessor *pp, const char *name) {
assert(pp);
assert(name);
size_t len = Vec_len(Macro)(pp->macros);
for (size_t i = 0; i < len; i = i + 1) {
const Macro *m = Vec_get(Macro)(pp->macros, i);
if (strcmp(m->name, name) == 0) {
return m;
}
}
return NULL;
}
static void Preprocessor_define_macro(Preprocessor *pp, Macro *macro) {
assert(pp);
assert(macro);
if (Preprocessor_find_macro(pp, macro->name)) {
ERROR("multiple definition of macro %s\n", macro->name);
}
Vec_push(Macro)(pp->macros, macro);
}
static void Preprocessor_parse_define(Preprocessor *pp) {
assert(pp);
assert(strcmp(Preprocessor_current(pp)->text, "define") == 0);
// 'define'
Preprocessor_consume(pp);
// identifier
Token *identifier = Preprocessor_consume(pp);
if (identifier->is_bol) {
ERROR("macro name missing\n");
} else if (identifier->kind != TokenKind_identifier) {
ERROR("macro name must be an identifier\n");
}
Vec(String) *parameters = NULL;
// ['(']
const Token *t = Preprocessor_current(pp);
if (t->kind == '(' && !t->is_bol && !t->has_spaces) {
// '('
Preprocessor_consume(pp);
// parameters
parameters = Vec_new(String)();
// [identifier]
t = Preprocessor_current(pp);
if (t->kind == TokenKind_identifier && !t->is_bol) {
UNIMPLEMENTED();
}
// ')'
t = Preprocessor_current(pp);
if (t->kind != ')' || t->is_bol) {
ERROR("unexpected end of line, expected ')'\n");
}
Preprocessor_consume(pp);
}
Vec(Token) *contents = Vec_new(Token)();
while (!t->is_bol) {
Vec_push(Token)(contents, Preprocessor_consume(pp));
t = Preprocessor_current(pp);
}
// Register the macro
Macro *macro = Macro_new(identifier->text, parameters, contents);
Preprocessor_define_macro(pp, macro);
}
static void Preprocessor_open_file(
const Preprocessor *pp, const char *hint, char **path, char **text) {
assert(pp);
assert(hint);
assert(path);
assert(text);
// Load from the current directory
const char *dir = Path_dir(pp->path);
*path = Path_join(dir, hint);
*text = File_read(*path);
if (*text) {
return;
}
// Load from include directories
for (size_t i = 0; i < Vec_len(String)(pp->include_paths); i = i + 1) {
dir = Vec_get(String)(pp->include_paths, i);
*path = Path_join(dir, hint);
*text = File_read(*path);
if (*text) {
return;
}
}
ERROR("could not open file %s\n", hint);
}
static void Preprocessor_parse_include(Preprocessor *pp) {
assert(pp);
assert(strcmp(Preprocessor_current(pp)->text, "include") == 0);
// 'include'
Preprocessor_consume(pp);
// String literal (TODO: macro, system path)
const Token *include_file = Preprocessor_consume(pp);
if (include_file->is_bol) {
ERROR("missing filename after #include\n");
} else if (include_file->kind != TokenKind_string) {
ERROR("expected string literal after #include\n");
}
if (!Preprocessor_current(pp)->is_bol) {
ERROR("extra tokens after #include %s\n", include_file->text);
}
const char *hint = include_file->string;
char *path;
char *text;
Preprocessor_open_file(pp, hint, &path, &text);
Preprocessor_read_file(pp, path, text);
}
static void Preprocessor_parse_directive(Preprocessor *pp) {
assert(pp);
assert(Preprocessor_current(pp)->kind == '#');
// '#'
Preprocessor_consume(pp);
const Token *directive = Preprocessor_current(pp);
if (directive->is_bol) {
// # <empty>
return;
} else if (strcmp(directive->text, "define") == 0) {
// # define
Preprocessor_parse_define(pp);
} else if (strcmp(directive->text, "include") == 0) {
// # include
Preprocessor_parse_include(pp);
} else {
ERROR("unknown preprocessor directive #%s\n", directive->text);
}
}
static void Preprocessor_expand_kw(Preprocessor *pp, Token *t) {
assert(pp);
assert(t);
assert(t->kind == TokenKind_identifier);
#define TOKEN_KW(name, text_) \
if (strcmp(t->text, text_) == 0) { \
t->kind = TokenKind_##name; \
Vec_push(Token)(pp->output, t); \
return; \
}
#include "Token.def"
Vec_push(Token)(pp->output, t);
}
static void Preprocessor_expand_function_macro(
Preprocessor *pp, const Macro *m, Token *macro_token) {
assert(pp);
assert(m);
assert(Macro_is_function(m));
assert(macro_token);
// '('
const Token *t = Preprocessor_current(pp);
if (t->kind != '(') {
Preprocessor_expand_kw(pp, macro_token);
return;
}
Preprocessor_consume(pp);
// Macro arguments
if (Vec_len(String)(m->parameters) != 0) {
UNIMPLEMENTED();
}
// ')'
t = Preprocessor_current(pp);
if (t->kind != ')') {
ERROR("missing ')' near %s\n", t->text);
}
Preprocessor_consume(pp);
// Expand the function macro
Vec(Token) *tokens = Vec_new(Token)();
for (size_t i = 0; i < Vec_len(Token)(m->contents); i = i + 1) {
const Token *src_token = Vec_get(Token)(m->contents, i);
Token *t = Token_clone_with_hidden(src_token, macro_token->hidden_set);
Vec_push(String)(t->hidden_set, m->name);
Vec_push(Token)(tokens, t);
}
Preprocessor_insert_tokens(pp, tokens, 0);
}
static void Preprocessor_expand_simple_macro(
Preprocessor *pp, const Macro *m, Token *macro_token) {
assert(pp);
assert(m);
assert(!Macro_is_function(m));
assert(macro_token);
Vec(Token) *tokens = Vec_new(Token)();
for (size_t i = 0; i < Vec_len(Token)(m->contents); i = i + 1) {
const Token *src_token = Vec_get(Token)(m->contents, i);
Token *t = Token_clone_with_hidden(src_token, macro_token->hidden_set);
Vec_push(String)(t->hidden_set, m->name);
Vec_push(Token)(tokens, t);
}
Preprocessor_insert_tokens(pp, tokens, 0);
}
static void Preprocessor_expand_identifier(Preprocessor *pp) {
assert(pp);
Token *t = Preprocessor_consume(pp);
assert(t);
assert(t->kind == TokenKind_identifier);
const Macro *m = Preprocessor_find_macro(pp, t->text);
if (!m || Token_contains_in_hidden_set(t, m->name)) {
// `t` is an identifier or a keyword
Preprocessor_expand_kw(pp, t);
} else if (Macro_is_function(m)) {
// `t` is a function macro
Preprocessor_expand_function_macro(pp, m, t);
} else {
// `t` is a non-function macro
Preprocessor_expand_simple_macro(pp, m, t);
}
}
static Token *
Preprocessor_read_file(Preprocessor *pp, const char *path, const char *text) {
assert(pp);
assert(path);
assert(text);
Vec(Token) *prev_queue = pp->queue;
Lexer *prev_lexer = pp->lexer;
const char *prev_path = pp->path;
pp->queue = Vec_new(Token)();
pp->lexer = Lexer_new(path, text);
pp->path = path;
const Token *t;
while ((t = Preprocessor_current(pp))->kind != '\0') {
if (t->kind == '#' && t->is_bol) {
// Preprocessor directive
Preprocessor_parse_directive(pp);
} else if (t->kind == TokenKind_identifier) {
// Expand identifiers
Preprocessor_expand_identifier(pp);
} else {
// Other tokens
Token *token = Preprocessor_consume(pp);
Vec_push(Token)(pp->output, token);
}
}
Token *eof = Preprocessor_consume(pp);
pp->queue = prev_queue;
pp->lexer = prev_lexer;
pp->path = prev_path;
return eof;
}
Vec(Token) *
Preprocessor_read(
Vec(String) * include_paths, const char *path, const char *text) {
assert(include_paths);
assert(path);
assert(text);
Preprocessor pp;
Preprocessor_init(&pp, include_paths);
Token *eof = Preprocessor_read_file(&pp, path, text);
Vec_push(Token)(pp.output, eof);
return pp.output;
}