[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [DotGNU]Walking The ILNode_List

From: nymia
Subject: RE: [DotGNU]Walking The ILNode_List
Date: Sun, 24 Mar 2002 14:41:14 -0800

OK, that looks interesting. I'll try and see if I can code along that line.
Think I have enough understanding on how to implement the %operation.

BTW, I was out starting Friday, just got back today (Sunday).

-----Original Message-----
From: address@hidden [mailto:address@hidden
Behalf Of Rhys Weatherley
Sent: Friday, March 22, 2002 4:13 PM
To: nymia
Cc: DotGNU Developers
Subject: Re: [DotGNU]Walking The ILNode_List

nymia wrote:

> Is CSTypeGather() in cs_gather.c a good base code for walking the
> ILNode_List? Or, in other words, what is the best approach in walking the
> parse tree?

No, it isn't a very good model.  CSTypeGather only
handles a small subset of the nodes, and it has tons
of special cases.  Walking the tree in this way should
only be done if there are no alternatives.  It's pretty
much unmaintainable except by gurus.

> I'm currently working on building a tree view of the parse tree. Any ideas
> are welcome.

A better way is to define a virtual operation called
"ILNode_Dump" or somesuch.  Then add cases for
each major node category.  e.g.

%operation %virtual void ILNode_Dump
     (ILNode *node, FILE *stream, int indent)

    PrintIndent(stream, indent);
    fprintf(stream, "%s\n", yykindname(node));
    ILNode_Dump(node->expr1, stream, indent + 1);
    ILNode_Dump(node->expr2, stream, indent + 1);

where "PrintIndent" is a helper function to indent
the output by a particular amount.

Here I have overridden ILNode_BinaryExpression,
because that will automatically handle every binary
expression in the tree, avoiding the need to separately
handle add, subtract, multiply, divide, etc, etc, etc.

You could of course add extra parameters to specify
"dump as text", "dump as HTML", "dump as XML", etc.
Or even have separate operations: ILNode_DumpText,
ILNode_DumpHTML, ILNode_DumpXML, etc.

However, a better approach may be to always dump
as XML, and then write a separate tool to post-process
the XML to turn it into text or HTML.  I would strongly
suggest doing that.

Create a "" file in "codegen" and put
all of the dump cases there.  Edit "" to
include this file.

The benefit of this approach is that treecc does most
of the hard work for you: it will check that you have
a case for every single node type, and give an error
if you forget something.  If you were to use the manual
walking procedure in CSTypeGather, you wouldn't
get the checks.



Developers mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]