Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!mips!spool.mu.edu!munnari.oz.au!goanna!wren!ajk From: ajk@wren.cs.rmit.OZ.AU (Alan Kent) Newsgroups: comp.lang.eiffel Subject: How can I do this in Eiffel? Message-ID: Date: 12 Apr 91 01:32:12 GMT Sender: news@goanna.cs.rmit.oz.au Distribution: comp Lines: 46 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). Frankly, I dont not know how to do this nicely. I only want to write the tree walking code once and I really want to be able to pass as a parameter somehow the operation to be done. I can do this in C by passing a pointer to a function. I could not see anything similar in Eiffel. I also got wondering how you would do it in general in an OO language. I guess you could (1) Pass a method name which is to be sent to each node in the tree (2) Same as (1), but pass arguments to the method. but this means that the node definition must contain the code I wish to do on the tree nodes. For example, if I want to count the number of IF nodes by using a "count_if_nodes" method, then the node superclass must contain such a method. This has the disadvantage if I want to write some new little function, I have to change my central class definition. Yuck. The alternative I guess would be (3) Pass a method name and an object to send the method to with the current node as a parameter. This seems similar to the hetrogeneneous discussion going on. If I understand what has been said, this sort of thing would be easy in a dynamically typed language. For a statically typed language for this to be completely safe, you need to be able to specify a function or method with types for parameters. I guess this is possible in ANSI-C. Anyway, any suggestions appreciated. Comments about nice ways of doing this if you could design your own language features would also be of interest to me. Dr Alan Kent ajk@goanna.cs.rmit.OZ.AU