aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: 85c4d6a1e5d65b47972ecfde2537d613df6593ba (plain) (blame)
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
#include "parser.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAX_FUNCTION_COUNT 1024
static Node *all_functions[MAX_FUNCTION_COUNT];
static i64 function_count = 0;

static Node *current_function = NULL;

#define BLOCK_STACK_SIZE 64
static Node *block_stack[BLOCK_STACK_SIZE];
static i64 block_stack_count = 0;
static i64 cur_stack_offset = 0;

// TODO: Probably use a vector here
#define GLOBAL_VARS_SIZE 1024
static Variable *global_vars[GLOBAL_VARS_SIZE];
static i64 global_vars_count = 0;
static i64 global_vars_offset = 0;

#define LEXER_STACK_SIZE 64
static Lexer *lexer_stack[LEXER_STACK_SIZE];
static i64 lexer_stack_count = 0;

Token do_assert_token(Token token, TokenType type, char *filename, int line)
{
    if (token.type != type) {
        Location_print(stderr, token.loc);
        fprintf(stderr, ": Expected token of type `%s` but got `%s`\n", token_type_to_str(type), token_type_to_str(token.type));
        fprintf(stderr, "Relevant location in compiler: %s:%d\n", filename, line);
        exit(1);
    }
    return token;
}

#define assert_token(token, type) do_assert_token(token, type, __FILE__, __LINE__)

/******
 * Some helpers
 */

void block_stack_push(Node *block)
{
    assert(block_stack_count < BLOCK_STACK_SIZE);
    assert(current_function);
    block_stack[block_stack_count++] = block;
}

void block_stack_pop()
{
    assert(block_stack_count > 0);
    assert(current_function);
    Node *block = block_stack[--block_stack_count];
    cur_stack_offset -= block->block.locals_size;
    assert(cur_stack_offset >= 0);
}

void dump_block_stack()
{
    for (i64 i = 0; i < block_stack_count; i++) {
        Node *block = block_stack[i];
        for (int i = 0; i < block->block.num_locals; i++) {
            printf("%s: offset: %lld\n", block->block.locals[i]->name, block->block.locals[i]->offset);
        }
        printf("\n");
    }
}

Node *builtin_print;
Node *builtin_putc;

void initialize_builtins()
{
    builtin_print = Node_new(AST_BUILTIN);
    builtin_print->func.name = "print";
    builtin_print->func.return_type = type_new(TYPE_INT);
    builtin_print->func.num_args = 1;
    builtin_print->func.args = (Variable *)calloc(sizeof(Variable), 1);
    builtin_print->func.args[0] = (Variable){"val", type_new(TYPE_INT), 0};

    builtin_putc = Node_new(AST_BUILTIN);
    builtin_putc->func.name = "putc";
    builtin_putc->func.return_type = type_new(TYPE_INT);
    builtin_putc->func.num_args = 1;
    builtin_putc->func.args = (Variable *)calloc(sizeof(Variable), 2);
    builtin_putc->func.args[0] = (Variable){"arg", type_new(TYPE_INT), 0};
}

Node *find_builtin_function(Token *token)
{
    if (strcmp(token->value.as_string, "putc") == 0) { return builtin_putc; }
    if (strcmp(token->value.as_string, "print") == 0) { return builtin_print; }
    return NULL;
}

Variable *find_local_variable(Token *token)
{
    if (current_function == NULL)
        return NULL;

    assert_token(*token, TOKEN_IDENTIFIER);
    for (i64 i = block_stack_count - 1; i >= 0; --i) {
        Node *block = block_stack[i];
        for (int i = 0; i < block->block.num_locals; i++) {
            if (strcmp(block->block.locals[i]->name, token->value.as_string) == 0) {
                return block->block.locals[i];
            }
        }
    }
    Node *func = current_function;
    for (int i = 0; i < func->func.num_args; i++) {
        if (strcmp(func->func.args[i].name, token->value.as_string) == 0) {
            return &func->func.args[i];
        }
    }
    return NULL;
}

