- if (n==null) {
- if (r.position.pos==0) r.reduce(this);
- } else if (r.position.pos==0) return;
- else if (n2==null) r.reduce(n);
- else r.reduce(n, n2);
+ if (n==null || n2==null || r.position.pos==0) {
+ if (r.position.pos==0) {
+ if (n==null) n = this;
+ else return;
+ }
+ if (n==null) return;
+ Forest[] holder = new Forest[r.position.pos];
+ if (r.position.pos==0) n.finish(r, r.zero(), n.phase(), holder);
+ else r.reduce(n, r.position.pos-1, n.phase(), holder);
+ } else {
+ Forest[] holder = new Forest[r.position.pos];
+ if (r.position.pos<=0) throw new Error("called wrong form of reduce()");
+ int pos = r.position.pos-1;
+ Forest old = holder[pos];
+ holder[pos] = n.pending();
+ if (pos==0) {
+ System.arraycopy(holder, 0, r.position.holder, 0, holder.length);
+ Forest rex = r.position.rewrite(n.phase().getLocation());
+ n2.finish(r, rex, n.phase(), holder);
+ } else {
+ r.reduce(n2, pos-1, n.phase(), holder);
+ }
+ holder[pos] = old;
+ }