SE450: A Tree: Visitor [9/25] Previous pageContentsNext page

Visitor is a "cleaned up" version of typecase.

file:visitor/expr/ExprVisitor.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
package visitor.expr;
import enumeration.Op;

public interface ExprVisitor<T> {
  T visitConst(int c);
  T visitBinOp(Expr l, Op op, Expr r);
}

file:visitor/expr/Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package visitor.expr;
import enumeration.Op;

public class Main {
  public static void main (String[] args) {
    Expr one = new Const(1);
    Expr onePtwo = new BinOp (new Const(1), Op.ADD, new Const(2));
    Expr threeMfour = new BinOp (new Const(3), Op.MUL, new Const(4));
    Expr m = new BinOp (onePtwo, Op.SUB, threeMfour);
    Expr n = new BinOp (m, Op.DIV, new Const(5));

    System.out.println (n.accept(new PostorderToString()));
    System.out.println ("Value: " + n.accept(new Eval()));
  }
}

class Eval implements ExprVisitor<Integer> {
  public Integer visitConst(int c) {
    return c;
  }
  public Integer visitBinOp(Expr l, Op op, Expr r) {
    return op.eval(l.accept(this), r.accept(this));
  }
}

class PostorderToString implements ExprVisitor<StringBuilder> {
  StringBuilder b = new StringBuilder();
  public StringBuilder visitConst(int c) {
    b.append (c + " ");
    return b;
  }
  public StringBuilder visitBinOp(Expr l, Op op, Expr r) {
    l.accept(this); r.accept(this); b.append (op + " ");
    return b;
  }
}

class PreOrderToString implements ExprVisitor<StringBuilder> {
  StringBuilder b = new StringBuilder();
  public StringBuilder visitConst(int c) {
    b.append (c + " ");
    return b;
  }
  public StringBuilder visitBinOp(Expr l, Op op, Expr r) {
    b.append (op + " "); l.accept(this); r.accept(this);
    return b;
  }
}

class InOrderToString implements ExprVisitor<StringBuilder> {
  StringBuilder b = new StringBuilder();
  public StringBuilder visitConst(int c) {
    b.append (c + " ");
    return b;
  }
  public StringBuilder visitBinOp(Expr l, Op op, Expr r) {
    l.accept(this); b.append (op + " "); r.accept(this);
    return b;
  }
}

file:visitor/expr/Expr.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package visitor.expr;
import enumeration.Op;

public interface Expr {
  <T> T accept(ExprVisitor<T> v);
}

class Const implements Expr {
  private final int c;
  public Const(int c) {
    this.c = c;
  }
  public <T> T accept(ExprVisitor<T> v) {
    return v.visitConst(c);
  }
}

class BinOp implements Expr {
  private final Expr l;
  private final Expr r;
  private final Op op;

  public BinOp(Expr l, Op op, Expr r) {
    if ((l == null) || (op == null) || (r == null)) {
      throw new IllegalArgumentException();
    }
    this.op = op;
    this.l = l;
    this.r = r;
  }
  public <T> T accept(ExprVisitor<T> v) {
    return v.visitBinOp(l, op, r);
  }
}

Previous pageContentsNext page