/// <summary>
 /// Initializes a new instance of the <see cref="MathEvaluator"/> class.
 /// </summary>
 public MathEvaluator()
 {
     _variables        = new VariableDictionary(this);
     _innerFunctions   = new List <string>(FunctionExpression.GetFunctionNames());
     _functions        = new ReadOnlyCollection <string>(_innerFunctions);
     _expressionCache  = new Dictionary <string, IExpression>(StringComparer.OrdinalIgnoreCase);
     _symbolStack      = new Stack <string>();
     _expressionQueue  = new Queue <IExpression>();
     _buffer           = new StringBuilder();
     _calculationStack = new Stack <double>();
     _parameters       = new Stack <double>(2);
 }
        private bool TryEndGroup(char c)
        {
            if (c != ')')
            {
                return(false);
            }

            bool hasStart = false;

            while (_symbolStack.Count > 0)
            {
                string p = _symbolStack.Pop();
                if (p == "(")
                {
                    hasStart = true;

                    if (_symbolStack.Count == 0)
                    {
                        break;
                    }

                    string n = _symbolStack.Peek();
                    if (FunctionExpression.IsFunction(n))
                    {
                        p = _symbolStack.Pop();
                        IExpression f = GetExpressionFromSymbol(p);
                        _expressionQueue.Enqueue(f);
                    }

                    break;
                }

                IExpression e = GetExpressionFromSymbol(p);
                _expressionQueue.Enqueue(e);
            }

            if (!hasStart)
            {
                throw new ParseException(Resources.UnbalancedParentheses);
            }

            return(true);
        }
Пример #3
0
        private IExpression GetExpressionFromSymbol(string p)
        {
            IExpression e;

            if (_expressionCache.ContainsKey(p))
                e = _expressionCache[p];
            else if (OperatorExpression.IsSymbol(p))
            {
                e = new OperatorExpression(p);
                _expressionCache.Add(p, e);
            }
            else if (FunctionExpression.IsFunction(p))
            {
                e = new FunctionExpression(p, false);
                _expressionCache.Add(p, e);
            }
            else if (ConvertExpression.IsConvertExpression(p))
            {
                e = new ConvertExpression(p);
                _expressionCache.Add(p, e);
            }
            else
                throw new ParseException(Resources.InvalidSymbolOnStack + p);

            return e;
        }