예제 #1
0
        public void TestAddListToExistChildren()
        {
            // Add child ^(nil 101 102 103) to root ^(5 6)
            // should add 101 102 103 to end of 5's child list
            CommonTree root = new CommonTree( new CommonToken( 5 ) );
            root.AddChild( new CommonTree( new CommonToken( 6 ) ) );

            // child tree
            CommonTree r0 = new CommonTree( (IToken)null );
            CommonTree c0, c1, c2;
            r0.AddChild( c0 = new CommonTree( new CommonToken( 101 ) ) );
            r0.AddChild( c1 = new CommonTree( new CommonToken( 102 ) ) );
            r0.AddChild( c2 = new CommonTree( new CommonToken( 103 ) ) );

            root.AddChild( r0 );

            assertNull( root.Parent );
            assertEquals( -1, root.ChildIndex );
            // check children of root all point at root
            assertEquals( root, c0.Parent );
            assertEquals( 1, c0.ChildIndex );
            assertEquals( root, c0.Parent );
            assertEquals( 2, c1.ChildIndex );
            assertEquals( root, c0.Parent );
            assertEquals( 3, c2.ChildIndex );
        }
        public void TestSeek()
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree( new CommonToken( 101 ) );
            ITree r1 = new CommonTree( new CommonToken( 102 ) );
            r0.AddChild( r1 );
            r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
            ITree r2 = new CommonTree( new CommonToken( 106 ) );
            r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
            r1.AddChild( r2 );
            r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
            r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );

            ITreeNodeStream stream = newStream( r0 );
            stream.Consume(); // consume 101
            stream.Consume(); // consume DN
            stream.Consume(); // consume 102
            stream.Seek( 7 );   // seek to 107
            Assert.AreEqual( 107, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume(); // consume 107
            stream.Consume(); // consume UP
            stream.Consume(); // consume UP
            Assert.AreEqual( 104, ( (ITree)stream.LT( 1 ) ).Type );
        }
예제 #3
0
        public void Test4Nodes()
        {
            // ^(101 ^(102 103) 104)
            CommonTree r0 = new CommonTree( new CommonToken( 101 ) );
            r0.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            r0.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
            r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );

            assertNull( r0.Parent );
            assertEquals( -1, r0.ChildIndex );
        }
예제 #4
0
        public void Test4Nodes()
        {
            // ^(101 ^(102 103) 104)
            ITree t = new CommonTree( new CommonToken( 101 ) );
            t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            t.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
            t.AddChild( new CommonTree( new CommonToken( 104 ) ) );

            ITreeNodeStream stream = newStream( t );
            string expecting = " 101 102 103 104";
            string found = ToNodesOnlyString( stream );
            Assert.AreEqual( expecting, found );

            expecting = " 101 2 102 2 103 3 104 3";
            found = ToTokenTypeString( stream );
            Assert.AreEqual( expecting, found );
        }
예제 #5
0
        public void TestAoverB()
        {
            ITree t = new CommonTree( new CommonToken( 101 ) );
            t.AddChild( new CommonTree( new CommonToken( 102 ) ) );

            ITreeNodeStream stream = newStream( t );
            string expecting = " 101 102";
            string found = ToNodesOnlyString( stream );
            Assert.AreEqual( expecting, found );

            expecting = " 101 2 102 3";
            found = ToTokenTypeString( stream );
            Assert.AreEqual( expecting, found );
        }
예제 #6
0
        public void TestLT()
        {
            // ^(101 ^(102 103) 104)
            ITree t = new CommonTree( new CommonToken( 101 ) );
            t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            t.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
            t.AddChild( new CommonTree( new CommonToken( 104 ) ) );

            ITreeNodeStream stream = newStream( t );
            Assert.AreEqual( 101, ( (ITree)stream.LT( 1 ) ).Type );
            Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 2 ) ).Type );
            Assert.AreEqual( 102, ( (ITree)stream.LT( 3 ) ).Type );
            Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 4 ) ).Type );
            Assert.AreEqual( 103, ( (ITree)stream.LT( 5 ) ).Type );
            Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 6 ) ).Type );
            Assert.AreEqual( 104, ( (ITree)stream.LT( 7 ) ).Type );
            Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 8 ) ).Type );
            Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 9 ) ).Type );
            // check way ahead
            Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 100 ) ).Type );
        }
