aboutsummaryrefslogtreecommitdiff
path: root/src/ast.c
blob: 0aa19d43b1fa77f5df43335627e4645b8de8452f (plain)
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#include "../include/ast.h"
#include "../include/parser.h"
#include "../include/compiler.h"
#include "../include/std/log.h"
#include "../include/std/hash.h"
#include <assert.h>
#include <stdarg.h>

#define throw(result) do { throw_debug_msg; longjmp(context->env, (result)); } while(0)
#define throw_if_error(result) \
    do { \
        int return_if_result; \
        return_if_result = (result); \
        if((return_if_result) != 0) \
            throw(return_if_result); \
    } while(0)

static void ast_resolve(Ast *self, AstCompilerContext *context);

static void resolve_data_init(AstResolveData *self) {
    self->status = AST_NOT_RESOLVED;
    self->parser = NULL;
    self->type = NULL;
}

int ast_create(ArenaAllocator *allocator, void *value, AstType type, Ast **result) {
    return_if_error(arena_allocator_alloc(allocator, sizeof(Ast), (void**)result));
    (*result)->value.data = value;
    (*result)->type = type;
    resolve_data_init(&(*result)->resolve_data);
    (*result)->ssa_reg = 0;
    return 0;
}

static bool ast_is_decl(Ast *self) {
    /* TODO: Add more types as they are introduced */
    return self->type == AST_FUNCTION_DECL || self->type == AST_STRUCT_DECL;
}

BufferView ast_get_name(Ast *self) {
    BufferView name;
    switch(self->type) {
        case AST_FUNCTION_DECL:
        case AST_STRUCT_DECL:
        case AST_IMPORT:
        case AST_STRING:
        case AST_BINOP:
        case AST_IF_STATEMENT:
        case AST_WHILE_STATEMENT:
            name = create_buffer_view_null();
            break;
        case AST_NUMBER:
            name = self->value.number->code_ref;
            break;
        case AST_LHS:
            name = self->value.lhs_expr->var_name;
            break;
        case AST_ASSIGN:
            name = ast_get_name(self->value.assign_expr->lhs_expr);
            break;
        case AST_FUNCTION_CALL:
            name = self->value.func_call->func.name;
            break;
        case AST_VARIABLE:
            name = self->value.variable->name;
            break;
        case AST_STRUCT_FIELD:
            name = self->value.struct_field->name;
            break;
    }
    return name;
}

static BufferView ast_get_code_reference(Ast *self) {
    return ast_get_name(self);
}

void function_signature_init(FunctionSignature *self) {
    self->params = 0;
    self->resolved = bool_false;
}

int funcdecl_init(FunctionDecl *self, FunctionSignature *signature, Scope *parent, ArenaAllocator *allocator) {
    self->signature = signature;
    self->ssa_func_index = 0;
    return scope_init(&self->body, parent, allocator);
}

int funccall_init(FunctionCall *self, BufferView name, ArenaAllocator *allocator) {
    variable_init(&self->func, name);
    return buffer_init(&self->args, allocator);
}

int structdecl_init(StructDecl *self, Scope *parent, ArenaAllocator *allocator) {
    return scope_init(&self->body, parent, allocator);
}

LhsExpr* structdecl_get_field_by_name(StructDecl *self, BufferView field_name) {
    Ast* result;
    if(!hash_map_get(&self->body.named_objects, field_name, &result))
        return NULL;
    return result->value.lhs_expr;
}

void structfield_init(StructField *self, BufferView name, BufferView type_name) {
    self->name = name;
    variable_init(&self->type, type_name);
}

void lhsexpr_init(LhsExpr *self, DeclFlag decl_flag, BufferView var_name) {
    assert(!((decl_flag & DECL_FLAG_EXTERN) && (decl_flag & DECL_FLAG_EXPORT)) && "Expression cant be both extern and export");
    self->decl_flags = decl_flag;
    self->type.type = VARIABLE_TYPE_NONE;
    self->type.value.variable = NULL;
    self->var_name = var_name;
    self->rhs_expr = NULL;
}

