Skip to content

Commit c72c817

Browse files
committed
fix(compiler): better error message for binop type mismatch
1 parent 6c3082b commit c72c817

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/jik/semantic.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,15 @@ infer_ternary(JikNode *nd)
10511051
jik_diag_fatal_error("ternary branches have incompatible types", jik_token_to_text(nd->token));
10521052
}
10531053

1054+
static char *
1055+
make_binop_error_msg(JikNode *nd)
1056+
{
1057+
return JIK_STRING_NCAT("unsupported binop between ",
1058+
jik_type_pretty_name(nd->val_binop.left->jik_type),
1059+
" and ",
1060+
jik_type_pretty_name(nd->val_binop.right->jik_type));
1061+
}
1062+
10541063
static void
10551064
infer_binop(JikNode *nd)
10561065
{
@@ -1064,7 +1073,7 @@ infer_binop(JikNode *nd)
10641073
// TODO: simpligy this to numeric types or similar
10651074
if (!binop_operands_are_of_type(nd,
10661075
(JikTypeName[]){TYPE_INTEGER, TYPE_FLOAT, TYPE_NOTYPE})) {
1067-
jik_diag_fatal_error("unsupported type for binop", jik_token_to_text(nd->token));
1076+
jik_diag_fatal_error(make_binop_error_msg(nd), jik_token_to_text(nd->token));
10681077
}
10691078
if (nd->val_binop.left->jik_type == &JIK_TYPE_FLOAT ||
10701079
nd->val_binop.right->jik_type == &JIK_TYPE_FLOAT) {
@@ -1076,15 +1085,15 @@ infer_binop(JikNode *nd)
10761085
}
10771086
else if (strcmp(nd->val_binop.val, "%") == 0) {
10781087
if (!binop_operands_are_of_type(nd, (JikTypeName[]){TYPE_INTEGER, TYPE_NOTYPE})) {
1079-
jik_diag_fatal_error("unsupported type for binop", jik_token_to_text(nd->token));
1088+
jik_diag_fatal_error(make_binop_error_msg(nd), jik_token_to_text(nd->token));
10801089
}
10811090
nd->jik_type = &JIK_TYPE_INT;
10821091
}
10831092
else if (strcmp(nd->val_binop.val, "<") == 0 || strcmp(nd->val_binop.val, ">") == 0 ||
10841093
strcmp(nd->val_binop.val, ">=") == 0 || strcmp(nd->val_binop.val, "<=") == 0) {
10851094
if (!binop_operands_are_of_type(nd,
10861095
(JikTypeName[]){TYPE_INTEGER, TYPE_FLOAT, TYPE_NOTYPE})) {
1087-
jik_diag_fatal_error("unsupported type for binop", jik_token_to_text(nd->token));
1096+
jik_diag_fatal_error(make_binop_error_msg(nd), jik_token_to_text(nd->token));
10881097
}
10891098
nd->jik_type = &JIK_TYPE_BOOL;
10901099
}
@@ -1097,18 +1106,18 @@ infer_binop(JikNode *nd)
10971106
TYPE_CHAR,
10981107
TYPE_ENUM,
10991108
TYPE_NOTYPE}))
1100-
jik_diag_fatal_error("unsupported type for binop", jik_token_to_text(nd->token));
1109+
jik_diag_fatal_error(make_binop_error_msg(nd), jik_token_to_text(nd->token));
11011110
nd->jik_type = &JIK_TYPE_BOOL;
11021111
if (binop_operands_are_of_type(nd, (JikTypeName[]){TYPE_INTEGER, TYPE_FLOAT, TYPE_NOTYPE}))
11031112
return;
11041113
if (!jik_node_types_equal(nd->val_binop.left, nd->val_binop.right)) {
1105-
jik_diag_fatal_error("binary operator between different types",
1114+
jik_diag_fatal_error(make_binop_error_msg(nd),
11061115
jik_token_to_text(nd->token));
11071116
}
11081117
}
11091118
else if (strcmp(nd->val_binop.val, "and") == 0 || strcmp(nd->val_binop.val, "or") == 0) {
11101119
if (!binop_operands_are_of_type(nd, (JikTypeName[]){TYPE_BOOL, TYPE_NOTYPE})) {
1111-
jik_diag_fatal_error("unsupported type for binop", jik_token_to_text(nd->token));
1120+
jik_diag_fatal_error(make_binop_error_msg(nd), jik_token_to_text(nd->token));
11121121
}
11131122
nd->jik_type = &JIK_TYPE_BOOL;
11141123
}

test/jik/test_compile_fail_operators.jik

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main():
1919
x := 1 and true
2020
end
2121
""",
22-
"unsupported type for binop")
22+
"unsupported binop between")
2323
end
2424

2525
func test_compile_fail_03(ts):
@@ -29,7 +29,7 @@ func main():
2929
x := 1 + true
3030
end
3131
""",
32-
"unsupported type for binop")
32+
"unsupported binop between")
3333
end
3434

3535
func test_compile_fail_04(ts):

0 commit comments

Comments
 (0)