intermediate checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index b332793..610af58 100644 (file)
@@ -36,6 +36,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     ////////////////////////////////////////////////////////////////////////////////
 
     public Element noFollow = null;
+    public String name = null;
+    public void setName(String name) { this.name = name; }
     public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); }
 
     Topology toAtom() {
@@ -45,9 +47,11 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     protected final Element[] elements;
 
+          HashSet<Sequence> needed;
+          HashSet<Sequence> hated;
     final HashSet<Sequence> needs;
     final HashSet<Sequence> hates;
-          boolean           lame  = false;
+    public boolean           lame  = false;
 
     final Position          firstp;
     Position firstp() { return firstp; }
@@ -56,13 +60,26 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
         this.needs = and==null ? new HashSet<Sequence>() : and;
         this.hates = not==null ? new HashSet<Sequence>() : not;
-        //for(Sequence s : needs) s.lame = true;
-        //for(Sequence s : hates) s.lame = true;
+        if (this.needs != null)
+            for(Sequence s : this.needs)
+                (s.needed==null?(s.needed=new HashSet<Sequence>()):s.needed).add(this);
+        if (this.hates != null)
+            for(Sequence s : this.hates)
+                (s.hated==null?(s.hated=new HashSet<Sequence>()):s.hated).add(this);
         this.elements = elements;
         this.firstp = new Position(0);
     }
 
-    Forest epsilonForm() { return firstp().rewrite(null); }
+    // DO NOT MESS WITH THE FOLLOWING LINE!!!
+    private Forest.Ref epsilonForm = null;
+    private boolean eps = false;
+    Forest epsilonForm() {
+        if (epsilonForm==null) {
+            epsilonForm = new Forest.Ref();
+            epsilonForm.merge(firstp().rewrite2(null));
+        }
+        return epsilonForm;
+    }
 
     protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
 
@@ -103,8 +120,17 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
         // Reduction /////////////////////////////////////////////////////////////////////////////////
 
-        <T> Forest<T> rewrite(Token.Location loc) {
-            for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
+        final <T> Forest<T> rewrite(Token.Location loc) {
+            if (this==firstp()) return epsilonForm();
+            return rewrite2(loc);
+        }
+
+        final <T> Forest<T> rewrite2(Token.Location loc) {
+            for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
+            for(int i=pos; i<elements.length; i++) {
+                if (holder[i]==null) holder[i] = elements[i].epsilonForm();
+                if (holder[i]==null) throw new Error("bad " + i);
+            }
             Forest<T> ret = Sequence.this.postReduce(loc, holder);
             for(int k=0; k<pos; k++) holder[k] = null; // to help GC
             return ret;
@@ -162,11 +188,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
     }
 
-    static class Unwrap extends Sequence {
+    public static class Unwrap extends Sequence {
         private boolean[] drops;
         public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not)                  { super(e, and, not); this.drops = null; }
         public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
+            for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
             if (drops==null) return Forest.create(loc, null, args, this, true, false);
             int count = 0;
             for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
@@ -178,7 +205,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     }
 
     static class RewritingSequence extends Sequence {
-        private final Object tag;
+        /*private*/public final Object tag;
         private final boolean[] drops;
         private int count = 0;
         public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
@@ -192,6 +219,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
+            //System.out.println("reduce \""+tag+"\"");
             return Forest.create(loc, (T)tag, args2, this, false, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {