Skip to content

Commit

Permalink
added logic to catch unmatched closing paren
Browse files Browse the repository at this point in the history
  • Loading branch information
bcwood committed May 15, 2014
1 parent 968c281 commit 2571b54
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions dotMath/EquationCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class EquationCompiler
private Token _currentToken;
private Token _nextToken;
private IEnumerator _tokenEnumerator;
private Stack _parentheses = new Stack();
private Dictionary<string, CVariable> _variables = new Dictionary<string, CVariable>();
private Dictionary<string, COperator> _operators = new Dictionary<string, COperator>();
private Dictionary<string, CFunction> _functions = new Dictionary<string, CFunction>();
Expand Down Expand Up @@ -108,7 +109,7 @@ public void Compile()
}

/// <summary>
/// Calls into the runnable function set to evaluate the function and returns the result.
/// Evaluates the function and returns the result.
/// </summary>
/// <returns>double value evaluation of the function in its current state</returns>
public double Calculate()
Expand All @@ -118,10 +119,42 @@ public double Calculate()

if (_nextToken != null)
throw new ApplicationException("Unrecognized token: " + _currentToken.ToString());
if (_parentheses.Count > 0)
throw new ApplicationException("Unmatched parentheses in equation.");

return _function.GetValue();
}

/// <summary>
/// Adds a custom function with the given name.
/// </summary>
/// <param name="name">Name of function.</param>
/// <param name="function">Function delegate.</param>
public void AddFunction(string name, Func<double, double> function)
{
_functions.Add(name, new CFunction(function));
}

/// <summary>
/// Adds a custom function with the given name.
/// </summary>
/// <param name="name">Name of function.</param>
/// <param name="function">Function delegate.</param>
public void AddFunction(string name, Func<double, double, double> function)
{
_functions.Add(name, new CFunction(function));
}

/// <summary>
/// Adds a custom function with the given name.
/// </summary>
/// <param name="name">Name of function.</param>
/// <param name="function">Function delegate.</param>
public void AddFunction(string name, Func<bool, double, double, double> function)
{
_functions.Add(name, new CFunction(function));
}

#region Operations and Compiling Functions

/// <summary>
Expand All @@ -144,7 +177,7 @@ private CValue Paren()
return value;

if (!string.Equals(_currentToken, ")"))
throw new ApplicationException("Unmatched parenthesis in equation.");
throw new ApplicationException("Unmatched parentheses in equation.");
}
else
{
Expand Down Expand Up @@ -431,21 +464,6 @@ private void InitFunctions()
});
}

public void AddFunction(string name, Func<double, double> function)
{
_functions.Add(name, new CFunction(function));
}

public void AddFunction(string name, Func<double, double, double> function)
{
_functions.Add(name, new CFunction(function));
}

public void AddFunction(string name, Func<bool, double, double, double> function)
{
_functions.Add(name, new CFunction(function));
}

/// <summary>
/// Manipulates the current Token position forward in the chain of tokens discovered by the parser.
/// </summary>
Expand All @@ -461,6 +479,20 @@ private void NextToken()

_currentToken = _nextToken;

if (string.Equals(_currentToken, "("))
_parentheses.Push(_currentToken);
else if (string.Equals(_currentToken, ")"))
{
try
{
_parentheses.Pop();
}
catch (InvalidOperationException)
{
throw new ApplicationException("Unmatched parentheses in equation.");
}
}

if (_tokenEnumerator.MoveNext())
_nextToken = (Token) _tokenEnumerator.Current;
else
Expand Down

0 comments on commit 2571b54

Please sign in to comment.