checkpoint
authoradam <adam@megacz.com>
Wed, 5 Jul 2006 06:29:13 +0000 (02:29 -0400)
committeradam <adam@megacz.com>
Wed, 5 Jul 2006 06:29:13 +0000 (02:29 -0400)
darcs-hash:20060705062913-5007d-7e4f4101ec37b951a663f86bee49a47592eae297.gz

Makefile
src/edu/berkeley/sbp/meta/AnnotationGrammarBindingResolver.java
src/edu/berkeley/sbp/meta/MetaGrammar.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/util/Reflection.java

index fec6d8d..daaf625 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,8 @@ tibdoc: edu.berkeley.sbp.jar
 
 demo: edu.berkeley.sbp.jar
        $(java) -cp $< edu.berkeley.sbp.misc.Demo \
 
 demo: edu.berkeley.sbp.jar
        $(java) -cp $< edu.berkeley.sbp.misc.Demo \
-               tests/meta.g \
-               tests/meta.g
+               tests/demo.g \
+               '(11+2*3)-44'
 
 regress:
        make boot
 
 regress:
        make boot
index 825dc56..ca10c04 100644 (file)
@@ -36,6 +36,8 @@ public class AnnotationGrammarBindingResolver extends GrammarBindingResolver {
 
     public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
         MetaGrammar.Production p = new MetaGrammar.Production(tag, nonTerminalName, els, labels, drops);
 
     public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
         MetaGrammar.Production p = new MetaGrammar.Production(tag, nonTerminalName, els, labels, drops);
+            if (labels != null && labels.length > 0 && "numeric".equals(labels[0]))
+                System.out.println("here! ");
         for(Method m : _cl.getMethods())
             if (new MetaGrammar.Target(m).isCompatible(p))
                 return new MetaGrammar.Target(m).makeSequence(p);
         for(Method m : _cl.getMethods())
             if (new MetaGrammar.Target(m).isCompatible(p))
                 return new MetaGrammar.Target(m).makeSequence(p);
index aa02d5f..0e8968f 100644 (file)
@@ -127,7 +127,7 @@ public class MetaGrammar {
                 return buildSequence(p)!=null;
 
             bind b = getBind();
                 return buildSequence(p)!=null;
 
             bind b = getBind();
-            System.out.println(_bindable.getClass().getSimpleName() + ": " + _bindable.getSimpleName());
+            //System.out.println(_bindable.getClass().getSimpleName() + ": " + _bindable.getSimpleName());
             if (b != null && getName().equals(p.tag))
                 return buildSequence(p)!=null;
 
             if (b != null && getName().equals(p.tag))
                 return buildSequence(p)!=null;
 
@@ -150,15 +150,27 @@ public class MetaGrammar {
                     argTagged++;
 
             // FIXME: can be smarter here
                     argTagged++;
 
             // FIXME: can be smarter here
-            if (names.length==p.count) {
-                int[] ret = new int[p.count];
-                for(int i=0; i<p.count; i++) ret[i] = i;
-                return ret;
-            } else if (argTagged==p.count) {
+            if (argTagged==p.count) {
                 int[] ret = new int[argtags.length];
                 int j = 0;
                 int[] ret = new int[argtags.length];
                 int j = 0;
-                for(int i=0; i<argtags.length; i++)
-                    ret[i] = argtags[i]==null ? -1 : (j++);
+                for(int i=0; i<argtags.length; i++) {
+                    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 (names.length==p.count) {
+                int[] ret = new int[p.count];
+                for(int i=0; i<p.count; i++) ret[i] = i;
                 return ret;
             } else {
                 return null;
                 return ret;
             } else {
                 return null;
@@ -206,7 +218,7 @@ public class MetaGrammar {
                     ret.add(null);
                 }
             }
                     ret.add(null);
                 }
             }
-            System.err.println("input tree: " + t);
+            //System.err.println("input tree: " + t);
             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[] o = (Object[])ret.toArray(new Object[0]);
             int max = 0;
             for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
@@ -237,17 +249,14 @@ public class MetaGrammar {
     }
     public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver()); }
     public static Union make(Tree t, String s, GrammarBindingResolver rm) {
     }
     public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver()); }
     public static Union make(Tree t, String s, GrammarBindingResolver rm) {
-        System.out.println("Head: " + t.head());
         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
         MetaGrammarBindings.Grammar g = (MetaGrammarBindings.Grammar)red.invoke(t.children());
         Context cx = new Context(g,rm);
         Union u = null;
         for(MetaGrammarBindings.NonTerminal nt : g.nonterminals) {
         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
         MetaGrammarBindings.Grammar g = (MetaGrammarBindings.Grammar)red.invoke(t.children());
         Context cx = new Context(g,rm);
         Union u = null;
         for(MetaGrammarBindings.NonTerminal nt : g.nonterminals) {
-            System.out.println(nt.name);
             Union el = (Union)cx.get(nt.name);
             StringBuffer st = new StringBuffer();
             el.toString(st);
             Union el = (Union)cx.get(nt.name);
             StringBuffer st = new StringBuffer();
             el.toString(st);
-            System.err.println(st);
             if (nt.name.equals(s)) u = el;
         }
         return u;
             if (nt.name.equals(s)) u = el;
         }
         return u;
index 3ba3d2a..ca3b7fe 100644 (file)
@@ -47,8 +47,8 @@ public class MetaGrammarBindings {
     }
     public static class NonTerminal extends Un {
         public String  name = null;
     }
     public static class NonTerminal extends Un {
         public String  name = null;
-        public @bind NonTerminal(@bind.arg("Word") String name,
-                                 @bind.arg("RHS") Seq[][] sequences) {
+        public @bind NonTerminal(@bind.arg String name,
+                                 @bind.arg Seq[][] sequences) {
             this.name = name;
             this.sequences = sequences;
         }
             this.name = name;
             this.sequences = sequences;
         }
index 05de7dc..2cc7b0a 100644 (file)
@@ -54,7 +54,7 @@ public final class Reflection {
 
     public static String show(Object o) {
         if (o==null) return "null";
 
     public static String show(Object o) {
         if (o==null) return "null";
-        if (o instanceof Show) return ((Show)o).toString();
+        if (o instanceof Show) return show((Show)o);
         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
         Object[] arr = (Object[])o;
         StringBuffer ret = new StringBuffer();
         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
         Object[] arr = (Object[])o;
         StringBuffer ret = new StringBuffer();
@@ -219,7 +219,7 @@ public final class Reflection {
                 if (f[i].getType() == String.class) tgt = stringify(tgt);
                 // FUGLY
                 tgt = coerce(tgt, f[i].getType());
                 if (f[i].getType() == String.class) tgt = stringify(tgt);
                 // FUGLY
                 tgt = coerce(tgt, f[i].getType());
-                System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
+                //System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
                 f[i].set(ret, tgt);
             }
             return ret;
                 f[i].set(ret, tgt);
             }
             return ret;
@@ -239,10 +239,10 @@ public final class Reflection {
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
-                System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
+                //System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
                 args[i] = tgt;
             }
                 args[i] = tgt;
             }
-            System.err.println("invoking " + _method + " with " + Reflection.show(args));
+            //System.err.println("invoking " + _method + " with " + Reflection.show(args));
             return _method.invoke(null, args);
         } catch (Exception e) {
             throw new RuntimeException(e);
             return _method.invoke(null, args);
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -259,7 +259,7 @@ public final class Reflection {
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
-                System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
+                //System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
                 args[i] = tgt;
             }
             return _ctor.newInstance(args);
                 args[i] = tgt;
             }
             return _ctor.newInstance(args);
@@ -284,6 +284,10 @@ public final class Reflection {
         if (c == char.class) {
             return o.toString().charAt(0);
         }
         if (c == char.class) {
             return o.toString().charAt(0);
         }
+        if (c==int.class || c==Integer.class) {
+            String s = (String)coerce(o, String.class);
+            return new Integer(Integer.parseInt(s));
+        }
 
         if (o.getClass().isArray() &&
             o.getClass().getComponentType().isArray() &&
 
         if (o.getClass().isArray() &&
             o.getClass().getComponentType().isArray() &&
@@ -311,12 +315,12 @@ public final class Reflection {
             for(int i=0; i<((Object[])o).length; i++) {
                 Object ob = (((Object[])o)[i]);
                 if (ob != null) {
             for(int i=0; i<((Object[])o).length; i++) {
                 Object ob = (((Object[])o)[i]);
                 if (ob != null) {
-                    System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
+                    //System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
                     ok = false;
                 }
             }
             if (ok) {
                     ok = false;
                 }
             }
             if (ok) {
-                System.err.println("hit with " + c.getComponentType().getName());
+                //System.err.println("hit with " + c.getComponentType().getName());
                 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
             }
         }
                 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
             }
         }