Skip to content

Commit bac8006

Browse files
committedMay 27, 2015
Code change needed for new tests to pass
1 parent 82b0fef commit bac8006

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed
 

‎Source/LoreSoft.MathExpressions/MathEvaluator.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class MathEvaluator : IDisposable
4242
private Stack<double> _parameters;
4343
private List<string> _innerFunctions;
4444
private uint _nestedFunctionDepth;
45+
private uint _nestedGroupDepth;
4546
private StringReader _expressionReader;
4647
private VariableDictionary _variables;
4748
private ReadOnlyCollection<string> _functions;
@@ -64,6 +65,7 @@ public MathEvaluator()
6465
_calculationStack = new Stack<double>();
6566
_parameters = new Stack<double>(2);
6667
_nestedFunctionDepth = 0;
68+
_nestedGroupDepth = 0;
6769
}
6870

6971

@@ -105,6 +107,7 @@ public double Evaluate(string expression)
105107
_expressionReader = new StringReader(expression);
106108
_symbolStack.Clear();
107109
_nestedFunctionDepth = 0;
110+
_nestedGroupDepth = 0;
108111
_expressionQueue.Clear();
109112

110113
ParseExpressionToQueue();
@@ -268,6 +271,7 @@ private bool TryStartGroup()
268271
}
269272

270273
_symbolStack.Push(_currentChar.ToString());
274+
_nestedGroupDepth++;
271275
return true;
272276
}
273277

@@ -276,7 +280,8 @@ private bool TryComma()
276280
if (_currentChar != ',')
277281
return false;
278282

279-
if (_nestedFunctionDepth <= 0)
283+
if (_nestedFunctionDepth <= 0 ||
284+
_nestedFunctionDepth < _nestedGroupDepth)
280285
{
281286
throw new ParseException(Resources.InvalidCharacterEncountered + _currentChar);
282287
}
@@ -309,7 +314,7 @@ private bool TryEndGroup()
309314

310315
bool hasStart = false;
311316

312-
while (_symbolStack.Count > 0)
317+
while (_symbolStack.Count > 0)
313318
{
314319
string p = _symbolStack.Pop();
315320
if (p == "(")
@@ -328,6 +333,8 @@ private bool TryEndGroup()
328333
_nestedFunctionDepth--;
329334
}
330335

336+
_nestedGroupDepth--;
337+
331338
break;
332339
}
333340

@@ -467,6 +474,12 @@ private double CalculateFromQueue()
467474
}
468475

469476
result = _calculationStack.Pop();
477+
478+
if (_calculationStack.Any())
479+
{
480+
throw new ParseException(String.Format("{0}Items '{1}' were remaining on calculation stack.", Resources.InvalidSymbolOnStack, string.Join(", ", _calculationStack)));
481+
}
482+
470483
return result;
471484
}
472485

0 commit comments

Comments
 (0)