diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-26 15:47:30 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-26 15:47:30 +0200 |
commit | 7eb82e159dc9d0b29d57410b067be941cc1bcbf7 (patch) | |
tree | c20927bff6402a9c0397086369abcc49a7307b84 /src/main.c | |
parent | e40b04daf942ae01125d43e5cd402a08735075fd (diff) | |
download | bdl-7eb82e159dc9d0b29d57410b067be941cc1bcbf7.tar.gz bdl-7eb82e159dc9d0b29d57410b067be941cc1bcbf7.zip |
Continue consolidation with proper naming
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 142 |
1 files changed, 71 insertions, 71 deletions
@@ -50,7 +50,7 @@ Str sym_kind_str[] = { | |||
50 | [SYM_STRUCT_FIELD] = cstr("STRUCT FIELD "), | 50 | [SYM_STRUCT_FIELD] = cstr("STRUCT FIELD "), |
51 | }; | 51 | }; |
52 | 52 | ||
53 | typedef Str Type; | 53 | typedef Str Symbol; |
54 | 54 | ||
55 | typedef struct Fun { | 55 | typedef struct Fun { |
56 | Str name; | 56 | Str name; |
@@ -69,7 +69,7 @@ typedef struct Struct { | |||
69 | Node *val; | 69 | Node *val; |
70 | } Struct; | 70 | } Struct; |
71 | 71 | ||
72 | MAPDEF(TypeMap, typemap, Str, Type, str_hash, str_eq) | 72 | MAPDEF(SymbolMap, symmap, Str, Symbol, str_hash, str_eq) |
73 | MAPDEF(FunMap, funmap, Str, Fun, str_hash, str_eq) | 73 | MAPDEF(FunMap, funmap, Str, Fun, str_hash, str_eq) |
74 | MAPDEF(EnumMap, enummap, Str, Enum, str_hash, str_eq) | 74 | MAPDEF(EnumMap, enummap, Str, Enum, str_hash, str_eq) |
75 | MAPDEF(StructMap, structmap, Str, Struct, str_hash, str_eq) | 75 | MAPDEF(StructMap, structmap, Str, Struct, str_hash, str_eq) |
@@ -78,7 +78,7 @@ typedef struct TypeScope { | |||
78 | sz id; | 78 | sz id; |
79 | sz depth; | 79 | sz depth; |
80 | Str name; | 80 | Str name; |
81 | TypeMap *types; | 81 | SymbolMap *symbols; |
82 | FunMap *funcs; | 82 | FunMap *funcs; |
83 | EnumMap *enums; | 83 | EnumMap *enums; |
84 | StructMap *structs; | 84 | StructMap *structs; |
@@ -104,10 +104,10 @@ typescope_alloc(Analyzer *a, TypeScope *parent) { | |||
104 | return scope; | 104 | return scope; |
105 | } | 105 | } |
106 | 106 | ||
107 | TypeMap * | 107 | SymbolMap * |
108 | find_type(TypeScope *scope, Str type) { | 108 | find_type(TypeScope *scope, Str type) { |
109 | while (scope != NULL) { | 109 | while (scope != NULL) { |
110 | TypeMap *val = typemap_lookup(&scope->types, type); | 110 | SymbolMap *val = symmap_lookup(&scope->symbols, type); |
111 | if (val != NULL) { | 111 | if (val != NULL) { |
112 | return val; | 112 | return val; |
113 | } | 113 | } |
@@ -164,11 +164,11 @@ find_struct(TypeScope *scope, Str type) { | |||
164 | 164 | ||
165 | void | 165 | void |
166 | graph_typescope(TypeScope *scope, Arena a) { | 166 | graph_typescope(TypeScope *scope, Arena a) { |
167 | if (!scope->types) { | 167 | if (!scope->symbols) { |
168 | return; | 168 | return; |
169 | } | 169 | } |
170 | TypeMapIter iter = typemap_iterator(scope->types, &a); | 170 | SymbolMapIter iter = symmap_iterator(scope->symbols, &a); |
171 | TypeMap *type = typemap_next(&iter, &a); | 171 | SymbolMap *type = symmap_next(&iter, &a); |
172 | print( | 172 | print( |
173 | "%d[shape=\"none\" label=<<TABLE ALIGN=\"left\" STYLE=\"rounded\" " | 173 | "%d[shape=\"none\" label=<<TABLE ALIGN=\"left\" STYLE=\"rounded\" " |
174 | "BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"6\" " | 174 | "BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"6\" " |
@@ -186,13 +186,13 @@ graph_typescope(TypeScope *scope, Arena a) { | |||
186 | "<TD ALIGN=\"left\"> %s</TD>" | 186 | "<TD ALIGN=\"left\"> %s</TD>" |
187 | "</TR>", | 187 | "</TR>", |
188 | type->key, type->val); | 188 | type->key, type->val); |
189 | type = typemap_next(&iter, &a); | 189 | type = symmap_next(&iter, &a); |
190 | } | 190 | } |
191 | println("</TABLE>>];"); | 191 | println("</TABLE>>];"); |
192 | 192 | ||
193 | sz this_id = scope->id; | 193 | sz this_id = scope->id; |
194 | while (scope->parent) { | 194 | while (scope->parent) { |
195 | if (scope->parent->types) { | 195 | if (scope->parent->symbols) { |
196 | println("%d:e->%d:w;", this_id, scope->parent->id); | 196 | println("%d:e->%d:w;", this_id, scope->parent->id); |
197 | break; | 197 | break; |
198 | } else { | 198 | } else { |
@@ -233,7 +233,7 @@ graph_functions(TypeScope *scope, Arena a) { | |||
233 | println("</TABLE>>];"); | 233 | println("</TABLE>>];"); |
234 | sz this_id = scope->id; | 234 | sz this_id = scope->id; |
235 | while (scope->parent) { | 235 | while (scope->parent) { |
236 | if (scope->parent->types) { | 236 | if (scope->parent->symbols) { |
237 | println("fun_%d:e->fun_%d:%s:w;", this_id, scope->parent->id, | 237 | println("fun_%d:e->fun_%d:%s:w;", this_id, scope->parent->id, |
238 | scope->name); | 238 | scope->name); |
239 | break; | 239 | break; |
@@ -270,7 +270,7 @@ emit_semantic_error(Analyzer *a, Node *n, Str msg) { | |||
270 | eprintln("%s:%d:%d: error: %s", a->file_name, n->line, n->col, msg); | 270 | eprintln("%s:%d:%d: error: %s", a->file_name, n->line, n->col, msg); |
271 | } | 271 | } |
272 | 272 | ||
273 | Type type_inference(Analyzer *a, Node *node, TypeScope *scope); | 273 | Symbol type_inference(Analyzer *a, Node *node, TypeScope *scope); |
274 | 274 | ||
275 | void | 275 | void |
276 | typecheck_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) { | 276 | typecheck_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) { |
@@ -303,7 +303,7 @@ typecheck_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) { | |||
303 | a->file_name, node->line, node->col, field_name); | 303 | a->file_name, node->line, node->col, field_name); |
304 | } | 304 | } |
305 | if (node->field_val) { | 305 | if (node->field_val) { |
306 | Type type = type_inference(a, node->field_val, scope); | 306 | Symbol type = type_inference(a, node->field_val, scope); |
307 | if (!str_eq(type, field_type)) { | 307 | if (!str_eq(type, field_type)) { |
308 | eprintln( | 308 | eprintln( |
309 | "%s:%d:%d: error: mismatched types in struct " | 309 | "%s:%d:%d: error: mismatched types in struct " |
@@ -346,7 +346,7 @@ typecheck_lit_field(Analyzer *a, Node *node, TypeScope *scope, Str symbol) { | |||
346 | return; | 346 | return; |
347 | } | 347 | } |
348 | Str field_type = s->val.type; | 348 | Str field_type = s->val.type; |
349 | Type type = type_inference(a, node->field_val, scope); | 349 | Symbol type = type_inference(a, node->field_val, scope); |
350 | if (!str_eq(type, field_type)) { | 350 | if (!str_eq(type, field_type)) { |
351 | eprintln( | 351 | eprintln( |
352 | "%s:%d:%d: error: mismatched types in struct " | 352 | "%s:%d:%d: error: mismatched types in struct " |
@@ -430,7 +430,7 @@ typecheck_returns(Analyzer *a, Node *node, Str expected) { | |||
430 | } | 430 | } |
431 | } | 431 | } |
432 | 432 | ||
433 | Type | 433 | Symbol |
434 | type_inference(Analyzer *a, Node *node, TypeScope *scope) { | 434 | type_inference(Analyzer *a, Node *node, TypeScope *scope) { |
435 | assert(a); | 435 | assert(a); |
436 | assert(scope); | 436 | assert(scope); |
@@ -442,7 +442,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
442 | case NODE_LET: { | 442 | case NODE_LET: { |
443 | node->type = cstr("nil"); | 443 | node->type = cstr("nil"); |
444 | Str symbol = node->var_name->value.str; | 444 | Str symbol = node->var_name->value.str; |
445 | if (typemap_lookup(&scope->types, symbol)) { | 445 | if (symmap_lookup(&scope->symbols, symbol)) { |
446 | eprintln( | 446 | eprintln( |
447 | "%s:%d:%d: error: symbol '%s' already exists in current " | 447 | "%s:%d:%d: error: symbol '%s' already exists in current " |
448 | "scope ", | 448 | "scope ", |
@@ -452,7 +452,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
452 | } | 452 | } |
453 | if (node->var_type) { | 453 | if (node->var_type) { |
454 | Str type_name = node->var_type->value.str; | 454 | Str type_name = node->var_type->value.str; |
455 | TypeMap *type = find_type(scope, type_name); | 455 | SymbolMap *type = find_type(scope, type_name); |
456 | if (type == NULL) { | 456 | if (type == NULL) { |
457 | eprintln("%s:%d:%d: error: unknown type '%s'", a->file_name, | 457 | eprintln("%s:%d:%d: error: unknown type '%s'", a->file_name, |
458 | node->var_type->line, node->var_type->col, | 458 | node->var_type->line, node->var_type->col, |
@@ -460,7 +460,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
460 | return cstr(""); | 460 | return cstr(""); |
461 | } | 461 | } |
462 | if (node->var_val) { | 462 | if (node->var_val) { |
463 | Type type = type_inference(a, node->var_val, scope); | 463 | Symbol type = type_inference(a, node->var_val, scope); |
464 | if (!type.size) { | 464 | if (!type.size) { |
465 | eprintln( | 465 | eprintln( |
466 | "%s:%d:%d: error: can't bind `nil` to variable " | 466 | "%s:%d:%d: error: can't bind `nil` to variable " |
@@ -485,21 +485,21 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
485 | } | 485 | } |
486 | } | 486 | } |
487 | } | 487 | } |
488 | typemap_insert(&scope->types, symbol, type->val, a->storage); | 488 | symmap_insert(&scope->symbols, symbol, type->val, a->storage); |
489 | return node->type; | 489 | return node->type; |
490 | } | 490 | } |
491 | 491 | ||
492 | // We don't know the type for this symbol, perform inference. | 492 | // We don't know the type for this symbol, perform inference. |
493 | Type type = type_inference(a, node->var_val, scope); | 493 | Symbol type = type_inference(a, node->var_val, scope); |
494 | if (type.size) { | 494 | if (type.size) { |
495 | typemap_insert(&scope->types, symbol, type, a->storage); | 495 | symmap_insert(&scope->symbols, symbol, type, a->storage); |
496 | node->var_name->type = type; | 496 | node->var_name->type = type; |
497 | } | 497 | } |
498 | return node->type; | 498 | return node->type; |
499 | } break; | 499 | } break; |
500 | case NODE_SET: { | 500 | case NODE_SET: { |
501 | Type name = type_inference(a, node->var_name, scope); | 501 | Symbol name = type_inference(a, node->var_name, scope); |
502 | Type val = type_inference(a, node->var_val, scope); | 502 | Symbol val = type_inference(a, node->var_val, scope); |
503 | if (!str_eq(name, val)) { | 503 | if (!str_eq(name, val)) { |
504 | eprintln( | 504 | eprintln( |
505 | "%s:%d:%d: error: type mismatch, trying to assing " | 505 | "%s:%d:%d: error: type mismatch, trying to assing " |
@@ -514,7 +514,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
514 | case NODE_STRUCT: { | 514 | case NODE_STRUCT: { |
515 | node->type = cstr("nil"); | 515 | node->type = cstr("nil"); |
516 | Str symbol = node->value.str; | 516 | Str symbol = node->value.str; |
517 | if (typemap_lookup(&scope->types, symbol) != NULL) { | 517 | if (symmap_lookup(&scope->symbols, symbol) != NULL) { |
518 | eprintln( | 518 | eprintln( |
519 | "%s:%d:%d: error: struct '%s' already exists in current " | 519 | "%s:%d:%d: error: struct '%s' already exists in current " |
520 | "scope", | 520 | "scope", |
@@ -527,13 +527,13 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
527 | Node *field = node->struct_field[i]; | 527 | Node *field = node->struct_field[i]; |
528 | typecheck_field(a, field, scope, symbol); | 528 | typecheck_field(a, field, scope, symbol); |
529 | } | 529 | } |
530 | typemap_insert(&scope->types, symbol, symbol, a->storage); | 530 | symmap_insert(&scope->symbols, symbol, symbol, a->storage); |
531 | return node->type; | 531 | return node->type; |
532 | } break; | 532 | } break; |
533 | case NODE_ENUM: { | 533 | case NODE_ENUM: { |
534 | node->type = cstr("nil"); | 534 | node->type = cstr("nil"); |
535 | Str symbol = node->value.str; | 535 | Str symbol = node->value.str; |
536 | if (typemap_lookup(&scope->types, symbol) != NULL) { | 536 | if (symmap_lookup(&scope->symbols, symbol) != NULL) { |
537 | eprintln( | 537 | eprintln( |
538 | "%s:%d:%d: error: enum '%s' already exists in current " | 538 | "%s:%d:%d: error: enum '%s' already exists in current " |
539 | "scope", | 539 | "scope", |
@@ -556,7 +556,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
556 | a->file_name, field->line, field->col, field_name); | 556 | a->file_name, field->line, field->col, field_name); |
557 | } | 557 | } |
558 | if (field->field_val) { | 558 | if (field->field_val) { |
559 | Type type = type_inference(a, field->field_val, scope); | 559 | Symbol type = type_inference(a, field->field_val, scope); |
560 | if (!str_eq(type, cstr("int"))) { | 560 | if (!str_eq(type, cstr("int"))) { |
561 | eprintln( | 561 | eprintln( |
562 | "%s:%d:%d: error: non int enum value for '%s.%s'", | 562 | "%s:%d:%d: error: non int enum value for '%s.%s'", |
@@ -568,11 +568,11 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
568 | (Enum){.name = field_name}, a->storage); | 568 | (Enum){.name = field_name}, a->storage); |
569 | field->type = cstr("int"); | 569 | field->type = cstr("int"); |
570 | } | 570 | } |
571 | typemap_insert(&scope->types, symbol, symbol, a->storage); | 571 | symmap_insert(&scope->symbols, symbol, symbol, a->storage); |
572 | return node->type; | 572 | return node->type; |
573 | } break; | 573 | } break; |
574 | case NODE_IF: { | 574 | case NODE_IF: { |
575 | Type cond_type = type_inference(a, node->cond_if, scope); | 575 | Symbol cond_type = type_inference(a, node->cond_if, scope); |
576 | if (!str_eq(cond_type, cstr("bool"))) { | 576 | if (!str_eq(cond_type, cstr("bool"))) { |
577 | emit_semantic_error( | 577 | emit_semantic_error( |
578 | a, node->cond_if, | 578 | a, node->cond_if, |
@@ -586,7 +586,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
586 | node->type = type_inference(a, node->cond_expr, next); | 586 | node->type = type_inference(a, node->cond_expr, next); |
587 | } | 587 | } |
588 | if (node->cond_else) { | 588 | if (node->cond_else) { |
589 | Type else_type; | 589 | Symbol else_type; |
590 | if (node->cond_else->kind == NODE_BLOCK) { | 590 | if (node->cond_else->kind == NODE_BLOCK) { |
591 | else_type = type_inference(a, node->cond_else, scope); | 591 | else_type = type_inference(a, node->cond_else, scope); |
592 | } else { | 592 | } else { |
@@ -602,7 +602,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
602 | return node->type; | 602 | return node->type; |
603 | } break; | 603 | } break; |
604 | case NODE_WHILE: { | 604 | case NODE_WHILE: { |
605 | Type cond_type = type_inference(a, node->while_cond, scope); | 605 | Symbol cond_type = type_inference(a, node->while_cond, scope); |
606 | if (!str_eq(cond_type, cstr("bool"))) { | 606 | if (!str_eq(cond_type, cstr("bool"))) { |
607 | emit_semantic_error( | 607 | emit_semantic_error( |
608 | a, node->cond_if, | 608 | a, node->cond_if, |
@@ -621,10 +621,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
621 | if (node->match_expr) { | 621 | if (node->match_expr) { |
622 | type_inference(a, node->match_expr, scope); | 622 | type_inference(a, node->match_expr, scope); |
623 | } | 623 | } |
624 | Type previous = cstr(""); | 624 | Symbol previous = cstr(""); |
625 | for (sz i = 0; i < array_size(node->match_cases); i++) { | 625 | for (sz i = 0; i < array_size(node->match_cases); i++) { |
626 | Node *expr = node->match_cases[i]; | 626 | Node *expr = node->match_cases[i]; |
627 | Type next = type_inference(a, expr, scope); | 627 | Symbol next = type_inference(a, expr, scope); |
628 | if (i != 0 && !str_eq(next, previous)) { | 628 | if (i != 0 && !str_eq(next, previous)) { |
629 | emit_semantic_error( | 629 | emit_semantic_error( |
630 | a, node, | 630 | a, node, |
@@ -649,7 +649,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
649 | } break; | 649 | } break; |
650 | case NODE_CASE_COND: { | 650 | case NODE_CASE_COND: { |
651 | if (node->case_value) { | 651 | if (node->case_value) { |
652 | Type cond = type_inference(a, node->case_value, scope); | 652 | Symbol cond = type_inference(a, node->case_value, scope); |
653 | if (!str_eq(cond, cstr("bool"))) { | 653 | if (!str_eq(cond, cstr("bool"))) { |
654 | emit_semantic_error(a, node, | 654 | emit_semantic_error(a, node, |
655 | cstr("non-boolean case condition")); | 655 | cstr("non-boolean case condition")); |
@@ -673,14 +673,14 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
673 | case NODE_NOT: | 673 | case NODE_NOT: |
674 | case NODE_AND: | 674 | case NODE_AND: |
675 | case NODE_OR: { | 675 | case NODE_OR: { |
676 | Type left = type_inference(a, node->left, scope); | 676 | Symbol left = type_inference(a, node->left, scope); |
677 | if (!str_eq(left, cstr("bool"))) { | 677 | if (!str_eq(left, cstr("bool"))) { |
678 | emit_semantic_error(a, node, | 678 | emit_semantic_error(a, node, |
679 | cstr("expected bool on logic expression")); | 679 | cstr("expected bool on logic expression")); |
680 | return cstr(""); | 680 | return cstr(""); |
681 | } | 681 | } |
682 | if (node->right) { | 682 | if (node->right) { |
683 | Type right = type_inference(a, node->right, scope); | 683 | Symbol right = type_inference(a, node->right, scope); |
684 | if (!str_eq(right, cstr("bool"))) { | 684 | if (!str_eq(right, cstr("bool"))) { |
685 | emit_semantic_error( | 685 | emit_semantic_error( |
686 | a, node, cstr("expected bool on logic expression")); | 686 | a, node, cstr("expected bool on logic expression")); |
@@ -696,8 +696,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
696 | case NODE_GT: | 696 | case NODE_GT: |
697 | case NODE_LE: | 697 | case NODE_LE: |
698 | case NODE_GE: { | 698 | case NODE_GE: { |
699 | Type left = type_inference(a, node->left, scope); | 699 | Symbol left = type_inference(a, node->left, scope); |
700 | Type right = type_inference(a, node->right, scope); | 700 | Symbol right = type_inference(a, node->right, scope); |
701 | if (!str_eq(left, right)) { | 701 | if (!str_eq(left, right)) { |
702 | emit_semantic_error( | 702 | emit_semantic_error( |
703 | a, node, cstr("mismatched types on binary expression")); | 703 | a, node, cstr("mismatched types on binary expression")); |
@@ -707,7 +707,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
707 | return node->type; | 707 | return node->type; |
708 | } break; | 708 | } break; |
709 | case NODE_BITNOT: { | 709 | case NODE_BITNOT: { |
710 | Type left = type_inference(a, node->left, scope); | 710 | Symbol left = type_inference(a, node->left, scope); |
711 | if (!strset_lookup(&a->integer_types, left)) { | 711 | if (!strset_lookup(&a->integer_types, left)) { |
712 | emit_semantic_error( | 712 | emit_semantic_error( |
713 | a, node, cstr("non integer type on bit twiddling expr")); | 713 | a, node, cstr("non integer type on bit twiddling expr")); |
@@ -720,8 +720,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
720 | case NODE_BITOR: | 720 | case NODE_BITOR: |
721 | case NODE_BITLSHIFT: | 721 | case NODE_BITLSHIFT: |
722 | case NODE_BITRSHIFT: { | 722 | case NODE_BITRSHIFT: { |
723 | Type left = type_inference(a, node->left, scope); | 723 | Symbol left = type_inference(a, node->left, scope); |
724 | Type right = type_inference(a, node->right, scope); | 724 | Symbol right = type_inference(a, node->right, scope); |
725 | if (!strset_lookup(&a->integer_types, left) || | 725 | if (!strset_lookup(&a->integer_types, left) || |
726 | !strset_lookup(&a->integer_types, right)) { | 726 | !strset_lookup(&a->integer_types, right)) { |
727 | emit_semantic_error( | 727 | emit_semantic_error( |
@@ -736,8 +736,8 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
736 | case NODE_DIV: | 736 | case NODE_DIV: |
737 | case NODE_MUL: | 737 | case NODE_MUL: |
738 | case NODE_MOD: { | 738 | case NODE_MOD: { |
739 | Type left = type_inference(a, node->left, scope); | 739 | Symbol left = type_inference(a, node->left, scope); |
740 | Type right = type_inference(a, node->right, scope); | 740 | Symbol right = type_inference(a, node->right, scope); |
741 | if (!strset_lookup(&a->numeric_types, left) || | 741 | if (!strset_lookup(&a->numeric_types, left) || |
742 | !strset_lookup(&a->numeric_types, right)) { | 742 | !strset_lookup(&a->numeric_types, right)) { |
743 | emit_semantic_error( | 743 | emit_semantic_error( |
@@ -769,7 +769,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
769 | return node->type; | 769 | return node->type; |
770 | } break; | 770 | } break; |
771 | case NODE_TYPE: { | 771 | case NODE_TYPE: { |
772 | TypeMap *type = find_type(scope, node->value.str); | 772 | SymbolMap *type = find_type(scope, node->value.str); |
773 | if (!type) { | 773 | if (!type) { |
774 | emit_semantic_error(a, node, cstr("unknown type")); | 774 | emit_semantic_error(a, node, cstr("unknown type")); |
775 | return cstr(""); | 775 | return cstr(""); |
@@ -780,7 +780,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
780 | case NODE_SYMBOL_IDX: | 780 | case NODE_SYMBOL_IDX: |
781 | case NODE_SYMBOL: { | 781 | case NODE_SYMBOL: { |
782 | Str symbol = node->value.str; | 782 | Str symbol = node->value.str; |
783 | TypeMap *type = find_type(scope, symbol); | 783 | SymbolMap *type = find_type(scope, symbol); |
784 | if (!type) { | 784 | if (!type) { |
785 | eprintln("%s:%d:%d: error: couldn't resolve symbol '%s'", | 785 | eprintln("%s:%d:%d: error: couldn't resolve symbol '%s'", |
786 | a->file_name, node->line, node->col, symbol); | 786 | a->file_name, node->line, node->col, symbol); |
@@ -887,10 +887,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
887 | return cstr(""); | 887 | return cstr(""); |
888 | } | 888 | } |
889 | // Check that actual parameters typecheck | 889 | // Check that actual parameters typecheck |
890 | Type args = cstr(""); | 890 | Symbol args = cstr(""); |
891 | for (sz i = 0; i < array_size(node->elements); i++) { | 891 | for (sz i = 0; i < array_size(node->elements); i++) { |
892 | Node *expr = node->elements[i]; | 892 | Node *expr = node->elements[i]; |
893 | Type type = type_inference(a, expr, scope); | 893 | Symbol type = type_inference(a, expr, scope); |
894 | args = str_concat(args, type, a->storage); | 894 | args = str_concat(args, type, a->storage); |
895 | if (i != array_size(node->elements) - 1) { | 895 | if (i != array_size(node->elements) - 1) { |
896 | args = str_concat(args, cstr(","), a->storage); | 896 | args = str_concat(args, cstr(","), a->storage); |
@@ -912,7 +912,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
912 | } break; | 912 | } break; |
913 | case NODE_BLOCK: { | 913 | case NODE_BLOCK: { |
914 | scope = typescope_alloc(a, scope); | 914 | scope = typescope_alloc(a, scope); |
915 | Type type; | 915 | Symbol type; |
916 | for (sz i = 0; i < array_size(node->elements); i++) { | 916 | for (sz i = 0; i < array_size(node->elements); i++) { |
917 | Node *expr = node->elements[i]; | 917 | Node *expr = node->elements[i]; |
918 | type = type_inference(a, expr, scope); | 918 | type = type_inference(a, expr, scope); |
@@ -921,10 +921,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
921 | return node->type; | 921 | return node->type; |
922 | } break; | 922 | } break; |
923 | case NODE_RETURN: { | 923 | case NODE_RETURN: { |
924 | Type ret_type = cstr(""); | 924 | Symbol ret_type = cstr(""); |
925 | for (sz i = 0; i < array_size(node->elements); i++) { | 925 | for (sz i = 0; i < array_size(node->elements); i++) { |
926 | Node *expr = node->elements[i]; | 926 | Node *expr = node->elements[i]; |
927 | Type type = type_inference(a, expr, scope); | 927 | Symbol type = type_inference(a, expr, scope); |
928 | ret_type = str_concat(ret_type, type, a->storage); | 928 | ret_type = str_concat(ret_type, type, a->storage); |
929 | if (i != array_size(node->elements) - 1) { | 929 | if (i != array_size(node->elements) - 1) { |
930 | ret_type = str_concat(ret_type, cstr(","), a->storage); | 930 | ret_type = str_concat(ret_type, cstr(","), a->storage); |
@@ -940,15 +940,15 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
940 | node->type = cstr("nil"); | 940 | node->type = cstr("nil"); |
941 | TypeScope *prev_scope = scope; | 941 | TypeScope *prev_scope = scope; |
942 | scope = typescope_alloc(a, scope); | 942 | scope = typescope_alloc(a, scope); |
943 | Type param_type = cstr(""); | 943 | Symbol param_type = cstr(""); |
944 | for (sz i = 0; i < array_size(node->func_params); i++) { | 944 | for (sz i = 0; i < array_size(node->func_params); i++) { |
945 | Node *param = node->func_params[i]; | 945 | Node *param = node->func_params[i]; |
946 | Str symbol = param->param_name->value.str; | 946 | Str symbol = param->param_name->value.str; |
947 | Type type = param->param_type->value.str; | 947 | Symbol type = param->param_type->value.str; |
948 | param->param_name->type = | 948 | param->param_name->type = |
949 | type_inference(a, param->param_type, scope); | 949 | type_inference(a, param->param_type, scope); |
950 | param->type = param->param_name->type; | 950 | param->type = param->param_name->type; |
951 | typemap_insert(&scope->types, symbol, type, a->storage); | 951 | symmap_insert(&scope->symbols, symbol, type, a->storage); |
952 | param_type = str_concat(param_type, type, a->storage); | 952 | param_type = str_concat(param_type, type, a->storage); |
953 | if (i != array_size(node->func_params) - 1) { | 953 | if (i != array_size(node->func_params) - 1) { |
954 | param_type = str_concat(param_type, cstr(","), a->storage); | 954 | param_type = str_concat(param_type, cstr(","), a->storage); |
@@ -959,10 +959,10 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
959 | } | 959 | } |
960 | node->fun_params = param_type; | 960 | node->fun_params = param_type; |
961 | 961 | ||
962 | Type ret_type = cstr(""); | 962 | Symbol ret_type = cstr(""); |
963 | for (sz i = 0; i < array_size(node->func_ret); i++) { | 963 | for (sz i = 0; i < array_size(node->func_ret); i++) { |
964 | Node *expr = node->func_ret[i]; | 964 | Node *expr = node->func_ret[i]; |
965 | Type type = type_inference(a, expr, scope); | 965 | Symbol type = type_inference(a, expr, scope); |
966 | ret_type = str_concat(ret_type, type, a->storage); | 966 | ret_type = str_concat(ret_type, type, a->storage); |
967 | if (i != array_size(node->func_ret) - 1) { | 967 | if (i != array_size(node->func_ret) - 1) { |
968 | ret_type = str_concat(ret_type, cstr(","), a->storage); | 968 | ret_type = str_concat(ret_type, cstr(","), a->storage); |
@@ -975,7 +975,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
975 | 975 | ||
976 | Str symbol = node->func_name->value.str; | 976 | Str symbol = node->func_name->value.str; |
977 | if (prev_scope->parent != NULL) { | 977 | if (prev_scope->parent != NULL) { |
978 | if (typemap_lookup(&prev_scope->types, symbol)) { | 978 | if (symmap_lookup(&prev_scope->symbols, symbol)) { |
979 | eprintln( | 979 | eprintln( |
980 | "%s:%d:%d: error: function '%s' already defined in " | 980 | "%s:%d:%d: error: function '%s' already defined in " |
981 | "current " | 981 | "current " |
@@ -984,7 +984,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
984 | symbol); | 984 | symbol); |
985 | return cstr(""); | 985 | return cstr(""); |
986 | } | 986 | } |
987 | typemap_insert(&prev_scope->types, symbol, symbol, a->storage); | 987 | symmap_insert(&prev_scope->symbols, symbol, symbol, a->storage); |
988 | } | 988 | } |
989 | scope->name = symbol; | 989 | scope->name = symbol; |
990 | funmap_insert(&prev_scope->funcs, symbol, | 990 | funmap_insert(&prev_scope->funcs, symbol, |
@@ -994,7 +994,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) { | |||
994 | a->storage); | 994 | a->storage); |
995 | 995 | ||
996 | if (node->func_body->kind == NODE_BLOCK) { | 996 | if (node->func_body->kind == NODE_BLOCK) { |
997 | Type type; | 997 | Symbol type; |
998 | for (sz i = 0; i < array_size(node->func_body->elements); i++) { | 998 | for (sz i = 0; i < array_size(node->func_body->elements); i++) { |
999 | Node *expr = node->func_body->elements[i]; | 999 | Node *expr = node->func_body->elements[i]; |
1000 | type = type_inference(a, expr, scope); | 1000 | type = type_inference(a, expr, scope); |
@@ -1043,38 +1043,38 @@ symbolic_analysis(Analyzer *a, Parser *parser) { | |||
1043 | }; | 1043 | }; |
1044 | for (sz i = 0; i < LEN(builtin_functions); i++) { | 1044 | for (sz i = 0; i < LEN(builtin_functions); i++) { |
1045 | Str symbol = builtin_functions[i]; | 1045 | Str symbol = builtin_functions[i]; |
1046 | typemap_insert(&types->types, symbol, symbol, a->storage); | 1046 | symmap_insert(&types->symbols, symbol, symbol, a->storage); |
1047 | funmap_insert(&types->funcs, symbol, | 1047 | funmap_insert(&types->funcs, symbol, |
1048 | (Fun){.name = symbol, | 1048 | (Fun){.name = symbol, |
1049 | .param_type = cstr("..."), | 1049 | .param_type = cstr("..."), |
1050 | .return_type = cstr("nil")}, | 1050 | .return_type = cstr("nil")}, |
1051 | a->storage); | 1051 | a->storage); |
1052 | } | 1052 | } |
1053 | Type builtin_types[] = { | 1053 | Symbol builtin_types[] = { |
1054 | cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), | 1054 | cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), |
1055 | cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"), | 1055 | cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"), |
1056 | cstr("f32"), cstr("f64"), cstr("ptr"), cstr("int"), | 1056 | cstr("f32"), cstr("f64"), cstr("ptr"), cstr("int"), |
1057 | cstr("uint"), cstr("str"), cstr("bool"), cstr("nil")}; | 1057 | cstr("uint"), cstr("str"), cstr("bool"), cstr("nil")}; |
1058 | for (sz i = 0; i < LEN(builtin_types); i++) { | 1058 | for (sz i = 0; i < LEN(builtin_types); i++) { |
1059 | Type type = builtin_types[i]; | 1059 | Symbol type = builtin_types[i]; |
1060 | typemap_insert(&types->types, type, type, a->storage); | 1060 | symmap_insert(&types->symbols, type, type, a->storage); |
1061 | } | 1061 | } |
1062 | Type numeric_types[] = { | 1062 | Symbol numeric_types[] = { |
1063 | cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), cstr("u32"), | 1063 | cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), cstr("u32"), |
1064 | cstr("s32"), cstr("u64"), cstr("s64"), cstr("f32"), cstr("f64"), | 1064 | cstr("s32"), cstr("u64"), cstr("s64"), cstr("f32"), cstr("f64"), |
1065 | cstr("ptr"), cstr("int"), cstr("uint"), | 1065 | cstr("ptr"), cstr("int"), cstr("uint"), |
1066 | }; | 1066 | }; |
1067 | for (sz i = 0; i < LEN(numeric_types); i++) { | 1067 | for (sz i = 0; i < LEN(numeric_types); i++) { |
1068 | Type type = numeric_types[i]; | 1068 | Symbol type = numeric_types[i]; |
1069 | strset_insert(&a->numeric_types, type, a->storage); | 1069 | strset_insert(&a->numeric_types, type, a->storage); |
1070 | } | 1070 | } |
1071 | Type integer_types[] = { | 1071 | Symbol integer_types[] = { |
1072 | cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), | 1072 | cstr("u8"), cstr("s8"), cstr("u16"), cstr("s16"), |
1073 | cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"), | 1073 | cstr("u32"), cstr("s32"), cstr("u64"), cstr("s64"), |
1074 | cstr("ptr"), cstr("int"), cstr("uint"), | 1074 | cstr("ptr"), cstr("int"), cstr("uint"), |
1075 | }; | 1075 | }; |
1076 | for (sz i = 0; i < LEN(integer_types); i++) { | 1076 | for (sz i = 0; i < LEN(integer_types); i++) { |
1077 | Type type = integer_types[i]; | 1077 | Symbol type = integer_types[i]; |
1078 | strset_insert(&a->integer_types, type, a->storage); | 1078 | strset_insert(&a->integer_types, type, a->storage); |
1079 | } | 1079 | } |
1080 | // Find top level function declarations. | 1080 | // Find top level function declarations. |
@@ -1082,7 +1082,7 @@ symbolic_analysis(Analyzer *a, Parser *parser) { | |||
1082 | Node *root = parser->nodes[i]; | 1082 | Node *root = parser->nodes[i]; |
1083 | if (root->kind == NODE_FUN) { | 1083 | if (root->kind == NODE_FUN) { |
1084 | Str symbol = root->func_name->value.str; | 1084 | Str symbol = root->func_name->value.str; |
1085 | if (typemap_lookup(&types->types, symbol)) { | 1085 | if (symmap_lookup(&types->symbols, symbol)) { |
1086 | eprintln( | 1086 | eprintln( |
1087 | "%s:%d:%d: error: function '%s' already defined in " | 1087 | "%s:%d:%d: error: function '%s' already defined in " |
1088 | "current " | 1088 | "current " |
@@ -1090,7 +1090,7 @@ symbolic_analysis(Analyzer *a, Parser *parser) { | |||
1090 | a->file_name, root->var_name->line, root->var_name->col, | 1090 | a->file_name, root->var_name->line, root->var_name->col, |
1091 | symbol); | 1091 | symbol); |
1092 | } | 1092 | } |
1093 | typemap_insert(&types->types, symbol, symbol, a->storage); | 1093 | symmap_insert(&types->symbols, symbol, symbol, a->storage); |
1094 | } | 1094 | } |
1095 | } | 1095 | } |
1096 | // Recursively fill symbol tables. | 1096 | // Recursively fill symbol tables. |
@@ -1222,12 +1222,12 @@ process_file(Str path) { | |||
1222 | for (sz i = 0; i < array_size(analyzer.types); i++) { | 1222 | for (sz i = 0; i < array_size(analyzer.types); i++) { |
1223 | Arena scratch = lexer_arena; | 1223 | Arena scratch = lexer_arena; |
1224 | TypeScope *scope = analyzer.types[i]; | 1224 | TypeScope *scope = analyzer.types[i]; |
1225 | TypeMapIter iter = typemap_iterator(scope->types, &scratch); | 1225 | SymbolMapIter iter = symmap_iterator(scope->types, &scratch); |
1226 | TypeMap *m = typemap_next(&iter, &scratch); | 1226 | SymbolMap *m = symmap_next(&iter, &scratch); |
1227 | while (m) { | 1227 | while (m) { |
1228 | println("scope: %x{2} -- %s: type: %s: %s", scope->id, path, m->key, | 1228 | println("scope: %x{2} -- %s: type: %s: %s", scope->id, path, m->key, |
1229 | m->val); | 1229 | m->val); |
1230 | m = typemap_next(&iter, &scratch); | 1230 | m = symmap_next(&iter, &scratch); |
1231 | } | 1231 | } |
1232 | } | 1232 | } |
1233 | #endif | 1233 | #endif |