checkpoint
authoradam <adam@megacz.com>
Wed, 5 Jul 2006 00:21:46 +0000 (20:21 -0400)
committeradam <adam@megacz.com>
Wed, 5 Jul 2006 00:21:46 +0000 (20:21 -0400)
darcs-hash:20060705002146-5007d-41334f56d2a0d9e4486a2a7c024c635614039776.gz

src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/misc/RegressionTests.java

index 6384939..2b99ffb 100644 (file)
@@ -91,20 +91,19 @@ public class Demo {
         public Object repeatTag() {
             return new Reducer() {
                     public String toString() { return ""; }
         public Object repeatTag() {
             return new Reducer() {
                     public String toString() { return ""; }
-                    public Object reduce(Tree t) {
-                        Object[] ret = new Object[t.numChildren()];
-                        for(int i=0; i<t.numChildren(); i++) {
-                            Tree tc = t.child(i);
+                    public Object reduce(Iterable<Tree> t) {
+                        ArrayList ret = new ArrayList();
+                        for(Tree tc : t) {
                             if (tc.head() != null && tc.head() instanceof Reducer)
                             if (tc.head() != null && tc.head() instanceof Reducer)
-                                ret[i] = ((Reducer)tc.head()).reduce(tc);
+                                ret.add(((Reducer)tc.head()).reduce(tc.children()));
                             else if (tc.numChildren() == 0)
                             else if (tc.numChildren() == 0)
-                                ret[i] = tc.head();
+                                ret.add(tc.head());
                             else {
                                 System.err.println("FIXME: don't know what to do about " + tc);
                             else {
                                 System.err.println("FIXME: don't know what to do about " + tc);
-                                ret[i] = null;
+                                ret.add(null);
                             }
                         }
                             }
                         }
-                        return ret;
+                        return ret.toArray(new Object[0]);
                     }
                 };
         }
                     }
                 };
         }
@@ -238,41 +237,42 @@ public class Demo {
             }
         }
         public Sequence makeSequence(Production p) {
             }
         }
         public Sequence makeSequence(Production p) {
-            return Sequence.rewritingSequence(new TargetReducer(p), p.elements, p.labels, p.drops);
+            return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this), p.elements, p.labels, p.drops);
         }
         public abstract Object plant(Object[] fields, int[] map);
         public boolean isRaw() { return false; }
         }
         public abstract Object plant(Object[] fields, int[] map);
         public boolean isRaw() { return false; }
-        public Object invokeRaw(Tree t) { return null; }
+        public Object invokeRaw(Iterable<Tree> t) { return null; }
         public class TargetReducer implements Reducer {
             private Production p;
             private int[] map;
         public class TargetReducer implements Reducer {
             private Production p;
             private int[] map;
-            public TargetReducer(Production p) {
+            private String name;
+            public TargetReducer(Production p, int[] map, String name) {
                 this.p = p;
                 this.p = p;
-                this.map = buildSequence(p);
+                this.map = map;
+                this.name = name;
             }
             }
-            public String toString() { return "reducer-"+Target.this; }
-            public Object reduce(Tree t) {
+            public String toString() { return name; }
+            public Object reduce(Iterable<Tree> t) {
                 if (isRaw()) return invokeRaw(t);
                 if (isRaw()) return invokeRaw(t);
-                Object[] objects = new Object[t.numChildren()];
-                for(int i=0; i<t.numChildren(); i++) {
-                    Tree tc = t.child(i);
+                ArrayList ret = new ArrayList();
+                for(Tree tc : t) {
                     if (tc.head() != null && tc.head() instanceof Reducer)
                     if (tc.head() != null && tc.head() instanceof Reducer)
-                        objects[i] = ((Reducer)tc.head()).reduce(tc);
+                        ret.add(((Reducer)tc.head()).reduce(tc.children()));
                     else if (tc.numChildren() == 0)
                     else if (tc.numChildren() == 0)
-                        objects[i] = tc.head();
+                        ret.add(tc.head());
                     else {
                         System.err.println("FIXME: don't know what to do about " + tc);
                     else {
                         System.err.println("FIXME: don't know what to do about " + tc);
-                        objects[i] = null;
+                        ret.add(null);
                     }
                 }
                 System.err.println("input tree: " + t);
                     }
                 }
                 System.err.println("input tree: " + t);
-                return plant(objects, map);
+                return plant(ret.toArray(new Object[0]), map);
             }
         }
     }
 
     public static interface Reducer {
             }
         }
     }
 
     public static interface Reducer {
-        public Object reduce(Tree t);
+        public Object reduce(Iterable<Tree> t);
     }
 
     public static class TargetClass extends Target {
     }
 
     public static class TargetClass extends Target {
@@ -394,7 +394,7 @@ public class Demo {
             return ret;
         }
         public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
             return ret;
         }
         public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