예제 #7
0
        public void TestListWithOneNode()
        {
            ITree root = new CommonTree( (IToken)null );

            root.AddChild( new CommonTree( new CommonToken( 101 ) ) );

            ITreeNodeStream stream = newStream( root );
            string expecting = " 101";
            string found = ToNodesOnlyString( stream );
            Assert.AreEqual( expecting, found );

            expecting = " 101";
            found = ToTokenTypeString( stream );
            Assert.AreEqual( expecting, found );
        }
예제 #8
0
        public void TestList()
        {
            ITree root = new CommonTree( (IToken)null );

            ITree t = new CommonTree( new CommonToken( 101 ) );
            t.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            t.GetChild( 0 ).AddChild( new CommonTree( new CommonToken( 103 ) ) );
            t.AddChild( new CommonTree( new CommonToken( 104 ) ) );

            ITree u = new CommonTree( new CommonToken( 105 ) );

            root.AddChild( t );
            root.AddChild( u );

            ITreeNodeStream stream = newStream( root );
            string expecting = " 101 102 103 104 105";
            string found = ToNodesOnlyString( stream );
            Assert.AreEqual( expecting, found );

            expecting = " 101 2 102 2 103 3 104 3 105";
            found = ToTokenTypeString( stream );
            Assert.AreEqual( expecting, found );
        }
예제 #9
0
        public void TestFlatList()
        {
            ITree root = new CommonTree( (IToken)null );

            root.AddChild( new CommonTree( new CommonToken( 101 ) ) );
            root.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            root.AddChild( new CommonTree( new CommonToken( 103 ) ) );

            ITreeNodeStream stream = newStream( root );
            string expecting = " 101 102 103";
            string found = ToNodesOnlyString( stream );
            assertEquals( expecting, found );

            expecting = " 101 102 103";
            found = ToTokenTypeString( stream );
            assertEquals( expecting, found );
        }
