dibyendumajumdar / ravi

Ravi is a dialect of Lua, featuring limited optional static typing, JIT and AOT compilers
http://ravilang.github.io/
Other
1.16k stars 60 forks source link

Improve type deduction #212

Closed XmiliaH closed 3 years ago

XmiliaH commented 3 years ago

Some improvements with type deduction.

Some examples

Expression Old Now
-{} RAVI_TTABLE RAVI_TANY
#("") RAVI_TANY RAVI_TNUMINT
~1.0 RAVI_TANY RAVI_TNUMINT
""..1 RAVI_TANY RAVI_TSTRING
"" and 1 RAVI_TANY RAVI_TNUMINT
dibyendumajumdar commented 3 years ago

Thank you - I will check it out.

XmiliaH commented 3 years ago

As can be seen in the failed test I removed that #{} is a RAVI_TNUMINT and instead a RAVI_TANY since the metamethod could return anything. The inserted cast to integer didn't show the same behavior as puc lua.

dibyendumajumdar commented 3 years ago

Yes meta methods make type inference tricky. I forced #{} to int by adding a type assertion - mainly because it is useful for optimization in loops. I accepted the trade off that if user made # return non integer then it would be an error.

XmiliaH commented 3 years ago

And I noticed that it is still compatible to lua, since lua code will not have the table annotated as table and the any case will return any.

dibyendumajumdar commented 3 years ago

Also note that meta methods are ignored by Ravi for integers / numbers when these are specialized. I should add this is for arithmetic/bit operations only.

dibyendumajumdar commented 3 years ago

The 'and' and '..' operators were left out - I guess inferring "" and 1 as 1 means we ignore meta-methods right?

XmiliaH commented 3 years ago

There is no metamethod for 'and', 'or', 'not'. Also '..' will not call metamethods if both sides are numbers or strings as far as I know.

dibyendumajumdar commented 3 years ago

And I noticed that it is still compatible to lua, since lua code will not have the table annotated as table and the any case will return any.

Indeed the type assertion is only applied when we have explicit annotation that it is a table

dibyendumajumdar commented 3 years ago

Thank you for the contribution - much appreciated. If all tests pass - and I can also run Suravi tests then I will merge it. If I see issues I will get back. But I can only look properly over the weekend.

dibyendumajumdar commented 3 years ago

Hey, I'd like to redirect this PR to a branch so that I can test it before I merge to master

XmiliaH commented 3 years ago

You can just check out the pull request locally as described in https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally.

XmiliaH commented 3 years ago

Note that I did need to revert #("") and "" .. 1 to RAVI_TANY since RAVI_TSTRING can be nil and nil might call metamethods.