checkpoint
[sbp.git] / src / edu / berkeley / sbp / Tree.java
index da0c56c..c525ffa 100644 (file)
@@ -12,38 +12,57 @@ public class Tree<T>
     implements Iterable<Tree<T>>,
                GraphViz.ToGraphViz {
 
-    final T           head;
-          Tree<T>[]   children;
-    final Input.Region    location;
+    private final T            head;
+    private final Tree<T>[]    children;
+    private final Input.Region location;
+    private final boolean      lift;
 
     public T                 head()        { return head; }
-    public int               numChildren() { return children.length; }
-    public Iterable<Tree<T>> children()    { return new ArrayIterator(children); }
-    public Iterator<Tree<T>> iterator()    { return new ArrayIterator(children); }
-    public Tree<T>           child(int i)  { return children[i]; }
+
+    private Tree<T> lifted() { return children[children.length-1]; }
+
+    public int               numChildren() {
+        if (lift && children.length > 0)
+            return (children.length-1)+lifted().numChildren();
+        return children.length;
+    }
+    public Iterable<Tree<T>> children()    { return this; }
+    public Iterator<Tree<T>> iterator()    {
+        if (lift)
+            return new ConcatenateIterator(new ArrayIterator(children, 0, children.length-1),
+                                           children[children.length-1].iterator());
+        return new ArrayIterator(children);
+    }
+    public Tree<T>           child(int i)  {
+        if (lift && i >= children.length-1)
+            return children[children.length-1].child(i-(children.length-1));
+        return children[i];
+    }
 
     public Input.Region    getRegion() { return location; }
 
     public Tree(Input.Region loc, T head)                   { this(loc, head, null); }
-    public Tree(Input.Region loc, T head, Tree<T>[] children) {
+    public Tree(Input.Region loc, T head, Tree<T>[] children) { this(loc, head, children, false); }
+    public Tree(Input.Region loc, T head, Tree<T>[] children, boolean lift) {
         this.location = loc;
         this.head = head;
+        this.lift = lift;
 
         Tree<T>[] children2 = children==null ? new Tree[0] : new Tree[children.length];
         if (children != null) System.arraycopy(children, 0, children2, 0, children.length);
         this.children = children2;
     }
 
-    protected String headToString() { return head==null?null:head.toString(); }
+    protected String headToString() { return head()==null?null:head().toString(); }
     protected String headToJava()   {
       // FIXME
-        if (head==null) return null;
-        if (head instanceof ToJava) {
+        if (head()==null) return null;
+        if (head() instanceof ToJava) {
             StringBuffer sb = new StringBuffer();
-            ((ToJava)head).toJava(sb);
+            ((ToJava)head()).toJava(sb);
             return sb.toString();
         }
-        return (head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\""));
+        return (head()==null?"null":("\""+StringUtil.toJavaString(head().toString())+"\""));
     }
     protected String left()   { return "{"; }
     protected String right()  { return "}"; }