예제 #10
0
        public void TestReplaceAtRight()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) ); // index 2

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );
            t.ReplaceChildren( 2, 2, newChild );
            string expecting = "(a b c x)";
            Assert.AreEqual( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #11
0
      public static DataType Check(AstNode node, Context context)
      {
          switch (node.Type)
          {
              case CCompilerLexer.PROGRAM:
                  {
                      if (context == null)
                          context = new Context(context);
                      CheckBlock(node, context);
                      return DataType.Void;
                  }
              case CCompilerLexer.BLOCK:
                  {
                      context = new Context(context);
                      CheckBlock(node, context);
                      return DataType.Void;
                  }

              case CCompilerLexer.VAR:
                  {
                      List<AstNode> nodes = new List<AstNode>();
                      DataType dataType = StrToDataType(node.GetChild(0).Text);
                      for (int i = 0; i < node.GetChild(0).ChildCount; i++)
                      {
                          AstNode temp = (AstNode)node.GetChild(0).GetChild(i);
                          if (temp.Token.Type == CCompilerLexer.ASSIGN)
                          {
                              Ident ident = context.InThisContext(temp.GetChild(0).Text);
                              if (ident != null)
                                  throw new ApplicationException(string.Format("Identifier {0} already exists", temp.GetChild(0).Text));
                              AstNode var = new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.VAR, "VAR"));
                              var.AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.IDENT, DataTypeToStr(dataType))));
                              var.GetChild(0).AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.IDENT, temp.GetChild(0).Text)));
                              nodes.Add(var);
                              nodes.Add(temp);
                          }
                          else
                          {
                              Ident ident = context.InThisContext(temp.Text);
                              if (ident != null)
                                  throw new IntepreterException(string.Format("Identifier {0} already exists", temp.Text));
                              AstNode var = new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.VAR, "VAR"));
                              var.AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.IDENT, DataTypeToStr(dataType))));
                              var.GetChild(0).AddChild(temp);
                              nodes.Add(var);
                          }
                          string name = nodes[0].GetChild(0).GetChild(0).Text;
                          context[name] = new Ident(name, context.ParentContext == null ? IdentType.GlobalVar : IdentType.LocalVar, dataType, nodes[0]);

                          Antlr.Runtime.Tree.CommonTree tree = new Antlr.Runtime.Tree.CommonTree();
                          foreach (AstNode n in nodes)
                              tree.AddChild(n);
                          node.Parent.ReplaceChildren(node.ChildIndex, node.ChildIndex, tree);
                      }

                      return DataType.Void;
                     
                  }

              case CCompilerLexer.FUNC_DECL:
                  {
                      DataType dataType = StrToDataType(node.GetChild(0).Text);
                      string name = node.GetChild(1).Text;
                      Ident ident = context[name];
                      if (ident != null)
                          throw new IntepreterException(string.Format("Identifier {0} already exists", name));
                      Ident func = new Ident(name, IdentType.Function, dataType, node);
                      context[name] = func;
                      context = new Context(context);
                      AstNode _params = (AstNode)node.GetChild(2);
                      for (int i = 0; i < _params.ChildCount; i++)
                      {
                          DataType paramDataType = StrToDataType(_params.GetChild(i).Text);
                          string paramName = _params.GetChild(i).GetChild(0).Text;
                          if (paramDataType == DataType.Void)
                              throw new IntepreterException(string.Format("In function {0} void param {1}", name, paramName));
                          context[paramName] = new Ident(paramName, IdentType.Param, paramDataType, (AstNode)_params.GetChild(i));
                      }
                      context.Function = func;
                      Check((AstNode)node.GetChild(3), context);
                      return DataType.Void;
                  }

              case CCompilerLexer.CALL:
                  {
                      Ident ident = context[node.GetChild(0).Text];
                      if (ident == null)
                          throw new IntepreterException(string.Format("Unknown identifier {0}", node.GetChild(0).Text));
                      if (ident.IdentType != IdentType.Function)
                          throw new IntepreterException(string.Format("Identifier {0} is not function", node.GetChild(0).Text));
                      if (node.GetChild(1).ChildCount != ident.Node.GetChild(2).ChildCount)
                          throw new IntepreterException(string.Format("Not equals params count in function {0}", node.GetChild(0).Text));
                      for (int i = 0; i < ident.Node.GetChild(2).ChildCount; i++)
                      {
                          DataType formalDataType = StrToDataType(ident.Node.GetChild(2).GetChild(i).Text);
                          DataType factDataType = Check((AstNode)node.GetChild(1).GetChild(i), context);
                          if (formalDataType != factDataType)
                          {
                              if (formalDataType == DataType.Double && factDataType == DataType.Int)
                                  convert((AstNode)node.GetChild(1).GetChild(i), DataType.Double);
                              else
                                  throw new IntepreterException(string.Format("In function {0} param {1} incopotible types {2} {3}", node.GetChild(0).Text, i, DataTypeToStr(formalDataType), DataTypeToStr(factDataType)));
                          }
                      }
                      return StrToDataType(ident.Node.GetChild(0).Text);
                  }

              case CCompilerLexer.IDENT:
                  {
                      Ident ident = context[node.Text];
                      if (ident == null)
                          throw new ApplicationException(string.Format("Unknown identifier {0}", node.Text));
                      if (ident.IdentType == IdentType.Function)
                      {
                          if (ident.DataType == DataType.Void)
                              throw new IntepreterException(string.Format("Function {0} returns void", ident.Name));
                          if (ident.Node.GetChild(1).ChildCount > 0)
                              throw new IntepreterException(string.Format("No params for function {0} call", ident.Name));
                          AstNode call = new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.CALL));
                          call.AddChild(node);
                          call.AddChild(new AstNode(new Antlr.Runtime.CommonToken(CCompilerLexer.ARGS)));
                          node.Parent.SetChild(node.ChildIndex, call);

                          node.DataType = ident.DataType;
                          return node.DataType;
                      }
                      else
                      {
                          node.DataType = ident.DataType;
                          return node.DataType;
                      }
                  }

              case CCompilerLexer.NUMBER:
                  {
                      node.DataType = node.Text.Contains(".") ? DataType.Double : DataType.Int;
                      return node.DataType;
                  }

              case CCompilerLexer.TRUE:
              case CCompilerLexer.FALSE:
                  {
                      node.DataType = DataType.Bool;
                      return node.DataType;
                  }

              case CCompilerLexer.ASSIGN:
                  {
                      Ident ident = context[node.GetChild(0).Text];
                      if (ident == null)
                          throw new IntepreterException(string.Format("Unknown identifier {0}", node.GetChild(0).Text));
                      if (ident.IdentType == IdentType.Function)
                          throw new IntepreterException(string.Format("Assign to function {0}", node.GetChild(0).Text));
                      DataType rightDataType = Check((AstNode)node.GetChild(1), context);
                      if (ident.DataType != rightDataType)
                      {
                          if (ident.DataType == DataType.Double && rightDataType == DataType.Int)
                              convert((AstNode)node.GetChild(1), DataType.Double);
                          else
                              throw new IntepreterException(string.Format("Assign incopotible types {0} {1}", DataTypeToStr(ident.DataType), DataTypeToStr(rightDataType)));
                      }
                      return DataType.Void;
                  }

              case CCompilerLexer.RETURN:
                  {
                      if (context.Function == null)
                          throw new ApplicationException(string.Format("Return not in function in line {0}", node.Line));

                      DataType returnDataType = Check((AstNode)node.GetChild(0), context);
                      if (context.Function.DataType != returnDataType)
                      {
                          if (context.Function.DataType == DataType.Double && returnDataType == DataType.Int)
                              convert((AstNode)node.GetChild(0), DataType.Double);
                          else
                              throw new IntepreterException(string.Format("Return incopotible types {0} {1}", DataTypeToStr(context.Function.DataType), DataTypeToStr(returnDataType)));
                      }
                      return DataType.Void;
                  }

              case CCompilerLexer.ADD:
              case CCompilerLexer.SUB:
              case CCompilerLexer.MUL:
              case CCompilerLexer.DIV:
              case CCompilerLexer.GE:
              case CCompilerLexer.LE:
              case CCompilerLexer.NE:
              case CCompilerLexer.EQ:
              case CCompilerLexer.GT:
              case CCompilerLexer.LT:
                  {
                      bool compareOperation = true;
                      switch (node.Type)
                      {
                          case CCompilerLexer.ADD:
                          case CCompilerLexer.SUB:
                          case CCompilerLexer.MUL:
                          case CCompilerLexer.DIV:
                              compareOperation = false;
                              break;
                      }

                      DataType leftDataType = Check((AstNode)node.GetChild(0), context);
                      DataType rightDataType = Check((AstNode)node.GetChild(1), context);
                      if (leftDataType != DataType.Double && leftDataType != DataType.Int)
                          throw new IntepreterException(string.Format("Left operand invalid type for operation {0}, line = {1}, pos = {2}", node.Text, node.Line, node.TokenStartIndex));
                      if (rightDataType != DataType.Double && rightDataType != DataType.Int)
                          throw new IntepreterException(string.Format("Right operand invalid type for operation {0}, line = {1}, pos = {2}", node.Text, node.Line, node.TokenStartIndex));
                      if (leftDataType == DataType.Double)
                      {
                          if (rightDataType == DataType.Int)
                              convert((AstNode)node.GetChild(1), DataType.Double);
                          node.DataType = compareOperation ? DataType.Bool : DataType.Double;
                          return node.DataType;
                      }
                      if (rightDataType == DataType.Double)
                      {
                          if (leftDataType == DataType.Int)
                              convert((AstNode)node.GetChild(0), DataType.Double);
                          node.DataType = compareOperation ? DataType.Bool : DataType.Double;
                          return node.DataType;
                      }
                      node.DataType = compareOperation ? DataType.Bool : DataType.Int;
                      return node.DataType;
                  }

              case CCompilerLexer.NOT:
                  {
                      DataType dataType = Check((AstNode)node.GetChild(0), context);
                      if (dataType != DataType.Bool)
                          throw new IntepreterException(string.Format("Not operator with type {0}", DataTypeToStr(dataType)));
                      node.DataType = DataType.Bool;
                      return node.DataType;
                  }

              case CCompilerLexer.AND:
              case CCompilerLexer.OR:
              case CCompilerLexer.WHILE:
                  {
                      DataType condDataType = Check((AstNode)node.GetChild(0), context);
                      if (condDataType != DataType.Bool)
                          throw new IntepreterException(string.Format("In while condition type is {0}", DataTypeToStr(condDataType)));
                      Check((AstNode)node.GetChild(1), context);
                      return DataType.Void;
                  }

              case CCompilerLexer.IF:
                  {
                      DataType condDataType = Check((AstNode)node.GetChild(0), context);
                      if (condDataType != DataType.Bool)
                          throw new IntepreterException(string.Format("In if condition type is {0}", DataTypeToStr(condDataType)));
                      Check((AstNode)node.GetChild(1), context);
                      if (node.ChildCount == 3)
                          Check((AstNode)node.GetChild(2), context);
                      return DataType.Void;
                  }

              case CCompilerLexer.FOR:
                  {
                      context = new Context(context);
                      CheckBlock((AstNode)node.GetChild(0), context);
                      DataType condDataType = Check((AstNode)node.GetChild(1), context);
                      if (condDataType != DataType.Bool)
                          throw new IntepreterException(string.Format("In while condition type is {0}", DataTypeToStr(condDataType)));
                      CheckBlock((AstNode)node.GetChild(2), context);
                      Check((AstNode)node.GetChild(3), context);
                      return DataType.Void;
                  }

              default:
                  {
                      return DataType.Void;
                      //throw new IntepreterException("Unknown token type");
                  }
          }
      }
