Example #1
0
        /// <summary>
        /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings.
        /// </summary>
        /// <param name="t">Root of AST Node tree.</param>
        /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns>
        public virtual AST dupList(AST t)
        {
            AST result = dupTree(t);             // if t == null, then result==null
            AST nt     = result;

            while (t != null)
            {
                // for each sibling of the root
                t = t.getNextSibling();
                nt.setNextSibling(dupTree(t));                 // dup each subtree, building new tree
                nt = nt.getNextSibling();
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Make a tree from a list of nodes.  The first element in the
        /// array is the root.  If the root is null, then the tree is
        /// a simple list not a tree.  Handles null children nodes correctly.
        /// For example, build(a, b, null, c) yields tree (a b c).  build(null,a,b)
        /// yields tree (nil a b).
        /// </summary>
        /// <param name="nodes">List of Nodes.</param>
        /// <returns>AST Node tree.</returns>
        public virtual AST make(params AST[] nodes)
        {
            if (nodes == null || nodes.Length == 0)
            {
                return(null);
            }
            AST root = nodes[0];
            AST tail = null;

            if (root != null)
            {
                root.setFirstChild(null);                 // don't leave any old pointers set
            }
            // link in children;
            for (int i = 1; i < nodes.Length; i++)
            {
                if (nodes[i] == null)
                {
                    continue;
                }
                // ignore null nodes
                if (root == null)
                {
                    // Set the root and set it up for a flat list
                    root = (tail = nodes[i]);
                }
                else if (tail == null)
                {
                    root.setFirstChild(nodes[i]);
                    tail = root.getFirstChild();
                }
                else
                {
                    tail.setNextSibling(nodes[i]);
                    tail = tail.getNextSibling();
                }
                // Chase tail to last sibling
                while (tail.getNextSibling() != null)
                {
                    tail = tail.getNextSibling();
                }
            }
            return(root);
        }