void assignmentexpr_init(AssignmentExpr *self, Ast *lhs_expr, Ast *rhs_expr) {
    self->lhs_expr = lhs_expr;
    self->rhs_expr = rhs_expr;
}

void import_init(Import *self, BufferView path) {
    self->path = path;
    self->file_scope = NULL;
}

int string_init(String *self, BufferView str) {
    /* TODO: Convert special characters. For example \n should be converted to binary newline etc */
    self->str = str;
    return 0;
}

void number_init(Number *self, i64 value, bool is_integer, BufferView code_ref) {
    self->value.integer = value;
    self->is_integer = is_integer;
    self->code_ref = code_ref;
}

void variable_init(Variable *self, BufferView name) {
    self->name = name;
    self->resolved_var = NULL;
}

void binop_init(Binop *self) {
    self->lhs = NULL;
    self->rhs = NULL;
    self->type = BINOP_ADD;
    self->grouped = bool_false;
}

int if_statement_init(IfStatement *self, Scope *parent, ArenaAllocator *allocator) {
    self->condition = NULL;
    self->else_if_stmt = NULL;
    return scope_init(&self->body, parent, allocator);
}

int else_if_statement_init(ElseIfStatement *self, Scope *parent, ArenaAllocator *allocator) {
    self->condition = NULL;
    self->next_else_if_stmt = NULL;
    return scope_init(&self->body, parent, allocator);
}

int while_statement_init(WhileStatement *self, Scope *parent, ArenaAllocator *allocator) {
    self->condition = NULL;
    return scope_init(&self->body, parent, allocator);
}

int scope_init(Scope *self, Scope *parent, ArenaAllocator *allocator) {
    return_if_error(buffer_init(&self->ast_objects, allocator));
    return_if_error(hash_map_init(&self->named_objects, allocator, sizeof(Ast*), hash_map_compare_string, amal_hash_string));
    self->parent = parent;
    self->parser = NULL;
    return 0;
}

int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ArenaAllocator *allocator) {
    char null_terminator;
    null_terminator = '\0';
    self->parser = NULL;

    return_if_error(buffer_init(&self->canonical_path, allocator));
    return_if_error(buffer_append(&self->canonical_path, canonical_path.data, canonical_path.size));
    return_if_error(buffer_append(&self->canonical_path, &null_terminator, 1));
    /* To exclude null-terminator character from size but not from data */
    self->canonical_path.size -= 1;
    return 0;
}

int scope_add_child(Scope *self, Ast *child) {
    Ast *existing_child;
    bool child_already_exists;

    /* TODO: Implement for parameter */
    if(child->type == AST_LHS) {
        BufferView var_name;
        var_name = child->value.lhs_expr->var_name;
        assert(var_name.data);
        child_already_exists = hash_map_get(&self->named_objects, var_name, &existing_child);
        if(child_already_exists)
            return AST_ERR_DEF_DUP;

        cleanup_if_error(hash_map_insert(&self->named_objects, var_name, &child));
    }
    cleanup_if_error(buffer_append(&self->ast_objects, &child, sizeof(Ast*)));
    return 0;

    cleanup:
    return AST_ERR;
}

void scope_resolve(Scope *self, AstCompilerContext *context) {
    Ast **ast;
    Ast **ast_end;

    ast = buffer_begin(&self->ast_objects);
    ast_end = buffer_end(&self->ast_objects);
    context->scope = self;
    for(; ast != ast_end; ++ast) {
        ast_resolve(*ast, context);
    }
    context->scope = self->parent;
}

static Parser* scope_get_parser(Scope *scope) {
    while(scope) {
        if(scope->parser)
            return scope->parser;
        scope = scope->parent;
    }
    return NULL;
}

static void parser_print_error(Parser *parser, const char *ref, const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    if(parser) {
        tokenizer_print_error_args(&parser->tokenizer,
                              tokenizer_get_code_reference_index(&parser->tokenizer, ref),
                              fmt, args);
    } else {
        /* TODO: Redirect error to compiler error callback if set */
        amal_log_error("TODO: Fix this:");
        amal_log_error(fmt, args);
    }
    va_end(args);
}

