SciRuby / rubex

rubex - A Ruby-like language for writing Ruby C extensions.
BSD 2-Clause "Simplified" License
451 stars 21 forks source link

FR - Infer type without declaration #36

Open arjunmenon opened 6 years ago

arjunmenon commented 6 years ago

Hey Just wanted to mention if it is feasible to modify the AST to infer variable type from the value it is set?

Currently, in C specific examples I was exploring, variables are declared. Recently was looking into Mirah and their that is not required even if calling Java methods. It is more ruby like. It properly compiles to classes, without any penalty.

v0dro commented 6 years ago

So the reason for not having type inference is that variables without a type specified as inferred as ruby objects and all operations performed on them delegate to the Ruby interpreter. For example:

i = 1
i = i + 1

will translate to:

VALUE i = INT2FIX(1);
ruby_interpreter_add_method(i, 1);

If we infer the type as a C type there will be no scope for assigning different objects to the same variable, which is possible in Ruby.

arjunmenon commented 6 years ago

Hey

If we infer the type as a C type there will be no scope for assigning different objects to the same variable, which is possible in Ruby.

I was not referring to that. If you loook at this example C function file, the variable a is specifically declared as int. For syntactic sugar, may be it is possible to infer a as int from the value itself. Since, we have already declared first_c_function takes two int, any variable using its params can be assumed to be int as well, unless explicitly type casted.

I am no expert on AST or how you have actually implemented the Type System. Just a thought. This old project does the same, but in a different manner.

v0dro commented 6 years ago

You have a point. But the system you are proposing is quite advanced given the stage of development of Rubex. In order to implement what you suggest it will be necessary to perform two passes of the code for type inference: one pass that understands how every variable is used and the second pass that will infer the type based on the usage of variables whose types are unspecified.

As of now I want to focus on building basic features that will make Rubex a go-to tool for writing Ruby C extensions and CUDA programming in Ruby.