From 9df460e90aeef096f391bdc0160cbfee5349eece Mon Sep 17 00:00:00 2001 From: Akuli Date: Wed, 15 Jan 2025 17:47:40 +0200 Subject: [PATCH] fixed everything --- .../step3_function_and_method_bodies.jou | 127 ++++++------------ tests/wrong_type/arrow_instead_of_dot.jou | 2 +- .../wrong_type/arrow_instead_of_dot_call.jou | 2 +- tests/wrong_type/arrow_operator_int.jou | 2 +- tests/wrong_type/arrow_operator_int_call.jou | 2 +- tests/wrong_type/arrow_operator_intptr.jou | 2 +- .../wrong_type/arrow_operator_intptr_call.jou | 2 +- .../wrong_type/dot_instead_of_arrow_call.jou | 2 +- tests/wrong_type/dot_operator_int.jou | 2 +- tests/wrong_type/dot_operator_int_call.jou | 2 +- tests/wrong_type/dot_operator_intptr.jou | 2 +- tests/wrong_type/dot_operator_intptr_call.jou | 2 +- 12 files changed, 49 insertions(+), 100 deletions(-) diff --git a/compiler/typecheck/step3_function_and_method_bodies.jou b/compiler/typecheck/step3_function_and_method_bodies.jou index 404943f1..dbe049cf 100644 --- a/compiler/typecheck/step3_function_and_method_bodies.jou +++ b/compiler/typecheck/step3_function_and_method_bodies.jou @@ -869,59 +869,29 @@ def typecheck_expression(ft: FileTypes*, expr: AstExpression*) -> None: case AstExpressionKind.GetClassField: if expr->class_field.uses_arrow_operator: temptype = typecheck_expression_not_void(ft, expr->class_field.instance) - match temptype->kind: - case TypeKind.Class: - snprintf( - msg, sizeof(msg), - "left side of '.' operator must be a pointer to an instance of a class, not %s (try . instead of ->)", - temptype->name) - fail(expr->location, msg) - case TypeKind.Pointer: - if temptype->value_type->kind != TypeKind.Class: - snprintf( - msg, sizeof(msg), - "left side of the '->' operator must be a pointer to an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) - case _: - snprintf( - msg, sizeof(msg), - "left side of the '->' operator must be a pointer to an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) -# if temptype->kind == TypeKind.Class and strlen(msg) + 50 < sizeof(msg): -# strcat(msg, " (try . instead of ->)") + if temptype->kind != TypeKind.Pointer or temptype->value_type->kind != TypeKind.Class: + snprintf( + msg, sizeof(msg), + "left side of '->' operator must be a pointer to an instance of a class, not %s", + temptype->name) + if temptype->kind == TypeKind.Class and strlen(msg) + 50 < sizeof(msg): + strcat(msg, " (try . instead of ->)") + fail(expr->location, msg) result = typecheck_class_field(temptype->value_type, expr->class_field.field_name, expr->location)->type else: temptype = typecheck_expression_not_void(ft, expr->class_field.instance) - match temptype->kind: - case TypeKind.Class: - pass - case TypeKind.Pointer: - if temptype->value_type->kind == TypeKind.Class: - snprintf( - msg, sizeof(msg), - "left side of '.' operator must be an instance of a class, not %s (try -> instead of .)", - temptype->name) - fail(expr->location, msg) - else: - snprintf( - msg, sizeof(msg), - "left side of the '.' operator must be an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) - case _: - snprintf( - msg, sizeof(msg), - "left side of the '.' operator must be an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) -# if ( -# temptype->kind == TypeKind.Pointer -# and temptype->value_type->kind == TypeKind.Class -# and strlen(msg) + 50 < sizeof(msg) -# ): -# strcat(msg, " (try -> instead of .)") + if temptype->kind != TypeKind.Class: + snprintf( + msg, sizeof(msg), + "left side of '.' operator must be an instance of a class, not %s", + temptype->name) + if ( + temptype->kind == TypeKind.Pointer + and temptype->value_type->kind == TypeKind.Class + and strlen(msg) + 50 < sizeof(msg) + ): + strcat(msg, " (try -> instead of .)") + fail(expr->location, msg) result = typecheck_class_field(temptype, expr->class_field.field_name, expr->location)->type case AstExpressionKind.Call: @@ -929,48 +899,27 @@ def typecheck_expression(ft: FileTypes*, expr: AstExpression*) -> None: result = typecheck_function_or_method_call(ft, &expr->call, NULL, expr->location) elif expr->call.uses_arrow_operator: temptype = typecheck_expression_not_void(ft, expr->call.method_call_self) - match temptype->kind: - case TypeKind.Pointer: - if temptype->value_type->kind != TypeKind.Class: - snprintf(msg, sizeof(msg), - "left side of the '->' operator must be a pointer to an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) - case TypeKind.Class: - snprintf(msg, sizeof(msg), - "left side of '.' operator must be a pointer to an instance of a class, not %s (try . instead of ->)", - temptype->name) - fail(expr->location, msg) - case _: - snprintf(msg, sizeof(msg), - "left side of the '->' operator must be a pointer to an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) -# if temptype->kind == TypeKind.Class and strlen(msg) + 50 < sizeof(msg): -# strcat(msg, " (try . instead of ->)") -# fail(expr->location, msg) + if temptype->kind != TypeKind.Pointer or temptype->value_type->kind != TypeKind.Class: + snprintf(msg, sizeof(msg), + "left side of '->' operator must be a pointer to an instance of a class, not %s", + temptype->name) + if temptype->kind == TypeKind.Class and strlen(msg) + 50 < sizeof(msg): + strcat(msg, " (try . instead of ->)") + fail(expr->location, msg) result = typecheck_function_or_method_call(ft, &expr->call, temptype->value_type, expr->location) else: temptype = typecheck_expression_not_void(ft, expr->call.method_call_self) - match temptype->kind: - case TypeKind.Pointer: - if temptype->value_type->kind == TypeKind.Class: - snprintf(msg, sizeof(msg), - "left side of the '.' operator must be an instance of a class, not %s (try -> instead of .)", - temptype->name) - fail(expr->location, msg) - else: - snprintf(msg, sizeof(msg), - "left side of the '.' operator must be an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) - case TypeKind.Class: - pass - case _: - snprintf(msg, sizeof(msg), - "left side of the '.' operator must be an instance of a class, not %s", - temptype->name) - fail(expr->location, msg) + if temptype->kind != TypeKind.Class: + snprintf(msg, sizeof(msg), + "left side of '.' operator must be an instance of a class, not %s", + temptype->name) + if ( + temptype->kind == TypeKind.Pointer + and temptype->value_type->kind == TypeKind.Class + and strlen(msg) + 50 < sizeof(msg) + ): + strcat(msg, " (try -> instead of .)") + fail(expr->location, msg) result = typecheck_function_or_method_call(ft, &expr->call, temptype, expr->location) diff --git a/tests/wrong_type/arrow_instead_of_dot.jou b/tests/wrong_type/arrow_instead_of_dot.jou index dfd3a45f..d236a9df 100644 --- a/tests/wrong_type/arrow_instead_of_dot.jou +++ b/tests/wrong_type/arrow_instead_of_dot.jou @@ -4,4 +4,4 @@ class Foo: def blah() -> None: f = Foo{} - f->n++ # Error: left side of '.' operator must be a pointer to an instance of a class, not Foo (try . instead of ->) + f->n++ # Error: left side of '->' operator must be a pointer to an instance of a class, not Foo (try . instead of ->) diff --git a/tests/wrong_type/arrow_instead_of_dot_call.jou b/tests/wrong_type/arrow_instead_of_dot_call.jou index d8ed5efd..59293d2c 100644 --- a/tests/wrong_type/arrow_instead_of_dot_call.jou +++ b/tests/wrong_type/arrow_instead_of_dot_call.jou @@ -4,4 +4,4 @@ class Foo: def blah() -> None: f = Foo{} - f->do_stuff() # Error: left side of '.' operator must be a pointer to an instance of a class, not Foo (try . instead of ->) + f->do_stuff() # Error: left side of '->' operator must be a pointer to an instance of a class, not Foo (try . instead of ->) diff --git a/tests/wrong_type/arrow_operator_int.jou b/tests/wrong_type/arrow_operator_int.jou index 721c54f8..ac893504 100644 --- a/tests/wrong_type/arrow_operator_int.jou +++ b/tests/wrong_type/arrow_operator_int.jou @@ -1,3 +1,3 @@ def foo() -> None: num = 1 - x = num->lol # Error: left side of the '->' operator must be a pointer to an instance of a class, not int + x = num->lol # Error: left side of '->' operator must be a pointer to an instance of a class, not int diff --git a/tests/wrong_type/arrow_operator_int_call.jou b/tests/wrong_type/arrow_operator_int_call.jou index d0a2d956..e742e897 100644 --- a/tests/wrong_type/arrow_operator_int_call.jou +++ b/tests/wrong_type/arrow_operator_int_call.jou @@ -1,3 +1,3 @@ def foo() -> None: num = 1 - x = num->lol() # Error: left side of the '->' operator must be a pointer to an instance of a class, not int + x = num->lol() # Error: left side of '->' operator must be a pointer to an instance of a class, not int diff --git a/tests/wrong_type/arrow_operator_intptr.jou b/tests/wrong_type/arrow_operator_intptr.jou index 78d4fd2b..986b235d 100644 --- a/tests/wrong_type/arrow_operator_intptr.jou +++ b/tests/wrong_type/arrow_operator_intptr.jou @@ -1,4 +1,4 @@ def foo() -> None: num = 1 pointer = &num - x = pointer->lol # Error: left side of the '->' operator must be a pointer to an instance of a class, not int* + x = pointer->lol # Error: left side of '->' operator must be a pointer to an instance of a class, not int* diff --git a/tests/wrong_type/arrow_operator_intptr_call.jou b/tests/wrong_type/arrow_operator_intptr_call.jou index f3cdccaa..5b541da3 100644 --- a/tests/wrong_type/arrow_operator_intptr_call.jou +++ b/tests/wrong_type/arrow_operator_intptr_call.jou @@ -1,4 +1,4 @@ def foo() -> None: num = 1 pointer = &num - x = pointer->lol() # Error: left side of the '->' operator must be a pointer to an instance of a class, not int* + x = pointer->lol() # Error: left side of '->' operator must be a pointer to an instance of a class, not int* diff --git a/tests/wrong_type/dot_instead_of_arrow_call.jou b/tests/wrong_type/dot_instead_of_arrow_call.jou index 36f1003b..9c88f58f 100644 --- a/tests/wrong_type/dot_instead_of_arrow_call.jou +++ b/tests/wrong_type/dot_instead_of_arrow_call.jou @@ -3,4 +3,4 @@ class Foo: def blah(ptr: Foo*) -> None: - ptr.foo() # Error: left side of the '.' operator must be an instance of a class, not Foo* (try -> instead of .) + ptr.foo() # Error: left side of '.' operator must be an instance of a class, not Foo* (try -> instead of .) diff --git a/tests/wrong_type/dot_operator_int.jou b/tests/wrong_type/dot_operator_int.jou index 971fb8b9..6e52940b 100644 --- a/tests/wrong_type/dot_operator_int.jou +++ b/tests/wrong_type/dot_operator_int.jou @@ -1,3 +1,3 @@ def foo() -> None: x = 123 - y = x.lolwat # Error: left side of the '.' operator must be an instance of a class, not int + y = x.lolwat # Error: left side of '.' operator must be an instance of a class, not int diff --git a/tests/wrong_type/dot_operator_int_call.jou b/tests/wrong_type/dot_operator_int_call.jou index 120cfaa5..6e0e953f 100644 --- a/tests/wrong_type/dot_operator_int_call.jou +++ b/tests/wrong_type/dot_operator_int_call.jou @@ -1,3 +1,3 @@ def foo() -> None: x = 123 - y = x.lolwat() # Error: left side of the '.' operator must be an instance of a class, not int + y = x.lolwat() # Error: left side of '.' operator must be an instance of a class, not int diff --git a/tests/wrong_type/dot_operator_intptr.jou b/tests/wrong_type/dot_operator_intptr.jou index d02d77fb..bfd3dc50 100644 --- a/tests/wrong_type/dot_operator_intptr.jou +++ b/tests/wrong_type/dot_operator_intptr.jou @@ -1,4 +1,4 @@ def foo() -> None: x = 123 y = &x - z = y.lolwat # Error: left side of the '.' operator must be an instance of a class, not int* + z = y.lolwat # Error: left side of '.' operator must be an instance of a class, not int* diff --git a/tests/wrong_type/dot_operator_intptr_call.jou b/tests/wrong_type/dot_operator_intptr_call.jou index 6df39b5e..b94cf104 100644 --- a/tests/wrong_type/dot_operator_intptr_call.jou +++ b/tests/wrong_type/dot_operator_intptr_call.jou @@ -1,4 +1,4 @@ def foo() -> None: x = 123 y = &x - z = y.lolwat() # Error: left side of the '.' operator must be an instance of a class, not int* + z = y.lolwat() # Error: left side of '.' operator must be an instance of a class, not int*