static Ast* __scope_get_resolved_variable(Scope *self, Scope *start, AstCompilerContext *context, BufferView name) {
    Ast *result;
    bool exists;
    Scope *prev_scope;

    assert(self);
    exists = hash_map_get(&self->named_objects, name, &result);
    if(!exists) {
        Parser *parser;
        if(self->parent)
            return __scope_get_resolved_variable(self->parent, start, context, name);

        parser = scope_get_parser(start);
        parser_print_error(parser, name.data, "Undefined reference to variable \"%.*s\"", name.size, name.data);
        throw(AST_ERR);
    }

    /*
    Need to change scope here because we are changing the visible scope
    and the ast object may be in another scope than the current
    resolving ast.
    */
    prev_scope = context->scope;
    context->scope = self;
    ast_resolve(result, context);
    context->scope = prev_scope;

    assert(result->type == AST_LHS);
    return result;
}

static Ast* scope_get_resolved_variable(Scope *self, AstCompilerContext *context, BufferView name) {
    return __scope_get_resolved_variable(self, self, context, name);
}

static void function_signature_resolve(FunctionSignature *self, AstCompilerContext *context) {
    /* TODO: Implement */
    if(self->resolved)
        return;
    self->resolved = bool_true;
    (void)self;
    (void)context;
}

static void variable_resolve(Variable *self, AstCompilerContext *context, AstResolveData *resolve_data) {
    if(!self->resolved_var)
        self->resolved_var = scope_get_resolved_variable(context->scope, context, self->name);
    resolve_data->type = self->resolved_var->resolve_data.type;
}

static LhsExpr* variable_get_resolved_type(Variable *self) {
    assert(self->resolved_var);
    return self->resolved_var->value.lhs_expr;
}

static void variable_type_resolve(LhsExpr *self, AstCompilerContext *context, AstResolveData *resolve_data) {
    VariableType *lhs_expr_type;
    lhs_expr_type = &self->type;
    switch(lhs_expr_type->type) {
        case VARIABLE_TYPE_NONE:
            return;
        case VARIABLE_TYPE_VARIABLE:
            variable_resolve(lhs_expr_type->value.variable, context, resolve_data);
            break;
        case VARIABLE_TYPE_SIGNATURE:
            function_signature_resolve(lhs_expr_type->value.signature, context);
            resolve_data->type = self;
            break;
    }
}

static BufferView variable_type_get_name(VariableType *self) {
    BufferView result;
    switch(self->type) {
        case VARIABLE_TYPE_NONE:
            result.data = NULL;
            result.size = 0;
            break;
        case VARIABLE_TYPE_VARIABLE:
            result = self->value.variable->name;
            break;
        case VARIABLE_TYPE_SIGNATURE:
            /* TODO: Save reference to signature in code in the FunctionSignature type */
            result.data = "fn()";
            result.size = 4;
            break;
    }
    return result;
}

static LhsExpr* lhsexpr_resolve_rhs(LhsExpr *self, AstCompilerContext *context) {
    LhsExpr *rhs_resolved_type;
    ast_resolve(self->rhs_expr, context);
    if(ast_is_decl(self->rhs_expr))
        rhs_resolved_type = self;
    else
        rhs_resolved_type = self->rhs_expr->resolve_data.type;
    return rhs_resolved_type;
}

