ruby interpreter written in ruby..

E

Eelco

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
 
R

Ryan Davis

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).
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

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?
 
E

Eelco

Dylan Evans schreef:
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?
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
 
T

Tim Hunter

Eelco said:
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)
 
R

Ryan Davis

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. :p
 
J

John Carter

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 Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

On Feb 8, 2009, at 03:40 , Dylan Evans wrote:

It seems to me that would be pointless, since it would require another

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


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.
 
J

Julian Leviston

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/

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 Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
R

Ryan Davis

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.
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

On Feb 8, 2009, at 17:51 , Dylan Evans wrote:

I see that squeak uses a C translator for it's implementation which is

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 386.
 
J

John Carter

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)

2) 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 Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top