[antlr-interest] Determine / check validity of type

Martijn Reuvers martijn.reuvers at gmail.com
Fri Jul 9 02:20:09 PDT 2010


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


More information about the antlr-interest mailing list