static void lhsexpr_resolve(Ast *ast, AstCompilerContext *context) {
    LhsExpr *self;

    assert(ast->type == AST_LHS);
    self = ast->value.lhs_expr;

    variable_type_resolve(self, context, &ast->resolve_data);

    /*
    TODO: When parameters and return types are implemented, AST_RESOLVE_END should be set after
    the parameters and return types have been resolved as recursive function calls should
    be allowed but recursive function calls still require parameters and return types to be known.
    */
    if(self->rhs_expr) {
        LhsExpr *rhs_resolve_type;
        if(self->rhs_expr->type == AST_FUNCTION_DECL) {
            /*
                The function declaration itself always resolves the signature, but we also do it here because we
                want to have the signature solved before setting the lhs expr as solved. Also function signatures can exist
                without lhs expr (anonymous function).
            */
            function_signature_resolve(self->rhs_expr->value.func_decl->signature, context);
            ast->resolve_data.status = AST_RESOLVED;
            /*
                If rhs is a function declaration then there is no need to wait until it has been resolved before setting the type as the type
                is @self (the lhs). We still need to continue after this, so rhs can be resolved.
            */
            if(!ast->resolve_data.type)
                ast->resolve_data.type = self;
        }

        rhs_resolve_type = lhsexpr_resolve_rhs(self, context);

        /* TODO: Add casting */
        if(ast->resolve_data.type && ast->resolve_data.type != rhs_resolve_type) {
            Parser *parser;
            parser = scope_get_parser(context->scope);
            /* 
                TODO: Instead of using self->var_name, using type name. This cant be done right now because
                type can be function signature.
            */
            parser_print_error(parser, self->var_name.data,
                "Variable type and variable assignment type (right-hand side) do not match");
            throw(AST_ERR);
        }
        ast->resolve_data.type = rhs_resolve_type;
    }
}

static LhsExpr* binop_get_lhs_expr(Binop *self) {
    if(self->rhs) {
        if(self->rhs->type == AST_LHS)
            return self->rhs->value.lhs_expr;
        else if(self->rhs->type == AST_BINOP)
            return binop_get_lhs_expr(self->rhs->value.binop);
    } else {
        if(self->lhs->type == AST_LHS)
            return self->lhs->value.lhs_expr;
        else if(self->lhs->type == AST_BINOP)
            return binop_get_lhs_expr(self->lhs->value.binop);
    }
    return NULL;
}

static void assignmentexpr_resolve(Ast *ast, AstCompilerContext *context) {
    AssignmentExpr *self;
    LhsExpr *lhs_source;

    assert(ast->type == AST_ASSIGN);
    self = ast->value.assign_expr;
    lhs_source = NULL;

    ast_resolve(self->lhs_expr, context);
    ast_resolve(self->rhs_expr, context);

    if(self->lhs_expr->type == AST_VARIABLE)
        lhs_source = variable_get_resolved_type(self->lhs_expr->value.variable);
    else if(self->lhs_expr->type == AST_BINOP)
        lhs_source = binop_get_lhs_expr(self->lhs_expr->value.binop);

    /* This also covers extern variables, since extern variables are always const */
    /* TODO: var.field type expressions should also be checked */
    if(lhs_source && LHS_EXPR_IS_CONST(lhs_source)) {
        Parser *parser;
        parser = scope_get_parser(context->scope);
        parser_print_error(parser, ast_get_code_reference(self->lhs_expr).data, "Can't assign to a const value");
        throw(AST_ERR);
    }

    /* TODO: Add casting */
    if(self->lhs_expr->resolve_data.type != self->rhs_expr->resolve_data.type) {
        Parser *parser;
        BufferView rhs_type_name;
        BufferView lhs_type_name;

        parser = scope_get_parser(context->scope);
        rhs_type_name = variable_type_get_name(&self->rhs_expr->resolve_data.type->type);
        lhs_type_name = variable_type_get_name(&self->lhs_expr->resolve_data.type->type);
        /* 
            TODO: Instead of using self->var_name, using type name. This cant be done right now because
            type can be function signature.
        */
        parser_print_error(parser, ast_get_code_reference(self->lhs_expr).data,
            "Can't cast data of type %.*s to type %.*s", rhs_type_name.size, rhs_type_name.data, lhs_type_name.size, lhs_type_name.data);
        throw(AST_ERR);
    }

    ast->resolve_data.type = self->lhs_expr->resolve_data.type;
}

static void import_resolve(Ast *ast, AstCompilerContext *context) {
    Import *self;
    assert(ast->type == AST_IMPORT);
    (void)context;
    self = ast->value.import;
    ast->resolve_data.type = &self->file_scope->parser->file_decl;
    assert(ast->resolve_data.type);
}

