Skip to content

Support for nested ternary #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ public void TypeTesting(string expression, Type type)
[TestCase("Abs(-4) > 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
[TestCase("Abs(-4) > 10 / 2?(true ? 6 : 3+2):(false?Abs(-18):100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
[TestCase("1==1?true:false", ExpectedResult = true, Category = "Conditional Operator t ? x : y")]
[TestCase("1 == 2 ? 3 == 4 ? 1 : 0 : 0", ExpectedResult = 0, Category = "Conditional Operator t ? x : y")]
[TestCase("1 == 1 ? 3 == 3 ? 1 : 0 : 0", ExpectedResult = 1, Category = "Conditional Operator t ? x : y")]
[TestCase("false ? true ? 10 : 20 : 30", ExpectedResult = 30, Category = "Conditional Operator t ? x : y")]
[TestCase("true ? false ? 10 : 20 : 30", ExpectedResult = 20, Category = "Conditional Operator t ? x : y")]
#endregion

#region Math Constants
Expand Down
24 changes: 20 additions & 4 deletions CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,8 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta
bool condition = (bool)ProcessStack(stack);

string restOfExpression = expression.Substring(i + 1);
// Track nesting level of ternary operators
int ternaryNestingLevel = 0;

for (int j = 0; j < restOfExpression.Length; j++)
{
Expand All @@ -2825,15 +2827,29 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta
j++;
GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(restOfExpression, ref j, false);
}
else if (s2.Equals("?"))
{
// Found nested ternary operator, increase nesting level
ternaryNestingLevel++;
}
else if (s2.Equals(":"))
{
stack.Clear();
if (ternaryNestingLevel == 0)
{
// This colon belongs to our ternary operator
stack.Clear();

stack.Push(condition ? Evaluate(restOfExpression.Substring(0, j)) : Evaluate(restOfExpression.Substring(j + 1)));
stack.Push(condition ? Evaluate(restOfExpression.Substring(0, j)) : Evaluate(restOfExpression.Substring(j + 1)));

i = expression.Length;
i = expression.Length;

return true;
return true;
}
else
{
// This colon belongs to a nested ternary operator, decrease nesting level
ternaryNestingLevel--;
}
}
}
}
Expand Down