From 268a9ac474f1b27c224524fdc2002dd247b9e6f4 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sun, 28 Sep 2025 23:01:06 +0200 Subject: [PATCH] Fix binop bugs --- compiler/NubLang/TypeChecking/TypeChecker.cs | 52 +++++++++++++++++--- example/src/main.nub | 10 ++-- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/compiler/NubLang/TypeChecking/TypeChecker.cs b/compiler/NubLang/TypeChecking/TypeChecker.cs index ceecac8..ae1b6cf 100644 --- a/compiler/NubLang/TypeChecking/TypeChecker.cs +++ b/compiler/NubLang/TypeChecking/TypeChecker.cs @@ -419,17 +419,48 @@ public sealed class TypeChecker { case BinaryOperatorSyntax.Equal: case BinaryOperatorSyntax.NotEqual: + { + var left = CheckExpression(expression.Left); + if (left.Type is not NubIntType and not NubFloatType and not NubBoolType) + { + throw new TypeCheckerException(Diagnostic + .Error("Equal and not equal operators must must be used with int, float or bool types") + .At(expression.Left) + .Build()); + } + + var right = CheckExpression(expression.Right, left.Type); + + return new BinaryExpressionNode(new NubBoolType(), left, op, right); + } case BinaryOperatorSyntax.GreaterThan: case BinaryOperatorSyntax.GreaterThanOrEqual: case BinaryOperatorSyntax.LessThan: case BinaryOperatorSyntax.LessThanOrEqual: - case BinaryOperatorSyntax.LogicalAnd: - case BinaryOperatorSyntax.LogicalOr: { var left = CheckExpression(expression.Left); if (left.Type is not NubIntType and not NubFloatType) { - throw new TypeCheckerException(Diagnostic.Error("Logical operators must must be used with int or float types").At(expression.Left).Build()); + throw new TypeCheckerException(Diagnostic + .Error("Greater than and less than operators must must be used with int or float types") + .At(expression.Left) + .Build()); + } + + var right = CheckExpression(expression.Right, left.Type); + + return new BinaryExpressionNode(new NubBoolType(), left, op, right); + } + case BinaryOperatorSyntax.LogicalAnd: + case BinaryOperatorSyntax.LogicalOr: + { + var left = CheckExpression(expression.Left); + if (left.Type is not NubBoolType) + { + throw new TypeCheckerException(Diagnostic + .Error("Logical and/or must must be used with bool types") + .At(expression.Left) + .Build()); } var right = CheckExpression(expression.Right, left.Type); @@ -445,7 +476,10 @@ public sealed class TypeChecker return new BinaryExpressionNode(left.Type, left, op, right); } - throw new TypeCheckerException(Diagnostic.Error("The plus operator must be used with int, float, cstring or string types").At(expression.Left).Build()); + throw new TypeCheckerException(Diagnostic + .Error("The plus operator must be used with int, float, cstring or string types") + .At(expression.Left) + .Build()); } case BinaryOperatorSyntax.Minus: case BinaryOperatorSyntax.Multiply: @@ -455,7 +489,10 @@ public sealed class TypeChecker var left = CheckExpression(expression.Left); if (left.Type is not NubIntType and not NubFloatType) { - throw new TypeCheckerException(Diagnostic.Error("Math operators must be used with int or float types").At(expression.Left).Build()); + throw new TypeCheckerException(Diagnostic + .Error("Math operators must be used with int or float types") + .At(expression.Left) + .Build()); } var right = CheckExpression(expression.Right, left.Type); @@ -471,7 +508,10 @@ public sealed class TypeChecker var left = CheckExpression(expression.Left); if (left.Type is not NubIntType) { - throw new TypeCheckerException(Diagnostic.Error("Bitwise operators must be used with int types").At(expression.Left).Build()); + throw new TypeCheckerException(Diagnostic + .Error("Bitwise operators must be used with int types") + .At(expression.Left) + .Build()); } var right = CheckExpression(expression.Right, left.Type); diff --git a/example/src/main.nub b/example/src/main.nub index 2f470c1..21aab74 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -8,12 +8,12 @@ extern "main" func main(args: []cstring): i64 raylib::InitWindow(1600, 900, "Hi from nub-lang") raylib::SetTargetFPS(240) - let x: i32 = 0 - let y: i32 = 0 - let width: i32 = 100 let height: i32 = 100 + let x: i32 = raylib::GetScreenWidth() / 2 - (width / 2) + let y: i32 = raylib::GetScreenHeight() / 2 - (height / 2) + let direction: raylib::Vector2 = { x = 1 y = 1 } let bgColor: raylib::Color = { r = 0 g = 0 b = 0 a = 255 } @@ -28,12 +28,12 @@ extern "main" func main(args: []cstring): i64 } raylib::EndDrawing() - if x + width > raylib::GetScreenWidth() + if x + width >= raylib::GetScreenWidth() || x <= 0 { direction.x = -direction.x } - if y + height > raylib::GetScreenHeight() + if y + height >= raylib::GetScreenHeight() || y <= 0 { direction.y = -direction.y }