Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!bony1!richieb From: richieb@bony1.bony.com (Richard Bielak) Newsgroups: comp.lang.eiffel Subject: Re: How can I do this in Eiffel? Message-ID: <1991Apr15.131833.230@bony1.bony.com> Date: 15 Apr 91 13:18:33 GMT References: Reply-To: richieb@bony1.UUCP (Richard Bielak) Distribution: comp Organization: Bank of New York Lines: 47 In article ajk@wren.cs.rmit.OZ.AU (Alan Kent) writes: >Can someone tell me if I am missing something obvious? I cannot see how to >do this nicely in Eiffel. > >I have a tree structure of nodes. These nodes are not all of exactly the >same class. To be precise, the tree is a parse tree for a language so you >have IF nodes, operator nodes, CASE nodes and so on. What I need to do >frequently is walk the tree and perform operations on nodes. For example, >count the number of variable declaration nodes, determine that maximum >stack depth needed by the function by examining expressions, determine >the number of nodes in the graph, determine the maximum depth of nesting >of loops and so on. I also wish to be able to add new nodes with different >structures at any time, so I cannot define a single superclass which can >walk the tree - each node type must have its own methods for walking down >into subnodes (eg: CASE has a list of alternatives, binary operators have >exactly two subnodes, who knows what I will want in the future). > [...] You can do exactly what you need by using the "reverse assigment" in Eiffel. Reverse assigment is one of few ways, in which you can check that two objects are type compatible. Here is how this might work: visit (n : NODE; t : SPECIFIC_TYPE_OF_NODE) is do t ?= n; -- reverse assigment if not t.Void then -- here t referes to the specific type -- so apply whatever things t.specific_routine end; end; -- visit This routine can then be called for every node in your tree. In the above example, I assume that NODE is a parent of SPECIFIC_NODE_TYPE. The "?=" will return a non-void value only when "n" actually references SPECIFIC_NODE_TYPE. Hope this helps. ...richie -- *-----------------------------------------------------------------------------* | Richie Bielak (212)-815-3072 | Programs are like baby squirrels. Once | | Internet: richieb@bony.com | you pick one up and handle it, you can't | | Bang: uunet!bony1!richieb | put it back. The mother won't feed it. |