예제 #12
0
        public void TestMarkRewindNested()
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree( new CommonToken( 101 ) );
            ITree r1 = new CommonTree( new CommonToken( 102 ) );
            r0.AddChild( r1 );
            r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
            ITree r2 = new CommonTree( new CommonToken( 106 ) );
            r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
            r1.AddChild( r2 );
            r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
            r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );

            ITreeNodeStream stream = newStream( r0 );
            int m = stream.Mark(); // MARK at start
            stream.Consume(); // consume 101
            stream.Consume(); // consume DN
            int m2 = stream.Mark(); // MARK on 102
            stream.Consume(); // consume 102
            stream.Consume(); // consume DN
            stream.Consume(); // consume 103
            stream.Consume(); // consume 106
            stream.Rewind( m2 );      // REWIND to 102
            Assert.AreEqual( 102, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            // stop at 103 and rewind to start
            stream.Rewind( m ); // REWIND to 101
            Assert.AreEqual( 101, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( 102, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.Down, ( (ITree)stream.LT( 1 ) ).Type );
        }
예제 #13
0
        public void TestReplaceOneWithTwoInMiddle()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChildren = (CommonTree)adaptor.Nil();
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "x" ) ) );
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "y" ) ) );

            t.ReplaceChildren( 1, 1, newChildren );
            string expecting = "(a b x y d)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #14
0
        public void TestList2()
        {
            // Add child ^(nil 101 102 103) to root 5
            // should pull 101 102 103 directly to become 5's child list
            CommonTree root = new CommonTree( new CommonToken( 5 ) );

            // child tree
            CommonTree r0 = new CommonTree( (IToken)null );
            CommonTree c0, c1, c2;
            r0.AddChild( c0 = new CommonTree( new CommonToken( 101 ) ) );
            r0.AddChild( c1 = new CommonTree( new CommonToken( 102 ) ) );
            r0.AddChild( c2 = new CommonTree( new CommonToken( 103 ) ) );

            root.AddChild( r0 );

            assertNull( root.Parent );
            assertEquals( -1, root.ChildIndex );
            // check children of root all point at root
            assertEquals( root, c0.Parent );
            assertEquals( 0, c0.ChildIndex );
            assertEquals( root, c0.Parent );
            assertEquals( 1, c1.ChildIndex );
            assertEquals( root, c0.Parent );
            assertEquals( 2, c2.ChildIndex );
        }
예제 #15
0
        public void TestReplaceInMiddle()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) ); // index 1
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );
            t.ReplaceChildren( 1, 1, newChild );
            string expecting = "(a b x d)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #16
0
        public void TestList()
        {
            // ^(nil 101 102 103)
            CommonTree r0 = new CommonTree( (IToken)null );
            CommonTree c0, c1, c2;
            r0.AddChild( c0 = new CommonTree( new CommonToken( 101 ) ) );
            r0.AddChild( c1 = new CommonTree( new CommonToken( 102 ) ) );
            r0.AddChild( c2 = new CommonTree( new CommonToken( 103 ) ) );

            assertNull( r0.Parent );
            assertEquals( -1, r0.ChildIndex );
            assertEquals( r0, c0.Parent );
            assertEquals( 0, c0.ChildIndex );
            assertEquals( r0, c1.Parent );
            assertEquals( 1, c1.ChildIndex );
            assertEquals( r0, c2.Parent );
            assertEquals( 2, c2.ChildIndex );
        }
