Exemplo n.º 1
0
        public void CreatingSimpleTree()
        {
            Tokenizer    tokenizer = new Tokenizer(s_errorHandler, true);
            List <Token> tokens    = tokenizer.process(File.OpenText("code4.txt"));

            Parser parser = new Parser(tokens, s_errorHandler);

            parser.process();

            //Console.WriteLine("Tree: " + parser.getAST().toStringTree());
            ASTPainter p = new ASTPainter();

            p.PaintAST(parser.getAST());

            AST root = parser.getAST();

            Assert.AreEqual(Token.TokenType.PROGRAM_ROOT, root.getTokenType());

            AST statementList = root.getChild(0);

            Assert.AreEqual(Token.TokenType.STATEMENT_LIST, statementList.getTokenType());

            AST multiplicationTree = statementList.getChild(1);

            Assert.AreEqual(Token.TokenType.OPERATOR, multiplicationTree.getTokenType());

            AST operand1 = multiplicationTree.getChild(0);
            AST operand2 = multiplicationTree.getChild(1);

            Assert.AreEqual("a", operand1.getTokenString());
            Assert.AreEqual("b", operand2.getTokenString());
        }
Exemplo n.º 2
0
        public void CreatingSimpleTree()
        {
            Tokenizer tokenizer = new Tokenizer(s_errorHandler, true);
            List<Token> tokens = tokenizer.process(File.OpenText("code4.txt"));

            Parser parser = new Parser(tokens, s_errorHandler);
            parser.process();

            //Console.WriteLine("Tree: " + parser.getAST().toStringTree());
            ASTPainter p = new ASTPainter();
            p.PaintAST(parser.getAST());

            AST root = parser.getAST();
            Assert.AreEqual(Token.TokenType.PROGRAM_ROOT, root.getTokenType());

            AST statementList = root.getChild(0);
            Assert.AreEqual(Token.TokenType.STATEMENT_LIST, statementList.getTokenType());

            AST multiplicationTree = statementList.getChild(1);
            Assert.AreEqual(Token.TokenType.OPERATOR, multiplicationTree.getTokenType());

            AST operand1 = multiplicationTree.getChild(0);
            AST operand2 = multiplicationTree.getChild(1);
            Assert.AreEqual("a", operand1.getTokenString());
            Assert.AreEqual("b", operand2.getTokenString());
        }
Exemplo n.º 3
0
        public void ReplaceASTNode()
        {
            AST parent  = new AST(new Token(Token.TokenType.NAME, "parent"));
            AST child1  = new AST(new Token(Token.TokenType.NAME, "child1"));
            AST child2  = new AST(new Token(Token.TokenType.NAME, "child2"));
            AST child2a = new AST(new Token(Token.TokenType.NAME, "child2a"));
            AST child2b = new AST(new Token(Token.TokenType.NAME, "child2b"));

            parent.addChild(child1);
            parent.addChild(child2);
            child2.addChild(child2a);
            child2.addChild(child2b);

            //AST child3 = new AST(new Token(Token.TokenType.NAME, "child3"));

            Console.WriteLine(parent.findParent(child2b).getTokenString());

            ASTPainter p = new ASTPainter();

            p.PaintAST(parent);

            //Assert.Fail();
        }