I'm currently trying to write a simple compiler for even simpler language, but I have problem with adding the Visitor
pattern.
I have a ILanguageVisitor
interface that looks like this:
interface ILanguageVisitor {
void Visit(GlobalStructure cs);
void Visit(GotoStructure cs);
void Visit(IfStructure cs);
void Visit(ElseIfStructure cs);
void Visit(ElseStructure cs);
...
}
All of these methods must be implement to create concrete visitor for specific architecture. But above this, there are some methods and fields that should be common for all possible visitors.
That is for example functionality of blocks, which consist of two or more Visit
calls, like:
Visit(If)
...
Visit(Else)
...
Visit(EndIf)
Visit(For)
...
Visit(EndFor)
This is because of the rules of block start and termination (like there can't be two else
s in one block, or that child block can't be terminated by parent as in case For ... If ... EndFor
).
My question is: if I have a behavior that should be common for all Visitor
s, should I create an abstract Visitor class, which would make these specific methods virtual
and other abstract
?
Will be there any loss of Visitor
´s point if I add a default behavior to its base?
In these kind of cases, I believe the best solution is to have both an interface and a base class which implements this interface and allows users to override certain methods. This way, the user can decide whether he'd like to: