Re: Calculating expressions with several types

"Jean Pariseau" <>
18 Dec 2000 00:44:11 -0500

          From comp.compilers

Related articles
Calculating expressions with several types (=?koi8-r?B?4c7Uz84=?=) (2000-12-11)
Re: Calculating expressions with several types (Fairbairn Family) (2000-12-13)
Re: Calculating expressions with several types (Chris Locke) (2000-12-13)
Re: Calculating expressions with several types (Jean Pariseau) (2000-12-18)
| List of all articles for this month |

From: "Jean Pariseau" <>
Newsgroups: comp.compilers
Date: 18 Dec 2000 00:44:11 -0500
Organization: Excite@Home - The Leader in Broadband
References: 00-12-044
Keywords: types
Posted-Date: 18 Dec 2000 00:44:11 EST

        Depending on how you have your interpreter set up, how much effort
you want to put into it, and using a language like C++ or Object
pascal, another option to the struct or record solution is to use
classes and polymorphism. For example you can have a base node that
would encapsulate the basic functionality to be used by every type of
node in the tree.

class BaseNode
                virtual Node* Interpret();
                virtual Node* Compile();
                virtual int NodeType;
        // and your ctor's and dtor's

class IntegerNode : public BaseNode
                int Value;

        virtual Node* Interpret()
                // don't need to do anything here for interpreter except maybe to
return this node

        virtual Node* Compile()
                // if we were to compile the tree, we could have it emit a line of
assembler or object code



class DoubleNode : public BaseNode

        ... similar to above except that this would be for a double instead of
an int
        double Value;

class AdditionNode : public BaseNode

        Node* ReturnNode;
        Node* RightChild;
        Node* LeftChild;
        virtual Node* Interpret()

                        switch (LeftChild->NodeType)
                                        case (type_Integer) :
                                                                // int = int + int
                                                                ReturnNode = new IntegerNode(LeftNode->Value
+ RightNode->Value);


                                                                        double = int + double
                                                                        ReturnNode = new
DoubleNode((double)LeftNode->Value, RightNode->Value);

                                                                        ReturnNode = NULL;
                                // we are done with the comparison stuff so we return a
pointer of the base node
                                return ReturnNode;


I'm sure you get the picture. To make life easier you could use
templates. check out for a great tutorial on an
object-oriiented parse tree.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.