X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FDemo.java;h=b6c05241d23d2b8c6a94484fe38f931de1cc8918;hb=117f3082281baf893dd183c0d50d809b147f2f1c;hp=63849398c4b76384b4f4f60d1c02d3e218dea432;hpb=ac0f7d32be59d02b1f8d48d2b23232adf8871b46;p=sbp.git diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 6384939..b6c0524 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -89,22 +89,21 @@ public class Demo { return false; } public Object repeatTag() { - return new Reducer() { + return new Functor,Object>() { public String toString() { return ""; } - public Object reduce(Tree t) { - Object[] ret = new Object[t.numChildren()]; - for(int i=0; i t) { + ArrayList ret = new ArrayList(); + for(Tree tc : t) { + if (tc.head() != null && tc.head() instanceof Functor) + ret.add(((Functor,Object>)tc.head()).invoke(tc.children())); 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); - ret[i] = null; + ret.add(null); } } - return ret; + return ret.toArray(new Object[0]); } }; } @@ -238,43 +237,40 @@ public class Demo { } } 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 Object invokeRaw(Tree t) { return null; } - public class TargetReducer implements Reducer { + public Object invokeRaw(Iterable t) { return null; } + public class TargetReducer implements Functor,Object> { 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.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 invoke(Iterable t) { if (isRaw()) return invokeRaw(t); - Object[] objects = new Object[t.numChildren()]; - for(int i=0; i,Object>)tc.head()).invoke(tc.children())); 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); - objects[i] = null; + ret.add(null); } } System.err.println("input tree: " + t); - return plant(objects, map); + return plant(ret.toArray(new Object[0]), map); } } } - public static interface Reducer { - public Object reduce(Tree t); - } - public static class TargetClass extends Target { public final Class _class; public TargetClass(Class _class) { this._class = _class; } @@ -394,7 +390,7 @@ public class Demo { return ret; } public boolean isRaw() { return _method.isAnnotationPresent(raw.class); } - public Object invokeRaw(Tree t) { + public Object invokeRaw(Iterable t) { try { return _method.invoke(null, new Object[] { t }); } catch (Exception e) { @@ -485,8 +481,8 @@ 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(); - MG.Grammar g = (MG.Grammar)red.reduce(t); + Functor,Object> red = (Functor,Object>)t.head(); + MG.Grammar g = (MG.Grammar)red.invoke(t.children()); Context cx = new Context(g,rm); Union u = null; for(MG.NonTerminal nt : g.nonterminals) { @@ -785,8 +781,8 @@ public class Demo { } public Context(Tree t, ReflectiveMeta rm) { this.rm = rm; - Reducer red = (Reducer)t.head(); - this.grammar = (MG.Grammar)red.reduce(t); + Functor,Object> red = (Functor,Object>)t.head(); + this.grammar = (MG.Grammar)red.invoke(t.children()); } public Union peek(String name) { return map.get(name); } public void put(String name, Union u) { map.put(name, u); }