Skip to content

Commit

Permalink
Fix parentheses around multi-expressions in calls
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Jul 9, 2024
1 parent 239c135 commit 432dbdf
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Underanalyzer/Decompiler/AST/Nodes/FunctionDeclNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Underanalyzer.Decompiler.AST;
/// <summary>
/// A function declaration within the AST.
/// </summary>
public class FunctionDeclNode : IFragmentNode, IExpressionNode, IConditionalValueNode
public class FunctionDeclNode : IFragmentNode, IMultiExpressionNode, IConditionalValueNode
{
/// <summary>
/// Name of the function, or null if anonymous.
Expand Down
13 changes: 12 additions & 1 deletion Underanalyzer/Decompiler/AST/Nodes/VariableCallNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ IStatementNode IASTNode<IStatementNode>.Clean(ASTCleaner cleaner)

public void Print(ASTPrinter printer)
{
bool canGenerateParentheses = true;
if (Instance is not null)
{
if (Function is VariableNode variable && variable is { Left: InstanceTypeNode instType } &&
Expand All @@ -86,10 +87,20 @@ public void Print(ASTPrinter printer)
{
Instance.Print(printer);
printer.Write('.');
canGenerateParentheses = false;
}
}
}
Function.Print(printer);
if (canGenerateParentheses && Function is IMultiExpressionNode)
{
printer.Write('(');
Function.Print(printer);
printer.Write(')');
}
else
{
Function.Print(printer);
}
printer.Write('(');
for (int i = 0; i < Arguments.Count; i++)
{
Expand Down
61 changes: 61 additions & 0 deletions UnderanalyzerTest/DecompileContext.DecompileToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,4 +2164,65 @@ b [8]
"""
);
}

[Fact]
public void TestFunctionDeclCall()
{
TestUtil.VerifyDecompileResult(
"""
:[0]
call.i @@This@@ 0
b [2]

> gml_Script_anon@1@A (locals=0, args=0)
:[1]
pushi.e 1
pop.v.i builtin.a
exit.i

:[2]
push.i [function]gml_Script_anon@1@A
conv.i.v
pushi.e -1
conv.i.v
call.i method 2
callv.v 0
popz.v
push.v builtin.a
push.v builtin.c
pushi.e -9
push.v [stacktop]self.d
dup.v 1 1
dup.v 0
push.v stacktop.b
callv.v 1
popz.v
pushi.e 123
conv.i.v
call.i @@This@@ 0
push.v builtin.a
conv.v.b
bf [4]

:[3]
push.v builtin.b
b [5]

:[4]
push.v builtin.c

:[5]
callv.v 1
popz.v
""",
"""
(function()
{
a = 1;
})();
a.b(c.d);
(a ? b : c)(123);
"""
);
}
}

0 comments on commit 432dbdf

Please sign in to comment.