static Scope* lhsexpr_get_scope(LhsExpr *self) {
    AstValue value;
    if(self->rhs_expr) {
        value = self->rhs_expr->value;
        switch(self->rhs_expr->type) {
            case AST_FUNCTION_DECL:
                return &value.func_decl->body;
            case AST_STRUCT_DECL:
                return &value.struct_decl->body;
            case AST_IMPORT:
                return &value.import->file_scope->parser->struct_decl.body;
            default:
                break;
        }
    }
    assert(bool_false && "Expected lhsexpr_get_scope to only be called for non-extern function declaration, struct declaration and import");
    return NULL;
}

/* @self has to have a resolved type before calling this */
static Parser* get_resolved_type_parser(Ast *self) {
    assert(self->resolve_data.type);
    return scope_get_parser(lhsexpr_get_scope(self->resolve_data.type));
}

static void funcdecl_resolve(FunctionDecl *self, AstCompilerContext *context) {
    /* TODO: Implement parameters and return types */
    function_signature_resolve(self->signature, context);
    scope_resolve(&self->body, context);
}

/*
    Dont need to check if @self is resolved, since it will always be partially resolved when called from @funccall_resolve.
    Meaning the resolve status wont be set to solved but the resolve type will be set.
*/
bool resolved_type_is_func_decl(Ast *self) {
    const LhsExpr *resolved_type = self->resolve_data.type;
    return (resolved_type->rhs_expr && resolved_type->rhs_expr->type == AST_FUNCTION_DECL) ||
            resolved_type->type.type == VARIABLE_TYPE_SIGNATURE;
}

static void funccall_resolve(Ast *self, AstCompilerContext *context) {
    FunctionCall *func_call;
    Ast **ast;
    Ast **ast_end;

    func_call = self->value.func_call;
    variable_resolve(&func_call->func, context, &self->resolve_data);
    /* Attemping to use call syntax (variable_name ( ) ) with a variable that is not a function */
    if(!resolved_type_is_func_decl(self)) {
        Parser *caller_parser;
        Parser *callee_parser;
        BufferView callee_code_ref;

        caller_parser = scope_get_parser(context->scope);
        callee_parser = get_resolved_type_parser(self);
        callee_code_ref = self->resolve_data.type->var_name;

        parser_print_error(caller_parser, func_call->func.name.data,
            "\"%.*s\" is not a function. Only functions can be called", func_call->func.name.size, func_call->func.name.data);
        /* TODO: use tokenizer_print_note, once it has been added */
        /* TODO: Print type */
        parser_print_error(callee_parser, callee_code_ref.data, "Type was declared here");
        throw(AST_ERR);
    }

    ast = buffer_begin(&func_call->args);
    ast_end = buffer_end(&func_call->args);
    for(; ast != ast_end; ++ast) {
        ast_resolve(*ast, context);
    }
}

static void structdecl_resolve(Ast *self, AstCompilerContext *context) {
    StructDecl *struct_decl;
    struct_decl = self->value.struct_decl;
    scope_resolve(&struct_decl->body, context);
}

static void structfield_resolve(Ast *self, AstCompilerContext *context) {
    /* TODO: Implement */
    StructField *struct_field;
    struct_field = self->value.struct_field;
    variable_resolve(&struct_field->type, context, &self->resolve_data);
}

static bool is_struct_decl(Ast *self) {
    const LhsExpr *resolved_type = self->resolve_data.type;
    assert(self->resolve_data.status == AST_RESOLVED);
    return resolved_type->rhs_expr && resolved_type->rhs_expr->type == AST_STRUCT_DECL;
}

