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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/*
* Copyright (C) 2023 Johnny Richard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "lexer.h"
#include "parser.h"
#include "scope.h"
#include "vector.h"
static bool
parser_expression_matches_the_expected_type(parser_t *parser, ast_node_t *expression, type_t type, token_t token);
static ast_node_t *
parser_parse_block_declarations(parser_t *parser, type_t result_type);
void
parser_init(parser_t *parser, lexer_t *lexer, scope_t *scope)
{
assert(parser && "parser must be defined");
assert(lexer && "lexer must be defined");
parser->lexer = lexer;
parser->scope = scope;
parser->errors_len = 0;
}
static void
parser_error_push_unexpected_kind(parser_t *parser, token_t *token, token_kind_t expected)
{
parser_error_t *error = &parser->errors[parser->errors_len++];
error->token = *token;
if (token->kind == TOKEN_EOF) {
sprintf(error->message, "expected '%s' but got end of file", token_kind_to_str(expected));
return;
}
sprintf(error->message, "expected '%s' but got '%s'", token_kind_to_str(expected), token_kind_to_str(token->kind));
}
static bool
expected_token(token_t *token, parser_t *parser, token_kind_t kind)
{
lexer_next_token(parser->lexer, token);
if (token->kind != kind) {
parser_error_push_unexpected_kind(parser, token, kind);
return false;
}
return true;
}
static bool
drop_expected_token(parser_t *parser, token_kind_t kind)
{
token_t ignored_token;
return expected_token(&ignored_token, parser, kind);
}
static bool
parser_parse_type(parser_t *parser, type_t *type)
{
token_t token;
if (!expected_token(&token, parser, TOKEN_NAME))
return false;
if (string_view_eq(token.value, string_view_from_str("i32"))) {
*type = TYPE_I32;
return true;
}
if (string_view_eq(token.value, string_view_from_str("bool"))) {
*type = TYPE_BOOL;
return true;
}
parser_error_t error;
error.token = token;
sprintf(error.message, "type '" SVFMT "' is not defined", SVARG(&token.value));
parser->errors[parser->errors_len++] = error;
return false;
}
static ast_node_t *
parser_literal_integer_node(token_t *token)
{
char number_as_str[token->value.size];
string_view_to_str(&token->value, number_as_str);
return ast_node_new_literal_integer(atoi(number_as_str));
}
static ast_node_t *
parser_literal_bool_node(token_t *token)
{
return ast_node_new_literal_bool(token->kind == TOKEN_TRUE);
}
static ast_binary_operation_kind_t
token_to_binary_operation_kind(token_t *token)
{
switch (token->kind) {
case TOKEN_PLUS:
return AST_BINOP_ADITION;
case TOKEN_MINUS:
return AST_BINOP_SUBTRACTION;
case TOKEN_STAR:
return AST_BINOP_MULTIPLICATION;
case TOKEN_SLASH:
return AST_BINOP_DIVISION;
case TOKEN_EQUAL:
return AST_BINOP_EQUAL;
case TOKEN_NOT_EQUAL:
return AST_BINOP_NOT_EQUAL;
case TOKEN_AND:
return AST_BINOP_AND;
case TOKEN_OR:
return AST_BINOP_OR;
case TOKEN_GT:
return AST_BINOP_GT;
case TOKEN_GT_EQUAL:
return AST_BINOP_GT_EQUAL;
case TOKEN_LT:
return AST_BINOP_LT;
case TOKEN_LT_EQUAL:
return AST_BINOP_LT_EQUAL;
default:
assert(false && "token mapping not found.");
}
}
ast_node_t *
parser_parse_expression(parser_t *parser);
static ast_node_t *
parser_parse_comparison(parser_t *parser);
static ast_node_t *
parser_parse_arithmetic1(parser_t *parser);
static ast_node_t *
parser_parse_arithmetic2(parser_t *parser);
static ast_node_t *
parser_parse_factor(parser_t *parser);
/**
*
* <expression> ::= <comparison>
* <comparison> ::= <arithmetic1> (('>' | '>=' | '=' | ...) arithmetic1)*
* <arithmetic1> ::= <arithmetic2> (('+' | '-') arithmetic2)*
* <arithmetic2> ::= <factor> (('*' | '/') factor)*
* <factor> ::= <integer> | '(' <expression> ')'
*
*/
ast_node_t *
parser_parse_expression(parser_t *parser)
{
return parser_parse_comparison(parser);
}
static ast_node_t *
parser_parse_comparison(parser_t *parser)
{
ast_node_t *node = parser_parse_arithmetic1(parser);
if (node == NULL) {
return NULL;
}
token_t token;
lexer_peek_next_token(parser->lexer, &token);
while (token.kind == TOKEN_GT || token.kind == TOKEN_LT || token.kind == TOKEN_GT_EQUAL ||
token.kind == TOKEN_LT_EQUAL || token.kind == TOKEN_EQUAL || token.kind == TOKEN_NOT_EQUAL ||
token.kind == TOKEN_AND || token.kind == TOKEN_OR) {
lexer_drop_next_token(parser->lexer);
ast_node_t *left = node;
ast_node_t *right = parser_parse_arithmetic1(parser);
if (right == NULL) {
ast_node_destroy(node);
return NULL;
}
node = ast_node_new_binary_operation(token_to_binary_operation_kind(&token), left, right, TYPE_BOOL);
lexer_peek_next_token(parser->lexer, &token);
}
return node;
}
static ast_node_t *
parser_parse_arithmetic1(parser_t *parser)
{
ast_node_t *node = parser_parse_arithmetic2(parser);
if (node == NULL) {
return NULL;
}
token_t token;
lexer_peek_next_token(parser->lexer, &token);
while (token.kind == TOKEN_PLUS || token.kind == TOKEN_MINUS) {
lexer_drop_next_token(parser->lexer);
ast_node_t *left = node;
ast_node_t *right = parser_parse_arithmetic2(parser);
if (right == NULL) {
ast_node_destroy(left);
return NULL;
}
node = ast_node_new_binary_operation(token_to_binary_operation_kind(&token), left, right, TYPE_I32);
lexer_peek_next_token(parser->lexer, &token);
}
return node;
}
static ast_node_t *
parser_parse_arithmetic2(parser_t *parser)
{
ast_node_t *node = parser_parse_factor(parser);
if (node == NULL) {
return NULL;
}
token_t token;
lexer_peek_next_token(parser->lexer, &token);
while (token.kind == TOKEN_STAR || token.kind == TOKEN_SLASH) {
lexer_drop_next_token(parser->lexer);
ast_node_t *left = node;
ast_node_t *right = parser_parse_factor(parser);
if (right == NULL) {
ast_node_destroy(node);
return NULL;
}
node = ast_node_new_binary_operation(token_to_binary_operation_kind(&token), left, right, TYPE_I32);
lexer_peek_next_token(parser->lexer, &token);
}
return node;
}
static ast_node_t *
parser_parse_factor(parser_t *parser)
{
token_t token;
lexer_next_token(parser->lexer, &token);
switch (token.kind) {
case TOKEN_NUMBER:
return parser_literal_integer_node(&token);
case TOKEN_TRUE:
return parser_literal_bool_node(&token);
case TOKEN_FALSE:
return parser_literal_bool_node(&token);
case TOKEN_OPAREN: {
ast_node_t *expression = parser_parse_expression(parser);
if (expression == NULL) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_CPAREN)) {
ast_node_destroy(expression);
return NULL;
}
return expression;
}
case TOKEN_NAME: {
// TODO: Check node kind, today accepts only variables
ast_node_t *var_node = scope_get(parser->scope, token.value);
if (var_node == NULL) {
parser_error_t error;
error.token = token;
sprintf(error.message, "identifier '" SVFMT "' not defined", SVARG(&token.value));
parser->errors[parser->errors_len++] = error;
return NULL;
}
return ast_node_new_variable(&var_node->data.variable_declaration.identifier,
var_node->data.variable_declaration.type);
}
default: {
parser_error_t error;
error.token = token;
sprintf(error.message, "unexpected '%s (" SVFMT ")' token", token_kind_to_str(token.kind), SVARG(&token.value));
parser->errors[parser->errors_len++] = error;
return NULL;
}
}
}
static ast_node_t *
parser_parse_return_stmt(parser_t *parser, type_t result_type)
{
token_t token;
if (!expected_token(&token, parser, TOKEN_KEYWORD_RETURN)) {
return NULL;
}
ast_node_t *expression = parser_parse_expression(parser);
if (expression == NULL) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_SEMICOLON)) {
ast_node_destroy(expression);
return NULL;
}
if (!parser_expression_matches_the_expected_type(parser, expression, result_type, token)) {
ast_node_destroy(expression);
return NULL;
}
return ast_node_new_return_stmt(expression);
}
static ast_node_t *
parser_parse_variable_assignment(parser_t *parser)
{
token_t variable_token;
if (!expected_token(&variable_token, parser, TOKEN_NAME)) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_ASSIGN))
return NULL;
ast_node_t *expression = parser_parse_expression(parser);
if (expression == NULL) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_SEMICOLON)) {
ast_node_destroy(expression);
return NULL;
}
ast_node_t *variable_declaration_node = scope_get(parser->scope, variable_token.value);
if (variable_declaration_node == NULL) {
parser_error_t error;
error.token = variable_token;
sprintf(error.message, "trying to assign '" SVFMT "' before defining it.", SVARG(&variable_token.value));
parser->errors[parser->errors_len++] = error;
return NULL;
}
assert(variable_declaration_node->kind == AST_VARIABLE_DECLARATION);
if (!parser_expression_matches_the_expected_type(
parser, expression, variable_declaration_node->data.variable_declaration.type, variable_token)) {
ast_node_destroy(expression);
return false;
}
return ast_node_new_variable_assignment(&variable_declaration_node->data.variable_declaration.identifier, expression);
}
static bool
parser_expression_matches_the_expected_type(parser_t *parser, ast_node_t *expression, type_t type, token_t token)
{
if (expression->result_type != type) {
parser_error_t error;
error.token = token;
sprintf(error.message,
"incompatible types: expected '%s', actual '%s'.",
ast_type_to_str(type),
ast_type_to_str(expression->result_type));
parser->errors[parser->errors_len++] = error;
return false;
}
return true;
}
static ast_node_t *
parser_parse_variable_declaration(parser_t *parser)
{
token_t variable_name;
if (!drop_expected_token(parser, TOKEN_KEYWORD_LET)) {
return NULL;
}
if (!expected_token(&variable_name, parser, TOKEN_NAME)) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_COLON)) {
return NULL;
}
type_t type;
if (!parser_parse_type(parser, &type)) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_ASSIGN)) {
return NULL;
}
ast_node_t *expression = parser_parse_expression(parser);
if (expression == NULL) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_SEMICOLON)) {
ast_node_destroy(expression);
return NULL;
}
if (!parser_expression_matches_the_expected_type(parser, expression, type, variable_name)) {
ast_node_destroy(expression);
return NULL;
}
ast_node_t *node = ast_node_new_variable_declaration(variable_name.value, type, expression);
scope_push(parser->scope, &node->data.variable_declaration.identifier, node);
return node;
}
static ast_node_t *
parser_parse_if_stmt(parser_t *parser, type_t result_type)
{
token_t if_token;
if (!expected_token(&if_token, parser, TOKEN_KEYWORD_IF)) {
return NULL;
}
ast_node_t *condition = parser_parse_expression(parser);
if (condition == NULL) {
return NULL;
}
if (!parser_expression_matches_the_expected_type(parser, condition, TYPE_BOOL, if_token)) {
ast_node_destroy(condition);
return NULL;
}
ast_node_t *body = parser_parse_block_declarations(parser, result_type);
if (body == NULL) {
ast_node_destroy(condition);
return NULL;
}
return ast_node_new_if_stmt(condition, body);
}
static bool
is_next_statement_a_variable_declaration(parser_t *parser)
{
token_t token;
lexer_peek_next_token(parser->lexer, &token);
return token.kind == TOKEN_KEYWORD_LET;
}
static bool
is_next_statement_a_variable_assignement(parser_t *parser)
{
token_t token;
lexer_peek_next_token(parser->lexer, &token);
if (token.kind != TOKEN_NAME) {
return false;
}
lexer_lookahead(parser->lexer, &token, 2);
return token.kind == TOKEN_ASSIGN;
}
static bool
is_next_statement_a_if_stmt(parser_t *parser)
{
token_t token;
lexer_peek_next_token(parser->lexer, &token);
return token.kind == TOKEN_KEYWORD_IF;
}
static bool
is_next_statement_return(parser_t *parser)
{
token_t token;
lexer_peek_next_token(parser->lexer, &token);
return token.kind == TOKEN_KEYWORD_RETURN;
}
static bool
is_block_end(parser_t *parser)
{
token_t token;
lexer_peek_next_token(parser->lexer, &token);
return token.kind == TOKEN_CCURLY || token.kind == TOKEN_EOF;
}
static void
parser_error_report_unexpected_token(parser_t *parser)
{
token_t token;
lexer_peek_next_token(parser->lexer, &token);
parser_error_t *error = &parser->errors[parser->errors_len++];
error->token = token;
sprintf(
error->message, "unexpected token '%s' value='" SVFMT "'", token_kind_to_str(token.kind), SVARG(&token.value));
}
static bool
parser_ensure_function_return_statement(parser_t *parser, ast_node_t *block, token_t *function_token)
{
assert(block->kind == AST_BLOCK);
ast_node_t *latest_node = vector_at(block->data.block.body, block->data.block.body->size - 1);
if (latest_node->kind != AST_RETURN_STMT) {
parser_error_t error;
error.token = *function_token;
sprintf(error.message, "expected 'return' keyword.");
parser->errors[parser->errors_len++] = error;
return false;
}
return true;
}
static ast_node_t *
parser_parse_block_declarations(parser_t *parser, type_t result_type)
{
if (!drop_expected_token(parser, TOKEN_OCURLY)) {
return NULL;
}
token_t current_token;
lexer_peek_next_token(parser->lexer, ¤t_token);
scope_enter(parser->scope);
vector_t *body = vector_new();
while (!is_block_end(parser)) {
if (is_next_statement_return(parser)) {
ast_node_t *return_node = parser_parse_return_stmt(parser, result_type);
if (return_node == NULL) {
scope_leave(parser->scope);
ast_node_destroy_vector(body);
return NULL;
}
vector_push_back(body, return_node);
continue;
}
if (is_next_statement_a_if_stmt(parser)) {
ast_node_t *if_stmt = parser_parse_if_stmt(parser, result_type);
if (if_stmt == NULL) {
scope_leave(parser->scope);
ast_node_destroy_vector(body);
return NULL;
}
vector_push_back(body, if_stmt);
continue;
}
if (is_next_statement_a_variable_declaration(parser)) {
ast_node_t *variable_node = parser_parse_variable_declaration(parser);
if (variable_node == NULL) {
scope_leave(parser->scope);
ast_node_destroy_vector(body);
return NULL;
}
vector_push_back(body, variable_node);
continue;
}
if (is_next_statement_a_variable_assignement(parser)) {
ast_node_t *variable_assignment = parser_parse_variable_assignment(parser);
if (variable_assignment == NULL) {
scope_leave(parser->scope);
ast_node_destroy_vector(body);
return NULL;
}
vector_push_back(body, variable_assignment);
continue;
}
parser_error_report_unexpected_token(parser);
scope_leave(parser->scope);
ast_node_destroy_vector(body);
return NULL;
}
scope_leave(parser->scope);
if (!drop_expected_token(parser, TOKEN_CCURLY)) {
vector_destroy(body);
return NULL;
}
return ast_node_new_block(body);
}
static bool
parser_parse_function_arguments(parser_t *parser)
{
return drop_expected_token(parser, TOKEN_OPAREN) && drop_expected_token(parser, TOKEN_CPAREN);
}
ast_node_t *
parser_parse_function_declaration(parser_t *parser)
{
token_t func_name_token;
if (!drop_expected_token(parser, TOKEN_KEYWORD_FN)) {
return NULL;
}
if (!expected_token(&func_name_token, parser, TOKEN_NAME)) {
return NULL;
}
if (!parser_parse_function_arguments(parser)) {
return NULL;
}
if (!drop_expected_token(parser, TOKEN_COLON)) {
return NULL;
}
type_t return_type;
if (!parser_parse_type(parser, &return_type)) {
return NULL;
}
ast_node_t *body = parser_parse_block_declarations(parser, return_type);
if (body == NULL) {
return NULL;
}
if (!parser_ensure_function_return_statement(parser, body, &func_name_token)) {
ast_node_destroy(body);
return NULL;
}
ast_node_t *node = ast_node_new_function_declaration(func_name_token.value, return_type, body);
// TODO: When implementing function calls the scope must be pushed before the
// body to be parsed, otherwise recursion not gonna work.
scope_push(parser->scope, &node->data.function.identifier, node);
return node;
}
|