| |
![Chad J's Gravatar profile Gravatar of Chad J](//www.gravatar.com/avatar/87fb15ffd0cbfe821372340d7940b142?d=identicon&s=32) | Posted by Chad J | Permalink Reply |
|
Chad J ![Chad J's Gravatar profile Gravatar of Chad J](//www.gravatar.com/avatar/87fb15ffd0cbfe821372340d7940b142?d=identicon&s=80)
| My objective is to make a parsetree framework that lets me dump the parsetree into a visual graph for debugging and whatnot. Here is my current setup:
// Class that all elements in the tree inherit from.
class Node
{
void printTree() {...} //right now it just prints to console,
//graphics/gui come later
template registerNode()
{
Node[] _getChildren()
{
OffsetTypeInfo[] otis = typeid(typeof(this)).offTi;
writefln( otis.length ); // prints 0 always, crap! bug 1348.
... // normally would do stuff with otis
}
}
}
// Generic element of the parsetree
class A
{
// Boilerplate defined in "Node"
mixin registerNode!();
// Children in various incantations.
Node lhs;
Node rhs;
Node[] otherNodes;
// Just a variable that shouldn't end
// up being a vertex in the resulting graph.
int someStateValue = 0;
}
Inside of the _getChildren function I am trying to find some way to iterate through ALL members of each Node. By ALL members, I mean the members of the most descended class, and its base class, and it base class's base class, and so on up until, but not including, Node's members.
Anyhow, now the problem part. I can't seem to iterate through the members of a class by using OffsetTypeInfo, since any class's corresponding offTi always has a length of zero. I have also tried tuples by using the this.tupleof property. Then I set up a loop like this inside of _getChildren:
Node[] children = new Node[0];
foreach( member; this.tupleof )
{
static if ( is( Node : typeof(member) )
{
children ~= member;
}
}
Originally I expected 'member' to just be some kind of alias for its corresponding identifier, such that member might be one of 'lhs', 'rhs', or 'otherNodes' in this example. Unfortunately, member is always null. I suspect that's because 'member' is a part of a compile-time expression tuple, and is itself a compile-time constant, thus at compile time it is always null. Well if I can somehow coax a run-time value out of member, I'd be in business, but I don't know how to do that.
So, does anyone have a way of doing this type of thing?
TIA,
Chad
|