it all works
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 39e4b11..83d9b6f 100644 (file)
@@ -60,7 +60,17 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         this.firstp = new Position(0);
     }
 
         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();
+            Forest fo = firstp().rewrite(null);
+            epsilonForm.merge(fo);
+        }
+        return epsilonForm;
+    }
 
     protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
 
 
     protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
 
@@ -101,10 +111,17 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
         // Reduction /////////////////////////////////////////////////////////////////////////////////
 
 
         // 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() && eps) return epsilonForm;
+            eps = true;
+            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
             Forest<T> ret = Sequence.this.postReduce(loc, holder);
             for(int k=0; k<pos; k++) holder[k] = null; // to help GC
+            if (this==firstp()) { if (epsilonForm==null) epsilonForm=new Forest.Ref(); epsilonForm.merge(ret); return epsilonForm; }
             return ret;
         }
 
             return ret;
         }
 
@@ -165,6 +182,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         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) {
         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++;
             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++;
@@ -190,6 +208,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];
             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) {
             return Forest.create(loc, (T)tag, args2, this, false, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {