3 ms·
Does it mean we could translate languages like Ruby and Python to Rust?
by faebi 7y ago
Does it mean we could translate languages like Ruby and Python to Rust?
- johannes1234321 7y agoTechnically it is possible. The gain won't be there for most cases, though as the generated code would be complex and bad as variables often have to be carried as "variant" types and you have to plug in to the library (which has a specific C API) and so on. Facebook for some time did this with their "HipHop for PHP" transpiler which translates PHP into C++ and then fed it to gcc. That worked, but due to all the type conversion and similar magic required on all calls this lead them to compilation times of 24 hours and interfaces which were hard to interact with for language bindings, thus they went back to write a scripting engine (HHVM) which now runs their forked PHP.
- K0nserv 7y agoWhile it might be possible my guess is it would be much more difficult than C. AFAIK Rust is more or less a superset of C whereas Python and Ruby have numerous advanced features that aren't present in Rust and would likely require a bespoke Rust runtime to emulate the behaviour. Types is also a whole can of worms here. I think in relation to Ruby, Python and other languages like them Rust's best application is leveraging the support for C FFIs to replace hot paths. While Rust has a significant learning curve it's probably much simpler, for say a ruby programmer, to pick it up and write safe performant code than C.
- jashmatthews 7y agoThere's an effort to write a Ruby implementation in Rust: https://github.com/artichoke/artichoke https://github.com/artichoke/artichoke At the moment it wraps the mruby VM, but I think Ryan's plan is to switch to the CRuby YARV VM and then replace it with a VM written in Rust. I'm also working on a CRuby JIT compiler written in Rust using CraneLift (a new compiler written in Rust).
- jashmatthews 7y agoCRuby's JIT basically works by translating Ruby to C.
- lmm 7y agoNot usefully, since those languages require an extreme level of dynamism (even if rarely used by practical programs). E.g. Python requires that an object's methods are a dictionary that can be accessed and manipulated as one, so you'll never be able to translate Python objects into Rust traits. You could of course write a Python interpreter in Rust.