Integrating It All
[ 330 ]
We start off the preceding code by creating a new AST node for the session class. We
build the constructor code block and create a ConstructorNode for the class. Finally,
we use the state variable declarations we found while parsing the state: block to
add properties to the class to represent each state variable. This newly created class is
added to the original ModuleNode object for the DSL script.
Next we work on the script class itself. We add a new constructor to it, change it so it
implements the trait, GameEngineTraits and adds the event handling methods to it:
void updateClientClass() {
addGameEngineTraits()
buildGameEngineConstructor()
buildClientEventMethods()
}
def addGameEngineTraits() {
def traitNode = ClassHelper.make(GameEngineTraits)
if (!classNode.implementsInterface(traitNode)) {
classNode.addInterface(traitNode)
}
}
def buildGameEngineConstructor() {
def constructorStatement = new ExpressionStatement(
new BinaryExpression(
new PropertyExpression(
new VariableExpression("this"),
new ConstantExpression("session")
),
new Token(Types.EQUALS, "=", -1, -1),
new VariableExpression("session")
)
)
def constructorNode = new ConstructorNode(
Modifier.PUBLIC,
[
new Parameter(
ClassHelper.make(Object, false), "session")
] as Parameter [],
[
ClassHelper.make(Exception, false),
ClassHelper.make(IOException, false)
] as ClassNode [],
http://www.ebook3000.com