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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "diag/diag.h"
#include "parser/parser.h"
struct Parser
{
struct Lexer* lexer;
struct Source source;
struct Token current;
struct Token previous;
bool had_error;
};
static void advance_parser(struct Parser* parser)
{
parser->previous = parser->current;
parser->current = scan_token(parser->lexer);
}
static bool check(struct Parser* parser, enum TokenType type)
{
return parser->current.type == type;
}
static void error_at(struct Parser* parser, struct Token token, const char* message)
{
report_error(parser->source, token, message);
parser->had_error = true;
}
static bool match_token(struct Parser* parser, enum TokenType type)
{
if (!check(parser, type))
return false;
advance_parser(parser);
return true;
}
static bool consume(struct Parser* parser, enum TokenType type, const char* message)
{
if (check(parser, type))
{
advance_parser(parser);
return true;
}
error_at(parser, parser->current, message);
return false;
}
static struct Expr* parse_expression(struct Parser* parser);
static enum StoreSize parse_store_size(struct Parser* parser);
static bool parse_const(struct Parser* parser, struct Program* program)
{
struct ConstDecl decl;
if (!consume(parser, TOKEN_IDENTIFIER, "expected constant name after 'const'"))
return false;
decl.name = parser->previous;
if (!consume(parser, TOKEN_EQUAL, "expected '=' after constant name"))
return false;
decl.value = parse_expression(parser);
if (decl.value == NULL)
return false;
add_const(program, decl);
return true;
}
static bool parse_data(struct Parser* parser, struct Program* program)
{
struct DataDecl decl;
if (!consume(parser, TOKEN_IDENTIFIER, "expected data name after 'data'"))
return false;
decl.name = parser->previous;
if (!consume(parser, TOKEN_EQUAL, "expected '=' after data name"))
return false;
if (!consume(parser, TOKEN_STRING, "expected string value after '='"))
return false;
decl.value = parser->previous;
add_data(program, decl);
return true;
}
static bool parse_enum(struct Parser* parser, struct Program* program)
{
struct EnumDecl decl = create_enum();
if (!consume(parser, TOKEN_IDENTIFIER, "expected enum name after 'enum'"))
goto error;
decl.name = parser->previous;
if (!consume(parser, TOKEN_LEFT_BRACE, "expected '{' after enum name"))
goto error;
while (!check(parser, TOKEN_RIGHT_BRACE))
{
if (check(parser, TOKEN_EOF))
{
error_at(parser, parser->current, "unterminated enum");
goto error;
}
if (!consume(parser, TOKEN_IDENTIFIER, "expected an enum member name"))
goto error;
add_enum_member(&decl, parser->previous);
match_token(parser, TOKEN_COMMA);
}
advance_parser(parser);
add_enum(program, decl);
return true;
error:
free(decl.members);
return false;
}
static bool parse_struct(struct Parser* parser, struct Program* program)
{
struct StructDecl decl = create_struct();
if (!consume(parser, TOKEN_IDENTIFIER, "expected struct name after 'struct'"))
goto error;
decl.name = parser->previous;
if (!consume(parser, TOKEN_LEFT_BRACE, "expected '{' after struct name"))
goto error;
while (!check(parser, TOKEN_RIGHT_BRACE))
{
if (check(parser, TOKEN_EOF))
{
error_at(parser, parser->current, "unterminated struct");
goto error;
}
struct StructField field;
if (!consume(parser, TOKEN_IDENTIFIER, "expected a field name"))
goto error;
field.name = parser->previous;
field.size = STORE_SIZE_QWORD;
if (match_token(parser, TOKEN_COLON))
{
field.size = parse_store_size(parser);
if (field.size == STORE_SIZE_NONE)
{
error_at(parser, parser->current, "expected a size (byte, word, dword, qword) after ':'");
goto error;
}
}
add_struct_field(&decl, field);
match_token(parser, TOKEN_COMMA);
}
advance_parser(parser);
add_struct(program, decl);
return true;
error:
free(decl.fields);
return false;
}
static bool parse_params(struct Parser* parser, struct ProcDecl* proc)
{
if (check(parser, TOKEN_RIGHT_PAREN))
return true;
do
{
struct Param param;
if (!consume(parser, TOKEN_IDENTIFIER, "expected parameter name"))
return false;
param.name = parser->previous;
if (!consume(parser, TOKEN_COLON, "expected ':' after parameter name"))
return false;
if (!consume(parser, TOKEN_IDENTIFIER, "expected register after ':'"))
return false;
param.reg = parser->previous;
add_param(proc, param);
}
while (match_token(parser, TOKEN_COMMA));
return true;
}
static bool is_assign_op(enum TokenType type)
{
return type == TOKEN_EQUAL
|| type == TOKEN_PLUS_EQUAL
|| type == TOKEN_MINUS_EQUAL
|| type == TOKEN_STAR_EQUAL
|| type == TOKEN_SLASH_EQUAL;
}
static struct Expr* alloc_expr(enum ExprKind kind)
{
struct Expr* expr = malloc(sizeof(struct Expr));
expr->kind = kind;
return expr;
}
static enum StoreSize parse_store_size(struct Parser* parser)
{
if (match_token(parser, TOKEN_BYTE))
return STORE_SIZE_BYTE;
if (match_token(parser, TOKEN_WORD))
return STORE_SIZE_WORD;
if (match_token(parser, TOKEN_DWORD))
return STORE_SIZE_DWORD;
if (match_token(parser, TOKEN_QWORD))
return STORE_SIZE_QWORD;
return STORE_SIZE_NONE;
}
static struct Expr* parse_primary(struct Parser* parser)
{
if (match_token(parser, TOKEN_CARET))
{
enum StoreSize size = parse_store_size(parser);
struct Expr* address = parse_primary(parser);
if (address == NULL)
return NULL;
struct Expr* deref = alloc_expr(EXPR_DEREF);
deref->deref.size = size;
deref->deref.address = address;
return deref;
}
if (check(parser, TOKEN_IDENTIFIER) || check(parser, TOKEN_INTEGER) || check(parser, TOKEN_CHAR))
{
advance_parser(parser);
struct Expr* expr = alloc_expr(EXPR_PRIMARY);
expr->primary.token = parser->previous;
return expr;
}
error_at(parser, parser->current, "expected an expression");
return NULL;
}
static struct Expr* parse_postfix(struct Parser* parser)
{
struct Expr* expr = parse_primary(parser);
if (expr == NULL)
return NULL;
while (match_token(parser, TOKEN_DOT))
{
// an identifier is a member (data.len); an integer is a register size
// suffix (r1.64), meaningful with the logical_registers extension
if (!check(parser, TOKEN_IDENTIFIER) && !check(parser, TOKEN_INTEGER))
{
error_at(parser, parser->current, "expected a member name or size after '.'");
free_expr(expr);
return NULL;
}
advance_parser(parser);
struct Expr* member = alloc_expr(EXPR_MEMBER);
member->member.object = expr;
member->member.member = parser->previous;
expr = member;
}
return expr;
}
static struct Expr* parse_binary(struct Parser* parser, struct Expr* (*operand)(struct Parser*), enum TokenType a, enum TokenType b)
{
struct Expr* left = operand(parser);
if (left == NULL)
return NULL;
while (check(parser, a) || check(parser, b))
{
advance_parser(parser);
struct Token op = parser->previous;
struct Expr* right = operand(parser);
if (right == NULL)
{
free_expr(left);
return NULL;
}
struct Expr* binary = alloc_expr(EXPR_BINARY);
binary->binary.left = left;
binary->binary.op = op;
binary->binary.right = right;
left = binary;
}
return left;
}
static struct Expr* parse_multiplicative(struct Parser* parser)
{
return parse_binary(parser, parse_postfix, TOKEN_STAR, TOKEN_SLASH);
}
static struct Expr* parse_expression(struct Parser* parser)
{
return parse_binary(parser, parse_multiplicative, TOKEN_PLUS, TOKEN_MINUS);
}
static bool is_compare_op(enum TokenType type)
{
return type == TOKEN_EQUAL_EQUAL
|| type == TOKEN_BANG_EQUAL
|| type == TOKEN_LESS
|| type == TOKEN_LESS_EQUAL
|| type == TOKEN_GREATER
|| type == TOKEN_GREATER_EQUAL;
}
static bool parse_call(struct Parser* parser, struct Token name, struct Statement* out)
{
struct Expr** args = NULL;
size_t count = 0;
size_t capacity = 0;
if (!check(parser, TOKEN_RIGHT_PAREN))
{
do
{
struct Expr* arg = parse_expression(parser);
if (arg == NULL)
goto error;
if (count == capacity)
{
capacity = capacity < 4 ? 4 : capacity * 2;
args = realloc(args, capacity * sizeof(struct Expr*));
}
args[count] = arg;
count += 1;
}
while (match_token(parser, TOKEN_COMMA));
}
if (!consume(parser, TOKEN_RIGHT_PAREN, "expected ')' after arguments"))
goto error;
out->kind = STATEMENT_CALL;
out->call.name = name;
out->call.args = args;
out->call.arg_count = count;
out->call.arg_capacity = capacity;
return true;
error:
for (size_t i = 0; i < count; i += 1)
free_expr(args[i]);
free(args);
return false;
}
static bool parse_statement(struct Parser* parser, struct Statement* out);
static bool parse_if(struct Parser* parser, struct Statement* out)
{
struct Expr* left = parse_expression(parser);
if (left == NULL)
return false;
if (!is_compare_op(parser->current.type))
{
error_at(parser, parser->current, "expected a comparison operator");
free_expr(left);
return false;
}
advance_parser(parser);
struct Token comparison = parser->previous;
struct Expr* right = parse_expression(parser);
if (right == NULL)
{
free_expr(left);
return false;
}
struct Statement* body = malloc(sizeof(struct Statement));
if (!parse_statement(parser, body))
{
free(body);
free_expr(left);
free_expr(right);
return false;
}
out->kind = STATEMENT_IF;
out->branch.left = left;
out->branch.comparison = comparison;
out->branch.right = right;
out->branch.body = body;
return true;
}
static bool parse_statement(struct Parser* parser, struct Statement* out)
{
if (match_token(parser, TOKEN_IF))
return parse_if(parser, out);
if (match_token(parser, TOKEN_SYSCALL))
{
out->kind = STATEMENT_SYSCALL;
return true;
}
if (match_token(parser, TOKEN_STACK))
{
if (!consume(parser, TOKEN_IDENTIFIER, "expected buffer name after 'stack'"))
return false;
struct Token name = parser->previous;
if (!consume(parser, TOKEN_LEFT_BRACKET, "expected '[' after buffer name"))
return false;
if (!consume(parser, TOKEN_INTEGER, "expected buffer size"))
return false;
struct Token size = parser->previous;
if (!consume(parser, TOKEN_RIGHT_BRACKET, "expected ']' after buffer size"))
return false;
out->kind = STATEMENT_STACK;
out->stack.name = name;
out->stack.size = size;
return true;
}
if (match_token(parser, TOKEN_GOTO))
{
if (!consume(parser, TOKEN_IDENTIFIER, "expected label after 'goto'"))
return false;
out->kind = STATEMENT_GOTO;
out->jump.label = parser->previous;
return true;
}
bool deref = match_token(parser, TOKEN_CARET);
enum StoreSize store_size = deref ? parse_store_size(parser) : STORE_SIZE_NONE;
if (!consume(parser, TOKEN_IDENTIFIER, "expected a statement"))
return false;
struct Token name = parser->previous;
if (!deref && match_token(parser, TOKEN_LEFT_PAREN))
return parse_call(parser, name, out);
if (!deref && match_token(parser, TOKEN_COLON))
{
out->kind = STATEMENT_LABEL;
out->label.name = name;
return true;
}
if (!is_assign_op(parser->current.type))
{
error_at(parser, parser->current, "expected an assignment operator");
return false;
}
advance_parser(parser);
struct Token op = parser->previous;
struct Expr* value = parse_expression(parser);
if (value == NULL)
return false;
out->kind = STATEMENT_ASSIGN;
out->assign.target_deref = deref;
out->assign.store_size = store_size;
out->assign.target = name;
out->assign.op = op;
out->assign.value = value;
return true;
}
static bool parse_proc(struct Parser* parser, struct Program* program)
{
struct ProcDecl decl = create_proc();
if (!consume(parser, TOKEN_IDENTIFIER, "expected procedure name after 'proc'"))
goto error;
decl.name = parser->previous;
if (match_token(parser, TOKEN_LEFT_PAREN))
{
if (!parse_params(parser, &decl))
goto error;
if (!consume(parser, TOKEN_RIGHT_PAREN, "expected ')' after parameters"))
goto error;
}
if (!consume(parser, TOKEN_LEFT_BRACE, "expected '{' to begin procedure body"))
goto error;
while (!check(parser, TOKEN_RIGHT_BRACE))
{
if (check(parser, TOKEN_EOF))
{
error_at(parser, parser->current, "unterminated procedure body");
goto error;
}
struct Statement statement;
if (!parse_statement(parser, &statement))
goto error;
add_statement(&decl, statement);
}
advance_parser(parser);
add_proc(program, decl);
return true;
error:
free_proc(&decl);
return false;
}
static bool token_text_is(struct Token token, const char* text)
{
size_t length = strlen(text);
return token.length == length && memcmp(token.start, text, length) == 0;
}
static bool parse_directive(struct Parser* parser, struct Program* program)
{
if (!consume(parser, TOKEN_IDENTIFIER, "expected directive name after '['"))
return false;
struct Token key = parser->previous;
if (!consume(parser, TOKEN_COLON, "expected ':' after directive name"))
return false;
if (!check(parser, TOKEN_IDENTIFIER) && !check(parser, TOKEN_INTEGER))
{
error_at(parser, parser->current, "expected a directive value");
return false;
}
advance_parser(parser);
struct Token value = parser->previous;
if (!consume(parser, TOKEN_RIGHT_BRACKET, "expected ']' to close directive"))
return false;
if (token_text_is(key, "bits"))
{
if (value.type != TOKEN_INTEGER || (!token_text_is(value, "64") && !token_text_is(value, "32")))
{
error_at(parser, value, "bits must be 32 or 64");
return false;
}
program->config.bits = token_text_is(value, "64") ? 64 : 32;
return true;
}
if (token_text_is(key, "entry"))
{
if (value.type != TOKEN_IDENTIFIER)
{
error_at(parser, value, "entry must be a procedure name");
return false;
}
program->config.has_entry = true;
program->config.entry = value;
return true;
}
if (token_text_is(key, "enable"))
{
if (value.type == TOKEN_IDENTIFIER && token_text_is(value, "logical_registers"))
{
program->config.logical_registers = true;
return true;
}
error_at(parser, value, "unknown extension");
return false;
}
error_at(parser, key, "unknown directive");
return false;
}
bool parse_program(struct Lexer* lexer, struct Program* out)
{
struct Parser parser;
parser.lexer = lexer;
parser.source.name = lexer->name;
parser.source.text = lexer->source;
parser.had_error = false;
advance_parser(&parser);
*out = create_program();
while (!check(&parser, TOKEN_EOF))
{
if (check(&parser, TOKEN_LEFT_BRACKET))
{
advance_parser(&parser);
if (!parse_directive(&parser, out))
return false;
}
else if (check(&parser, TOKEN_CONST))
{
advance_parser(&parser);
if (!parse_const(&parser, out))
return false;
}
else if (check(&parser, TOKEN_DATA))
{
advance_parser(&parser);
if (!parse_data(&parser, out))
return false;
}
else if (check(&parser, TOKEN_ENUM))
{
advance_parser(&parser);
if (!parse_enum(&parser, out))
return false;
}
else if (check(&parser, TOKEN_STRUCT))
{
advance_parser(&parser);
if (!parse_struct(&parser, out))
return false;
}
else if (check(&parser, TOKEN_PROC))
{
advance_parser(&parser);
if (!parse_proc(&parser, out))
return false;
}
else
{
error_at(&parser, parser.current, "expected a top-level declaration");
return false;
}
}
return !parser.had_error;
}
|