-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
460 lines (398 loc) · 10.6 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "nooc.h"
#include "stack.h"
#include "ir.h"
#include "util.h"
#include "array.h"
#include "type.h"
#include "map.h"
static const struct token *tok;
static struct stack blocks;
static int loopcount;
static void parsenametypes(struct nametypes *const nametypes);
static size_t parsetype();
#define EXPECTADV(t) { expect(t); tok = tok->next; }
static void
expect(const enum tokentype type)
{
if (!tok)
error(tok->line, tok->col, "unexpected null token!");
if (tok->type != type) {
error(tok->line, tok->col, "expected %s but got %s", tokenstr[type], tokenstr[tok->type]);
}
}
static void
parsestring(struct expr *const expr)
{
expr->kind = EXPR_LIT;
expr->class = C_STR;
expr->d.v.v.s = (struct slice){ 0 };
const struct slice str = tok->slice;
for (size_t i = 0; i < str.len; i++) {
switch (str.data[i]) {
case '\\':
if (++i < str.len) {
char c;
switch (str.data[i]) {
case 'n':
c = '\n';
array_add((&expr->d.v.v.s), c);
break;
case '\\':
c = '\\';
array_add((&expr->d.v.v.s), c);
break;
default:
error(tok->line, tok->col, "invalid string escape!");
}
} else {
error(tok->line, tok->col, "string escape without parameter");
}
break;
default:
array_add((&expr->d.v.v.s), str.data[i]);
}
}
tok = tok->next;
}
static void
parsenum(struct expr *const expr)
{
expr->kind = EXPR_LIT;
expr->class = C_INT;
errno = 0;
if (sizeof(long) == 8)
expr->d.v.v.i64 = strtol(tok->slice.data, NULL, 10);
else if (sizeof(long long) == 8)
expr->d.v.v.i64 = strtoll(tok->slice.data, NULL, 10);
else
die("parsenum: unhandled long size");
if (errno)
error(tok->line, tok->col, "failed to parse number");
tok = tok->next;
}
static enum class
typetoclass(const struct type *const type)
{
switch (type->class) {
case TYPE_INT:
return C_INT;
case TYPE_ARRAY:
return C_STR;
case TYPE_REF:
return C_REF;
default:
die("unknown type class");
}
return 0; // warning
}
static void parseblock(struct block *const block);
#define BINARYOP(x) expr.kind = EXPR_BINARY; expr.d.bop.kind = (x);
#define UNARYOP(x) expr.kind = EXPR_UNARY; expr.d.uop.kind = (x);
static size_t
parseexpr(struct block *const block)
{
struct expr expr = { 0 };
const struct type *type;
const struct decl *decl;
if (tok->type == TOK_LPAREN) {
tok = tok->next;
size_t ret = parseexpr(block);
EXPECTADV(TOK_RPAREN);
return ret;
}
expr.start = tok;
switch (tok->type) {
case TOK_LOOP:
expr.kind = EXPR_LOOP;
tok = tok->next;
loopcount += 1;
parseblock(&expr.d.loop.block);
loopcount -= 1;
break;
case TOK_IF:
expr.kind = EXPR_COND;
tok = tok->next;
expr.d.cond.cond = parseexpr(block);
if (exprs.data[expr.d.cond.cond].class != C_BOOL)
error(expr.start->line, expr.start->col, "expected boolean expression for if condition");
parseblock(&expr.d.cond.bif);
if (tok->type == TOK_ELSE) {
tok = tok->next;
parseblock(&expr.d.cond.belse);
}
break;
case TOK_NOT:
tok = tok->next;
UNARYOP(UOP_NOT);
expr.d.uop.expr = parseexpr(block);
if (exprs.data[expr.d.uop.expr].class != C_BOOL)
error(tok->line, tok->col, "expected boolean expression as not operand");
expr.class = C_BOOL;
break;
case TOK_EQUAL:
BINARYOP(BOP_EQUAL);
goto bool_common;
case TOK_GREATER:
BINARYOP(BOP_GREATER);
bool_common:
tok = tok->next;
expr.d.bop.left = parseexpr(block);
expr.d.bop.right = parseexpr(block);
if (exprs.data[expr.d.bop.left].class != exprs.data[expr.d.bop.right].class)
error(tok->line, tok->col, "expected boolean expression operands to be of same class");
expr.class = C_BOOL;
break;
case TOK_PLUS:
BINARYOP(BOP_PLUS);
goto binary_common;
case TOK_MINUS:
BINARYOP(BOP_MINUS);
binary_common:
tok = tok->next;
expr.d.bop.left = parseexpr(block);
expr.d.bop.right = parseexpr(block);
if (exprs.data[expr.d.bop.left].class != exprs.data[expr.d.bop.right].class)
error(tok->line, tok->col, "expected binary expression operands to be of same class");
expr.class = exprs.data[expr.d.bop.left].class;
break;
case TOK_DOLLAR:
UNARYOP(UOP_REF);
expr.class = C_REF;
tok = tok->next;
expr.d.uop.expr = parseexpr(block);
break;
case TOK_LSQUARE:
expr.kind = EXPR_ACCESS;
tok = tok->next;
expect(TOK_NUM);
struct expr index = { 0 };
parsenum(&index);
if (index.d.v.v.i64 < 0)
error(tok->line, tok->col, "expected non-negative integer for array index");
expr.d.access.index = index.d.v.v.i64;
expect(TOK_RSQUARE);
tok = tok->next;
expr.d.access.array = parseexpr(block);
expr.class = C_INT; //FIXME: determine from parent type
break;
case TOK_NAME:
// a procedure definition
if (slice_cmplit(&tok->slice, "proc") == 0) {
struct decl param = { 0 };
int8_t offset = 0;
expr.kind = EXPR_PROC;
expr.class = C_PROC;
tok = tok->next;
parsenametypes(&expr.d.proc.in);
if (tok->type == TOK_LPAREN)
parsenametypes(&expr.d.proc.out);
for (int i = expr.d.proc.in.len - 1; i >= 0; i--) {
param.s = expr.d.proc.in.data[i].name;
param.type = expr.d.proc.in.data[i].type;
param.in = true;
type = &types.data[param.type];
offset += type->size;
array_add((&expr.d.proc.block.decls), param);
}
for (size_t i = 0; i < expr.d.proc.out.len; i++) {
param.s = expr.d.proc.out.data[i].name;
param.type = typeref(expr.d.proc.out.data[i].type);
param.in = param.out = true;
type = &types.data[param.type];
offset += type->size;
array_add((&expr.d.proc.block.decls), param);
}
parseblock(&expr.d.proc.block);
// a function call
} else if (tok->next && tok->next->type == TOK_LPAREN) {
expr.d.call.name = tok->slice;
decl = finddecl(&blocks, expr.d.call.name);
if (slice_cmplit(&expr.d.call.name, "syscall") == 0) {
expr.class = C_INT;
} else {
if (decl == NULL)
error(expr.start->line, expr.start->col, "undeclared procedure '%.*s'", expr.d.s.len, expr.d.s.data);
type = &types.data[decl->type];
if (type->d.params.out.len == 1) {
struct type *rettype = &types.data[*type->d.params.out.data];
expr.class = typetoclass(rettype);
} else if (type->d.params.out.len > 1)
error(tok->line, tok->col, "only one return supported");
}
tok = tok->next->next;
expr.kind = EXPR_FCALL;
while (tok->type != TOK_RPAREN) {
size_t pidx = parseexpr(block);
array_add((&expr.d.call.params), pidx);
if (tok->type == TOK_RPAREN)
break;
EXPECTADV(TOK_COMMA);
}
EXPECTADV(TOK_RPAREN);
// an ident
} else {
expr.kind = EXPR_IDENT;
expr.d.s = tok->slice;
decl = finddecl(&blocks, expr.d.s);
if (decl == NULL)
error(expr.start->line, expr.start->col, "undeclared identifier '%.*s'", expr.d.s.len, expr.d.s.data);
expr.class = typetoclass(&types.data[decl->type]);
tok = tok->next;
}
break;
case TOK_NUM:
parsenum(&expr);
break;
case TOK_STRING:
parsestring(&expr);
break;
default:
error(tok->line, tok->col, "invalid token for expression");
}
array_add((&exprs), expr);
return exprs.len - 1;
}
static void
parsetypelist(struct typelist *const list)
{
EXPECTADV(TOK_LPAREN);
size_t type;
while (tok->type != TOK_RPAREN) {
type = parsetype();
array_add(list, type);
if (tok->type == TOK_RPAREN)
break;
EXPECTADV(TOK_COMMA);
}
tok = tok->next;
}
static size_t
parsetype()
{
struct type type = { 0 };
struct mapkey key;
union mapval val;
if (tok->type == TOK_NAME && slice_cmplit(&tok->slice, "proc") == 0) {
type.class = TYPE_PROC;
tok = tok->next;
parsetypelist(&type.d.params.in);
if (tok->type == TOK_LPAREN)
parsetypelist(&type.d.params.out);
} else if (tok->type == TOK_DOLLAR) {
type.class = TYPE_REF;
type.size = 8;
tok = tok->next;
type.d.subtype = parsetype();
} else if (tok->type == TOK_LSQUARE) {
struct expr len = { 0 };
type.class = TYPE_ARRAY;
type.size = 0;
tok = tok->next;
expect(TOK_NUM);
parsenum(&len);
if (len.d.v.v.i64 <= 0)
error(tok->line, tok->col, "expected positive integer for array size");
type.d.arr.len = len.d.v.v.i64;
EXPECTADV(TOK_RSQUARE);
type.d.arr.subtype = parsetype();
} else {
mapkey(&key, tok->slice.data, tok->slice.len);
val = mapget(typesmap, &key);
if (!val.n)
error(tok->line, tok->col, "unknown type");
tok = tok->next;
return val.n;
}
return type_put(&type);
}
static void
parsenametypes(struct nametypes *const nametypes)
{
EXPECTADV(TOK_LPAREN);
struct nametype nametype;
while (tok->type != TOK_RPAREN) {
nametype = (struct nametype){ 0 };
expect(TOK_NAME);
nametype.name = tok->slice;
tok = tok->next;
nametype.type = parsetype();
array_add(nametypes, nametype);
if (tok->type == TOK_RPAREN)
break;
EXPECTADV(TOK_COMMA);
}
tok = tok->next;
}
static void
parseblock(struct block *const block)
{
struct statement statement;
bool toplevel = stackpeek(&blocks) == NULL;
stackpush(&blocks, block);
if (!toplevel)
EXPECTADV(TOK_LCURLY);
while (!(tok->type == TOK_NONE || (!toplevel && tok->type == TOK_RCURLY))) {
statement = (struct statement){ 0 };
statement.start = tok;
if (tok->type == TOK_LET) {
struct decl decl = { 0 };
decl.toplevel = toplevel;
decl.start = tok;
statement.kind = STMT_DECL;
tok = tok->next;
expect(TOK_NAME);
decl.s = tok->slice;
tok = tok->next;
decl.type = parsetype();
EXPECTADV(TOK_EQUAL);
if (finddecl(&blocks, decl.s))
error(tok->line, tok->col, "repeat declaration!");
decl.val = parseexpr(block);
array_add((&block->decls), decl);
statement.idx = block->decls.len - 1;
array_add(block, statement);
} else if (tok->type == TOK_RETURN) {
statement.kind = STMT_RETURN;
tok = tok->next;
array_add((block), statement);
} else if (tok->type == TOK_BREAK) {
if (!loopcount)
error(tok->line, tok->col, "break statement outside of loop");
statement.kind = STMT_BREAK;
tok = tok->next;
array_add((block), statement);
} else if (tok->type == TOK_NAME && tok->next && tok->next->type == TOK_EQUAL) {
struct assgn assgn = { 0 };
assgn.start = tok;
statement.kind = STMT_ASSGN;
assgn.s = tok->slice;
tok = tok->next->next;
assgn.val = parseexpr(block);
array_add((&assgns), assgn);
statement.idx = assgns.len - 1;
array_add(block, statement);
} else {
statement.kind = STMT_EXPR;
statement.idx = parseexpr(block);
array_add(block, statement);
}
}
if (!toplevel)
EXPECTADV(TOK_RCURLY);
stackpop(&blocks);
}
struct block
parse(const struct token *const start)
{
tok = start;
struct block block = { 0 };
parseblock(&block);
if (blocks.data)
free(blocks.data);
blocks = (struct stack){ 0 };
return block;
}