diff --git a/Source/Parser/Expressions/IntegerConstantExpression.cs b/Source/Parser/Expressions/IntegerConstantExpression.cs index c050bf51..a3ed8797 100644 --- a/Source/Parser/Expressions/IntegerConstantExpression.cs +++ b/Source/Parser/Expressions/IntegerConstantExpression.cs @@ -136,6 +136,19 @@ public ExpressionBase Combine(ExpressionBase right, MathematicOperation operatio return stringLeft.Combine(right, operation); } + if (operation == MathematicOperation.Multiply) + { + if (Value == 1) + return right; + if (Value == 0) + return this; + } + else if (operation == MathematicOperation.Add) + { + if (Value == 0) + return right; + } + return null; } diff --git a/Source/Parser/Expressions/Trigger/MemoryValueExpression.cs b/Source/Parser/Expressions/Trigger/MemoryValueExpression.cs index 9c820e1b..e43e7dd4 100644 --- a/Source/Parser/Expressions/Trigger/MemoryValueExpression.cs +++ b/Source/Parser/Expressions/Trigger/MemoryValueExpression.cs @@ -217,6 +217,13 @@ public ExpressionBase Combine(ExpressionBase right, MathematicOperation operatio } var combined = integerConstant.Combine(right, operation); + if (combined == null) + { + // cannot directly combine these. try wrapping in remember. + var rememberRecallExpression = new RememberRecallExpression(this); + return rememberRecallExpression.Combine(right, operation); + } + var result = clone.ApplyMathematic(combined, MathematicOperation.Add); if (result != null) return result; diff --git a/Tests/Parser/Expressions/IntegerConstantExpressionTests.cs b/Tests/Parser/Expressions/IntegerConstantExpressionTests.cs index 0feaaee6..18547a4e 100644 --- a/Tests/Parser/Expressions/IntegerConstantExpressionTests.cs +++ b/Tests/Parser/Expressions/IntegerConstantExpressionTests.cs @@ -44,6 +44,9 @@ public void TestAppendStringNegative() [TestCase("7", "/", "3.5", ExpressionType.FloatConstant, "2.0")] [TestCase("5", "%", "3.5", ExpressionType.FloatConstant, "1.5")] [TestCase("1", "+", "\"A\"", ExpressionType.StringConstant, "\"1A\"")] + [TestCase("1", "*", "byte(1)", ExpressionType.MemoryAccessor, "byte(0x000001)")] + [TestCase("0", "*", "byte(1)", ExpressionType.IntegerConstant, "0")] + [TestCase("0", "+", "byte(1)", ExpressionType.MemoryAccessor, "byte(0x000001)")] public void TestCombine(string left, string operation, string right, ExpressionType expectedType, string expected) { ExpressionTests.AssertCombine(left, operation, right, expectedType, expected); diff --git a/Tests/Parser/Expressions/Trigger/MemoryValueExpressionTests.cs b/Tests/Parser/Expressions/Trigger/MemoryValueExpressionTests.cs index 3d574049..4135159f 100644 --- a/Tests/Parser/Expressions/Trigger/MemoryValueExpressionTests.cs +++ b/Tests/Parser/Expressions/Trigger/MemoryValueExpressionTests.cs @@ -81,6 +81,10 @@ public void TestBuildTrigger(string input, string expected) ExpressionType.MemoryAccessor, "remembered(bit1(0x000001) + 1) / 2")] // adjustment not evenly divisible and can't have division distributed [TestCase("bit1(0x0001) * 2 + bit2(0x0001) * 2", "/", "2", ExpressionType.MemoryAccessor, "bit1(0x000001) + bit2(0x000001)")] // factor on each subcondition can be reduced + [TestCase("dword(dword(0x001234) + 4)", "*", "(byte(0x0001) * 2) + 3", + ExpressionType.MemoryAccessor, "dword(dword(0x001234) + 0x04) * remembered(byte(0x000001) * 2 + 3)")] + [TestCase("(byte(0x0001) * 2) + 3", "*", "dword(dword(0x001234) + 4)", + ExpressionType.MemoryAccessor, "dword(dword(0x001234) + 0x04) * remembered(byte(0x000001) * 2 + 3)")] public void TestCombine(string left, string operation, string right, ExpressionType expectedType, string expected) { ExpressionTests.AssertCombine(left, operation, right, expectedType, expected);