unary op bugfix

This commit is contained in:
nub31
2025-09-28 23:13:07 +02:00
parent 268a9ac474
commit 2ac16e6aa7
2 changed files with 25 additions and 12 deletions

View File

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