Created
September 30, 2013 13:02
-
-
Save ds300/6763496 to your computer and use it in GitHub Desktop.
Illustration of AST Visitor pattern. (This is bad java, just to give you an idea of how things fit together)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseAST { | |
public abstract void accept(Visitor v); | |
} | |
public abstract class Visitor { | |
public abstract void visitBinOpNode(Expression lhs, | |
Op op, | |
Expression rhs); | |
public abstract void visitIdentNode(String ident); | |
} | |
public abstract class Expression extends BaseAST {} | |
public class BinaryOperator extends Expression { | |
public BinaryOperator (Expression lhs, | |
Op op, | |
Expression rhs) { | |
this.lhs = lhs. | |
... | |
... | |
} | |
@Override | |
public void accept(Visitor v) { | |
v.visitBinOpNode(this.lhs, this.op, this.rhs) | |
} | |
} | |
public class Identity extends Expression { | |
final String ident; | |
public Identity(String ident) { | |
this.ident = ident; | |
} | |
@Override | |
public void accept(Visitor v) { | |
v.visitIdentNode(this.ident); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment