I’ve been asked recently, why writing a Ruby compiler in Ruby would make any sense. The argument was, that by compiling Ruby code to native machine code, you’d lose all the benefits of a ’scripting language’.
Now, you could argue, if you’d want to degrade Ruby simply to a scripting language – and I don’t think that Ruby is only that (whatever a ’scripting language’ may be…) – then you’d lose quite some flexibility (e.g. simply changing a file and run it again etc.).
But there’s a more important thing to consider here:
All the implementations out there currently are (mostly) written in another language. JRuby in Java, IronRuby in C#, MRI (default Ruby intepreter) in C and even Rubinius is written to great part in C++.
The reason is simple: Since you need some way to actually get your code running, you’ll need a host language to get the Ruby VM running on.
Now consider writing a native Ruby compiler in Ruby itself:
Once you’re able to bootstrap the compiler (meaning, to be able to compile it with itself), you’ve got a natively compiled executable, written in Ruby, where you don’t need any runtime/VM for. You could actually write a Ruby Interpreter / VM completely in Ruby itself, since you could also compile it with the native compiler. Additionally, making changes and understanding the source code of all these parts would be much easier for a normal Ruby programmer than any other system, since it’s all written in the language it’s targeting.
Now this would be awesome, wouldn’t it?
Any Ruby programmer could actually work on the language platform with the language itself. I think this would just be really cool.
So once the compiler would work well enough to actually compile quite large programms, it shouldn’t be too hard to get a Ruby Interpreter written in Ruby.
Of course, writing a language implementation in the language it’s implementing isn’t something new. Most Lisp implementations have been written in Lisp, the same goes for C & C++ compilers. In the case of Lisp, we have a dynamically typed language which gets compiled. And if you take a look at some performance benchmarks, you’ll see that there are Common Lisp implementations out there, that are really fast for a dynamic language (very well comparable to the speed of C!). This obviously takes some time, but it shows what can be possible.


One Response
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
I don´t know if it is possible to come near to C/C++-Speed but it´s of course a big advantage for every programmer who uses the compiler written in the language they are programming in.