Fix binop bugs

This commit is contained in:
nub31
2025-09-28 23:01:06 +02:00
parent 48a2dc0ee7
commit 268a9ac474
2 changed files with 51 additions and 11 deletions

View File

@@ -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);