Skip to content

Instantly share code, notes, and snippets.

@d11wtq d11wtq/generics.cpp
Last active Aug 29, 2015

Embed
What would you like to do?
class ASTNode {
public:
template <class T> virtual T accept(Visitor<T> *visitor);
};
template <class T> class Visitor {
public:
virtual T visit(CallNode *node) = 0;
virtual T visit(BinExprNode *node) = 0;
};
class Optimizer : public Visitor<ASTNode *> {
public:
ASTNode *visit(BinExprNode *node) {
ASTNode *optimizedL = <ASTNode *>node->left()->accept(this);
ASTNode *optimizedR = <ASTNode *>node->right()->accept(this);
return new BinExprNode(optimizedL, node->op(), optimizedR);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.