Fix binop bugs

This commit is contained in:
nub31
2025-09-28 23:01:06 +02:00
parent 0efde77d2c
commit d6b313a2b3
2 changed files with 51 additions and 11 deletions

View File

@@ -419,17 +419,48 @@ public sealed class TypeChecker
{ {
case BinaryOperatorSyntax.Equal: case BinaryOperatorSyntax.Equal:
case BinaryOperatorSyntax.NotEqual: 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.GreaterThan:
case BinaryOperatorSyntax.GreaterThanOrEqual: case BinaryOperatorSyntax.GreaterThanOrEqual:
case BinaryOperatorSyntax.LessThan: case BinaryOperatorSyntax.LessThan:
case BinaryOperatorSyntax.LessThanOrEqual: case BinaryOperatorSyntax.LessThanOrEqual:
case BinaryOperatorSyntax.LogicalAnd:
case BinaryOperatorSyntax.LogicalOr:
{ {
var left = CheckExpression(expression.Left); var left = CheckExpression(expression.Left);
if (left.Type is not NubIntType and not NubFloatType) 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); var right = CheckExpression(expression.Right, left.Type);
@@ -445,7 +476,10 @@ public sealed class TypeChecker
return new BinaryExpressionNode(left.Type, left, op, right); 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.Minus:
case BinaryOperatorSyntax.Multiply: case BinaryOperatorSyntax.Multiply:
@@ -455,7 +489,10 @@ public sealed class TypeChecker
var left = CheckExpression(expression.Left); var left = CheckExpression(expression.Left);
if (left.Type is not NubIntType and not NubFloatType) 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); var right = CheckExpression(expression.Right, left.Type);
@@ -471,7 +508,10 @@ public sealed class TypeChecker
var left = CheckExpression(expression.Left); var left = CheckExpression(expression.Left);
if (left.Type is not NubIntType) 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); var right = CheckExpression(expression.Right, left.Type);

View File

@@ -8,12 +8,12 @@ extern "main" func main(args: []cstring): i64
raylib::InitWindow(1600, 900, "Hi from nub-lang") raylib::InitWindow(1600, 900, "Hi from nub-lang")
raylib::SetTargetFPS(240) raylib::SetTargetFPS(240)
let x: i32 = 0
let y: i32 = 0
let width: i32 = 100 let width: i32 = 100
let height: 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 direction: raylib::Vector2 = { x = 1 y = 1 }
let bgColor: raylib::Color = { r = 0 g = 0 b = 0 a = 255 } 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() raylib::EndDrawing()
if x + width > raylib::GetScreenWidth() if x + width >= raylib::GetScreenWidth() || x <= 0
{ {
direction.x = -direction.x direction.x = -direction.x
} }
if y + height > raylib::GetScreenHeight() if y + height >= raylib::GetScreenHeight() || y <= 0
{ {
direction.y = -direction.y direction.y = -direction.y
} }