Exemplo n.º 1
0
        private bool TryNumber(char lastChar)
        {
            bool isNumber = NumberExpression.IsNumber(_currentChar);
            // only negative when last char is group start or symbol
            bool isNegative = NumberExpression.IsNegativeSign(_currentChar) &&
                              (lastChar == '\0' || lastChar == '(' || OperatorExpression.IsSymbol(lastChar));

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

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

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

            while (NumberExpression.IsNumber(p))
            {
                _currentChar = (char)_expressionReader.Read();
                _buffer.Append(_currentChar);
                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);
        }
Exemplo n.º 2
0
        private bool TryString()
        {
            if (!char.IsLetter(_currentChar))
            {
                return(false);
            }

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

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

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

            if (_variables.ContainsKey(_buffer.ToString()))
            {
                double           value      = _variables[_buffer.ToString()];
                NumberExpression expression = new NumberExpression(value);
                _expressionQueue.Enqueue(expression);

                return(true);
            }

            if (IsFunction(_buffer.ToString()))
            {
                _symbolStack.Push(_buffer.ToString());
                _nestedFunctionDepth++;
                return(true);
            }

            throw new ParseException(Resources.InvalidVariableEncountered + _buffer);
        }