Skip to content

Commit

Permalink
fix error optimizing clause containing only always true/false express…
Browse files Browse the repository at this point in the history
…ions (#488)
  • Loading branch information
Jamiras authored May 22, 2024
1 parent 5a5f3ed commit 99c6ea3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Source/Parser/Expressions/Trigger/RequirementClauseExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,9 @@ public override RequirementExpressionBase Optimize(TriggerBuilderContext context
if (_conditions == null || _conditions.Count == 0)
return this;

if (_conditions.Count == 1)
return _conditions[0].Optimize(context);

var achievementContext = context as AchievementBuilderContext;
if (achievementContext != null && achievementContext.HasPauseIf == null)
{
Expand Down Expand Up @@ -1115,6 +1118,17 @@ public override RequirementExpressionBase Optimize(TriggerBuilderContext context
newRequirements.RemoveAt(i);
}
}
else if (newRequirements.Count == 0)
{
// the only way for there to be 0 conditions left at this point is if all the
// conditions evaluated to true when Operation was And, or all the conditions
// evaluated to false when Operation was Or. return a singular expression
// reflecting that result.
RequirementExpressionBase result = (Operation == ConditionalOperation.And) ?
new AlwaysTrueExpression() : new AlwaysFalseExpression();
CopyLocation(result);
return result;
}
else
{
if (newRequirements.Count < 100)
Expand Down Expand Up @@ -1638,7 +1652,7 @@ public override ExpressionBase Combine(ExpressionBase right, ConditionalOperatio
/// </returns>
public RequirementClauseExpression EnsureLastConditionHasNoHitTarget()
{
if (_conditions != null)
if (_conditions != null && _conditions.Count > 0)
{
var lastCondition = _conditions.Last();

Expand Down
1 change: 1 addition & 0 deletions Tests/Parser/Functions/RepeatedFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void TestAppendString(string input)
"N:0xH004567=4_Z:0xH005678=5_N:0xH001234=1_O:0xH002345=2_0xH003456=3.23.")]
[TestCase("repeated(24, once(bit3(0x1234) < prev(bit3(0x1234)) && never(repeated(3, bit3(0x1234) == 0))) && bit5(0x1234) == 0)",
"Z:0xP001234=0.3._N:0xP001234<d0xP001234.1._0xR001234=0.24.")]
[TestCase("repeated(25, always_true() && never(byte(0x1234) == 1)", "Z:0xH001234=1_1=1.25.")]
public void TestBuildTrigger(string input, string expected)
{
var clause = TriggerExpressionTests.Parse<TalliedRequirementExpression>(input);
Expand Down

0 comments on commit 99c6ea3

Please sign in to comment.