static void binop_resolve_dot_access(Ast *ast, AstCompilerContext *context) {
    Binop *self;
    Scope *lhs_scope;
    Parser *caller_parser;
    Parser *callee_parser;
    BufferView caller_code_ref;
    BufferView callee_code_ref;

    assert(ast->type == AST_BINOP);
    self = ast->value.binop;
    caller_parser = scope_get_parser(context->scope);

    if(self->lhs->type != AST_VARIABLE) {
        /* TODO: Allow field access for numbers and string as well */
        BufferView code_ref;
        code_ref = ast_get_code_reference(self->lhs);
        parser_print_error(caller_parser, code_ref.data, "Accessing fields is only applicable for variables");
        throw(AST_ERR);
    }
    
    lhs_scope = lhsexpr_get_scope(self->lhs->resolve_data.type);
    self->rhs->resolve_data.type = scope_get_resolved_variable(lhs_scope, context, self->rhs->value.variable->name)->resolve_data.type;

    callee_parser = get_resolved_type_parser(self->rhs);
    caller_code_ref = ast_get_code_reference(self->rhs);
    callee_code_ref = self->rhs->resolve_data.type->var_name;

    if(!is_struct_decl(self->lhs)) {
        parser_print_error(caller_parser, caller_code_ref.data, "Can only access field of structs");
        /* TODO: use tokenizer_print_note, once it has been added */
        /* TODO: Print type */
        parser_print_error(callee_parser, callee_code_ref.data, "Type was declared here");
        throw(AST_ERR);
    }

    if(!LHS_EXPR_IS_PUB(self->rhs->resolve_data.type)) {
        parser_print_error(caller_parser, caller_code_ref.data, "Can't access non-public field \"%.*s\"", caller_code_ref.size, caller_code_ref.data);
        /* TODO: use tokenizer_print_note, once it has been added */
        /* TODO: Print type */
        parser_print_error(callee_parser, callee_code_ref.data, "Type was declared non-public here");
        throw(AST_ERR);
    }
}

static void binop_resolve(Ast *ast, AstCompilerContext *context) {
    Binop *self;
    assert(ast->type == AST_BINOP);
    self = ast->value.binop;
    ast_resolve(self->lhs, context);
    if(self->type == BINOP_DOT && (self->rhs->type == AST_VARIABLE || self->rhs->type == AST_FUNCTION_CALL)) {
        binop_resolve_dot_access(ast, context);
        /* Only function call has extra data that needs to be resolved (args) */
        if(self->rhs->type == AST_FUNCTION_CALL) {
            Scope *prev_scope = context->scope;
            context->scope = lhsexpr_get_scope(self->lhs->resolve_data.type);
            ast_resolve(self->rhs, context);
            context->scope = prev_scope;
        }
        self->rhs->resolve_data.status = AST_RESOLVED;
        ast->resolve_data.type = self->rhs->resolve_data.type;
    } else {
        ast_resolve(self->rhs, context);
        /* TODO: Convert types that can be safely converted */
        assert(self->lhs->resolve_data.type);
        assert(self->rhs->resolve_data.type);
        if(self->rhs->resolve_data.type != self->lhs->resolve_data.type) {
            /*
            TODO: For this first error, only print the line without a reference to code.
            This requires change in tokenizer_print_error to be able to take a line as reference.

            TODO: Use note for the additional information instead of error.
            */
            Parser *parser;
            parser = scope_get_parser(context->scope);
            parser_print_error(parser, ast_get_code_reference(self->rhs).data,
                "Can't cast type \"%.*s\" to type \"%.*s\"",
                    self->rhs->resolve_data.type->var_name.size, self->rhs->resolve_data.type->var_name.data,
                    self->lhs->resolve_data.type->var_name.size, self->lhs->resolve_data.type->var_name.data);
            parser_print_error(parser, ast_get_code_reference(self->lhs).data,
                "Left-hand side is of type %.*s",
                    self->lhs->resolve_data.type->var_name.size, self->lhs->resolve_data.type->var_name.data);
            parser_print_error(parser, ast_get_code_reference(self->rhs).data,
                "Right-hand side is of type %.*s",
                    self->rhs->resolve_data.type->var_name.size, self->rhs->resolve_data.type->var_name.data);
            throw(AST_ERR);
        } else if(!is_arithmetic_type(self->lhs->resolve_data.type, context->compiler)) { /* TODO: Optimize this? store arithmetic type in the LhsExpr itself? */
            /* TODO: Point the error at the binop instead of LHS */
            Parser *parser;
            parser = scope_get_parser(context->scope);
            parser_print_error(parser, ast_get_code_reference(self->lhs).data,
                "Arithmetic operation can only be performed with the types i8, u8, i16, u16, i32, u32, i64, u64, isize and usize",
                    self->lhs->resolve_data.type->var_name.size, self->lhs->resolve_data.type->var_name.data);
            throw(AST_ERR);
        }
        ast->resolve_data.type = self->lhs->resolve_data.type;
    }
}

