Ruby interpreter written in ruby


#1

Hi there,

I am new to programming and ruby… Trying to read through the pickaxe
and do the exercises on Sphere Online Judge.
I was wondering is there a ruby written ruby interpreter available?
somehow i`d like to be able to read the interpreter source.

greetz Cat


#2

On Feb 8, 2009, at 01:45 , Eelco wrote:

I was wondering is there a ruby written ruby interpreter available?
somehow i`d like to be able to read the interpreter source.

no, not 100% (yet) at least. the rubinius project is farthest along in
that effort.

http://rubini.us/

the doco on that site is… well… not good. Just download the source
and poke around. There is a fair amount in C++ still (last I looked)
but a lot more pure ruby code than available in MRI or any other ruby
impl project (last I looked).


#3

On Sun, Feb 8, 2009 at 7:45 PM, Eelco removed_email_address@domain.invalid wrote:

Hi there,

I am new to programming and ruby… Trying to read through the pickaxe and
do the exercises on Sphere Online Judge.
I was wondering is there a ruby written ruby interpreter available? somehow
i`d like to be able to read the interpreter source.

greetz Cat

It seems to me that would be pointless, since it would require another
interpreter to function. Of course the other alternative would be to
write a
ruby compiler which would be awkward in a dynamic language. Of course if
it
wasn’t actually compiled then you would have the overhead of
interpreting a
stack of ruby. Why would you want a ruby interpreter written in ruby?


#4

Eelco wrote:

I wasn`t thinking about the why or what…
Just as this is the first language i am trying to understand i thought i
could learn a lot by reading the code

There’s a huge amount of very good Ruby code out there in Ruby libraries
and applications. Rails, Rake, RubyGems. All written in Ruby.

Also take a look at the past Ruby quizzes (rubyquiz.com)


#5

Smalltalk is written in itself. It’s quite useful! You can understand
a lot about a language if it has such a high level of self-
describingness. Rubinius is such a project.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/


#6

Dylan E. schreef:

greetz Cat

That makes sense…
I wasn`t thinking about the why or what…
Just as this is the first language i am trying to understand i thought i
could learn a lot by reading the code
As C is jibberish to me…

Eelco


#7

On Feb 8, 2009, at 03:40 , Dylan E. wrote:

It seems to me that would be pointless, since it would require another
interpreter to function. Of course the other alternative would be to
write a
ruby compiler which would be awkward in a dynamic language. Of
course if it
wasn’t actually compiled then you would have the overhead of
interpreting a
stack of ruby. Why would you want a ruby interpreter written in ruby?

smalltalk (squeak, at the very least), many lisps/schemes, factor,
every language designed by wirth… yeah. must be pointless. :stuck_out_tongue:


#8

On Mon, Feb 9, 2009 at 7:55 AM, Ryan D. removed_email_address@domain.invalid
wrote:

stack of ruby. Why would you want a ruby interpreter written in ruby?

smalltalk (squeak, at the very least), many lisps/schemes, factor, every
language designed by wirth… yeah. must be pointless. :stuck_out_tongue:

I see that squeak uses a C translator for it’s implementation which is
kinda cool, and the lisps use either a bootstrap implementation or a
translator.I was just thinking of a plain ruby interpreter entirely in
ruby,
which really would be pointless.
Of course new languages written in interpreted languages can be good
prototypes.


#9

I don’t think a measure such as that is useful for anything much. IMHO
you’d want a language written in itself so you could understand it
better at a lower level: pedagogy.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/


#10

On Feb 8, 2009, at 17:51 , Dylan E. wrote:

I see that squeak uses a C translator for it’s implementation which is
kinda cool, and the lisps use either a bootstrap implementation or a
translator.I was just thinking of a plain ruby interpreter entirely
in ruby,
which really would be pointless.

there you go using that word (“pointless”) again… As someone who
worked on smalltalk for many years and was working on metaruby before
I was professionally working on rubinius, I have to highly disagree.
I’m gonna leave it at that tho.


#11

On Sun, 8 Feb 2009, Dylan E. wrote:

I was wondering is there a ruby written ruby interpreter available? somehow
i`d like to be able to read the interpreter source.

Hokay, so this is at the trivial “HEY! You Cheated!!” level… but
it’s still useful…

