[antlr-interest] Determine / check validity of type

Jim Idle jimi at temporal-wave.com
Fri Jul 9 09:52:32 PDT 2010


First of all you need to produce an AST, then a tree walking grammar. In each rule of an expression you return a type such that the expression itself returns a type. An operator then invokes a function with the operation and the types of the arguments, which then issues an error message and returns a type to propagate (which is the result of the operation or an error). Future checks do not issue error messages if any of the operands are the error type (so you do not get myriad errors cascading), but return the error type.

When a function is declared (or built-in declared) the arguments are typed, you make the same check as for operators and the return type of the expression is the return type of the function as declared. 

There is quite a bit of work involved but it is very orthogonal and so not difficult when you see the pattern.

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Martijn Reuvers
> Sent: Friday, July 09, 2010 2:20 AM
> To: antlr ANTLR
> Subject: [antlr-interest] Determine / check validity of type
> 
> Hello everyone,
> 
> I have a question related to determining the type returned from an
> expression on 'compile' time (not runtime!).
> 
> For example take the following code:
> 
> ***************************
> 
> function someFunction(int a) return string {
>   return "a string!"
> }
> 
> int finalValue = (100+5)*2 + someFunction("a"+someFunction(1))
> 
> // Note the errors, we do not allow adding a text to an int, and the
> function invoke contains an expression returning a string (should be
> an int), but expression itself is ok.
> 
> ***************************
> 
> Assume the code is parsed and transformed to an AST without error. We
> are walking the tree and walk the variable 'finalValue', and see the
> value must be of type 'int'. Tree would look something like:
> 
>          var_assignment
>    /                            \
> finalValue(type=int)     expression
> 
> 
> Where would you determine the first error? While evaluating the +
> (between left-side exp and someFunction()) and log an error there?
> 
> The second error I find harder, the invoke of: someFunction(..) .
> 
> Looking up the MethodSymbol for it will tell the formal parameter
> exptected is an int, but how now to determine that the expression
> given is actually returning an int? Would you walk the entire
> parameter: expression (in this case) "a"+someFunction(1) ? But as said
> before we cannot execute the code.
> 
> Anyone having an idea on how to do this in a flexible (and hopefully
> not too difficult) way?
> 
> Thanks a lot for thinking along!
> 
> Martijn
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list