static void else_if_statement_resolve(ElseIfStatement *else_if_stmt, AstCompilerContext *context) {
    if(else_if_stmt->condition)
        ast_resolve(else_if_stmt->condition, context);

    scope_resolve(&else_if_stmt->body, context);
    if(else_if_stmt->next_else_if_stmt)
        else_if_statement_resolve(else_if_stmt->next_else_if_stmt, context);
}

static void if_statement_resolve(IfStatement *if_stmt, AstCompilerContext *context) {
    assert(if_stmt->condition);
    ast_resolve(if_stmt->condition, context);
    scope_resolve(&if_stmt->body, context);
    if(if_stmt->else_if_stmt)
        else_if_statement_resolve(if_stmt->else_if_stmt, context);
}

static void while_statement_resolve(WhileStatement *while_stmt, AstCompilerContext *context) {
    ast_resolve(while_stmt->condition, context);
    scope_resolve(&while_stmt->body, context);
}

void ast_resolve(Ast *self, AstCompilerContext *context) {
    assert(self);
    assert(context->parser);
    /* 
    TODO: Move these to the types that need checks for recursive dependency (function declaration, struct declaration)
    For function declaration, it should be marked as resolved when the signature has been resolved
    instead of the whole function declaration including the body
    because the body can have function call that calls functions that are resolving
    or even recursive function call, which should be allowed.
    */
    /*
    This check is outside lhs_expr mutex for optimization purpose as most times there wont be
    a race in multiple threads to resolve an AST expression.
    */
    if(self->resolve_data.status == AST_RESOLVED) {
        return;
    } else if(self->resolve_data.status == AST_RESOLVING && self->resolve_data.parser == context->parser) {
        Parser *parser;
        parser = scope_get_parser(context->scope);
        parser_print_error(parser, ast_get_code_reference(self).data, "Found recursive dependency");
        throw(AST_ERR);
    }

    self->resolve_data.status = AST_RESOLVING;
    self->resolve_data.parser = context->parser;
    switch(self->type) {
        case AST_NUMBER: {
            Number *number;
            number = self->value.number;
            /* TODO: Support other number types */
            if(number->is_integer)
                self->resolve_data.type = (LhsExpr*)context->compiler->default_types.i64;
            else
                self->resolve_data.type = (LhsExpr*)context->compiler->default_types.f64;
            break;
        }
        case AST_FUNCTION_DECL:
            funcdecl_resolve(self->value.func_decl, context);
            break;
        case AST_FUNCTION_CALL:
            funccall_resolve(self, context);
            break;
        case AST_STRUCT_DECL:
            structdecl_resolve(self, context);
            break;
        case AST_STRUCT_FIELD:
            structfield_resolve(self, context);
            break;
        case AST_LHS:
            lhsexpr_resolve(self, context);
            break;
        case AST_ASSIGN:
            assignmentexpr_resolve(self, context);
            break;
        case AST_IMPORT:
            /* TODO: When @import(...).data syntax is added, implement the resolve for it */
            import_resolve(self, context);
            break;
        case AST_STRING:
            /* TODO: Convert special combinations. For example \n to newline */
            self->resolve_data.type = (LhsExpr*)context->compiler->default_types.str;
            break;
        case AST_VARIABLE:
            variable_resolve(self->value.variable, context, &self->resolve_data);
            break;
        case AST_BINOP:
            binop_resolve(self, context);
            break;
        case AST_IF_STATEMENT:
            if_statement_resolve(self->value.if_stmt, context);
            break;
        case AST_WHILE_STATEMENT:
            while_statement_resolve(self->value.while_stmt, context);
            break;
    }
    /* TODO: See comment at the top of this function */
    self->resolve_data.status = AST_RESOLVED;
}