-        public Object invokeRaw(Tree t) {
+        public Object invokeRaw(Iterable<Tree> t) {
             try {
                 return _method.invoke(null, new Object[] { t });
             } catch (Exception e) {
             try {
                 return _method.invoke(null, new Object[] { t });
             } catch (Exception e) {
@@ -486,7 +486,7 @@ public class Demo {
     public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
     public static Union make(Tree t, String s, ReflectiveMeta rm) {
         Reducer red = (Reducer)t.head();
     public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
     public static Union make(Tree t, String s, ReflectiveMeta rm) {
         Reducer red = (Reducer)t.head();
-        MG.Grammar g = (MG.Grammar)red.reduce(t);
+        MG.Grammar g = (MG.Grammar)red.reduce(t.children());
         Context cx = new Context(g,rm);
         Union u = null;
         for(MG.NonTerminal nt : g.nonterminals) {
         Context cx = new Context(g,rm);
         Union u = null;
         for(MG.NonTerminal nt : g.nonterminals) {
@@ -786,7 +786,7 @@ public class Demo {
         public Context(Tree t, ReflectiveMeta rm) {
             this.rm = rm;
             Reducer red = (Reducer)t.head();
         public Context(Tree t, ReflectiveMeta rm) {
             this.rm = rm;
             Reducer red = (Reducer)t.head();
-            this.grammar = (MG.Grammar)red.reduce(t);
+            this.grammar = (MG.Grammar)red.reduce(t.children());
         }
         public Union peek(String name) { return map.get(name); }
         public void  put(String name, Union u) { map.put(name, u); }
         }
         public Union peek(String name) { return map.get(name); }
         public void  put(String name, Union u) { map.put(name, u); }
index b186b43..dc52644 100644 (file)
@@ -59,7 +59,7 @@ public class RegressionTests {
             System.err.println("expanding...");
 
             Tree t = r2.expand1();
             System.err.println("expanding...");
 
             Tree t = r2.expand1();
-            TestCase[] expanded = (TestCase[])((Demo.Reducer)t.head()).reduce(t);
+            TestCase[] expanded = (TestCase[])((Demo.Reducer)t.head()).reduce(t.children());
             System.err.println("executing...");
             for(TestCase tc : expanded) {
                 tc.execute();
             System.err.println("executing...");
             for(TestCase tc : expanded) {
                 tc.execute();
@@ -103,9 +103,9 @@ public class RegressionTests {
             });
         }
         public static class TestCaseMakerHelper extends MG {
             });
         }
         public static class TestCaseMakerHelper extends MG {
-            public static @tag("grammaro") @raw Object grammaro(Tree t) {
+            public static @tag("grammaro") @raw Object grammaro(Iterable<Tree> t) {
                 System.out.println("working on " + t);
                 System.out.println("working on " + t);
-                return Demo.make(t.child(0), "s", new ReflectiveMetaPlain());
+                return Demo.make(t.iterator().next(), "s", new ReflectiveMetaPlain());
             }
             //public static @tag("tca")           Object tca(Object[] o) throws IOException {
             //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
             }
             //public static @tag("tca")           Object tca(Object[] o) throws IOException {
             //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }