checkpoint
[sbp.git] / src / edu / berkeley / sbp / Tree.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.util.*;
4 import edu.berkeley.sbp.bind.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /** a tree (or node in a tree); see jargon.txt for details */
10 public class Tree<T>
11     extends PrintableTree<Tree<T>>
12     implements Iterable<Tree<T>>,
13                GraphViz.ToGraphViz {
14
15     private final Input.Region location;
16     private final T            head;
17     private final Tree<T>[]    children;
18     private final boolean      lift;
19
20     /** the element at the head of the tree */
21     public T                 head()        { return head; }
22
23     private Tree<T> lifted() { return children[children.length-1]; }
24
25     /** the number of children the tree has */
26     public int               numChildren() {
27         return lift
28             ? (children.length-1)+lifted().numChildren()
29             : children.length;
30     }
31
32     /** the tree's children */
33     public Iterable<Tree<T>> children()    { return this; }
34
35     /** the tree's children */
36     public Iterator<Tree<T>> iterator()    {
37         return lift
38             ? new ConcatenateIterator(new ArrayIterator(children, 0, children.length-1),
39                                       children[children.length-1].iterator())
40             : new ArrayIterator(children);
41     }
42
43     /** get the <tt>i</t>th child */
44     public Tree<T> child(int i)  {
45         return lift && i >= children.length-1
46             ? children[children.length-1].child(i-(children.length-1))
47             : children[i];
48     }
49
50     /** get the input region that this tree was parsed from */
51     public Input.Region    getRegion() { return location; }
52
53     public Tree(Input.Region loc, T head)                     { this(loc, head, null); }
54     public Tree(Input.Region loc, T head, Tree<T>[] children) { this(loc, head, children, false); }
55
56     /** package-private constructor, allows setting the "lift" bit */
57     Tree(Input.Region loc, T head, Tree<T>[] children, boolean lift) {
58         this.location = loc;
59         this.head = head;
60         this.lift = lift && children != null && children.length > 0;
61         this.children = ArrayUtil.clone(children, Tree.class);
62     }
63
64
65     // PrintableTree /////////////////////////////////////////////////////////////////////////////
66
67     protected String headToString() { return head()==null?null:head().toString(); }
68     protected String headToJava()   {
69       // FIXME
70         if (head()==null) return null;
71         if (head() instanceof ToJava) {
72             StringBuffer sb = new StringBuffer();
73             ((ToJava)head()).toJava(sb);
74             return sb.toString();
75         }
76         return (head()==null?"null":("\""+StringUtil.toJavaString(head().toString())+"\""));
77     }
78     protected String left()   { return "{"; }
79     protected String right()  { return "}"; }
80     protected boolean ignoreSingleton() { return false; }
81
82
83     // ToGraphViz /////////////////////////////////////////////////////////////////////////////
84
85     public GraphViz.Node toGraphViz(GraphViz gv) {
86         if (gv.hasNode(this)) return gv.createNode(this);
87         GraphViz.Node n = gv.createNode(this);
88         n.label = head()==null ? "" : head().toString();
89         for(Tree t : this) n.edge(t, null);
90         return n;
91     }
92     public boolean isTransparent() { return false; }
93     public boolean isHidden() { return false; }
94
95    
96 }