Re: Calculating expressions with several types

"Fairbairn Family" <>
13 Dec 2000 15:01:04 -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: "Fairbairn Family" <>
Newsgroups: comp.compilers
Date: 13 Dec 2000 15:01:04 -0500
Organization: ihug ( New Zealand )
References: 00-12-044
Keywords: types, interpreter
Posted-Date: 13 Dec 2000 15:01:04 EST


"" <> wrote in message
> Some weeks ago I began writing my own interpreter.
> Everything was good until I wanted to use different types (int,str,real).
> But I've no idea how to calculate an expression with several types!
> The algorithm for calculating integer expressions is ready.
> By the way, I use recursive-descent algorithm.
> Do you know how to extend it to support these types?
> I need your help VERY-VERY much. I'll be very grateful for your help. :-)

You have most of what you need already. What you need to do is rewrite
all your functions such as "expr" to return a value which has some
type information attached to it.

This is what John said - I'll just expand on it to help guide you along a
bit further.

To keep track of the type information you could use a C struct which
contains an int value which represents what type the value is (str, int,
real etc) and fields which store values of all the posible types.

The defintion of such a structure could be:

struct {
          int type; // a number used to select a member below

          double real_val;
          int int_val;

No doubt someone will have a better suggestion - but this should be enough
to get the ball rolling for you. For example all the members such as
real_val and int_val should be placed in a union to save some memory. Also
the type field could/should be replaced with an enumerated type.

Having the functions all return these structures is the easy part. The more
difficult part is rewriting the functions to make use of these structures.
For example you can't simply expect code such as "r += term()" to work any
more, as you previously used.

Instead you need to do something like:

temp = term();

if (r.type == temp.type) {
          switch (r.type) {
                    case intTYPE:
                              r.int_val = r.int_val + temp.int_val;
                  case realTYPE:
                              r.real_val = r.real_val + temp.real_val;
} else {
          printf("Types of factors used in expression are not compatable\n");

As you see if is all basically the same except that you need a specific case
of your code for each specific type of value which could be returned via
such functions as term(). The interesting part is when you start adding
support for using numerious types in one expression - for example in Pascal
you can add bytes to words - the above code won't allow this sort of thing,
at least without modification.

That should be enough to help you get started. Email me if you need any more
help. This sort of thing can start getting really complicated and diverse
when you start allowing the user to define their own types and add things as
Pascal's records and C's struct types.

> I'm a student...
Same here...

Hope this helps,
Christopher Fairbairn.

Post a followup to this message

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