checkpoint
authoradam <adam@megacz.com>
Fri, 14 Jul 2006 01:00:50 +0000 (21:00 -0400)
committeradam <adam@megacz.com>
Fri, 14 Jul 2006 01:00:50 +0000 (21:00 -0400)
darcs-hash:20060714010050-5007d-2569ae941160ae7af4314a481fb1bfa106b3bf46.gz

src/edu/berkeley/sbp/bind/Bindable.java
src/edu/berkeley/sbp/meta/MetaGrammar.java
src/edu/berkeley/sbp/meta/Production.java

index 4a10187..ddd6242 100644 (file)
@@ -29,6 +29,18 @@ public abstract class Bindable implements ToJava {
         return null;
     }
 
         return null;
     }
 
+    public Binding createBinding(final int[] map) {
+        return new Binding() {
+                public Object invoke(Object[] o) {
+                    int max = 0;
+                    for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
+                    Object[] o2 = new Object[max+1];
+                    for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
+                    return impose(o2);
+                }
+            };
+    }
+
     private static class BindableMethod extends Bindable {
         private final Method _method;
         public String toString() { return "BindableMethod["+_method+"]"; }
     private static class BindableMethod extends Bindable {
         private final Method _method;
         public String toString() { return "BindableMethod["+_method+"]"; }
index a4c620b..2340de2 100644 (file)
@@ -60,18 +60,7 @@ public class MetaGrammar {
         public TreeBindable(Bindable b) { this._bindable = b; }
 
         private int[] buildSequence(Production p) {
         public TreeBindable(Bindable b) { this._bindable = b; }
 
         private int[] buildSequence(Production p) {
-            Annotation[][] annotations = _bindable.getArgAnnotations();
-            Class[]        types       = _bindable.getArgTypes();
-            String[]       names       = _bindable.getArgNames();
-            String name                = _bindable.getSimpleName();
-            int len                    = annotations.length;
-            int ofs                    = 0;
-            bind.arg[] argtags  = new bind.arg[len];
-            for(int i=0; i<names.length; i++)
-                for(Annotation a : annotations[i+ofs])
-                    if (a instanceof bind.arg)
-                        argtags[i+ofs] = (bind.arg)a;
-            return TreeBindable.this.buildSequence(p, names, argtags, types);
+            return p.buildSequence(_bindable);
         }
 
         public String  getName()   { return _bindable.getSimpleName(); }
         }
 
         public String  getName()   { return _bindable.getSimpleName(); }
@@ -106,45 +95,6 @@ public class MetaGrammar {
             return false;
         }
 
             return false;
         }
 
-        private int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
-            int argTagged = 0;
-            boolean hasloc = types.length>0 && types[0]==Input.Region.class;
-            for(int i=0; i<argtags.length; i++) {
-                if (i==0 && types[0]==Input.Region.class) continue;
-                if (argtags[i] != null)
-                    argTagged++;
-            }
-            int numNames = names.length;
-            if (hasloc) numNames--;
-
-            // FIXME: can be smarter here
-            if (argTagged==p.count) {
-                int[] ret = new int[argtags.length];
-                int j = 0;
-                for(int i=0; i<argtags.length; i++) {
-                    if (i==0 && types[0]==Input.Region.class) continue;
-                    if (argtags[i]==null) continue;
-                    if (argtags[i].value().equals(""))
-                        ret[i] = j++;
-                    else {
-                        ret[i] = -1;
-                        for(int k=0; k<names.length; k++)
-                            if (argtags[i].value().equals(names[k])){
-                                ret[i] = k;
-                                break;
-                            }
-                        if (ret[i]==-1) return null;
-                    }
-                }
-                return ret;
-            } else if (numNames==p.count) {
-                int[] ret = new int[p.count];
-                for(int i=0; i<p.count; i++) ret[i] = i+(hasloc?1:0);
-                return ret;
-            } else {
-                return null;
-            }
-        }
         public Sequence makeSequence(final Production p) {
 
             if (_bindable.getArgTypes().length > 0 &&
         public Sequence makeSequence(final Production p) {
 
             if (_bindable.getArgTypes().length > 0 &&
@@ -229,11 +179,7 @@ public class MetaGrammar {
                 }
             }
             Object[] o = (Object[])ret.toArray(new Object[0]);
                 }
             }
             Object[] o = (Object[])ret.toArray(new Object[0]);
-            int max = 0;
-            for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
-            Object[] o2 = new Object[max+1];
-            for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
-            return _bindable.impose(o2);
+            return _bindable.createBinding(map).invoke(o);
         }
     }
 
         }
     }
 
index 0abb996..117eb57 100644 (file)
@@ -25,4 +25,57 @@ public  class Production {
             if (!drops[i])
                 count++;
     }
             if (!drops[i])
                 count++;
     }
+
+    public int[] buildSequence(Bindable _bindable) {
+        Annotation[][] annotations = _bindable.getArgAnnotations();
+        Class[]        types       = _bindable.getArgTypes();
+        String[]       names       = _bindable.getArgNames();
+        String name                = _bindable.getSimpleName();
+        int len                    = annotations.length;
+        int ofs                    = 0;
+        bind.arg[] argtags  = new bind.arg[len];
+        for(int i=0; i<names.length; i++)
+            for(Annotation a : annotations[i+ofs])
+                if (a instanceof bind.arg)
+                    argtags[i+ofs] = (bind.arg)a;
+
+        int argTagged = 0;
+        boolean hasloc = types.length>0 && types[0]==Input.Region.class;
+        for(int i=0; i<argtags.length; i++) {
+            if (i==0 && types[0]==Input.Region.class) continue;
+            if (argtags[i] != null)
+                argTagged++;
+        }
+        int numNames = names.length;
+        if (hasloc) numNames--;
+
+        // FIXME: can be smarter here
+        if (argTagged==count) {
+            int[] ret = new int[argtags.length];
+            int j = 0;
+            for(int i=0; i<argtags.length; i++) {
+                if (i==0 && types[0]==Input.Region.class) continue;
+                if (argtags[i]==null) continue;
+                if (argtags[i].value().equals(""))
+                    ret[i] = j++;
+                else {
+                    ret[i] = -1;
+                    for(int k=0; k<names.length; k++)
+                        if (argtags[i].value().equals(names[k])){
+                            ret[i] = k;
+                            break;
+                        }
+                    if (ret[i]==-1) return null;
+                }
+            }
+            return ret;
+        } else if (numNames==count) {
+            int[] ret = new int[count];
+            for(int i=0; i<count; i++) ret[i] = i+(hasloc?1:0);
+            return ret;
+        } else {
+            return null;
+        }
+    }
+
 }
 }