예제 #1
0
        private IToken ReadNumber(bool pComma = false)
        {
            //CheckNegative();

            string numberStr = $"{Chars.Dequeue()}";

            while (IsDigit(Chars.Peek()))
            {
                numberStr += Chars.Dequeue();
            }

            double number = double.Parse(numberStr);

            if (!pComma && Chars.Peek().Equals('.'))
            {
                Chars.Dequeue();
                numberStr = "";
                int decCounter = 1;
                while (IsDigit(Chars.Peek()))
                {
                    numberStr  += Chars.Dequeue();
                    decCounter *= 10;
                }
                number += (double.Parse(numberStr) / decCounter);
            }

            return(new NumberToken
            {
                Number = number
            });
        }
예제 #2
0
        public TreeNode Parse(PositionQueue <IToken> pTokens)
        {
            /*
             * 1.
             * Get the first item and initialise the tree with it.
             */
            TreeNode root = new TreeNode(pTokens.Dequeue());

            /*
             * 2.
             * Currently the root node is also the current node.
             * The current node is the node we currently lie on.
             */
            TreeNode current = root;

            while (pTokens.Count > 0)
            {
                /*
                 * 3.
                 * Get the next item.
                 * This will be called the new item.
                 */
                current = ProcessTokenToNode(pTokens.Dequeue(), current);
            }

            while (current.Parent != null)
            {
                current = current.Parent;
            }

            return(current);
        }
예제 #3
0
        public PositionQueue <IToken> Analyze(string pInput)
        {
            Tokens = new PositionQueue <IToken>();
            Chars  = BuildQueue(pInput);

            while (Chars.Count > 0)
            {
                IToken token = null;
                if (IsWhitespace(Chars.Peek()))
                {
                    Chars.Dequeue();
                }
                else if (IsDigit(Chars.Peek()))
                {
                    token = ReadNumber();
                }
                else if (IsOperation(Chars.Peek()))
                {
                    if (Tokens[Tokens.Count - 1].GetTokenType() == TokenType.Operator)
                    {
                        throw new NotImplementedException($"unexpected symbol \"{Chars.Dequeue()}\" at position {Chars.Position}");
                    }
                    else
                    {
                        token = ReadOperation();
                    }
                }
                else
                {
                    throw new NotImplementedException($"unexpected symbol \"{Chars.Dequeue()}\" at position {Chars.Position}");
                }
                if (token != null)
                {
                    Tokens.Enqueue(token);
                }
            }

            return(Tokens);
        }