Represents a symbol token.
Наследование: IToken
Пример #1
0
        public void EqualsSameObjectTest()
        {
            var token = new SymbolToken(Symbols.Comma);

            Assert.True(token.Equals(token));
            Assert.Equal(token, token);
        }
Пример #2
0
        public void EqualsNullTest()
        {
            var token = new SymbolToken(Symbols.Comma);

            Assert.False(token.Equals(null));
            Assert.NotEqual(null, token);
        }
Пример #3
0
        public void EqualsDiffTypeTest()
        {
            var token = new SymbolToken(Symbols.Comma);

            Assert.False(token.Equals(1));
            Assert.NotEqual((object)1, token);
        }
Пример #4
0
        public void EqualsDiffSymbolTest()
        {
            var token1 = new SymbolToken(Symbols.Comma);
            var token2 = new SymbolToken(Symbols.CloseBrace);

            Assert.False(token1.Equals(token2));
            Assert.NotEqual(token1, token2);
        }
Пример #5
0
        public void ToStringTest()
        {
            var token = new SymbolToken(Symbols.Comma);

            Assert.Equal("Symbol: Comma", token.ToString());
        }
Пример #6
0
        private IEnumerable<IToken> ConvertToReversePolishNotation(IEnumerable<IToken> tokens)
        {
            var output = new List<IToken>();
            var stack = new Stack<IToken>();

            var openBracketToken = new SymbolToken(Symbols.OpenBracket);
            var openBraceToken = new SymbolToken(Symbols.OpenBrace);
            foreach (var token in tokens)
            {
                IToken stackToken;
                if (token is SymbolToken)
                {
                    var t = token as SymbolToken;
                    switch (t.Symbol)
                    {
                        case Symbols.OpenBracket:
                        case Symbols.OpenBrace:
                            stack.Push(token);
                            break;
                        case Symbols.CloseBracket:
                        case Symbols.CloseBrace:
                            stackToken = stack.Pop();
                            while (!stackToken.Equals(openBracketToken) && !stackToken.Equals(openBraceToken))
                            {
                                output.Add(stackToken);
                                stackToken = stack.Pop();
                            }
                            break;
                        case Symbols.Comma:
                            stackToken = stack.Pop();

                            while (!stackToken.Equals(openBracketToken) && !stackToken.Equals(openBraceToken))
                            {
                                output.Add(stackToken);
                                stackToken = stack.Pop();
                            }

                            stack.Push(stackToken);
                            break;
                    }
                }
                else if (token is NumberToken || token is VariableToken)
                {
                    output.Add(token);
                }
                else
                {
                    while (stack.Count != 0 && (stackToken = stack.Peek()).Priority >= token.Priority)
                    {
                        if (stackToken.Equals(openBracketToken) || stackToken.Equals(openBraceToken))
                            break;
                        output.Add(stack.Pop());
                    }

                    stack.Push(token);
                }
            }

            if (stack.Count != 0)
                output.AddRange(stack);

            return output;
        }