Variable *find_global_variable(Token *token)
{
    for (int i = 0; i < global_vars_count; i++) {
        if (strcmp(global_vars[i]->name, token->value.as_string) == 0) {
            return global_vars[i];
        }
    }
    return NULL;
}

Node *find_function_definition(Token *token)
{
    assert_token(*token, TOKEN_IDENTIFIER);
    for (i64 i = 0; i < function_count; i++) {
        Node *function = all_functions[i];
        if (strcmp(function->func.name, token->value.as_string) == 0) {
            return function;
        }
    }
    return NULL;
}

void add_global_variable(Variable *var)
{
    var->offset = global_vars_offset;
    int var_size = size_for_type(var->type);
    global_vars_offset += var_size;
    global_vars[global_vars_count++] = var;
}

// TODO: rename this, it's ugly
void add_variable_to_current_block(Variable *var)
{
    // Set offset for variable
    Node *cur_block = block_stack[block_stack_count - 1];

    int new_len = (cur_block->block.num_locals + 1);
    // TODO: Align the stack to a certain size?
    int var_size = size_for_type(var->type);

    // Add to the block
    // FIXME: Use a map here
    cur_block->block.locals = realloc(cur_block->block.locals, sizeof(Variable *) * new_len);
    cur_block->block.locals[cur_block->block.num_locals] = var;
    cur_block->block.num_locals++;

    assert(current_function);
    // Update current stack offset (w.r.t function stack frame) and block size
    cur_stack_offset += var_size;
    block_stack[block_stack_count-1]->block.locals_size += var_size;
    var->offset = cur_stack_offset;

    // Update function's max locals size
    i64 max_offset = i64max(current_function->func.max_locals_size, cur_stack_offset);
    current_function->func.max_locals_size = max_offset;

    assert(cur_stack_offset >= 0);
    assert(block_stack_count > 0);
}

Type *parse_type(Lexer *lexer)
{
    Type *type;
    Token token = Lexer_peek(lexer);
    if (token.type == TOKEN_INT) {
        type = type_new(TYPE_INT);
        Lexer_next(lexer);
    } else {
        type = type_new(TYPE_NONE);
    }

    while (Lexer_peek(lexer).type == TOKEN_STAR) {
        Lexer_next(lexer);
        Type *ptr = type_new(TYPE_PTR);
        ptr->ptr = type;
        type = ptr;
    }

    return type;
}

Node *parse_literal(Lexer *lexer)
{
    Node *node = Node_new(AST_LITERAL);
    Token token = assert_token(Lexer_next(lexer), TOKEN_INTLIT);
    node->literal.type = type_new(TYPE_INT);
    node->literal.as_int = token.value.as_int;
    return node;
}

Node *parse_expression(Lexer *);

Node *parse_var_declaration(Lexer *lexer)
{
    bool is_global = (current_function == NULL);
    Token token = assert_token(Lexer_next(lexer), TOKEN_LET);

    token = assert_token(Lexer_next(lexer), TOKEN_IDENTIFIER);
    // NOTE: We don't allow shadowing of variables in the any blocks,
    //       this is by design since it's a common mistake.
    if (find_local_variable(&token) != NULL)
        die_location(token.loc, "Variable `%s` already declared in function", token.value.as_string);
    if (find_global_variable(&token) != NULL)
        die_location(token.loc, "Variable `%s` already declared globally", token.value.as_string);

    Node *node = Node_new(AST_VARDECL);
    node->var_decl.var.name = token.value.as_string;

    token = Lexer_next(lexer);
    if (token.type != TOKEN_COLON)
        die_location(token.loc, "Missing type specifier for variable `%s`", node->var_decl.var.name);
    node->var_decl.var.type = parse_type(lexer);

    if (is_global) {
        add_global_variable(&node->var_decl.var);
    } else {
        add_variable_to_current_block(&node->var_decl.var);
    }

    token = Lexer_next(lexer);
    if (token.type == TOKEN_ASSIGN) {
        if (is_global)
            die_location(token.loc, "Cannot initialize global variable `%s` outside function", node->var_decl.var.name);
        node->var_decl.value = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_SEMICOLON);
    } else {
        assert_token(token, TOKEN_SEMICOLON);
    }

    return node;
}

Node *parse_function_call_args(Lexer *lexer, Node *func)
{
    Token identifier = assert_token(Lexer_next(lexer), TOKEN_IDENTIFIER);
    Node *call = Node_new(AST_FUNCCALL);
    call->call.func = func;
    assert_token(Lexer_next(lexer), TOKEN_OPEN_PAREN);
    Token token = Lexer_peek(lexer);

    while (token.type != TOKEN_CLOSE_PAREN) {
        Node *arg = parse_expression(lexer);

        int new_size = call->call.num_args + 1;
        call->call.args = realloc(call->call.args, sizeof(Node *) * new_size);
        call->call.args[call->call.num_args++] = arg;

        if (new_size > func->func.num_args)
            die_location(identifier.loc, "Too many arguments to function `%s`", func->func.name);

        token = Lexer_peek(lexer);
        if (token.type == TOKEN_COMMA) {
            Lexer_next(lexer);
            token = Lexer_peek(lexer);
        }
    }

    if (call->call.num_args != func->func.num_args)
        die_location(identifier.loc, "Too few arguments to function `%s`", func->func.name);

    assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN);
    return call;
}

Node *parse_identifier(Lexer *lexer)
{
    Token token = assert_token(Lexer_peek(lexer), TOKEN_IDENTIFIER);

    // TODO: Check for global variables when added
    Node *expr;
    Variable *var = find_local_variable(&token);
    if (var != NULL) {
        Lexer_next(lexer);
        expr = Node_new(AST_LOCAL_VAR);
        expr->variable = var;
        return expr;
    }

    Variable *gvar = find_global_variable(&token);
    if (gvar != NULL) {
        Lexer_next(lexer);
        expr = Node_new(AST_GLOBAL_VAR);
        expr->variable = gvar;
        return expr;
    }

    Node *func = find_function_definition(&token);
    if (func != NULL) {
        return parse_function_call_args(lexer, func);
    }

    Node *builtin = find_builtin_function(&token);
    if (builtin != NULL) {
        return parse_function_call_args(lexer, builtin);
    }

    die_location(token.loc, "Unknown identifier `%s`", token.value.as_string);
    return NULL;
}

Node *parse_factor(Lexer *lexer)
{
    // TODO: Parse more complicated things
    Token token = Lexer_peek(lexer);
    Node *expr = NULL;
    if (token.type == TOKEN_MINUS) {
        Lexer_next(lexer);
        expr = Node_new(OP_NEG);
        expr->unary_expr = parse_factor(lexer);
    } else if (token.type == TOKEN_TILDE) {
        Lexer_next(lexer);
        expr = Node_new(OP_BWINV);
        expr->unary_expr = parse_factor(lexer);
    } else if (token.type == TOKEN_EXCLAMATION) {
        Lexer_next(lexer);
        expr = Node_new(OP_NOT);
        expr->unary_expr = parse_factor(lexer);
    } else if (token.type == TOKEN_OPEN_PAREN) {
        Lexer_next(lexer);
        expr = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN);
    } else if (token.type == TOKEN_INTLIT) {
        expr = parse_literal(lexer);
    } else if (token.type == TOKEN_IDENTIFIER) {
        expr = parse_identifier(lexer);
    } else if (token.type == TOKEN_AMPERSAND) {
        Lexer_next(lexer);
        expr = Node_new(OP_ADDROF);
        expr->unary_expr = parse_factor(lexer);
        if (!is_lvalue(expr->unary_expr->type))
            die_location(token.loc, "Cannot take address of non-lvalue");
    } else if (token.type == TOKEN_STAR) {
        Lexer_next(lexer);
        expr = Node_new(OP_DEREF);
        // TODO: IMPORTANT: Make sure the `unary_expr` is a pointer type. For this
        // to work, we need to to be able to evaluate the type for complex expressions,
        // which we do not support as of now.
        expr->unary_expr = parse_factor(lexer);
    } else {
        die_location(token.loc, ": Expected token found in parse_factor: `%s`", token_type_to_str(token.type));
    }
    return expr;
}

#define BINOP_PARSER(next_parser, predicate)                                   \
  Node *expr = next_parser(lexer);                                             \
  Token token = Lexer_peek(lexer);                                             \
  while (predicate(token.type)) {                                              \
    Lexer_next(lexer);                                                         \
    Node *op = Node_new(binary_token_to_op(token.type));                       \
    Node *right = next_parser(lexer);                                          \
    op->binary.left = expr;                                                    \
    op->binary.right = right;                                                  \
    expr = op;                                                                 \
    token = Lexer_peek(lexer);                                                 \
  }                                                                            \
  return expr;

bool is_term_token(TokenType type) { return type == TOKEN_STAR ||  type == TOKEN_SLASH ||  type == TOKEN_PERCENT; }
Node *parse_term(Lexer *lexer) { BINOP_PARSER(parse_factor, is_term_token); }

bool is_additive_token(TokenType type) { return type == TOKEN_PLUS || type == TOKEN_MINUS; }
Node *parse_additive(Lexer *lexer) { BINOP_PARSER(parse_term, is_additive_token); }

bool is_relational_token(TokenType type) { return type == TOKEN_LT || type == TOKEN_LEQ || type == TOKEN_GT || type == TOKEN_GEQ; }
Node *parse_relational(Lexer *lexer) { BINOP_PARSER(parse_additive, is_relational_token); }

bool is_equality_token(TokenType type) { return type == TOKEN_EQ || type == TOKEN_NEQ; }
Node *parse_equality(Lexer *lexer) { BINOP_PARSER(parse_relational, is_equality_token); }

bool is_logical_and_token(TokenType type) { return type == TOKEN_AND; }
Node *parse_logical_and(Lexer *lexer) { BINOP_PARSER(parse_equality, is_logical_and_token); }

bool is_logical_or_token(TokenType type) { return type == TOKEN_OR; }
Node *parse_logical_or(Lexer *lexer) { BINOP_PARSER(parse_logical_and, is_logical_or_token); }

Node *parse_conditional_exp(Lexer *lexer)
{
    Node *expr = parse_logical_or(lexer);
    Token token = Lexer_peek(lexer);
    if (token.type == TOKEN_QUESTION) {
        Lexer_next(lexer);
        Node *then_expr = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_COLON);
        Node *else_expr = parse_expression(lexer);
        Node *conditional = Node_new(AST_CONDITIONAL);
        conditional->conditional.cond = expr;
        conditional->conditional.do_then = then_expr;
        conditional->conditional.do_else = else_expr;
        expr = conditional;
    }
    return expr;
}

Node *parse_expression(Lexer *lexer)
{
    Node *node = parse_conditional_exp(lexer);
    // FIXME: This is a hack to handle assignment expressions
    //        and can probably be done properly.
    if (is_lvalue(node->type)) {
        Token token = Lexer_peek(lexer);
        if (token.type == TOKEN_ASSIGN) {
            Lexer_next(lexer);
            Node *assign = Node_new(OP_ASSIGN);
            assign->assign.var = node;
            assign->assign.value = parse_expression(lexer);
            node = assign;
        }
    }
    return node;
}

Node *parse_block(Lexer *lexer);

