====== It seems like many implementations are using Abstract Syntax Tree rewriting to enable DCI. How does it work? ======
[[http://en.wikipedia.org/wiki/Abstract_syntax_tree|AST]] rewriting can emulate the [[http://folk.uio.no/trygver/2012/DCIExecutionModel-2.1.pdf|DCI execution model]] by rewriting the RoleMethods and the calls to them as private class methods in the Context class.
Some pseudocode:
Context OKCorrallShowdown {
role Shape {
void move(Point) {
}
void draw() {
}
};
role Cowboy {
void move(Point) {
}
void draw() {
}
void shoot() {
}
};
// Constructor
void new(Person x) {
Cowboy <- x; // inject the roles
Shape <- x;
}
void LightsCameraActionSystemOperation() {
Cowboy.move(Point(3,4));
Shape.draw(); // a frame of the animation
Cowboy.draw();
Cowboy.shoot();
Shape.draw(); // a frame of the animation
}
};
There is nothing ambiguous about the above code: there is enough information to be able to call the right methods, so it can be implemented (by the compiler) using static name mangling:
class OKCorrallShowdown {
private Person Shape;
private void Shape_move(Point) {
}
private void Shape_draw() {
}
private Person Cowboy;
private void Cowboy_move(Point) {
}
private void Cowboy_draw() {
}
private void Cowboy_shoot() {
}
public void new(Person x) {
Cowboy = x;
Shape = x;
}
public void LightsCameraActionSystemOperation() {
Cowboy_move(Point(3,4));
Shape_draw();
Cowboy_draw();
Cowboy_shoot();
Shape_draw();
}
};
This approach is completely unambiguous, even if an object has been injected with a role from a different Context (somewhere up the Context stack).