User Tools

Site Tools


it_seems_like_many_implementations_are_using_abstract_syntax_tree_rewriting_to_enable_dci._how_does_it_work

This is an old revision of the document!


It seems like many implementations are using Abstract Syntax Tree rewriting to enable DCI. How does it work?

AST rewriting can emulate the 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() {
          }
     };
 
     void LightsCameraActionSystemOperation(Person x) {
	Cowboy <- x;	// inject the roles
	Shape <- x;
 
	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 LightsCameraActionSystemOperation(Person x) {
	Cowboy = x;
	Shape = x;
 
	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).

it_seems_like_many_implementations_are_using_abstract_syntax_tree_rewriting_to_enable_dci._how_does_it_work.1390389059.txt.gz · Last modified: 2014/01/22 11:10 by gazoot