package oops;
import oops.parser.Goal;
import oops.parser.GoalAdapter;
import ck.Node;

/** collect sum: product [{ '+' product | '-' product }] as a Node tree.
  */
public class sum extends GoalAdapter {
  /** presents result of reduction.
      This is saved, or combined with the current tree.
      @param sender just received reduce(); must implement build.
      @param node was created by sender; must be a Number.
    */
  public void shift (Goal sender, Object node) {
    if (result == null) result = node;
    else result = ((build)sender).build((Number)result, (Number)node);
  }

  /** callback to build a tree.
    */
  public interface build {
    Object build (Number a, Number b);
  }

  /** builds a Node.Add tree.
    */
  public static class add extends GoalAdapter implements build {
    public Object build (Number a, Number b) {
      return new Node.Add(a, b);
    }
  }

  /** builds a Node.Sub tree.
    */
  public static class sub extends GoalAdapter implements build {
    public Object build (Number a, Number b) {
      return new Node.Sub(a, b);
    }
  }
}

