001package enumeration;
002
003public interface Expr {
004        void printPostorder();
005        int evaluate();
006}
007
008class Const implements Expr {
009        private final int v;
010        public Const(int v) {
011                this.v = v;
012        }
013
014        public int evaluate() {
015                return v;
016        }
017        public void printPostorder() {
018                System.out.print(v + " ");
019        }
020}
021
022class BinOp implements Expr {
023        private final Expr l;
024        private final Expr r;
025        private final Op op;
026
027        public BinOp(Expr l, Op op, Expr r) {
028                if ((l == null) || (op == null) || (r == null)) {
029                        throw new IllegalArgumentException();
030                }
031                this.op = op;
032                this.l = l;
033                this.r = r;
034        }
035
036        public int evaluate() {
037                return op.eval(l.evaluate(), r.evaluate());
038        }
039        public void printPostorder() {
040                l.printPostorder();
041                r.printPostorder();
042                System.out.print(op + " ");
043        }
044}