# Re: Calculating expressions with several types

## "Jean Pariseau" <jparis11@home.com>18 Dec 2000 00:44:11 -0500

From comp.compilers

Related articles
Calculating expressions with several types div@infonet.nnov.ru (=?koi8-r?B?4c7Uz84=?=) (2000-12-11)
Re: Calculating expressions with several types lgcl01@es.co.nz (Fairbairn Family) (2000-12-13)
Re: Calculating expressions with several types chris@cjl1.demon.co.uk (Chris Locke) (2000-12-13)
Re: Calculating expressions with several types jparis11@home.com (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 http://home.com/faster References: 00-12-044 Keywords: types Posted-Date: 18 Dec 2000 00:44:11 EST

Don,
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
{
public:
virtual Node* Interpret();
virtual Node* Compile();
virtual int NodeType;
// and your ctor's and dtor's
}

class IntegerNode : public BaseNode
{
public:
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;
};

{

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

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

}

case(type_Double):
{
double = int + double
ReturnNode = new
DoubleNode((double)LeftNode->Value, RightNode->Value);
}

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

//end

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

Jean

Post a followup to this message