From feb2d1a2866b75ca3a09f1ad508a99c55f9c27e0 Mon Sep 17 00:00:00 2001 From: Devin Garner Date: Tue, 17 Dec 2024 08:29:03 -0700 Subject: [PATCH] Bug fix Jass Parser converting -2147483648 to --2147483648 --- .../Script/MapTriggersDecompiler.cs | 2 +- .../Extensions/ExpressionSyntaxExtensions.cs | 2 +- .../Parser/DecimalLiteralExpressionParser.cs | 2 +- .../Syntax/JassDecimalLiteralExpressionSyntax.cs | 11 +++++++++-- .../Parser/ExpressionParserTests.cs | 3 +++ 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/War3Net.CodeAnalysis.Decompilers/Script/MapTriggersDecompiler.cs b/src/War3Net.CodeAnalysis.Decompilers/Script/MapTriggersDecompiler.cs index ef32be5c..e480b7f4 100644 --- a/src/War3Net.CodeAnalysis.Decompilers/Script/MapTriggersDecompiler.cs +++ b/src/War3Net.CodeAnalysis.Decompilers/Script/MapTriggersDecompiler.cs @@ -156,7 +156,7 @@ setVariableStatement.Indexer is JassVariableReferenceExpressionSyntax i && var variableDefinition = mapTriggers.Variables.Single(v => string.Equals(v.Name, variableName, StringComparison.Ordinal)); - variableDefinition.ArraySize = arraySize; + variableDefinition.ArraySize = (int)arraySize; if (TryDecompileVariableDefinitionInitialValue(setVariableStatement.Value.Expression, variableDefinition.Type, out var initialValue)) { diff --git a/src/War3Net.CodeAnalysis.Jass/Extensions/ExpressionSyntaxExtensions.cs b/src/War3Net.CodeAnalysis.Jass/Extensions/ExpressionSyntaxExtensions.cs index 620a3cc1..9ec05df1 100644 --- a/src/War3Net.CodeAnalysis.Jass/Extensions/ExpressionSyntaxExtensions.cs +++ b/src/War3Net.CodeAnalysis.Jass/Extensions/ExpressionSyntaxExtensions.cs @@ -29,7 +29,7 @@ public static bool TryGetIntegerExpressionValue(this IExpressionSyntax expressio switch (expression) { case JassDecimalLiteralExpressionSyntax decimalLiteralExpression: - value = decimalLiteralExpression.Value; + value = (int)decimalLiteralExpression.Value; return true; case JassOctalLiteralExpressionSyntax octalLiteralExpression: diff --git a/src/War3Net.CodeAnalysis.Jass/Parser/DecimalLiteralExpressionParser.cs b/src/War3Net.CodeAnalysis.Jass/Parser/DecimalLiteralExpressionParser.cs index cdae40e6..f3dace28 100644 --- a/src/War3Net.CodeAnalysis.Jass/Parser/DecimalLiteralExpressionParser.cs +++ b/src/War3Net.CodeAnalysis.Jass/Parser/DecimalLiteralExpressionParser.cs @@ -17,7 +17,7 @@ internal partial class JassParser { internal static Parser GetDecimalLiteralExpressionParser() { - return Try(UnsignedInt(10)) + return Try(Long(10)) .Select(value => new JassDecimalLiteralExpressionSyntax(value)) .Labelled("decimal literal"); } diff --git a/src/War3Net.CodeAnalysis.Jass/Syntax/JassDecimalLiteralExpressionSyntax.cs b/src/War3Net.CodeAnalysis.Jass/Syntax/JassDecimalLiteralExpressionSyntax.cs index 2f5a6b36..12e557ea 100644 --- a/src/War3Net.CodeAnalysis.Jass/Syntax/JassDecimalLiteralExpressionSyntax.cs +++ b/src/War3Net.CodeAnalysis.Jass/Syntax/JassDecimalLiteralExpressionSyntax.cs @@ -5,18 +5,25 @@ // // ------------------------------------------------------------------------------ +using System; using System.Globalization; namespace War3Net.CodeAnalysis.Jass.Syntax { public class JassDecimalLiteralExpressionSyntax : IExpressionSyntax { - public JassDecimalLiteralExpressionSyntax(int value) + public JassDecimalLiteralExpressionSyntax(long value) { + if (value < Int32.MinValue || value-1 > Int32.MaxValue) + { + //workaround to allow -2147483648 to be wrapped as new JassUnaryExpressionSyntax(UnaryOperatorType.Minus, new JassDecimalLiteralExpressionSyntax(2147483648)) + throw new ArgumentException(); + } + Value = value; } - public int Value { get; init; } + public long Value { get; init; } public bool Equals(IExpressionSyntax? other) { diff --git a/tests/War3Net.CodeAnalysis.Jass.Tests/Parser/ExpressionParserTests.cs b/tests/War3Net.CodeAnalysis.Jass.Tests/Parser/ExpressionParserTests.cs index 66c35822..b928e2b8 100644 --- a/tests/War3Net.CodeAnalysis.Jass.Tests/Parser/ExpressionParserTests.cs +++ b/tests/War3Net.CodeAnalysis.Jass.Tests/Parser/ExpressionParserTests.cs @@ -196,6 +196,9 @@ public void TestExpressionParser(string expression, IExpressionSyntax? expected #endregion #region UnaryExpression + yield return new object?[] { @"-2147483647", new JassUnaryExpressionSyntax(UnaryOperatorType.Minus, new JassDecimalLiteralExpressionSyntax(2147483647)) }; + yield return new object?[] { @"-2147483648", new JassUnaryExpressionSyntax(UnaryOperatorType.Minus, new JassDecimalLiteralExpressionSyntax(2147483648)) }; + yield return new object?[] { @"--2147483648", new JassUnaryExpressionSyntax(UnaryOperatorType.Minus, new JassUnaryExpressionSyntax(UnaryOperatorType.Minus, new JassDecimalLiteralExpressionSyntax(2147483648))) }; yield return new object?[] { @"+6", new JassUnaryExpressionSyntax(UnaryOperatorType.Plus, new JassDecimalLiteralExpressionSyntax(6)) }; yield return new object?[] { @"-7", new JassUnaryExpressionSyntax(UnaryOperatorType.Minus, new JassDecimalLiteralExpressionSyntax(7)) }; yield return new object?[] { @"+ 6", new JassUnaryExpressionSyntax(UnaryOperatorType.Plus, new JassDecimalLiteralExpressionSyntax(6)) };