Parenthesize expressions used with operators

This commit is contained in:
Yoshi Askharoun
2025-07-28 23:27:25 -05:00
parent c67e9728f9
commit f491df4c66
2 changed files with 34 additions and 24 deletions
+29 -19
View File
@@ -36,6 +36,7 @@ partial class Decompiler
var controlBlocks = ControlFlowAnalyzer.CreateGraph(methodBody);
var dotGraph = ControlFlowAnalyzer.SerializeToGraphviz(controlBlocks);
Console.WriteLine(dotGraph);
Stack<CodeBlockInfo> blockStack = [];
blockStack.Push(new(0, methodBody[^1].Offset, SyntaxKind.Block, null));
@@ -338,10 +339,10 @@ partial class Decompiler
}
else
{
var methodExpression = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
var methodExpression = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
methodTargetExpression,
IdentifierName(methodSchema.Name)
);
IdentifierName(methodSchema.Name)
);
methodResult = InvocationExpression(methodExpression, ArgumentList([.. parameters]));
}
@@ -403,8 +404,6 @@ partial class Decompiler
private ExpressionSyntax DecompileOperation(Instruction instruction, Stack<object> stack)
{
var opHost = _context.GetImportedType(instruction.Operands.ElementAt(0));
var op = (OperationType)(int)(byte)instruction.Operands.ElementAt(1).Value;
var isUnary = TypeSchema.IsUnaryOperation(op);
var opSyntax = OperationToSyntaxKind(op);
@@ -413,7 +412,7 @@ partial class Decompiler
if (isUnary)
{
var left = IrisExpression.ToSyntax(stack.Pop(), _context);
var left = ParenthesizedExpression(IrisExpression.ToSyntax(stack.Pop(), _context));
var isPostfix = op is OperationType.PostIncrement or OperationType.PostDecrement;
operationExpr = isPostfix
@@ -425,7 +424,10 @@ partial class Decompiler
var right = IrisExpression.ToSyntax(stack.Pop(), _context);
var left = IrisExpression.ToSyntax(stack.Pop(), _context);
operationExpr = BinaryExpression(opSyntax, left, right);
operationExpr = BinaryExpression(opSyntax,
ParenthesizedExpression(left),
ParenthesizedExpression(right)
);
}
stack.Push(operationExpr);
@@ -434,15 +436,21 @@ partial class Decompiler
private static ExpressionSyntax LogicalNotOf(ExpressionSyntax originalExpression)
{
if (originalExpression is IsPatternExpressionSyntax isPatternExpression)
ExpressionSyntax negatedExpression = null;
var innerExpression = originalExpression;
if (originalExpression is ParenthesizedExpressionSyntax parenthesizedExpression)
innerExpression = parenthesizedExpression.Expression;
if (innerExpression is IsPatternExpressionSyntax isPatternExpression)
{
var baseTypePattern = isPatternExpression.Pattern;
return isPatternExpression.WithPattern(UnaryPattern(baseTypePattern));
negatedExpression = isPatternExpression.WithPattern(UnaryPattern(baseTypePattern));
}
if (originalExpression is BinaryExpressionSyntax binaryExpression)
if (innerExpression is BinaryExpressionSyntax binaryExpression)
{
SyntaxKind notOperatorToken;
var notOperatorToken = SyntaxKind.None;
var operatorToken = binaryExpression.OperatorToken.Kind();
switch (operatorToken)
@@ -470,21 +478,23 @@ partial class Decompiler
case SyntaxKind.AmpersandAmpersandToken:
case SyntaxKind.BarBarToken:
// Apply De Morgan's laws
notOperatorToken = operatorToken is SyntaxKind.AmpersandAmpersandToken
var invertedOperatorToken = operatorToken is SyntaxKind.AmpersandAmpersandToken
? SyntaxKind.LogicalOrExpression : SyntaxKind.LogicalAndExpression;
return BinaryExpression(notOperatorToken,
negatedExpression = BinaryExpression(invertedOperatorToken,
LogicalNotOf(binaryExpression.Left),
LogicalNotOf(binaryExpression.Right));
default:
goto defaultCase;
break;
}
return binaryExpression.WithOperatorToken(Token(notOperatorToken));
negatedExpression ??= binaryExpression.WithOperatorToken(Token(notOperatorToken));
}
defaultCase:
return PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, ParenthesizedExpression(originalExpression));
negatedExpression ??= PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, ParenthesizedExpression(originalExpression));
if (originalExpression is ParenthesizedExpressionSyntax)
negatedExpression = ParenthesizedExpression(negatedExpression);
return negatedExpression;
}
private static SyntaxKind OperationToSyntaxKind(OperationType operation)
+5 -5
View File
@@ -358,7 +358,7 @@
<Inset Inset="{styles1:Styles.CellPadding}" Name="CellPadding" />
<Int32 Int32="11" Name="PrimaryPivotSpacing" />
<Int32 Int32="13" Name="SecondaryPivotSpacing" />
<Int32 Int32="{SecondaryPivotSpacing + 10}" Name="SecondaryPivotSpacingCDLand" />
<Int32 Int32="{(SecondaryPivotSpacing) + (10)}" Name="SecondaryPivotSpacingCDLand" />
<Single Single="0.3" Name="ContentTypesFilterAlpha" />
<Int32 Int32="5000" Name="PressEscapeToExitTimeout" />
<Image Source="res://ZuneShellResources!transparent.png" Name="TransparentPixel" />
@@ -833,16 +833,16 @@
if (zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage)
goto UIB_0038;
/* UIB_0038 */
if (zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage &amp;&amp; string.IsNullOrEmpty(zuneUI:Shell.SessionStartupPath))
if ((zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage) &amp;&amp; (string.IsNullOrEmpty(zuneUI:Shell.SessionStartupPath)))
goto UIB_0054;
/* UIB_0058 */
if (zuneUI:ZuneShell.DefaultInstance.CurrentPage is not zuneUI:QuickplayPage &amp;&amp; zuneUI:ZuneShell.DefaultInstance.CurrentPage is not zuneUI:StartupPage || !(string.IsNullOrEmpty(zuneUI:Shell.SessionStartupPath)) || configuration:ClientConfiguration.Shell.StartupPage != zuneUI:Shell.MainFrame.Quickplay.DefaultUIPath)
if ((zuneUI:ZuneShell.DefaultInstance.CurrentPage is not zuneUI:QuickplayPage) &amp;&amp; (((zuneUI:ZuneShell.DefaultInstance.CurrentPage is not zuneUI:StartupPage) || (!((string.IsNullOrEmpty(zuneUI:Shell.SessionStartupPath))))) || ((configuration:ClientConfiguration.Shell.StartupPage) != (zuneUI:Shell.MainFrame.Quickplay.DefaultUIPath))))
goto UIB_0083;
/* UIB_0066 */
if (zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage)
goto UIB_007F;
/* UIB_0083 */
if (zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:QuickplayPage || zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage &amp;&amp; string.IsNullOrEmpty(zuneUI:Shell.SessionStartupPath) &amp;&amp; configuration:ClientConfiguration.Shell.StartupPage == zuneUI:Shell.MainFrame.Quickplay.DefaultUIPath || zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage &amp;&amp; zuneUI:Shell.SessionStartupPath == zuneUI:Shell.MainFrame.Quickplay.DefaultUIPath)
if (((zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:QuickplayPage) || (((zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage) &amp;&amp; (string.IsNullOrEmpty(zuneUI:Shell.SessionStartupPath))) &amp;&amp; ((configuration:ClientConfiguration.Shell.StartupPage) == (zuneUI:Shell.MainFrame.Quickplay.DefaultUIPath)))) || ((zuneUI:ZuneShell.DefaultInstance.CurrentPage is zuneUI:StartupPage) &amp;&amp; ((zuneUI:Shell.SessionStartupPath) == (zuneUI:Shell.MainFrame.Quickplay.DefaultUIPath))))
goto UIB_0097;
source = QuickplayFrameBackground.Value;
goto UIB_00AA;
@@ -2636,7 +2636,7 @@ Default.Color = styles:Styles.NowPlayingMarketplaceColor;</Script>
<Boolean Name="AntialiasEdges" />
</Properties>
<Scripts>
<Script>AntialiasEdges = iris:Application.RenderingType != iris:RenderingType.GDI;</Script>
<Script>AntialiasEdges = (iris:Application.RenderingType) != (iris:RenderingType.GDI);</Script>
</Scripts>
</Class>
<Class Name="SyncBulletStyle" Base="style:StyleSet">