X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FReflectiveGrammar.java;h=5f90b7f045bbcbad6ee84b531ae5f6f598a9e3fb;hb=3c03a0a1131b46a23ccfdeab2cb4fbd59ee05b7a;hp=ecf186de6d53e3f75e4d4892ab6f7875e20b725b;hpb=45d799349e635f1a99e3974e4504a43d5a7aaf33;p=sbp.git diff --git a/src/edu/berkeley/sbp/misc/ReflectiveGrammar.java b/src/edu/berkeley/sbp/misc/ReflectiveGrammar.java index ecf186d..5f90b7f 100644 --- a/src/edu/berkeley/sbp/misc/ReflectiveGrammar.java +++ b/src/edu/berkeley/sbp/misc/ReflectiveGrammar.java @@ -8,12 +8,16 @@ import edu.berkeley.sbp.tib.*; import edu.berkeley.sbp.chr.*; import edu.berkeley.sbp.util.*; -public class ReflectiveGrammar extends MetaGrammar { - +public class ReflectiveGrammar /*extends MetaGrammar*/ { + /* final Class baseClass; public ReflectiveGrammar(Class baseClass) { this.baseClass = baseClass; } public Object convertLabel(String label) { return new ClassMark(label); } + public Object repeatTag() { return arrayMark; } + + private static class ArrayMark { } + private static final ArrayMark arrayMark = new ArrayMark(); private static class ClassMark { public final String clazz; @@ -33,45 +37,67 @@ public class ReflectiveGrammar extends MetaGrammar { public Object build(Tree t) throws Exception { return buildHead(t, null); } public Object buildHead(Tree t, Class c) throws Exception { - System.out.println("buildHead " + (c==null?null:c.getName()) + " " + t); + //System.out.println("buildHead " + (c==null?null:c.getName()) + " " + t); Object h = t.head(); - if (h != null && h instanceof ClassMark) return buildBody(t, Class.forName(baseClass.getName()+"$"+((ClassMark)h).clazz)); + if (h != null && h instanceof ClassMark) { + String clazz = ReflectiveWalker.mangle(((ClassMark)h).clazz); + Class c2 = (c!=null && c.getName().endsWith("$"+clazz)) ? c : Reflection.forNameOrNull(baseClass.getName()+"$"+clazz); + if (c2!=null) return buildBody(t, c2); + for(Method m : baseClass.getMethods()) { + if (!m.getName().equals(clazz)) continue; + Object[] o = new Object[m.getParameterTypes().length]; + for(int i=0; i=t.numChildren() ? null : buildHead(t.child(i), m.getParameterTypes()[i]); + return m.invoke(null, o); + } + throw new RuntimeException("couldn't figure out what to invoke for ClassMark " + clazz); + } else if (h==arrayMark && c==null) { + c = Object[].class; + } if (c.isArray()) { Object[] ret = new Object[t.numChildren()]; for(int i=0; i 0) throw new RuntimeException("can't buildHead() on a tree with children when the head is of type " + h.getClass().getName()); + if (t.numChildren() > 0) + throw new RuntimeException("can't buildHead() on a tree with children when the head is of type " + + h.getClass().getName() + "(c=="+(c==null?"null":c.getName())+")"); return h; } public Object buildBody(Tree t, Class c) throws Exception { - System.out.println("buildBody " + (c==null?null:c.getName()) + " " + t); + //System.out.println("buildBody " + (c==null?null:c.getName()) + " " + t); c = resolveClass(t, c); + if (c==null) return buildHead(t, null); Object o = c.newInstance(); Field[] f = c.getFields(); OUTER: for(int i=0; i t, Class c) throws Exception { if (c==null) return null; - System.out.println("resolving " + c.getName()); + if (c==int.class) return c; + if (c==String.class) return c; + //System.out.println("resolving " + c.getName()); if (Reflection.isConcrete(c)) return c; Class ret = null; Class[] subs = (Class[])c.getField("subclasses").get(null); OUTER: for(int i=0; i