Last active
August 29, 2015 13:57
-
-
Save d11wtq/9575063 to your computer and use it in GitHub Desktop.
This file contains 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
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