How do I find the arity of an Expression? (dmd hacking)

Ellery Newcomer ellery-newcomer at utulsa.edu
Mon Nov 30 15:09:56 PST 2009


On 11/30/2009 12:32 PM, Chad J wrote:
> Ellery Newcomer wrote:
>> On 11/30/2009 03:53 AM, Ary Borenszweig wrote:
>>> Chad J wrote:
>>>> Given an Expression object in dmd, I'd like to know how many
>>>> subexpressions it contains and, even better, iterate over them. I'd
>>>> like to do this in a general way, without having to create cases for all
>>>> of the different kinds of Expressions. Is there some way to do this
>>>> that I've been missing?
>>>>
>>>> Thanks,
>>>> - Chad
>>>
>>> Where's the Expression object defined in D? I don't think there's such a
>>> thing (I mean, a reflection capability to get an expression out of
>>> something).
>>
>> DMD source, eg parse.c, expression.c, etc
>
> Right.  That.

Not that I know anything about DMD outside parse.c, but in expression.h, 
there be decls along the lines of

struct UnaExp{
   Expression* e1;
}
struct BinExp{
   Expression* e1;
   Expression* e2;
}

Iterating them would just be a tree walk. That takes care of 90% and ...

Oh. Yeah. There are a bunch of special cases.

But from what I've seen, iterating over an expression or any part of the 
AST involves implementing a function (like semantic) at each subclass. 
I'm betting there is no other way to do this.

I love ANTLR. It generates an arbitrary child/sibling AST, and walking 
it is a piece of cake.




More information about the Digitalmars-d-learn mailing list