[IronPython] Type analysis of expression
Jeff Hardy
jdhardy at gmail.com
Sat Oct 24 23:57:35 CEST 2009
On Sat, Oct 24, 2009 at 3:22 PM, Christian Schmidt
<christian2.schmidt at gmx.de> wrote:
> Hi Jeff,
>
I'm going to assume that you have a way of determing the types of the
input variables a, b, and c. If not, well, I'm not sure how much help
this will be. You can use the IronPython parser classes to convert the
expressions into an AST; the tricky part is doing the actual
inference.
>> Can you give some examples of expressions you need to infer?
>
> arithmetic operations: a + b / c
> functions: sqrt(a)
These are mostly straightforward - Python's type conversion rules for
arithmetic operators are well-defined. Presumably, sqrt is defined in
your code (or is just Math.sqrt) - thus the return type is the same as
the input type.
> logical expressions: a if (b or c) else d+e
These are more difficult because of the conditional, if 'a' and 'd+e'
are different types. You could just give an error if that's the case,
or see if they have a common 'supertype' (i.e. if 'a' is int and 'd+e'
is float, use float).
>
> I would also like to allow for enumerables in expressions:
> sum(a)
> sum([sqrt(x) for x in a if x>0])**2
I would just make some assumptions - perhaps sum will always be either
int or float?
A full, accurate type inference engine for Python would be quite a
challenge, but you only need part of one. I don't think IPy has
anything builtin to help you, though.
- Jeff
More information about the Ironpython-users
mailing list