Ejemplo n.º 1
0
        private List <Token.Token> GetToken(string formula)
        {
            List <Token.Token> tokens = new List <Token.Token>();

            string current = "";

            foreach (char c in formula)
            {
                Token.Token nextToken     = null;
                bool        finishCurrent = true;
                switch (c)
                {
                case '^':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation1, '^');
                    break;

                case '/':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation2, '/');
                    break;

                case '*':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation2, '*');
                    break;

                case '+':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation3, '+');
                    break;

                case '-':
                    if (current.Length >= 2)
                    {
                        // allow - for scientific notation (1e-10)
                        if (char.ToLower(current[current.Length - 1]) == 'e' &&
                            char.IsNumber(current[current.Length - 2]))
                        {
                            current      += '-';
                            finishCurrent = false;
                            break;
                        }
                    }
                    nextToken = new SingleCharToken(Token.Token.Type.Operation3, '-');
                    break;

                case ',':
                    nextToken = new SingleCharToken(Token.Token.Type.Seperator, ',');
                    break;

                case '(':
                    if (current.Length > 0)
                    {
                        // this is a function
                        nextToken = new FunctionToken(current);
                        current   = "";
                    }
                    else
                    {
                        nextToken = new SingleCharToken(Token.Token.Type.BracketOpen, '(');
                    }
                    break;

                case ')':
                    nextToken = new SingleCharToken(Token.Token.Type.BracketClose, ')');
                    break;

                default:
                    if (char.IsWhiteSpace(c))
                    {
                        break;
                    }

                    current      += c;
                    finishCurrent = false;
                    break;
                }

                if (finishCurrent && current.Length > 0)
                {
                    // add value token
                    tokens.Add(MakeTokenFromString(current));
                    current = "";
                }
                if (nextToken != null)
                {
                    tokens.Add(nextToken);
                }
            }

            if (current.Length > 0)
            {
                tokens.Add(MakeTokenFromString(current));
            }

            return(tokens);
        }
Ejemplo n.º 2
0
        private List <Token.Token> GetToken(string formula)
        {
            List <Token.Token> tokens = new List <Token.Token>();

            string current = "";

            foreach (char c in formula)
            {
                Token.Token nextToken     = null;
                bool        finishCurrent = true;
                switch (c)
                {
                case ' ':
                    break;

                case '^':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation1, '^');
                    break;

                case '/':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation2, '/');
                    break;

                case '*':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation2, '*');
                    break;

                case '+':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation3, '+');
                    break;

                case '-':
                    nextToken = new SingleCharToken(Token.Token.Type.Operation3, '-');
                    break;

                case ',':
                    nextToken = new SingleCharToken(Token.Token.Type.Seperator, ',');
                    break;

                case '(':
                    if (current.Length > 0)
                    {
                        // this is a function
                        nextToken = new FunctionToken(current);
                        current   = "";
                    }
                    else
                    {
                        nextToken = new SingleCharToken(Token.Token.Type.BracketOpen, '(');
                    }
                    break;

                case ')':
                    nextToken = new SingleCharToken(Token.Token.Type.BracketClose, ')');
                    break;

                default:
                    current      += c;
                    finishCurrent = false;
                    break;
                }

                if (finishCurrent && current.Length > 0)
                {
                    // add value token
                    tokens.Add(MakeTokenFromString(current));
                    current = "";
                }
                if (nextToken != null)
                {
                    tokens.Add(nextToken);
                }
            }

            if (current.Length > 0)
            {
                tokens.Add(MakeTokenFromString(current));
            }

            return(tokens);
        }