
import java.util.*;

public class Evaluator implements ExpressionVisitor {

  private Stack<Integer> values = new Stack<Integer>();

  public void visit(IntExp e) {
    values.push( e.getValue() );
  }

  public void visit(AddExp e) {
    // nothing to be done here 
  }
  public void visitBefore(AddExp e) {
    // nothing to be done here 
  }
  public void visitAfter(AddExp e) {
    // now the values of the two subexpressions
    // should be on the stack: pop pop add push
    int x = values.pop();
    int y = values.pop();
    values.push(x+y);
  }

  public int finalValue() {
    // pop last value and return
    return values.pop();
  }
}