Node *parse_statement(Lexer *lexer)
{
    Node *node;
    Token token = Lexer_peek(lexer);

    if (token.type == TOKEN_RETURN) {
        assert_token(Lexer_next(lexer), TOKEN_RETURN);
        node = Node_new(AST_RETURN);
        node->unary_expr = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_SEMICOLON);
    } else if (token.type == TOKEN_IF) {
        Lexer_next(lexer);
        node = Node_new(AST_IF);
        assert_token(Lexer_next(lexer), TOKEN_OPEN_PAREN);
        node->conditional.cond = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN);
        // TODO: Allow blocks in here once implemented, currently
        //       we can onle have a single statement in the if/else
        node->conditional.do_then = parse_statement(lexer);
        token = Lexer_peek(lexer);
        if (token.type == TOKEN_ELSE) {
            Lexer_next(lexer);
            node->conditional.do_else = parse_statement(lexer);
        }
    } else if (token.type == TOKEN_WHILE) {
        Lexer_next(lexer);
        node = Node_new(AST_WHILE);
        assert_token(Lexer_next(lexer), TOKEN_OPEN_PAREN);
        node->loop.cond = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN);
        node->loop.body = parse_statement(lexer);
    }  else if (token.type == TOKEN_FOR) {
        Lexer_next(lexer);
        node = Node_new(AST_FOR);
        assert_token(Lexer_next(lexer), TOKEN_OPEN_PAREN);

        // All of the expressions in the for loop are optional

        // TODO: Allow this to be a declaration, need to inject
        //       the variable into the symbol table for the block
        if (Lexer_peek(lexer).type != TOKEN_SEMICOLON)
            node->loop.init = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_SEMICOLON);

        if (Lexer_peek(lexer).type != TOKEN_SEMICOLON)
            node->loop.cond = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_SEMICOLON);

        if (Lexer_peek(lexer).type != TOKEN_CLOSE_PAREN)
            node->loop.step = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN);

        node->loop.body = parse_statement(lexer);
    } else if (token.type == TOKEN_OPEN_BRACE) {
        node = parse_block(lexer);
    } else if (token.type == TOKEN_DEFER) {
        Lexer_next(lexer);
        node = Node_new(AST_DEFER);
        node->unary_expr = parse_statement(lexer);
    } else {
        // Default to trying to handle it as an expression
        node = parse_expression(lexer);
        assert_token(Lexer_next(lexer), TOKEN_SEMICOLON);
    }

    return node;
}

Node *parse_block(Lexer *lexer)
{
    assert_token(Lexer_next(lexer), TOKEN_OPEN_BRACE);

    Node *block = Node_new(AST_BLOCK);
    Token token = Lexer_peek(lexer);

    block_stack_push(block);

    while (token.type != TOKEN_CLOSE_BRACE) {
        Node *block_item;
        if (token.type == TOKEN_LET) {
            block_item = parse_var_declaration(lexer);
        } else {
            // Default to a statement
            block_item = parse_statement(lexer);
        }
        Node_add_child(block, block_item);
        token = Lexer_peek(lexer);
    }

    block_stack_pop();

    assert_token(Lexer_next(lexer), TOKEN_CLOSE_BRACE);
    return block;
}

void push_new_function(Node *func)
{
    assert(func->type == AST_FUNC);
    assert(function_count < MAX_FUNCTION_COUNT);
    all_functions[function_count++] = func;
    current_function = func;
}

