Exemple #1
0
        public static void tests()
        {
            ConstantToken t = new ConstantToken(5, false, false);

            Console.WriteLine(t.Print());
            if (t is MathToken)
            {
                Console.WriteLine("T is a Constant");
            }
            AdditionToken add = new AdditionToken();

            Console.WriteLine(add.GetType());
            if (add is ConstantToken)
            {
                Console.WriteLine("add is a operator token");
            }
            else
            {
                Console.WriteLine("add is not an operator token");
            }
            Tokenizer tokenizer = new Tokenizer();
            MathToken token1    = new AdditionToken();
            MathToken token2    = new ConstantToken(5, false, false);
            MathToken token3    = new MultiplicationToken();
            MathToken token4    = new ConstantToken(3, false, false);
            MathToken token5    = new DivisionToken();
            MathToken token6    = new ConstantToken(2, false, false);
            MathToken token7    = new ModuloToken();

            tokenizer.AddToken(token1);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.AddToken(token2);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.AddToken(token3);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.AddToken(token1);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.AddToken(token4);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.AddToken(token5);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.AddToken(token6);
            Console.WriteLine(tokenizer.ToString());
            tokenizer.constructTree();
            Console.WriteLine(tokenizer.ToString());
            Console.WriteLine(tokenizer.TokenList[0].GetResult().Print());
            var c = 5 - -4;

            Console.WriteLine(c.ToString());
            Console.Read();
        }
Exemple #2
0
        public void buildTree()
        {
            //now the tree is ready to be built:
            int a = 1;

            while (a < TokenList.Count - 1)
            {
                Console.WriteLine($"test {a} result {this.ToString()}");
                OperatorToken newToken;
                switch (TokenList[a].Print())
                {
                case "*":
                    //create new token
                    newToken = new MultiplicationToken();
                    //take left and right values (could be constants or ready built tokens:
                    //                                 a
                    // before: ... | 5 | [ + ] | 2 | [ * ] | 3 |
                    newToken.build(TokenList[a - 1], TokenList[a + 1]);
                    // before: ... | 5 | [ + ] | 2 | [ * ] | 3 |
                    a = a - 1;
                    //                           a
                    // before: ... | 5 | [ + ] | 2 | [ * ] | 3 |
                    TokenList[a] = newToken;
                    // after: ... | 5 | [ + ] | [2*3] | [ * ] | 3 |
                    TokenList.RemoveRange(a + 1, 2);
                    //        ... | 5 | [ + ] | [2*3] |
                    break;

                case "/":
                    //create new token
                    newToken = new DivisionToken();
                    newToken.build(TokenList[a - 1], TokenList[a + 1]);
                    a            = a - 1;
                    TokenList[a] = newToken;
                    TokenList.RemoveRange(a + 1, 2);
                    break;

                case "%":
                    newToken = new ModuloToken();
                    newToken.build(TokenList[a - 1], TokenList[a + 1]);
                    a            = a - 1;
                    TokenList[a] = newToken;
                    TokenList.RemoveRange(a + 1, 2);
                    break;

                case "+":
                    break;

                case "-":
                    break;
                }
                a++;
            }
            a = 1;
            while (a < TokenList.Count - 1)
            {
                Console.WriteLine($"test {a} result {this.ToString()}");
                OperatorToken newToken;
                switch (TokenList[a].Print())
                {
                case "+":
                    //create new token
                    newToken = new AdditionToken();
                    //take left and right values (could be constants or ready built tokens:
                    //                                 a
                    // before: ... | 5 | [ + ] | 2 | [ * ] | 3 |
                    newToken.build(TokenList[a - 1], TokenList[a + 1]);
                    // before: ... | 5 | [ + ] | 2 | [ * ] | 3 |
                    a = a - 1;
                    //                           a
                    // before: ... | 5 | [ + ] | 2 | [ * ] | 3 |
                    TokenList[a] = newToken;
                    // after: ... | 5 | [ + ] | [2*3] | [ * ] | 3 |
                    TokenList.RemoveRange(a + 1, 2);
                    //        ... | 5 | [ + ] | [2*3] |
                    break;

                case "-":
                    //create new token
                    newToken = new SubtractionToken();
                    newToken.build(TokenList[a - 1], TokenList[a + 1]);
                    a            = a - 1;
                    TokenList[a] = newToken;
                    TokenList.RemoveRange(a + 1, 2);
                    break;
                }
                a++;
            }
        }