Ejemplo n.º 1
0
        private Token[] Tokenizer(string s)
        {
            List <Token> list = new List <Token>();

            Token pending = null;

            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (c == ' ')
                {
                    if (pending != null)
                    {
                        list.Add(pending);
                        pending = null;
                    }
                }
                else if (c == '+' || c == '-' || c == '/' || c == '*' || c == '(' || c == ')')
                {
                    if (pending != null)
                    {
                        list.Add(pending);
                        pending = null;
                    }

                    list.Add(new Token(i));
                }
                else if (c >= '0' && c <= '9')
                {
                    if (pending != null)
                    {
                        pending.Advance();
                    }
                    else
                    {
                        pending = new Token(i);
                    }
                }
                else
                {
                    throw new Exception("Tokenize: Unexpected symbol");
                }
            }

            if (pending != null)
            {
                list.Add(pending);
            }

            return(list.ToArray());
        }