Half a Visitor, but Full Object-Oriented Semantic Check
In implementing semantic checks my original software design was based on the material in many object-oriented compiler texts - the visitor design pattern. I won’t restate theory or design of the visitor design pattern, it is a favorite pattern of mine (next to the singleton), but in my implementation of the Mynx compiler phase of sentence and statement semantic checks, it created more overhead than it helped to resolve.
Sentences, once created after a parse then have a Context singleton object passed. The Context object stores information among the sentences, including error reports.
In the original, pure visitor design pattern, the Context object then had a method called and the sentence passed for the semantic check, a`la:
The visitor pattern, but in the method “doSemanticCheckSentence”, the sentence passed then needs to have information moved by accessor methods...the infamous getXXX and setXXX.
All the getters and setters are moving information back and forth from within the sentence -- I was writing more code to move data than to do the semantic checks. Encapsulation is great, but when information is within the object and you want to do checks to verify semantics, it is a pain because you have to do much more data movement then semantic check.
Then I realized passing a Context object is useful to centralize a focus for semantic information, but the actual sentence should do its own semantic check. The distinction is using the sentence as data to be processed -- the full visitor design pattern, versus letting the sentence check itself -- but passing semantic information to a central semantic nexus.
In the bombastic hyperbole of object-oriented, a message is sent to the sentence stating “sentence, do a semantic check”. The strange twist is a design pattern, a holy grail of object-oriented development, turns an object that can do its own thing into a data element - back to procedural passing data among different methods.
The Context object is a nexus to store contextual semantic information, such as alerts and error reports. Thus far, there are a total of 109-discrete semantic test cases, one for success and one for failure of a single semantic check. So thus far, a total of 218-semantic checks as discrete Mynx classes and programs.
Moving from full visitor design pattern but instance as data to half-visitor instance as an object has made the semantic check implementation go faster. Sticking with the full visitor design pattern and I’d be writing data movement accessor methods -- and still implementing the semantic checks. A cautionary tale of blindly using a design pattern can make a design more rigid, and require over-decoration of a class with accessor methods to get the data.
Sentences, once created after a parse then have a Context singleton object passed. The Context object stores information among the sentences, including error reports.
theSentence.check(Context.getContext());
In the original, pure visitor design pattern, the Context object then had a method called and the sentence passed for the semantic check, a`la:
public void check(Context ctx)
{
ctx.doSemanticCheckSentence(this);
}
The visitor pattern, but in the method “doSemanticCheckSentence”, the sentence passed then needs to have information moved by accessor methods...the infamous getXXX and setXXX.
All the getters and setters are moving information back and forth from within the sentence -- I was writing more code to move data than to do the semantic checks. Encapsulation is great, but when information is within the object and you want to do checks to verify semantics, it is a pain because you have to do much more data movement then semantic check.
Then I realized passing a Context object is useful to centralize a focus for semantic information, but the actual sentence should do its own semantic check. The distinction is using the sentence as data to be processed -- the full visitor design pattern, versus letting the sentence check itself -- but passing semantic information to a central semantic nexus.
In the bombastic hyperbole of object-oriented, a message is sent to the sentence stating “sentence, do a semantic check”. The strange twist is a design pattern, a holy grail of object-oriented development, turns an object that can do its own thing into a data element - back to procedural passing data among different methods.
The Context object is a nexus to store contextual semantic information, such as alerts and error reports. Thus far, there are a total of 109-discrete semantic test cases, one for success and one for failure of a single semantic check. So thus far, a total of 218-semantic checks as discrete Mynx classes and programs.
Moving from full visitor design pattern but instance as data to half-visitor instance as an object has made the semantic check implementation go faster. Sticking with the full visitor design pattern and I’d be writing data movement accessor methods -- and still implementing the semantic checks. A cautionary tale of blindly using a design pattern can make a design more rigid, and require over-decoration of a class with accessor methods to get the data.
Labels: mynx, sentence semantics, visitor design pattern

