/
parse.c
359 lines (302 loc) · 7.02 KB
/
parse.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "array.h"
#include "ast.h"
#include "parse.h"
#include "token.h"
#include "grammar.h"
static bool
tok_str_cmp(struct token *t, const char* lexeme) {
size_t len = strlen(lexeme);
if (len != t->length) {
return false;
}
return !strncmp(t->lexeme, lexeme, len);
}
static struct token*
peek(struct parser* p, int offs) {
struct token* t = array_get(p->tokens, p->pos + offs);
return t;
}
static struct token*
next(struct parser* p) {
return peek(p, 0);
}
static struct token*
consume(struct parser* p) {
struct token* t = peek(p, 0);
p->pos += 1;
return t;
}
static struct token*
expect_lexeme(struct parser* p, const char* lexeme) {
const struct token* next_tok = next(p);
if (!strncmp(next_tok->lexeme, lexeme, next_tok->length)) {
return consume(p);
}
printf("error: expected '%s'\n", lexeme);
return NULL;
}
static bool
is_type(struct token* t) {
static char* TYPES[] = {
"_Alignas", "auto", "_Bool", "char",
"_Complex", "const", "double", "enum",
"extern", "float", "_Imaginary", "inline",
"int", "long", "_Noreturn" ,"register", "restrict",
"short", "signed", "static", "struct", "typedef",
"typeof", "union", "unsigned", "void", "volatile",
};
#define array_len(x) (sizeof(x) / sizeof(x[0]))
for (int i = 0; i < array_len(TYPES); i++) {
if (tok_str_cmp(t, TYPES[i])) {
return true;
}
}
return false;
}
static void
parse_decl_spec(struct parser* p, struct decl_spec* specs) {
char keyword[512] = {0};
for (int i = 0;;i++) {
struct token* tok = next(p);
memcpy(keyword, tok->lexeme, tok->length);
keyword[tok->length + 1] = 0;
if (i >= 32) {
fprintf(stderr, "tell the developer to go read about dynamic memory allocation\n");
exit(0);
}
struct decl_spec spec;
if (is_type_qualifier(keyword)) {
printf("%s is a type qualifier!\n", keyword);
spec.type = DS_TYPE_QUALIFIER;
} else if (is_type_specifier(keyword)) {
printf("%s is a type specifier!\n", keyword);
spec.type = DS_TYPE_SPECIFIER;
} else if (is_storage_class_specifier(keyword)) {
printf("%s is a storage class specifier!\n", keyword);
spec.type = DS_STORAGE_CLASS;
} else {
return;
}
spec.t = consume(p);
specs[i] = spec;
}
}
static bool
parse_decl(struct parser* p, struct ast_node* node) {
assert(node != NULL);
return false;
}
// compare a token to the given string
static bool
scmp_token(struct token* t, char* str) {
return !strncmp(t->lexeme, str, t->length);
}
static bool
has_next(struct parser* p) {
return p->pos < p->tokens->size;
}
static void
skip_parens(struct parser* p) {
consume(p); // (
for (int i = 0; has_next(p) && !scmp_token(next(p), ")"); i++) {
consume(p);
}
consume(p); // )
}
static bool
is_func_def(struct parser* p) {
uint64_t old_pos = p->pos;
bool result = false;
for (;;) {
struct token* t = next(p);
if (tok_str_cmp(t, ";")) {
break;
}
if (is_type(t)) {
consume(p);
continue;
}
if (tok_str_cmp(t, "(")) {
skip_parens(p);
continue;
}
consume(p);
skip_parens(p);
if (tok_str_cmp(next(p), "{") || is_type(next(p))) {
result = true;
}
break;
}
p->pos = old_pos;
return result;
}
static struct array*
parse_types(struct parser* p) {
struct array* types = new_array(1);
struct type* last_type = NULL;
for (;;) {
struct type* type = malloc(sizeof(*type));
struct token* t = next(p);
// normal primitive type
// e.g int or float, etc.
if (is_type(t)) {
type->iden = consume(p);
} else if (tok_str_cmp(t, "*")) {
consume(p);
type->ptr = last_type;
// pop
types->size--;
} else {
break;
}
last_type = type;
array_add(types, type);
}
return types;
}
static struct ast_node*
parse_assign_expr(struct parser* p) {
printf("parse_assign_expr: \n");
print_tok(next(p));
printf("\n");
return NULL;
}
static struct ast_node*
parse_comma_expr(struct parser* p) {
struct ast_node* node = parse_assign_expr(p);
if (!node) {
fprintf(stderr, "failed to parse_assign_expr\n");
exit(0);
}
return node;
}
static struct ast_node*
parse_expr(struct parser* p) {
struct ast_node* expr = parse_comma_expr(p);
if (!expr) {
fprintf(stderr, "failed to parse_comma_expr\n");
exit(0);
}
return expr;
}
static bool
parse_if(struct parser* p, struct stat_node* stat) {
if (!tok_str_cmp(next(p), "if")) {
return false;
}
consume(p);
expect_lexeme(p, "(");
struct ast_node* condition = parse_expr(p);
expect_lexeme(p, ")");
}
static bool
parse_stat(struct parser* p, struct stat_node* stat) {
struct token* t = next(p);
if (tok_str_cmp(t, "if")) {
return parse_if(p, stat);
}
else if (tok_str_cmp(t, "else")) {
assert(0 && "unimplemented");
}
else if (tok_str_cmp(t, "for")) {
assert(0 && "unimplemented");
}
else if (tok_str_cmp(t, "while")) {
assert(0 && "unimplemented");
}
else if (tok_str_cmp(t, "return")) {
assert(0 && "unimplemented");
}
else if (tok_str_cmp(t, "break")) {
assert(0 && "unimplemented");
}
else if (tok_str_cmp(t, "continue")) {
assert(0 && "unimplemented");
}
printf("what is: ");
print_tok(t);
return false;
}
static void
parse_func_def(struct parser* p, struct ast_node* node) {
node->func = malloc(sizeof(*node->func));
// parse specs.
struct decl_spec* specs = malloc(sizeof(*specs));
node->func->spec = specs;
parse_decl_spec(p, specs);
// parse types.
struct array* types = parse_types(p);
struct token* func_name = consume(p);
if (func_name->type != T_IDENTIFIER) {
fprintf(stderr, "expected identifier for func name, found: \n");
print_tok(func_name);
putchar('\n');
}
expect_lexeme(p, "(");
for (int i = 0; has_next(p) && !tok_str_cmp(next(p), ")"); i++) {
if (i > 0) {
expect_lexeme(p, ",");
}
struct array* t = parse_types(p);
struct token* iden = consume(p);
}
expect_lexeme(p, ")");
if (!tok_str_cmp(next(p), "{")) {
expect_lexeme(p, ";");
return;
}
struct array* stats = new_array(32);
// parse block!
expect_lexeme(p, "{");
for (int i = 0; has_next(p) && !tok_str_cmp(next(p), "}"); i++) {
struct stat_node* stat = malloc(sizeof(*stat));
if (!parse_stat(p, stat)) {
assert(0 && "shit!");
}
array_add(stats, stat);
}
expect_lexeme(p, "}");
}
static struct ast_node*
parse_node(struct parser* p) {
// this is kind of annoying because
// we have to do this check multiple times
struct ast_node* node = malloc(sizeof(*node));
if (is_func_def(p)) {
parse_func_def(p, node);
node->kind = AST_FUNC_DEF;
printf("Parsed function: \n");
return node;
} else {
parse_decl(p, node);
node->kind = AST_DECL;
printf("Parsed declaration: \n");
return node;
}
printf("stuck on: \n");
print_tok(next(p));
exit(1);
return NULL;
}
struct array*
parse(struct array* tokens) {
struct parser p = {
.tokens = tokens,
.pos = 0,
};
// maybe measure and make
// this a reasonable assumption
// but for now it'll do
struct array* ast_nodes = new_array(32);
while (p.pos < tokens->size) {
struct ast_node* node = parse_node(&p);
if (node != NULL) {
array_add(ast_nodes, node);
}
}
return ast_nodes;
}