Re: ASTs and Type Check Analysis

CranCran77 <>
Mon, 25 Aug 2008 08:04:44 -0700 (PDT)

          From comp.compilers

Related articles
ASTs and Type Check Analysis (CranCran77) (2008-08-20)
Re: ASTs and Type Check Analysis (Hans-Peter Diettrich) (2008-08-24)
Re: ASTs and Type Check Analysis (2008-08-24)
Re: ASTs and Type Check Analysis (CranCran77) (2008-08-25)
Re: ASTs and Type Check Analysis (Bartc) (2008-08-25)
Re: ASTs and Type Check Analysis (Hans-Peter Diettrich) (2008-08-26)
| List of all articles for this month |

From: CranCran77 <>
Newsgroups: comp.compilers
Date: Mon, 25 Aug 2008 08:04:44 -0700 (PDT)
Organization: Compilers Central
References: 08-08-044 08-08-054
Keywords: code, types, optimize
Posted-Date: 25 Aug 2008 13:04:41 EDT

On Aug 23, 6:11 pm, Hans-Peter Diettrich <> wrote:

In the simple example I provided, I know my end result (without
optimizations) would result in the following:


Had the code been optimized by the compiler at compile time, the
resulting code would have been:


But I'm not at the "optimization" stage just yet; that is still to

The goal presently is to understand how I should handle type
assignments to nodes within the AST tree.

As you see in my example, I had several nodes that were
CASTIntegerLiteral, but I question if that is the right approach or
not. In my opinion, the parser which is creating the AST tree should
not be responsible for determining the number type of Integer, Real,
or Long. A simple CASTNumberLiteral node seems more appropriate to me
and allow the type analysis check phase when traversing the tree
determine whether its Integer, Real, or Long right?

If my thought process is correct, then when my type check visitor
traverses the nodes of the tree, certain AST nodes need to be
decorated with type information, such as Integer, Real, or Long. When
examining expressions such as binary ones, both sides to the operation
need to be examined to make sure they both conform to the same type,
and if not handle that appropriately.

For example, "PRINT 2.5*3" would throw an exception with type mismatch
because 3 would be evaluated to integer where-as 2.5 would be viewed
as a real. A different method could be that it would turn the
statement into "PRINT 2.5*3.0" so that both sides are viewed as real
values for the computation to happen correctly.

In this case during the binary expression type check, the right side
of the operation would become the child of another another AST node
that handles converting the result into a given type by an internal
function call. So the tree would resemble the following:

    +- CASTSimpleName
            +- Name: PRINT
    +- CASTExprList
            +- List Size: 1
            +- CASTBinaryExpr
                    +- Operation: '*'
                    +- CASTNumberInteger
                            +- Number: 2.5
                    +- CASTFunctionCall
                            +- CASTSimpleName
                                    +- Name: INT2REAL
                            +- CASTExprList
                                    +- List Size: 1
                                    +- CASTNumberLiteral
                                            +- Number: 3

I suppose what I see is that I could use a class object called
CASTType with derived classes for Integer, Long, Real, and String.
Each derived class would contain information about the size of the
type for storage purposes and/or reference pointer for strings in the
constant pool.

So each CASTNumberLiteral and CASTBinaryExpr objects would be assigned
a CASTType object during the type check phase. Each CASTFunctionCall
I also believe would get assigned a type as well in keeping with the
assembly example above.

Am I on the right track?

> The efforts may depend on the actual brand of BASIC you're targeting.
> When a variable can hold any type at any time, depending on the last
> assignment, or depending on the last declaration passed in control
> flow, a type analysis IMO will be quite fruitless.
> Otherwise, when the type of a variable is known at compile time, you
> have the usual choice of up-/downcasting to the next applicable type in
> expression evaluation. That's easy when numerical and textual (string)
> data cannot be mixed. Otherwise you'll have to specify what the result
> of e.g. 1+"1" shall be - it might be "11", 2, or some even more weird
> result. That's one of the common pitfalls also in Java :-(

Post a followup to this message

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