// $Id: IncrementBy.java,v 1.1 1999/11/15 05:26:53 leavens Exp $

package lib.atomic_expression;

public class IncrementBy implements Visitor {
    protected int n;
    public IncrementBy(int n) {
        this.n = n;
    }
    public Object visitNil(Nil n) {
        return n;
    }
    public Object visitAtom(Atom a) {
        return new Atom(new Integer(((Integer)(a.getObject())).intValue()
                                    + n));
    }
    public Object visitExpr(Expr e) {
        return new Expr((AtomicExpression)((e.getLeft()).visit(this)),
                        (AtomicExpression)((e.getRight()).visit(this)));
    }

    /** to test, execute "java lib.atomic_expression.IncrementBy" **/
    public static void main(String [] argv) {
        IncrementBy ib = new IncrementBy(1);
        ToString ts = new ToString();
        AtomicExpression myExp
            = new Expr(new Atom(new Integer(4)),
                       new Expr(new Atom(new Integer(2)), new Nil()));
        AtomicExpression bigger
            = new Expr(myExp,
                       new Expr(myExp, new Nil()));
        AtomicExpression [] tests = { 
            new Nil(),
            new Atom(new Integer(3)),
            myExp,
            bigger
        };
        for (int i = 0; i < tests.length; i++) {
            System.out.println(tests[i].visit(ts)
                           + ".visit(" + ib + ") = "
                           + ((AtomicExpression)(tests[i].visit(ib)))
                               .visit(ts));
        }
    }

    public String toString() {
        return "new IncrementBy()";
    }
}
