Пример #1
0
        private bool TryNumber(char c, char l)
        {
            bool isNumber = NumberExpression.IsNumber(c);
            // only negative when last char is group start or symbol
            bool isNegative = NumberExpression.IsNegativeSign(c) &&
                              (l == '\0' || l == '(' || OperatorExpression.IsSymbol(l));

            if (!isNumber && !isNegative)
            {
                return(false);
            }

            _buffer.Length = 0;
            _buffer.Append(c);

            char p = (char)_expressionReader.Peek();

            while (NumberExpression.IsNumber(p))
            {
                _buffer.Append((char)_expressionReader.Read());
                p = (char)_expressionReader.Peek();
            }

            double value;

            if (!(double.TryParse(_buffer.ToString(), out value)))
            {
                throw new ParseException(Resources.InvalidNumberFormat + _buffer);
            }

            NumberExpression expression = new NumberExpression(value);

            _expressionQueue.Enqueue(expression);

            return(true);
        }
Пример #2
0
        private bool TryOperator(char c)
        {
            if (!OperatorExpression.IsSymbol(c))
            {
                return(false);
            }

            bool   repeat;
            string s = c.ToString();

            do
            {
                string p = _symbolStack.Count == 0 ? string.Empty : _symbolStack.Peek();
                repeat = false;
                if (_symbolStack.Count == 0)
                {
                    _symbolStack.Push(s);
                }
                else if (p == "(")
                {
                    _symbolStack.Push(s);
                }
                else if (Precedence(s) > Precedence(p))
                {
                    _symbolStack.Push(s);
                }
                else
                {
                    IExpression e = GetExpressionFromSymbol(_symbolStack.Pop());
                    _expressionQueue.Enqueue(e);
                    repeat = true;
                }
            } while (repeat);

            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;
        }