aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-26 16:06:45 +0200
committerBad Diode <bd@badd10de.dev>2024-06-26 16:06:45 +0200
commit8194ea8fb891ecda56e9efc0069a27ae37ba0a1b (patch)
treed79dc83958b93704c1ff926ec1392a23605adf29 /src/main.c
parent7eb82e159dc9d0b29d57410b067be941cc1bcbf7 (diff)
downloadbdl-8194ea8fb891ecda56e9efc0069a27ae37ba0a1b.tar.gz
bdl-8194ea8fb891ecda56e9efc0069a27ae37ba0a1b.zip
Further consolidate symbol names
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c223
1 files changed, 122 insertions, 101 deletions
diff --git a/src/main.c b/src/main.c
index d997245..45c5213 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,7 +28,8 @@ init(void) {
28 28
29typedef enum { 29typedef enum {
30 SYM_UNKNOWN, 30 SYM_UNKNOWN,
31 SYM_BUILTIN, 31 SYM_BUILTIN_FUN,
32 SYM_BUILTIN_TYPE,
32 SYM_FUN, 33 SYM_FUN,
33 SYM_VAR, 34 SYM_VAR,
34 SYM_PARAM, 35 SYM_PARAM,
@@ -40,7 +41,8 @@ typedef enum {
40 41
41Str sym_kind_str[] = { 42Str sym_kind_str[] = {
42 [SYM_UNKNOWN] = cstr("UNKNOWN "), 43 [SYM_UNKNOWN] = cstr("UNKNOWN "),
43 [SYM_BUILTIN] = cstr("BUILTIN "), 44 [SYM_BUILTIN_FUN] = cstr("BUILTIN FUN "),
45 [SYM_BUILTIN_TYPE] = cstr("BUILTIN TYPE "),
44 [SYM_FUN] = cstr("FUNCTION "), 46 [SYM_FUN] = cstr("FUNCTION "),
45 [SYM_VAR] = cstr("VARIABLE "), 47 [SYM_VAR] = cstr("VARIABLE "),
46 [SYM_PARAM] = cstr("PARAMETER "), 48 [SYM_PARAM] = cstr("PARAMETER "),
@@ -50,7 +52,10 @@ Str sym_kind_str[] = {
50 [SYM_STRUCT_FIELD] = cstr("STRUCT FIELD "), 52 [SYM_STRUCT_FIELD] = cstr("STRUCT FIELD "),
51}; 53};
52 54
53typedef Str Symbol; 55typedef struct Symbol {
56 Str name;
57 SymbolKind kind;
58} Symbol;
54 59
55typedef struct Fun { 60typedef struct Fun {
56 Str name; 61 Str name;
@@ -74,7 +79,7 @@ MAPDEF(FunMap, funmap, Str, Fun, str_hash, str_eq)
74MAPDEF(EnumMap, enummap, Str, Enum, str_hash, str_eq) 79MAPDEF(EnumMap, enummap, Str, Enum, str_hash, str_eq)
75MAPDEF(StructMap, structmap, Str, Struct, str_hash, str_eq) 80MAPDEF(StructMap, structmap, Str, Struct, str_hash, str_eq)
76 81
77typedef struct TypeScope { 82typedef struct Scope {
78 sz id; 83 sz id;
79 sz depth; 84 sz depth;
80 Str name; 85 Str name;
@@ -82,30 +87,30 @@ typedef struct TypeScope {
82 FunMap *funcs; 87 FunMap *funcs;
83 EnumMap *enums; 88 EnumMap *enums;
84 StructMap *structs; 89 StructMap *structs;
85 struct TypeScope *parent; 90 struct Scope *parent;
86} TypeScope; 91} Scope;
87 92
88typedef struct Analyzer { 93typedef struct Analyzer {
89 Arena *storage; 94 Arena *storage;
90 Str file_name; 95 Str file_name;
91 sz typescope_gen; 96 sz typescope_gen;
92 TypeScope **types; 97 Scope **scopes;
93 StrSet *numeric_types; 98 StrSet *numeric_types;
94 StrSet *integer_types; 99 StrSet *integer_types;
95} Analyzer; 100} Analyzer;
96 101
97TypeScope * 102Scope *
98typescope_alloc(Analyzer *a, TypeScope *parent) { 103typescope_alloc(Analyzer *a, Scope *parent) {
99 TypeScope *scope = arena_calloc(sizeof(TypeScope), a->storage); 104 Scope *scope = arena_calloc(sizeof(Scope), a->storage);
100 scope->parent = parent; 105 scope->parent = parent;
101 scope->id = a->typescope_gen++; 106 scope->id = a->typescope_gen++;
102 scope->depth = parent == NULL ? 0 : parent->depth + 1; 107 scope->depth = parent == NULL ? 0 : parent->depth + 1;
103 array_push(a->types, scope, a->storage); 108 array_push(a->scopes, scope, a->storage);
104 return scope; 109 return scope;
105} 110}
106 111
107SymbolMap * 112SymbolMap *
108find_type(TypeScope *scope, Str type) { 113find_type(Scope *scope, Str type) {
109 while (scope != NULL) { 114 while (scope != NULL) {
110 SymbolMap *val = symmap_lookup(&scope->symbols, type); 115 SymbolMap *val = symmap_lookup(&scope->symbols, type);
111 if (val != NULL) { 116 if (val != NULL) {
@@ -117,7 +122,7 @@ find_type(TypeScope *scope, Str type) {
117} 122}
118 123
119FunMap * 124FunMap *
120find_fun(TypeScope *scope, Str type) { 125find_fun(Scope *scope, Str type) {
121 while (scope != NULL) { 126 while (scope != NULL) {
122 FunMap *val = funmap_lookup(&scope->funcs, type); 127 FunMap *val = funmap_lookup(&scope->funcs, type);
123 if (val != NULL) { 128 if (val != NULL) {
@@ -130,11 +135,11 @@ find_fun(TypeScope *scope, Str type) {
130 135
131typedef struct FindEnumResult { 136typedef struct FindEnumResult {
132 EnumMap *map; 137 EnumMap *map;
133 TypeScope *scope; 138 Scope *scope;
134} FindEnumResult; 139} FindEnumResult;
135 140
136FindEnumResult 141FindEnumResult
137find_enum(TypeScope *scope, Str type) { 142find_enum(Scope *scope, Str type) {
138 while (scope != NULL) { 143 while (scope != NULL) {
139 EnumMap *val = enummap_lookup(&scope->enums, type); 144 EnumMap *val = enummap_lookup(&scope->enums, type);
140 if (val != NULL) { 145 if (val != NULL) {
@@ -147,11 +152,11 @@ find_enum(TypeScope *scope, Str type) {
147 152
148typedef struct FindStructResult { 153typedef struct FindStructResult {
149 StructMap *map; 154 StructMap *map;
150 TypeScope *scope; 155 Scope *scope;
151} FindStructResult; 156} FindStructResult;
152 157
153FindStructResult 158FindStructResult
154find_struct(TypeScope *scope, Str type) { 159find_struct(Scope *scope, Str type) {
155 while (scope != NULL) { 160 while (scope != NULL) {
156 StructMap *val = structmap_lookup(&scope->structs, type); 161 StructMap *val = structmap_lookup(&scope->structs, type);
157 if (val != NULL) { 162 if (val != NULL) {
@@ -163,7 +168,7 @@ find_struct(TypeScope *scope, Str type) {
163} 168}
164 169
165void 170void
166graph_typescope(TypeScope *scope, Arena a) { 171graph_typescope(Scope *scope, Arena a) {
167 if (!scope->symbols) { 172 if (!scope->symbols) {
168 return; 173 return;
169 } 174 }
@@ -202,7 +207,7 @@ graph_typescope(TypeScope *scope, Arena a) {
202} 207}
203 208
204void 209void
205graph_functions(TypeScope *scope, Arena a) { 210graph_functions(Scope *scope, Arena a) {
206 if (!scope->funcs) { 211 if (!scope->funcs) {
207 return; 212 return;
208 } 213 }
@@ -244,7 +249,7 @@ graph_functions(TypeScope *scope, Arena a) {
244} 249}
245 250
246void 251void
247graph_types(TypeScope **scopes, Arena a) { 252graph_types(Scope **scopes, Arena a) {
248 if (scopes == NULL) return; 253 if (scopes == NULL) return;
249 println("digraph types {"); 254 println("digraph types {");
250 println("rankdir=LR;"); 255 println("rankdir=LR;");
@@ -253,7 +258,7 @@ graph_types(TypeScope **scopes, Arena a) {
253 println("overlap=scale;"); 258 println("overlap=scale;");
254 println("bgcolor=\"transparent\";"); 259 println("bgcolor=\"transparent\";");
255 for (sz i = 0; i < array_size(scopes); i++) { 260 for (sz i = 0; i < array_size(scopes); i++) {
256 TypeScope *scope = scopes[i]; 261 Scope *scope = scopes[i];
257 if (!scope) { 262 if (!scope) {
258 continue; 263 continue;
259 } 264 }
@@ -270,10 +275,10 @@ emit_semantic_error(Analyzer *a, Node *n, Str msg) {
270 eprintln("%s:%d:%d: error: %s", a->file_name, n->line, n->col, msg); 275 eprintln("%s:%d:%d: error: %s", a->file_name, n->line, n->col, msg);
271} 276}
272 277
273Symbol type_inference(Analyzer *a, Node *node, TypeScope *scope); 278Str type_inference(Analyzer *a, Node *node, Scope *scope);
274 279
275void 280void
276typecheck_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) { 281typecheck_field(Analyzer *a, Node *node, Scope *scope, Str symbol) {
277 if (node->field_type->kind == NODE_COMPOUND_TYPE) { 282 if (node->field_type->kind == NODE_COMPOUND_TYPE) {
278 Str field_name = str_concat(symbol, cstr("."), a->storage); 283 Str field_name = str_concat(symbol, cstr("."), a->storage);
279 field_name = str_concat(field_name, node->value.str, a->storage); 284 field_name = str_concat(field_name, node->value.str, a->storage);
@@ -303,7 +308,7 @@ typecheck_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) {
303 a->file_name, node->line, node->col, field_name); 308 a->file_name, node->line, node->col, field_name);
304 } 309 }
305 if (node->field_val) { 310 if (node->field_val) {
306 Symbol type = type_inference(a, node->field_val, scope); 311 Str type = type_inference(a, node->field_val, scope);
307 if (!str_eq(type, field_type)) { 312 if (!str_eq(type, field_type)) {
308 eprintln( 313 eprintln(
309 "%s:%d:%d: error: mismatched types in struct " 314 "%s:%d:%d: error: mismatched types in struct "
@@ -325,7 +330,7 @@ typecheck_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) {
325} 330}
326 331
327void 332void
328typecheck_lit_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) { 333typecheck_lit_field(Analyzer *a, Node *node, Scope *scope, Str symbol) {
329 if (node->field_val->kind == NODE_COMPOUND_TYPE) { 334 if (node->field_val->kind == NODE_COMPOUND_TYPE) {
330 Str type = cstr("\\{ "); 335 Str type = cstr("\\{ ");
331 for (sz i = 0; i < array_size(node->field_val->elements); i++) { 336 for (sz i = 0; i < array_size(node->field_val->elements); i++) {
@@ -346,7 +351,7 @@ typecheck_lit_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) {
346 return; 351 return;
347 } 352 }
348 Str field_type = s->val.type; 353 Str field_type = s->val.type;
349 Symbol type = type_inference(a, node->field_val, scope); 354 Str type = type_inference(a, node->field_val, scope);
350 if (!str_eq(type, field_type)) { 355 if (!str_eq(type, field_type)) {
351 eprintln( 356 eprintln(
352 "%s:%d:%d: error: mismatched types in struct " 357 "%s:%d:%d: error: mismatched types in struct "
@@ -430,8 +435,8 @@ typecheck_returns(Analyzer *a, Node *node, Str expected) {
430 } 435 }
431} 436}
432 437
433Symbol 438Str
434type_inference(Analyzer *a, Node *node, TypeScope *scope) { 439type_inference(Analyzer *a, Node *node, Scope *scope) {
435 assert(a); 440 assert(a);
436 assert(scope); 441 assert(scope);
437 if (!node) { 442 if (!node) {
@@ -460,7 +465,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
460 return cstr(""); 465 return cstr("");
461 } 466 }
462 if (node->var_val) { 467 if (node->var_val) {
463 Symbol type = type_inference(a, node->var_val, scope); 468 Str type = type_inference(a, node->var_val, scope);
464 if (!type.size) { 469 if (!type.size) {
465 eprintln( 470 eprintln(
466 "%s:%d:%d: error: can't bind `nil` to variable " 471 "%s:%d:%d: error: can't bind `nil` to variable "
@@ -490,16 +495,18 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
490 } 495 }
491 496
492 // We don't know the type for this symbol, perform inference. 497 // We don't know the type for this symbol, perform inference.
493 Symbol type = type_inference(a, node->var_val, scope); 498 Str type = type_inference(a, node->var_val, scope);
494 if (type.size) { 499 if (type.size) {
495 symmap_insert(&scope->symbols, symbol, type, a->storage); 500 symmap_insert(&scope->symbols, symbol,
501 (Symbol){.name = type, .kind = SYM_VAR},
502 a->storage);
496 node->var_name->type = type; 503 node->var_name->type = type;
497 } 504 }
498 return node->type; 505 return node->type;
499 } break; 506 } break;
500 case NODE_SET: { 507 case NODE_SET: {
501 Symbol name = type_inference(a, node->var_name, scope); 508 Str name = type_inference(a, node->var_name, scope);
502 Symbol val = type_inference(a, node->var_val, scope); 509 Str val = type_inference(a, node->var_val, scope);
503 if (!str_eq(name, val)) { 510 if (!str_eq(name, val)) {
504 eprintln( 511 eprintln(
505 "%s:%d:%d: error: type mismatch, trying to assing " 512 "%s:%d:%d: error: type mismatch, trying to assing "
@@ -527,7 +534,9 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
527 Node *field = node->struct_field[i]; 534 Node *field = node->struct_field[i];
528 typecheck_field(a, field, scope, symbol); 535 typecheck_field(a, field, scope, symbol);
529 } 536 }
530 symmap_insert(&scope->symbols, symbol, symbol, a->storage); 537 symmap_insert(&scope->symbols, symbol,
538 (Symbol){.name = symbol, .kind = SYM_STRUCT},
539 a->storage);
531 return node->type; 540 return node->type;
532 } break; 541 } break;
533 case NODE_ENUM: { 542 case NODE_ENUM: {
@@ -556,7 +565,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
556 a->file_name, field->line, field->col, field_name); 565 a->file_name, field->line, field->col, field_name);
557 } 566 }
558 if (field->field_val) { 567 if (field->field_val) {
559 Symbol type = type_inference(a, field->field_val, scope); 568 Str type = type_inference(a, field->field_val, scope);
560 if (!str_eq(type, cstr("int"))) { 569 if (!str_eq(type, cstr("int"))) {
561 eprintln( 570 eprintln(
562 "%s:%d:%d: error: non int enum value for '%s.%s'", 571 "%s:%d:%d: error: non int enum value for '%s.%s'",
@@ -568,11 +577,13 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
568 (Enum){.name = field_name}, a->storage); 577 (Enum){.name = field_name}, a->storage);
569 field->type = cstr("int"); 578 field->type = cstr("int");
570 } 579 }
571 symmap_insert(&scope->symbols, symbol, symbol, a->storage); 580 symmap_insert(&scope->symbols, symbol,
581 (Symbol){.name = symbol, .kind = SYM_ENUM},
582 a->storage);
572 return node->type; 583 return node->type;
573 } break; 584 } break;
574 case NODE_IF: { 585 case NODE_IF: {
575 Symbol cond_type = type_inference(a, node->cond_if, scope); 586 Str cond_type = type_inference(a, node->cond_if, scope);
576 if (!str_eq(cond_type, cstr("bool"))) { 587 if (!str_eq(cond_type, cstr("bool"))) {
577 emit_semantic_error( 588 emit_semantic_error(
578 a, node->cond_if, 589 a, node->cond_if,
@@ -582,15 +593,15 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
582 if (node->cond_expr->kind == NODE_BLOCK) { 593 if (node->cond_expr->kind == NODE_BLOCK) {
583 node->type = type_inference(a, node->cond_expr, scope); 594 node->type = type_inference(a, node->cond_expr, scope);
584 } else { 595 } else {
585 TypeScope *next = typescope_alloc(a, scope); 596 Scope *next = typescope_alloc(a, scope);
586 node->type = type_inference(a, node->cond_expr, next); 597 node->type = type_inference(a, node->cond_expr, next);
587 } 598 }
588 if (node->cond_else) { 599 if (node->cond_else) {
589 Symbol else_type; 600 Str else_type;
590 if (node->cond_else->kind == NODE_BLOCK) { 601 if (node->cond_else->kind == NODE_BLOCK) {
591 else_type = type_inference(a, node->cond_else, scope); 602 else_type = type_inference(a, node->cond_else, scope);
592 } else { 603 } else {
593 TypeScope *next = typescope_alloc(a, scope); 604 Scope *next = typescope_alloc(a, scope);
594 else_type = type_inference(a, node->cond_else, next); 605 else_type = type_inference(a, node->cond_else, next);
595 } 606 }
596 if (!str_eq(node->type, else_type)) { 607 if (!str_eq(node->type, else_type)) {
@@ -602,7 +613,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
602 return node->type; 613 return node->type;
603 } break; 614 } break;
604 case NODE_WHILE: { 615 case NODE_WHILE: {
605 Symbol cond_type = type_inference(a, node->while_cond, scope); 616 Str cond_type = type_inference(a, node->while_cond, scope);
606 if (!str_eq(cond_type, cstr("bool"))) { 617 if (!str_eq(cond_type, cstr("bool"))) {
607 emit_semantic_error( 618 emit_semantic_error(
608 a, node->cond_if, 619 a, node->cond_if,
@@ -621,10 +632,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
621 if (node->match_expr) { 632 if (node->match_expr) {
622 type_inference(a, node->match_expr, scope); 633 type_inference(a, node->match_expr, scope);
623 } 634 }
624 Symbol previous = cstr(""); 635 Str previous = cstr("");
625 for (sz i = 0; i < array_size(node->match_cases); i++) { 636 for (sz i = 0; i < array_size(node->match_cases); i++) {
626 Node *expr = node->match_cases[i]; 637 Node *expr = node->match_cases[i];
627 Symbol next = type_inference(a, expr, scope); 638 Str next = type_inference(a, expr, scope);
628 if (i != 0 && !str_eq(next, previous)) { 639 if (i != 0 && !str_eq(next, previous)) {
629 emit_semantic_error( 640 emit_semantic_error(
630 a, node, 641 a, node,
@@ -649,7 +660,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
649 } break; 660 } break;
650 case NODE_CASE_COND: { 661 case NODE_CASE_COND: {
651 if (node->case_value) { 662 if (node->case_value) {
652 Symbol cond = type_inference(a, node->case_value, scope); 663 Str cond = type_inference(a, node->case_value, scope);
653 if (!str_eq(cond, cstr("bool"))) { 664 if (!str_eq(cond, cstr("bool"))) {
654 emit_semantic_error(a, node, 665 emit_semantic_error(a, node,
655 cstr("non-boolean case condition")); 666 cstr("non-boolean case condition"));
@@ -673,14 +684,14 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
673 case NODE_NOT: 684 case NODE_NOT:
674 case NODE_AND: 685 case NODE_AND:
675 case NODE_OR: { 686 case NODE_OR: {
676 Symbol left = type_inference(a, node->left, scope); 687 Str left = type_inference(a, node->left, scope);
677 if (!str_eq(left, cstr("bool"))) { 688 if (!str_eq(left, cstr("bool"))) {
678 emit_semantic_error(a, node, 689 emit_semantic_error(a, node,
679 cstr("expected bool on logic expression")); 690 cstr("expected bool on logic expression"));
680 return cstr(""); 691 return cstr("");
681 } 692 }
682 if (node->right) { 693 if (node->right) {
683 Symbol right = type_inference(a, node->right, scope); 694 Str right = type_inference(a, node->right, scope);
684 if (!str_eq(right, cstr("bool"))) { 695 if (!str_eq(right, cstr("bool"))) {
685 emit_semantic_error( 696 emit_semantic_error(
686 a, node, cstr("expected bool on logic expression")); 697 a, node, cstr("expected bool on logic expression"));
@@ -696,8 +707,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
696 case NODE_GT: 707 case NODE_GT:
697 case NODE_LE: 708 case NODE_LE:
698 case NODE_GE: { 709 case NODE_GE: {
699 Symbol left = type_inference(a, node->left, scope); 710 Str left = type_inference(a, node->left, scope);
700 Symbol right = type_inference(a, node->right, scope); 711 Str right = type_inference(a, node->right, scope);
701 if (!str_eq(left, right)) { 712 if (!str_eq(left, right)) {
702 emit_semantic_error( 713 emit_semantic_error(
703 a, node, cstr("mismatched types on binary expression")); 714 a, node, cstr("mismatched types on binary expression"));
@@ -707,7 +718,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
707 return node->type; 718 return node->type;
708 } break; 719 } break;
709 case NODE_BITNOT: { 720 case NODE_BITNOT: {
710 Symbol left = type_inference(a, node->left, scope); 721 Str left = type_inference(a, node->left, scope);
711 if (!strset_lookup(&a->integer_types, left)) { 722 if (!strset_lookup(&a->integer_types, left)) {
712 emit_semantic_error( 723 emit_semantic_error(
713 a, node, cstr("non integer type on bit twiddling expr")); 724 a, node, cstr("non integer type on bit twiddling expr"));
@@ -720,8 +731,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
720 case NODE_BITOR: 731 case NODE_BITOR:
721 case NODE_BITLSHIFT: 732 case NODE_BITLSHIFT:
722 case NODE_BITRSHIFT: { 733 case NODE_BITRSHIFT: {
723 Symbol left = type_inference(a, node->left, scope); 734 Str left = type_inference(a, node->left, scope);
724 Symbol right = type_inference(a, node->right, scope); 735 Str right = type_inference(a, node->right, scope);
725 if (!strset_lookup(&a->integer_types, left) || 736 if (!strset_lookup(&a->integer_types, left) ||
726 !strset_lookup(&a->integer_types, right)) { 737 !strset_lookup(&a->integer_types, right)) {
727 emit_semantic_error( 738 emit_semantic_error(
@@ -736,8 +747,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
736 case NODE_DIV: 747 case NODE_DIV:
737 case NODE_MUL: 748 case NODE_MUL:
738 case NODE_MOD: { 749 case NODE_MOD: {
739 Symbol left = type_inference(a, node->left, scope); 750 Str left = type_inference(a, node->left, scope);
740 Symbol right = type_inference(a, node->right, scope); 751 Str right = type_inference(a, node->right, scope);
741 if (!strset_lookup(&a->numeric_types, left) || 752 if (!strset_lookup(&a->numeric_types, left) ||
742 !strset_lookup(&a->numeric_types, right)) { 753 !strset_lookup(&a->numeric_types, right)) {
743 emit_semantic_error( 754 emit_semantic_error(
@@ -774,7 +785,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
774 emit_semantic_error(a, node, cstr("unknown type")); 785 emit_semantic_error(a, node, cstr("unknown type"));
775 return cstr(""); 786 return cstr("");
776 } 787 }
777 node->type = type->val; 788 node->type = type->val.name;
778 return node->type; 789 return node->type;
779 } break; 790 } break;
780 case NODE_SYMBOL_IDX: 791 case NODE_SYMBOL_IDX:
@@ -787,8 +798,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
787 return cstr(""); 798 return cstr("");
788 } 799 }
789 800
790 FindEnumResult e = find_enum(scope, type->val); 801 FindEnumResult e = find_enum(scope, type->val.name);
791 if (e.map && str_eq(symbol, type->val)) { 802 if (e.map && str_eq(symbol, type->val.name)) {
792 if (!node->next) { 803 if (!node->next) {
793 eprintln( 804 eprintln(
794 "%s:%d:%d: error: unspecified enum field for symbol " 805 "%s:%d:%d: error: unspecified enum field for symbol "
@@ -797,7 +808,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
797 return cstr(""); 808 return cstr("");
798 } 809 }
799 // Check if there is a next and it matches the enum field. 810 // Check if there is a next and it matches the enum field.
800 Str field = str_concat(type->val, cstr("."), a->storage); 811 Str field = str_concat(type->val.name, cstr("."), a->storage);
801 field = str_concat(field, node->next->value.str, a->storage); 812 field = str_concat(field, node->next->value.str, a->storage);
802 if (!enummap_lookup(&e.scope->enums, field)) { 813 if (!enummap_lookup(&e.scope->enums, field)) {
803 eprintln( 814 eprintln(
@@ -808,13 +819,13 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
808 return cstr(""); 819 return cstr("");
809 } 820 }
810 node->next->type = cstr("int"); 821 node->next->type = cstr("int");
811 node->type = type->val; 822 node->type = type->val.name;
812 return node->next->type; 823 return node->next->type;
813 } 824 }
814 825
815 FindStructResult s = find_struct(scope, type->val); 826 FindStructResult s = find_struct(scope, type->val.name);
816 if (s.map) { 827 if (s.map) {
817 if (str_eq(symbol, type->val)) { 828 if (str_eq(symbol, type->val.name)) {
818 eprintln( 829 eprintln(
819 "%s:%d:%d: error: struct incomplete struct literal " 830 "%s:%d:%d: error: struct incomplete struct literal "
820 "'%s', did you mean to use %s:{}?", 831 "'%s', did you mean to use %s:{}?",
@@ -823,7 +834,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
823 return cstr(""); 834 return cstr("");
824 } else { 835 } else {
825 if (node->next) { 836 if (node->next) {
826 Str chain = type->val; 837 Str chain = type->val.name;
827 Node *next = node; 838 Node *next = node;
828 while (next->next) { 839 while (next->next) {
829 next = next->next; 840 next = next->next;
@@ -844,7 +855,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
844 } 855 }
845 } 856 }
846 } 857 }
847 node->type = type->val; 858 node->type = type->val.name;
848 return node->type; 859 return node->type;
849 } break; 860 } break;
850 case NODE_STRUCT_LIT: { 861 case NODE_STRUCT_LIT: {
@@ -887,10 +898,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
887 return cstr(""); 898 return cstr("");
888 } 899 }
889 // Check that actual parameters typecheck 900 // Check that actual parameters typecheck
890 Symbol args = cstr(""); 901 Str args = cstr("");
891 for (sz i = 0; i < array_size(node->elements); i++) { 902 for (sz i = 0; i < array_size(node->elements); i++) {
892 Node *expr = node->elements[i]; 903 Node *expr = node->elements[i];
893 Symbol type = type_inference(a, expr, scope); 904 Str type = type_inference(a, expr, scope);
894 args = str_concat(args, type, a->storage); 905 args = str_concat(args, type, a->storage);
895 if (i != array_size(node->elements) - 1) { 906 if (i != array_size(node->elements) - 1) {
896 args = str_concat(args, cstr(","), a->storage); 907 args = str_concat(args, cstr(","), a->storage);
@@ -912,7 +923,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
912 } break; 923 } break;
913 case NODE_BLOCK: { 924 case NODE_BLOCK: {
914 scope = typescope_alloc(a, scope); 925 scope = typescope_alloc(a, scope);
915 Symbol type; 926 Str type;
916 for (sz i = 0; i < array_size(node->elements); i++) { 927 for (sz i = 0; i < array_size(node->elements); i++) {
917 Node *expr = node->elements[i]; 928 Node *expr = node->elements[i];
918 type = type_inference(a, expr, scope); 929 type = type_inference(a, expr, scope);
@@ -921,10 +932,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
921 return node->type; 932 return node->type;
922 } break; 933 } break;
923 case NODE_RETURN: { 934 case NODE_RETURN: {
924 Symbol ret_type = cstr(""); 935 Str ret_type = cstr("");
925 for (sz i = 0; i < array_size(node->elements); i++) { 936 for (sz i = 0; i < array_size(node->elements); i++) {
926 Node *expr = node->elements[i]; 937 Node *expr = node->elements[i];
927 Symbol type = type_inference(a, expr, scope); 938 Str type = type_inference(a, expr, scope);
928 ret_type = str_concat(ret_type, type, a->storage); 939 ret_type = str_concat(ret_type, type, a->storage);
929 if (i != array_size(node->elements) - 1) { 940 if (i != array_size(node->elements) - 1) {
930 ret_type = str_concat(ret_type, cstr(","), a->storage); 941 ret_type = str_concat(ret_type, cstr(","), a->storage);
@@ -938,17 +949,19 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
938 } break; 949 } break;
939 case NODE_FUN: { 950 case NODE_FUN: {
940 node->type = cstr("nil"); 951 node->type = cstr("nil");
941 TypeScope *prev_scope = scope; 952 Scope *prev_scope = scope;
942 scope = typescope_alloc(a, scope); 953 scope = typescope_alloc(a, scope);
943 Symbol param_type = cstr(""); 954 Str param_type = cstr("");
944 for (sz i = 0; i < array_size(node->func_params); i++) { 955 for (sz i = 0; i < array_size(node->func_params); i++) {
945 Node *param = node->func_params[i]; 956 Node *param = node->func_params[i];
946 Str symbol = param->param_name->value.str; 957 Str symbol = param->param_name->value.str;
947 Symbol type = param->param_type->value.str; 958 Str type = param->param_type->value.str;
948 param->param_name->type = 959 param->param_name->type =
949 type_inference(a, param->param_type, scope); 960 type_inference(a, param->param_type, scope);
950 param->type = param->param_name->type; 961 param->type = param->param_name->type;
951 symmap_insert(&scope->symbols, symbol, type, a->storage); 962 symmap_insert(&scope->symbols, symbol,
963 (Symbol){.name = type, .kind = SYM_PARAM},
964 a->storage);
952 param_type = str_concat(param_type, type, a->storage); 965 param_type = str_concat(param_type, type, a->storage);
953 if (i != array_size(node->func_params) - 1) { 966 if (i != array_size(node->func_params) - 1) {
954 param_type = str_concat(param_type, cstr(","), a->storage); 967 param_type = str_concat(param_type, cstr(","), a->storage);
@@ -959,10 +972,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
959 } 972 }
960 node->fun_params = param_type; 973 node->fun_params = param_type;
961 974
962 Symbol ret_type = cstr(""); 975 Str ret_type = cstr("");
963 for (sz i = 0; i < array_size(node->func_ret); i++) { 976 for (sz i = 0; i < array_size(node->func_ret); i++) {
964 Node *expr = node->func_ret[i]; 977 Node *expr = node->func_ret[i];
965 Symbol type = type_inference(a, expr, scope); 978 Str type = type_inference(a, expr, scope);
966 ret_type = str_concat(ret_type, type, a->storage); 979 ret_type = str_concat(ret_type, type, a->storage);
967 if (i != array_size(node->func_ret) - 1) { 980 if (i != array_size(node->func_ret) - 1) {
968 ret_type = str_concat(ret_type, cstr(","), a->storage); 981 ret_type = str_concat(ret_type, cstr(","), a->storage);
@@ -984,7 +997,9 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
984 symbol); 997 symbol);
985 return cstr(""); 998 return cstr("");
986 } 999 }
987 symmap_insert(&prev_scope->symbols, symbol, symbol, a->storage); 1000 symmap_insert(&scope->symbols, symbol,
1001 (Symbol){.name = symbol, .kind = SYM_FUN},
1002 a->storage);
988 } 1003 }
989 scope->name = symbol; 1004 scope->name = symbol;
990 funmap_insert(&prev_scope->funcs, symbol, 1005 funmap_insert(&prev_scope->funcs, symbol,
@@ -994,7 +1009,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
994 a->storage); 1009 a->storage);
995 1010
996 if (node->func_body->kind == NODE_BLOCK) { 1011 if (node->func_body->kind == NODE_BLOCK) {
997 Symbol type; 1012 Str type;
998 for (sz i = 0; i < array_size(node->func_body->elements); i++) { 1013 for (sz i = 0; i < array_size(node->func_body->elements); i++) {
999 Node *expr = node->func_body->elements[i]; 1014 Node *expr = node->func_body->elements[i];
1000 type = type_inference(a, expr, scope); 1015 type = type_inference(a, expr, scope);
@@ -1032,7 +1047,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
1032 1047
1033void 1048void
1034symbolic_analysis(Analyzer *a, Parser *parser) { 1049symbolic_analysis(Analyzer *a, Parser *parser) {
1035 TypeScope *types = typescope_alloc(a, NULL); 1050 Scope *scope = typescope_alloc(a, NULL);
1036 assert(a); 1051 assert(a);
1037 assert(parser); 1052 assert(parser);
1038 1053
@@ -1043,38 +1058,42 @@ symbolic_analysis(Analyzer *a, Parser *parser) {
1043 }; 1058 };
1044 for (sz i = 0; i < LEN(builtin_functions); i++) { 1059 for (sz i = 0; i < LEN(builtin_functions); i++) {
1045 Str symbol = builtin_functions[i]; 1060 Str symbol = builtin_functions[i];
1046 symmap_insert(&types->symbols, symbol, symbol, a->storage); 1061 symmap_insert(&scope->symbols, symbol,
1047 funmap_insert(&types->funcs, symbol, 1062 (Symbol){.name = symbol, .kind = SYM_BUILTIN_FUN},
1063 a->storage);
1064 funmap_insert(&scope->funcs, symbol,
1048 (Fun){.name = symbol, 1065 (Fun){.name = symbol,
1049 .param_type = cstr("..."), 1066 .param_type = cstr("..."),
1050 .return_type = cstr("nil")}, 1067 .return_type = cstr("nil")},
1051 a->storage); 1068 a->storage);
1052 } 1069 }
1053 Symbol builtin_types[] = { 1070 Str builtin_types[] = {
1054 cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), 1071 cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"),
1055 cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"), 1072 cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"),
1056 cstr("f32"), cstr("f64"), cstr("ptr"), cstr("int"), 1073 cstr("f32"), cstr("f64"), cstr("ptr"), cstr("int"),
1057 cstr("uint"), cstr("str"), cstr("bool"), cstr("nil")}; 1074 cstr("uint"), cstr("str"), cstr("bool"), cstr("nil")};
1058 for (sz i = 0; i < LEN(builtin_types); i++) { 1075 for (sz i = 0; i < LEN(builtin_types); i++) {
1059 Symbol type = builtin_types[i]; 1076 Str type = builtin_types[i];
1060 symmap_insert(&types->symbols, type, type, a->storage); 1077 symmap_insert(&scope->symbols, type,
1078 (Symbol){.name = type, .kind = SYM_BUILTIN_TYPE},
1079 a->storage);
1061 } 1080 }
1062 Symbol numeric_types[] = { 1081 Str numeric_types[] = {
1063 cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), cstr("u32"), 1082 cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), cstr("u32"),
1064 cstr("s32"), cstr("u64"), cstr("s64"), cstr("f32"), cstr("f64"), 1083 cstr("s32"), cstr("u64"), cstr("s64"), cstr("f32"), cstr("f64"),
1065 cstr("ptr"), cstr("int"), cstr("uint"), 1084 cstr("ptr"), cstr("int"), cstr("uint"),
1066 }; 1085 };
1067 for (sz i = 0; i < LEN(numeric_types); i++) { 1086 for (sz i = 0; i < LEN(numeric_types); i++) {
1068 Symbol type = numeric_types[i]; 1087 Str type = numeric_types[i];
1069 strset_insert(&a->numeric_types, type, a->storage); 1088 strset_insert(&a->numeric_types, type, a->storage);
1070 } 1089 }
1071 Symbol integer_types[] = { 1090 Str integer_types[] = {
1072 cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), 1091 cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"),
1073 cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"), 1092 cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"),
1074 cstr("ptr"), cstr("int"), cstr("uint"), 1093 cstr("ptr"), cstr("int"), cstr("uint"),
1075 }; 1094 };
1076 for (sz i = 0; i < LEN(integer_types); i++) { 1095 for (sz i = 0; i < LEN(integer_types); i++) {
1077 Symbol type = integer_types[i]; 1096 Str type = integer_types[i];
1078 strset_insert(&a->integer_types, type, a->storage); 1097 strset_insert(&a->integer_types, type, a->storage);
1079 } 1098 }
1080 // Find top level function declarations. 1099 // Find top level function declarations.
@@ -1082,7 +1101,7 @@ symbolic_analysis(Analyzer *a, Parser *parser) {
1082 Node *root = parser->nodes[i]; 1101 Node *root = parser->nodes[i];
1083 if (root->kind == NODE_FUN) { 1102 if (root->kind == NODE_FUN) {
1084 Str symbol = root->func_name->value.str; 1103 Str symbol = root->func_name->value.str;
1085 if (symmap_lookup(&types->symbols, symbol)) { 1104 if (symmap_lookup(&scope->symbols, symbol)) {
1086 eprintln( 1105 eprintln(
1087 "%s:%d:%d: error: function '%s' already defined in " 1106 "%s:%d:%d: error: function '%s' already defined in "
1088 "current " 1107 "current "
@@ -1090,13 +1109,15 @@ symbolic_analysis(Analyzer *a, Parser *parser) {
1090 a->file_name, root->var_name->line, root->var_name->col, 1109 a->file_name, root->var_name->line, root->var_name->col,
1091 symbol); 1110 symbol);
1092 } 1111 }
1093 symmap_insert(&types->symbols, symbol, symbol, a->storage); 1112 symmap_insert(&scope->symbols, symbol,
1113 (Symbol){.name = symbol, .kind = SYM_FUN},
1114 a->storage);
1094 } 1115 }
1095 } 1116 }
1096 // Recursively fill symbol tables. 1117 // Recursively fill symbol tables.
1097 for (sz i = 0; i < array_size(parser->nodes); i++) { 1118 for (sz i = 0; i < array_size(parser->nodes); i++) {
1098 Node *root = parser->nodes[i]; 1119 Node *root = parser->nodes[i];
1099 type_inference(a, root, types); 1120 type_inference(a, root, scope);
1100 } 1121 }
1101} 1122}
1102 1123
@@ -1174,7 +1195,7 @@ process_file(Str path) {
1174 // Printing symbol tables. 1195 // Printing symbol tables.
1175 if (mode == PRINT_SYMTABLES) { 1196 if (mode == PRINT_SYMTABLES) {
1176 // graph_symbols(analyzer.scopes, lexer_arena); 1197 // graph_symbols(analyzer.scopes, lexer_arena);
1177 graph_types(analyzer.types, lexer_arena); 1198 graph_types(analyzer.scopes, lexer_arena);
1178 } 1199 }
1179 if (mode == PRINT_SEMANTIC) { 1200 if (mode == PRINT_SEMANTIC) {
1180 graph_ast(parser.nodes); 1201 graph_ast(parser.nodes);
@@ -1183,9 +1204,9 @@ process_file(Str path) {
1183 1204
1184#if DEBUG == 1 1205#if DEBUG == 1
1185 println("========== enums =========="); 1206 println("========== enums ==========");
1186 for (sz i = 0; i < array_size(analyzer.types); i++) { 1207 for (sz i = 0; i < array_size(analyzer.scopes); i++) {
1187 Arena scratch = lexer_arena; 1208 Arena scratch = lexer_arena;
1188 TypeScope *scope = analyzer.types[i]; 1209 Scope *scope = analyzer.scopes[i];
1189 EnumMapIter iter = enummap_iterator(scope->enums, &scratch); 1210 EnumMapIter iter = enummap_iterator(scope->enums, &scratch);
1190 EnumMap *m = enummap_next(&iter, &scratch); 1211 EnumMap *m = enummap_next(&iter, &scratch);
1191 while (m) { 1212 while (m) {
@@ -1195,9 +1216,9 @@ process_file(Str path) {
1195 } 1216 }
1196 } 1217 }
1197 println("========= structs ========="); 1218 println("========= structs =========");
1198 for (sz i = 0; i < array_size(analyzer.types); i++) { 1219 for (sz i = 0; i < array_size(analyzer.scopes); i++) {
1199 Arena scratch = lexer_arena; 1220 Arena scratch = lexer_arena;
1200 TypeScope *scope = analyzer.types[i]; 1221 Scope *scope = analyzer.scopes[i];
1201 StructMapIter iter = structmap_iterator(scope->structs, &scratch); 1222 StructMapIter iter = structmap_iterator(scope->structs, &scratch);
1202 StructMap *m = structmap_next(&iter, &scratch); 1223 StructMap *m = structmap_next(&iter, &scratch);
1203 while (m) { 1224 while (m) {
@@ -1207,9 +1228,9 @@ process_file(Str path) {
1207 } 1228 }
1208 } 1229 }
1209 println("======== functions ========"); 1230 println("======== functions ========");
1210 for (sz i = 0; i < array_size(analyzer.types); i++) { 1231 for (sz i = 0; i < array_size(analyzer.scopes); i++) {
1211 Arena scratch = lexer_arena; 1232 Arena scratch = lexer_arena;
1212 TypeScope *scope = analyzer.types[i]; 1233 Scope *scope = analyzer.scopes[i];
1213 FunMapIter iter = funmap_iterator(scope->funcs, &scratch); 1234 FunMapIter iter = funmap_iterator(scope->funcs, &scratch);
1214 FunMap *m = funmap_next(&iter, &scratch); 1235 FunMap *m = funmap_next(&iter, &scratch);
1215 while (m) { 1236 while (m) {
@@ -1218,15 +1239,15 @@ process_file(Str path) {
1218 m = funmap_next(&iter, &scratch); 1239 m = funmap_next(&iter, &scratch);
1219 } 1240 }
1220 } 1241 }
1221 println("========== types =========="); 1242 println("========= symbols =========");
1222 for (sz i = 0; i < array_size(analyzer.types); i++) { 1243 for (sz i = 0; i < array_size(analyzer.scopes); i++) {
1223 Arena scratch = lexer_arena; 1244 Arena scratch = lexer_arena;
1224 TypeScope *scope = analyzer.types[i]; 1245 Scope *scope = analyzer.scopes[i];
1225 SymbolMapIter iter = symmap_iterator(scope->types, &scratch); 1246 SymbolMapIter iter = symmap_iterator(scope->symbols, &scratch);
1226 SymbolMap *m = symmap_next(&iter, &scratch); 1247 SymbolMap *m = symmap_next(&iter, &scratch);
1227 while (m) { 1248 while (m) {
1228 println("scope: %x{2} -- %s: type: %s: %s", scope->id, path, m->key, 1249 println("scope: %x{2} -- %s: %s %s: %s", scope->id, path,
1229 m->val); 1250 sym_kind_str[m->val.kind], m->key, m->val.name);
1230 m = symmap_next(&iter, &scratch); 1251 m = symmap_next(&iter, &scratch);
1231 } 1252 }
1232 } 1253 }