aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-24 19:36:46 +0200
committerBad Diode <bd@badd10de.dev>2024-06-24 19:36:46 +0200
commit2e7daa8e0765d293b1029131f6fde0592546bc0d (patch)
tree762f3e6f9a75b1e480243060e76f60a39d2c8e05
parent1c7d30ab77960be3a0c7ae259d601fa6aed640b3 (diff)
downloadbdl-2e7daa8e0765d293b1029131f6fde0592546bc0d.tar.gz
bdl-2e7daa8e0765d293b1029131f6fde0592546bc0d.zip
Fix set typechecking
-rw-r--r--src/main.c16
-rw-r--r--tests/semantics.bad2
2 files changed, 14 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 9d8ace8..33d193b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,6 +11,7 @@
11// are assigning a struct literal, not just a symbol. 11// are assigning a struct literal, not just a symbol.
12// TODO: nested structs 12// TODO: nested structs
13// TODO: unions 13// TODO: unions
14// TODO: match deconstruct structs
14 15
15typedef enum ExecMode { 16typedef enum ExecMode {
16 RUN_NORMAL, 17 RUN_NORMAL,
@@ -507,8 +508,17 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
507 return node->type; 508 return node->type;
508 } break; 509 } break;
509 case NODE_SET: { 510 case NODE_SET: {
510 type_inference(a, node->var_name, scope); 511 Type name = type_inference(a, node->var_name, scope);
511 type_inference(a, node->var_val, scope); 512 Type val = type_inference(a, node->var_val, scope);
513 if (!str_eq(name, val)) {
514 eprintln(
515 "%s:%d:%d: error: type mismatch, trying to assing "
516 "%s"
517 " to a variable of type %s",
518 a->file_name, node->line, node->col,
519 val, name);
520 return cstr("");
521 }
512 node->type = cstr("nil"); 522 node->type = cstr("nil");
513 return node->type; 523 return node->type;
514 } break; 524 } break;
@@ -816,7 +826,7 @@ type_inference(Analyzer *a, Node *node, TypeScope *scope) {
816 } 826 }
817 node->next->type = cstr("int"); 827 node->next->type = cstr("int");
818 node->type = type->val; 828 node->type = type->val;
819 return node->type; 829 return node->next->type;
820 } 830 }
821 831
822 StructMap *s = find_struct(scope, type->val); 832 StructMap *s = find_struct(scope, type->val);
diff --git a/tests/semantics.bad b/tests/semantics.bad
index a6810a9..95e827b 100644
--- a/tests/semantics.bad
+++ b/tests/semantics.bad
@@ -20,7 +20,7 @@ struct item {
20; } 20; }
21; let a: item = item ; this shouldn't work, or should it return the default val? 21; let a: item = item ; this shouldn't work, or should it return the default val?
22let c: item 22let c: item
23set c.id = "hi" 23set c.id = 1
24let d = c.id 24let d = c.id
25; set a.name = "hello" 25; set a.name = "hello"
26; let b = a.id 26; let b = a.id