ri ‘Kernel#eval’
------------------------------------------------------------ Kernel#eval
eval(string [, binding [, filename [,lineno]]]) => obj

  Evaluates the Ruby expression(s) in _string_. If _binding_ is
  given, the evaluation is performed in its context. The binding may
  be a +Binding+ object or a +Proc+ object. If the optional
  _filename_ and _lineno_ parameters are present, they will be used
  when reporting syntax errors.

     def getBinding(str)
       return binding
     end
     str = "hello"
     eval "str + ' Fred'"                      #=> "hello Fred"
     eval "str + ' Fred'", getBinding("bye")   #=> "bye Fred"

Why would you want a ruby interpreter written in ruby?

Actually the original poster asked a very intelligent question.

Why would you want such a thing?

It’s a very effective measure of the simplicity and expressiveness
combined of the language.

For example, here is a minimal (non-trivial) Joy0 interpeter written
in Joy0 that will interpret itself.
Source http://www.latrobe.edu.au/philosophy/phimvt/joy/jp-joyjoy.html

joy0 ==
[ [ [ joy0 body joy0 ]
[ [] ]
[ pop pop pop ]
[ cons pop cons ]
[ opcase pop opcase ]
[ body pop body ]
[ i pop joy0 ]
[ step pop [joy0] cons step ]
[ [] cons i ] ]
opcase
i ]
step

Yup. That was it. All of it.

Says something very very powerful about the simplicity of the Joy
language.

I’ll admit I learnt several things about the language when I read that
close enough to understand it.

I bet you’d have a good few “Aha!” moments if you could do the same
with Ruby.

Conversely, if you could strip Ruby to a Ruby0 essence in which you
could easily write a ruby0 in ruby0 interpreter… you’d learn what
was the essence of the language and what was sugar.

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : removed_email_address@domain.invalid
New Zealand


#12

On Mon, Feb 9, 2009 at 4:54 PM, Ryan D. removed_email_address@domain.invalid
wrote:

there you go using that word (“pointless”) again… As someone who worked
on smalltalk for many years and was working on metaruby before I was
professionally working on rubinius, I have to highly disagree. I’m gonna
leave it at that tho.

Seriously? You understand what i’m saying is pointless right? I’m referring
to a ruby interpreter written in ruby yes, but then this interpreter is
interpreted by for example rubinius, or jruby, or any other ruby
interpreter. Ok as an exercise that might be an interesting project, or
perhaps it could be useful for some sort of sandboxing, but what about
day
to day use? What function would that serve? If you wanted you could keep
stacking interpreters on top of each other until you had an expensive


#13

On Mon, 9 Feb 2009, Ryan D. wrote:

As someone who worked on smalltalk for many years and was working on
metaruby before I was professionally working on rubinius, I have to
highly disagree.

I strongly agree with you, what you have been doing is very
interesting and valuable.

Which leads to some questions I have for you… you probably don’t
have the answers to them, but your thoughts about what the answers may
be should be interesting.

  1. The “lisp in lisp” / “joy in joy” / … interpreters I have seen have
    sort of glossed over the lexical / parsing issue by assuming that they
    are interpreting an already internalised representation of the
    program.

This is part of the classic basic data representation === the basic
program
representation ambiguity of these languages.

We can pretty much do this in Ruby too… A ruby program can be
represented as an Array of Symbols, Numbers and Strings.

So assuming at least a lexical analyser that will chew on Ruby program
text and spit out an Array of Symbols, Numbers and Strings…

Q1a) What would be the Ruby0 language? ie. What is the smallest subset
of Ruby that could be used to write a self-interpreting interpreter.

Q1b) Could there be a way of hijacking the Ruby internal lexer to
cough up such an Array of Objects?

Q1c) What would be the RubyLex0 definition? (The simplist subset of
Ruby lexical analysis that could be used as a front end to a Ruby0
interpreter)

  1. A major gotcha is Regular Expressions are a “language within a
    language”.

2a) It would be a fun thing to do to create a Ruby Regex to Ruby
compiler - and then extend the semantics hugely.

2b) What would be the simplist Ruby0 in Ruby0 definition including
the lexical analysis from text?

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : removed_email_address@domain.invalid
New Zealand