Natalie – a work-in-progress Ruby compiler, written in Ruby and C++
github.com
github.com
Natalie: An early-stage Ruby implementation that compiles to C++ - https://news.ycombinator.com/item?id=29660883 - Dec 2021 (50 comments)
Natalie: A work-in-progress Ruby implementation, compiled to C++ - https://news.ycombinator.com/item?id=28207921 - Aug 2021 (2 comments)
In particular, `method_missing` and other fundamentally dynamic things that make Ruby feel like Ruby can't be done in Crystal, at least not without extensive runtime support.
(I haven't looked at this implementation, but I suspect that they end up doing similar things in C++ for the same reason.)
> It provides an ahead-of-time compiler using C++ and gcc/clang as the backend
Which is kind of the new "in" lately.
EDIT: If you see the the animation in the GitHub repo, you'll see at a certain point a file named "bs" (which is a binary file). I guess the standard Ruby compiler doesn't allow you to spit out binaries out of the box (?).
Ruby is an interpreted language, programs are not compiled (except for the JIT compiler).
I wonder what makes this different from mruby[1] which seems to be very well supported for many years.
You can make almost any general purpose language "compiled", depending on how fat you're willing to make the runtime. And you can certainly create an interpreter/JIT VM for C/C++. It's not a question of possibility, but one of "why?".
I say some applications because there are still people using Ruby to write non-web things, and I doubt if this project will ever support Rails in the short term
Even if it only ends up compiling a subset of Ruby, it could be a foundation for a Ruby equivalent of one of the several tools for accelerating Python by compiling a subset and allowing it to interface with regular interpreted code.
Interpreters are almost always written in statically typed compiled languages. When you feed an interpreter source code in Ruby, Python, Perl, etc. the net effect is that you have a compiled binary program that's executing your dynamic code.
So it shouldn't too great of a leap to understand that you can do full ahead-of-time compilation of dynamic code so long as you're prepared to embed a rich runtime into the binary.
Common Lisp is one of the most dynamic languages in existence, and has been ahead-of-time compiled for decades.
Of course, the dominant compilation paradigm for dynamic languages these days is "just-in-time" (e.g. JavaScript V8, Python PyPy, Ruby YJIT), because it allows you to use runtime frequency and type information to compile parts of the source more intelligently.
But either way, the whole "dynamic languages cannot be compiled" thing isn't true.