Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Source/Parser/Expressions/IntegerConstantExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions Source/Parser/Expressions/Trigger/MemoryValueExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Tests/Parser/Expressions/IntegerConstantExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading