Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a language be interpreted by itself (like Rubinius)?

I've been programming in Ruby for a while now with just the standard MRI implementation of Ruby, but I've always been curious about the other implementations I hear so much about.

I was reading about Rubinius the other day, a Ruby interpreter written in Ruby. I tried looking it up in various places, but I was having a hard time figuring out exactly how something like this works. I've never had much experience in compilers or language writing but I'm really interested to figure it out.

How exactly can a language be interpreted by itself? Is there a basic step in compiling that I don't understand where this makes sense? Can someone explain this to me like I'm an idiot (because that wouldn't be too far off base anyways)

like image 624
joeellis Avatar asked May 30 '10 06:05

joeellis


2 Answers

It's simpler than you think.

Rubinius is not 100% written in Ruby, just mostly.

From http://rubini.us/

A large aspect of popular languages such as C and Java is that the majority of the functionality available to the programmer is written in the language itself. Rubinius has the goal of adding Ruby to that list. Rubyists could more easily add features to the language, fix bugs, and learn how the language works. Wherever possible Rubinius is written in Ruby. Where not possible (yet), it's C++.

like image 136
jefflunt Avatar answered Oct 10 '22 03:10

jefflunt


The concept you are looking for is compiler bootstrapping.

Basically bootstrapping means writing a compiler (or an interpreter) for language x in language x. This is done either by writing a basic compiler on a lower level by hand (i.e. writing a C compiler in Assembly), or by using a different high-level language.

Read more about bootstrapping on wikipedia. Greg's answer regarding meta-circular evaluators is also highly recommended, including the relevant chapter in SICP.

like image 35
Yuval Adam Avatar answered Oct 10 '22 03:10

Yuval Adam