void parse_func_args(Lexer *lexer, Node *func)
{
    assert_token(Lexer_next(lexer), TOKEN_OPEN_PAREN);
    Token token = Lexer_peek(lexer);
    while (token.type != TOKEN_CLOSE_PAREN) {
        token = assert_token(Lexer_next(lexer), TOKEN_IDENTIFIER);
        // TODO: Check for shadowing with globals
        assert_token(Lexer_next(lexer), TOKEN_COLON);
        Type *type = parse_type(lexer);

        i64 new_count = func->func.num_args + 1;
        func->func.args = realloc(func->func.args, sizeof(Variable) * new_count);
        Variable *var = &func->func.args[func->func.num_args++];
        var->name = token.value.as_string;
        var->type = type;

        token = Lexer_peek(lexer);
        if (token.type == TOKEN_COMMA) {
            Lexer_next(lexer);
            token = Lexer_peek(lexer);
        }
    }
    assert_token(Lexer_next(lexer), TOKEN_CLOSE_PAREN);

    // Set the offsets for the arguments

    // IMPORTANT: We want to skip the saved ret_addr+old_rbp that we
    //            pushed on the stack. Each of these is 8 bytes.
    int offset = -16;
    for (int i = 0; i < func->func.num_args; i++) {
        Variable *var = &func->func.args[i];
        var->offset = offset;
        // TODO: Do we need to align the stack here?
        int var_size = size_for_type(var->type);
        offset -= var_size;
    }
}

Node *parse_func(Lexer *lexer)
{
    Token token;
    token = assert_token(Lexer_next(lexer), TOKEN_FN);

    Node *func = Node_new(AST_FUNC);
    push_new_function(func);

    token = assert_token(Lexer_next(lexer), TOKEN_IDENTIFIER);
    func->func.name = token.value.as_string;
    parse_func_args(lexer, func);

    token = Lexer_peek(lexer);
    if (token.type == TOKEN_COLON) {
        // TODO: Parse all return types
        assert_token(Lexer_next(lexer), TOKEN_COLON);
        func->func.return_type = parse_type(lexer);
    } else {
        // No return type, void fn.
        func->func.return_type = type_new(TYPE_NONE);
    }

    // Make sure there's no funny business with the stack offset
    assert(cur_stack_offset == 0);
    assert(block_stack_count == 0);
    func->func.body = parse_block(lexer);
    assert(block_stack_count == 0);
    assert(cur_stack_offset == 0);

    // Reset current function
    current_function = NULL;

    return func;
}

void push_new_lexer(Lexer *lexer)
{
    assert(lexer_stack_count < LEXER_STACK_SIZE);
    lexer_stack[lexer_stack_count++] = lexer;
}

Lexer *remove_lexer()
{
    assert(lexer_stack_count > 0);
    free(lexer_stack[--lexer_stack_count]);
    if (lexer_stack_count == 0)
        return NULL;
    return lexer_stack[lexer_stack_count - 1];
}

Node *parse_program(Lexer *lexer)
{
    initialize_builtins();
    Node *program = Node_new(AST_PROGRAM);

    push_new_lexer(lexer);

    Token token = Lexer_peek(lexer);
    while (token.type != TOKEN_EOF) {
        if (token.type == TOKEN_FN) {
            Node *func = parse_func(lexer);
            Node_add_child(program, func);
        } else if (token.type == TOKEN_LET) {
            Node *var_decl = parse_var_declaration(lexer);
            Node_add_child(program, var_decl);
        } else if (token.type == TOKEN_IMPORT) {
            // TODO: Handle circular imports
            // TODO: Handle complex import graphs (#pragma once)
            // TODO: Validation of imports
            // TODO: Have default directories to search for imports
            Lexer_next(lexer);
            token = assert_token(Lexer_next(lexer), TOKEN_STRINGLIT);
            char *filename = token.value.as_string;
            lexer = Lexer_new_open_file(filename);
            push_new_lexer(lexer);
        } else {
            die_location(token.loc, "Unexpected token in parse_program: `%s`\n", token_type_to_str(token.type));
            exit(1);
            break;
        }

        token = Lexer_peek(lexer);
        while (token.type == TOKEN_EOF && lexer_stack_count > 1) {
            lexer = remove_lexer();
            token = Lexer_peek(lexer);
        }
    }
    program->block.locals_size = global_vars_offset;
    return program;
}