public void WhenIParseTheCalculatorTokensIntoReversePolishNotation()
        {
            var tp       = new TokensParser();
            var rpTokens = tp.ReversePolishParse(_calculatorStringTokens.StringTokens);

            this._calculatorReversePolishTokens = new CalculatorReversePolishTokens(rpTokens);
        }
        public void ExceptionUnclosedBrackets()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _one, _plus, _two
            };

            TokensParser tp = new TokensParser();

            Assert.Throws <ArgumentException>(() => tp.ReversePolishParse(unparsedTokens));
        }
        public void ExceptionChainOperation()
        {
            var unparsedTokens = new List <Token> {
                _plus, _multiply
            };

            TokensParser tp = new TokensParser();

            Assert.Throws <ArgumentException>(() => tp.ReversePolishParse(unparsedTokens));
        }
        public void ExceptionOperationNotParseToBaseSizePolish()
        {
            var unparsedTokens = new List <Token> {
                _one
            };

            TokensParser tp = new TokensParser();

            Assert.Throws <ArgumentException>(() => tp.ReversePolishParse(unparsedTokens));
        }
 public void SetUp()
 {
     _tokenizer = new Mock <ITokenizer> ();
     _tokenizer
     .Setup(x => x.Tokens)
     .Returns(new List <IToken>
     {
         new ProgramToken(),
         new IdentifierToken("Test"),
         new SemiColonToken()
     }.AsReadOnly());
     _tokenParser = new TokensParser(_tokenizer.Object);
 }
        public void ParseTokensSingleOperationWithBrackets()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _one, _minus, _two, _closeBracket
            };
            var expectedTokens = new List <Token> {
                _one, _two, _minus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            Assert.True(parsedTokens.SequenceEqual(expectedTokens));
        }
        public void ParseTokensSingleOperation()
        {
            var unparsedTokens = new List <Token> {
                _one, _plus, _two
            };
            var expectedTokens = new List <Token> {
                _one, _two, _plus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            Assert.True(parsedTokens.SequenceEqual(expectedTokens));
        }
        public void ParseTokensPrecedenceInOrderWithNestedInnerBracket()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _openBracket, _one, _divide, _two, _closeBracket, _minus, _three, _closeBracket, _plus, _four
            };
            var expectedTokens = new List <Token> {
                _one, _two, _divide, _three, _minus, _four, _plus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            Assert.Equal(expectedTokens, parsedTokens);
        }
        public void ParseTokensSingleFunction()
        {
            var unparsedTokens = new List <Token> {
                _sin, _openBracket, _one, _closeBracket
            };
            var expectedTokens = new List <Token> {
                _one, _sin
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            Assert.True(parsedTokens.SequenceEqual(expectedTokens));
        }
        public void ParseTokensPrecedenceVaries()
        {
            var unparsedTokens = new List <Token> {
                _one, _plus, _two, _exponent, _three, _divide, _four
            };
            var expectedTokens = new List <Token> {
                _one, _two, _three, _exponent, _four, _divide, _plus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            Assert.Equal(expectedTokens, parsedTokens);
        }
        public void ParseTokensPrecedenceNotInOrderWithInnerBracket()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _one, _plus, _two, _closeBracket, _multiply, _three
            };
            var expectedTokens = new List <Token> {
                _one, _two, _plus, _three, _multiply
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            Assert.Equal(expectedTokens, parsedTokens);
        }
        public void ParseTokensVariable()
        {
            var unparsedTokens = new List <Token> {
                _one, _plus, _varX
            };
            var expectedTokens = new List <Token> {
                _one, _varX, _plus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
        public void ParseTokensNestedFunction()
        {
            var unparsedTokens = new List <Token> {
                _sin, _openBracket, _one, _plus, _sin, _openBracket, _two, _closeBracket, _closeBracket
            };
            var expectedTokens = new List <Token> {
                _one, _two, _sin, _plus, _sin
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
        public void ParseTokensPrecedenceInOrderWithOuterBracket()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _one, _multiply, _two, _divide, _three, _closeBracket
            };
            var expectedTokens = new List <Token> {
                _one, _two, _multiply, _three, _divide
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
        public void ParseTokensPrecedenceInOrderWithInnerBracket()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _three, _plus, _two, _closeBracket, _minus, _minus, _one
            };
            var expectedTokens = new List <Token> {
                _three, _two, _plus, _negativeOne, _minus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
        public void ParseTokensNegativeNumberMixOperation()
        {
            var unparsedTokens = new List <Token> {
                _one, _plus, _three, _minus, _minus, _two
            };
            var expectedTokens = new List <Token> {
                _one, _three, _plus, _negativeTwo, _minus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            //Assert.True(parsedTokens.SequenceEqual(expectedTokens));
            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
        public void ParseTokensSingleFunctionMix()
        {
            var unparsedTokens = new List <Token> {
                _two, _plus, _three, _divide, _sin, _openBracket, _one, _closeBracket
            };
            var expectedTokens = new List <Token> {
                _two, _three, _one, _sin, _divide, _plus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            //Assert.True(parsedTokens.SequenceEqual(expectedTokens));
            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
        public void ParseTokensPrecedenceNotInOrderWithMultipleBrackets()
        {
            var unparsedTokens = new List <Token> {
                _openBracket, _one, _plus, _two, _closeBracket, _plus, _openBracket, _three, _multiply, _four, _closeBracket
            };
            var expectedTokens = new List <Token> {
                _one, _two, _plus, _three, _four, _multiply, _plus
            };

            TokensParser tp           = new TokensParser();
            var          parsedTokens = tp.ReversePolishParse(unparsedTokens);

            //Assert.Equal(expectedTokens, parsedTokens);
            for (int i = 0; i < expectedTokens.Count; i++)
            {
                Assert.True(expectedTokens[i].Value == parsedTokens[i].Value);
            }
        }
Example #19
0
        static double EvaluateFromString(string inputString, string varValue)
        {
            var sp = new StringParser();
            var tp = new TokensParser();
            var tc = new TokenCalculator();

            var parsedTokens  = sp.ParseString(inputString);
            var orderedTokens = tp.ReversePolishParse(parsedTokens);
            var varTokens     = orderedTokens.FindAll(token => token.Type == "variable");
            var mappedVars    = new List <string>();

            foreach (var varToken in varTokens)
            {
                if (!mappedVars.Contains(varToken.Value))
                {
                    mappedVars.Add(varToken.Value);
                    tc.BuildVariableMap(varToken.Value, varValue);
                }
            }
            var evaluation = tc.EvaluateReversePolishExpression(orderedTokens);

            return(double.Parse(evaluation));
        }
Example #20
0
 public void SetUp()
 {
     word        = "aba";
     tokenParser = new TokensParser();
 }