Correct inverted if blocks in scripts

This commit is contained in:
Yoshi Askharoun
2025-07-20 10:16:46 -05:00
parent 9849ca2a98
commit 8cbf503008
2 changed files with 47 additions and 6 deletions
+44 -3
View File
@@ -149,11 +149,11 @@ partial class Decompiler
var jumpCondition = opCode switch
{
OpCode.JumpIfFalse or
OpCode.JumpIfFalsePeek => PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, rawJumpCondition),
OpCode.JumpIfFalsePeek => rawJumpCondition,
OpCode.JumpIfTruePeek => rawJumpCondition,
OpCode.JumpIfTruePeek => LogicalNotOf(rawJumpCondition),
OpCode.JumpIfNullPeek => BinaryExpression(SyntaxKind.EqualsExpression,
OpCode.JumpIfNullPeek => BinaryExpression(SyntaxKind.ExclamationEqualsToken,
rawJumpCondition,
LiteralExpression(SyntaxKind.NullLiteralExpression)),
@@ -164,6 +164,9 @@ partial class Decompiler
blockStack.Push(ifBlock);
break;
case OpCode.ReturnVoid:
break;
default:
if (!TryDecompileExpression(instruction, stack, blockStack))
throw new NotImplementedException();
@@ -366,6 +369,44 @@ partial class Decompiler
return operationExpr;
}
private static ExpressionSyntax LogicalNotOf(ExpressionSyntax originalExpression)
{
if (originalExpression is not BinaryExpressionSyntax binaryExpression)
goto defaultCase;
SyntaxKind notOperatorToken;
switch (binaryExpression.OperatorToken.Kind())
{
case SyntaxKind.EqualsEqualsToken:
notOperatorToken = SyntaxKind.ExclamationEqualsToken;
break;
case SyntaxKind.LessThanToken:
notOperatorToken = SyntaxKind.GreaterThanEqualsToken;
break;
case SyntaxKind.GreaterThanToken:
notOperatorToken = SyntaxKind.LessThanEqualsToken;
break;
case SyntaxKind.LessThanEqualsToken:
notOperatorToken = SyntaxKind.GreaterThanToken;
break;
case SyntaxKind.GreaterThanEqualsToken:
notOperatorToken = SyntaxKind.LessThanToken;
break;
default:
goto defaultCase;
}
return binaryExpression.WithOperatorToken(Token(notOperatorToken));
defaultCase:
return PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, originalExpression);
}
private static SyntaxKind OperationToSyntaxKind(OperationType operation)
{
return operation switch