From 2ac16e6aa7ec3d1dcaa67071c6ce85a398bdc466 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sun, 28 Sep 2025 23:13:07 +0200 Subject: [PATCH] unary op bugfix --- compiler/NubLang/TypeChecking/TypeChecker.cs | 20 +++++++++++++------- example/src/main.nub | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/compiler/NubLang/TypeChecking/TypeChecker.cs b/compiler/NubLang/TypeChecking/TypeChecker.cs index ae1b6cf..4ad08d0 100644 --- a/compiler/NubLang/TypeChecking/TypeChecker.cs +++ b/compiler/NubLang/TypeChecking/TypeChecker.cs @@ -296,7 +296,7 @@ public sealed class TypeChecker ArrayIndexAccessSyntax expression => CheckArrayIndexAccess(expression), ArrayInitializerSyntax expression => CheckArrayInitializer(expression), BinaryExpressionSyntax expression => CheckBinaryExpression(expression), - UnaryExpressionSyntax expression => CheckUnaryExpression(expression), + UnaryExpressionSyntax expression => CheckUnaryExpression(expression, expectedType), DereferenceSyntax expression => CheckDereference(expression), DotFuncCallSyntax expression => CheckDotFuncCall(expression), FuncCallSyntax expression => CheckFuncCall(expression), @@ -525,26 +525,32 @@ public sealed class TypeChecker } } - private UnaryExpressionNode CheckUnaryExpression(UnaryExpressionSyntax expression) + private UnaryExpressionNode CheckUnaryExpression(UnaryExpressionSyntax expression, NubType? expectedType) { switch (expression.Operator) { case UnaryOperatorSyntax.Negate: { - var operand = CheckExpression(expression.Operand); - if (operand.Type is not NubIntType { Signed: false } and not NubFloatType) + var operand = CheckExpression(expression.Operand, expectedType); + if (operand.Type is not NubIntType { Signed: true } and not NubFloatType) { - throw new TypeCheckerException(Diagnostic.Error("Negation operator must be used with signed integer or float types").At(expression).Build()); + throw new TypeCheckerException(Diagnostic + .Error("Negation operator must be used with signed integer or float types") + .At(expression) + .Build()); } return new UnaryExpressionNode(operand.Type, UnaryOperator.Negate, operand); } case UnaryOperatorSyntax.Invert: { - var operand = CheckExpression(expression.Operand); + var operand = CheckExpression(expression.Operand, expectedType); if (operand.Type is not NubBoolType) { - throw new TypeCheckerException(Diagnostic.Error("Invert operator must be used with booleans").At(expression).Build()); + throw new TypeCheckerException(Diagnostic + .Error("Invert operator must be used with booleans") + .At(expression) + .Build()); } return new UnaryExpressionNode(operand.Type, UnaryOperator.Invert, operand); diff --git a/example/src/main.nub b/example/src/main.nub index 21aab74..9c2657f 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -28,14 +28,21 @@ extern "main" func main(args: []cstring): i64 } raylib::EndDrawing() - if x + width >= raylib::GetScreenWidth() || x <= 0 + if x <= 0 { - direction.x = -direction.x + direction.x = 1 } - - if y + height >= raylib::GetScreenHeight() || y <= 0 + else if x + width >= raylib::GetScreenWidth() { - direction.y = -direction.y + direction.x = -1 + } + else if y <= 0 + { + direction.y = 1 + } + else if y + height >= raylib::GetScreenHeight() + { + direction.y = -1 } x = x + @floatToInt(i32, direction.x)