예제 #17
0
        public void TestDupTree()
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            CommonTree r0 = new CommonTree( new CommonToken( 101 ) );
            CommonTree r1 = new CommonTree( new CommonToken( 102 ) );
            r0.AddChild( r1 );
            r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
            ITree r2 = new CommonTree( new CommonToken( 106 ) );
            r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
            r1.AddChild( r2 );
            r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
            r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );

            CommonTree dup = (CommonTree)( new CommonTreeAdaptor() ).DupTree( r0 );

            assertNull( dup.Parent );
            assertEquals( -1, dup.ChildIndex );
            dup.SanityCheckParentAndChildIndexes();
        }
예제 #18
0
        public void TestBecomeRoot5()
        {
            // ^(nil 5) becomes new root of ^(101 102 103)
            CommonTree newRoot = new CommonTree( (IToken)null );
            newRoot.AddChild( new CommonTree( new CommonToken( 5 ) ) );

            CommonTree oldRoot = new CommonTree( new CommonToken( 101 ) );
            oldRoot.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            oldRoot.AddChild( new CommonTree( new CommonToken( 103 ) ) );

            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            adaptor.BecomeRoot( newRoot, oldRoot );
            newRoot.SanityCheckParentAndChildIndexes();
        }
예제 #19
0
        public void TestMarkRewindEntire()
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree( new CommonToken( 101 ) );
            ITree r1 = new CommonTree( new CommonToken( 102 ) );
            r0.AddChild( r1 );
            r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
            ITree r2 = new CommonTree( new CommonToken( 106 ) );
            r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
            r1.AddChild( r2 );
            r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
            r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );

            ITreeNodeStream stream = newStream( r0 );
            int m = stream.Mark(); // MARK
            for ( int k = 1; k <= 13; k++ )
            { // consume til end
                stream.LT( 1 );
                stream.Consume();
            }
            Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Rewind( m );      // REWIND

            // consume til end again :)
            for ( int k = 1; k <= 13; k++ )
            { // consume til end
                stream.LT( 1 );
                stream.Consume();
            }
            Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 1 ) ).Type );
        }
