Exemplo n.º 1
0
        /// <summary>
        /// Reads an operator from the given string expression, starting at the given position.
        /// </summary>
        /// <param name="expr">The expression to read the operator from.</param>
        /// <param name="pos">The position to start reading at.</param>
        /// <returns>The result of the read.</returns>
        internal ReadResult ReadOperator(string expr, int pos)
        {
            ReadResult result = new ReadResult() { Position = pos };
            char c = expr[pos];

            if (!char.IsWhiteSpace(c))
            {
                IOperationProvider provider = this.providerFactory.GetProvider(c);

                if (provider != null)
                {
                    result.Token = provider.CreateOperator(c);
                    result.Position = pos + 1;
                    result.Success = true;
                }
            }

            return result;
        }
Exemplo n.º 2
0
        internal ReadResult ReadOperand(string expr, int pos)
        {
            ReadResult result = new ReadResult() { Position = pos };
            StringBuilder sb = new StringBuilder();
            bool hasPeriod = false;
            char c;

            do
            {
                c = expr[pos];

                if (char.IsDigit(c))
                {
                    sb.Append(c);
                    pos++;
                }
                else if (c == '.')
                {
                    if (!hasPeriod)
                    {
                        sb.Append(c);
                        pos++;
                        hasPeriod = true;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            while (pos < expr.Length);

            if (sb.Length > 0)
            {
                try
                {
                    if (hasPeriod)
                    {
                        result.Token = new Operand(double.Parse(sb.ToString(), CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        result.Token = new Operand(long.Parse(sb.ToString(), CultureInfo.InvariantCulture));
                    }

                    result.Position = pos;
                    result.Success = true;
                }
                catch (FormatException)
                {
                }
                catch (OverflowException)
                {
                }
            }

            return result;
        }