예제 #20
0
        static bool RewriteNode(ITree root, out ITree output, RewriteRule rule)
        {
            ITree newRoot;
            bool needsRewrite = rule(root, out newRoot);
            if (newRoot == null)
            {
                newRoot = new CommonTree(new CommonToken(root.Type, root.Text));

                for (int i = 0; i < root.ChildCount; i++)
                {
                    ITree newChild;
                    bool branchNeedsRewrite = RewriteNode(root.GetChild(i), out newChild, rule);
                    needsRewrite = needsRewrite || branchNeedsRewrite;
                    newRoot.AddChild(newChild);
                }
            }
            output = newRoot;
            return needsRewrite;
        }
예제 #21
0
        public void TestMarkRewindInMiddle()
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree( new CommonToken( 101 ) );
            ITree r1 = new CommonTree( new CommonToken( 102 ) );
            r0.AddChild( r1 );
            r1.AddChild( new CommonTree( new CommonToken( 103 ) ) );
            ITree r2 = new CommonTree( new CommonToken( 106 ) );
            r2.AddChild( new CommonTree( new CommonToken( 107 ) ) );
            r1.AddChild( r2 );
            r0.AddChild( new CommonTree( new CommonToken( 104 ) ) );
            r0.AddChild( new CommonTree( new CommonToken( 105 ) ) );

            ITreeNodeStream stream = newStream( r0 );
            for ( int k = 1; k <= 7; k++ )
            { // consume til middle
                //System.out.println(((Tree)stream.LT(1)).getType());
                stream.Consume();
            }
            Assert.AreEqual( 107, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Mark(); // MARK
            stream.Consume(); // consume 107
            stream.Consume(); // consume UP
            stream.Consume(); // consume UP
            stream.Consume(); // consume 104
            stream.Rewind();      // REWIND
            stream.Mark(); // keep saving nodes though

            Assert.AreEqual( 107, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( 104, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            // now we're past rewind position
            Assert.AreEqual( 105, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( 1 ) ).Type );
            stream.Consume();
            Assert.AreEqual( TokenTypes.EndOfFile, ( (ITree)stream.LT( 1 ) ).Type );
            Assert.AreEqual( TokenTypes.Up, ( (ITree)stream.LT( -1 ) ).Type );
        }
예제 #22
0
        public void TestReplaceOneWithTwoAtRight()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChildren = (CommonTree)adaptor.Nil();
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "x" ) ) );
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "y" ) ) );

            t.ReplaceChildren( 2, 2, newChildren );
            string expecting = "(a b c x y)";
            Assert.AreEqual( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #23
0
        public void TestReplaceTwoWithOneAtRight()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );

            t.ReplaceChildren( 1, 2, newChild );
            string expecting = "(a b x)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #24
0
        public void TestReplaceTwoWithOneAtLeft()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );

            t.ReplaceChildren( 0, 1, newChild );
            string expecting = "(a x d)";
            Assert.AreEqual( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #25
0
        public void TestReplaceWithOneChildren()
        {
            // assume token type 99 and use text
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            CommonTree c0 = new CommonTree( new CommonToken( 99, "b" ) );
            t.AddChild( c0 );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "c" ) );
            t.ReplaceChildren( 0, 0, newChild );
            string expecting = "(a c)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
예제 #26
0
 static ITree CreatePlayerNode(int player)
 {
     ITree intTree = new CommonTree(new CommonToken(BoardGameLexer.INT, player.ToString()));
     ITree litInt = new CommonTree(new CommonToken(BoardGameLexer.LIT_INT, "LIT_INT"));
     litInt.AddChild(intTree);
     ITree playerNode = new CommonTree(new CommonToken(BoardGameLexer.PLAYERREF, "PLAYERREF"));
     playerNode.AddChild(litInt);
     return playerNode;
 }