how can we make a ruby compiler

R

Robin

how can we make a thing that compiles ruby into c++ source code?
does anyone want to help me make one....
-r

(e-mail address removed)
 
R

Robert Dober

how can we make a thing that compiles ruby into c++ source code?
does anyone want to help me make one....
-r

(e-mail address removed)
Short answer: You cant
Correct answer: The compiled code needs to incorporate an interpreter
or recompiler to answer the dynamic needs of Ruby. Study the
formidable techniques implemented in JRuby for that matter.

HTH
Robert
 
E

Edmond Kachale

2010/10/1 Robert Dober said:
Short answer: You cant

I second Robert.

Some of significant points to consider:

- Ruby is duck-typed and C++ is strictly typed. A Ruby variable can be
string at one point, a harsh at some instance, an array at some point, a=
nd
Fixnum at another point. This might be difficult to track when converted=
to
C++.


- Ruby and C++ are not closest syntactic relatives. Ruby has some
features (data structures, e.t.c) that make it incomparable to C++. With=
a
lot code spread over gems and with gems coming out almost daily), I do n=
ot
know how easy it can become to bring all that code to a C++ syntax.


- Coding styles among developers differ a lot in Ruby, because of its
flexibility

---
Edmond
Software Developer | Baobab Health Trust (http://www.baobabhealth.org/) |
Malawi

Cell: +265 999 465 137 | +265 881 234 717

*"Many people doubt open source software and probably don=92t realize that
there is an alternative=85 which is just as good.." -- Kevin Scannell*
 
E

Eleanor McHugh

how can we make a thing that compiles ruby into c++ source code?
does anyone want to help me make one....

It's possible in principle, but in practice you'd end up building a ruby =
interpreter as part of that and the performance speed up would be =
marginal without adopting a radically different design to that which is =
currently well understood.

And C++ might not be the most appropriate target language...

Ellie

Eleanor McHugh
Games With Brains
http://feyeleanor.tel
 
R

Roger Pack

how can we make a thing that compiles ruby into c++ source code?
does anyone want to help me make one....

basically you'd need to "parse" the ruby, and then convert it into C++
(using correct types, et al). Unfortunately this wouldn't work for
ruby's more dynamic features, like eval, but would get you somewhere.

Mirah is an example of this, somewhat.
Cheers!
-r
 
L

Luc

Hi all.

This might be slightly off-topic but something interesting would be to
look at a canonical Ruby interpreter written in Ruby, in the lisp /
scheme fashion.

I would be interested in knowing whether someone has done that
already.

Thanks.
 
C

Chuck Remes

Hi all.

This might be slightly off-topic but something interesting would be to
look at a canonical Ruby interpreter written in Ruby, in the lisp /
scheme fashion.

I would be interested in knowing whether someone has done that
already.

The closest we have is the Rubinius Project:

http://rubini.us

It just hit release 1.1.0 and is looking really good. Most of the standard library is in Ruby while certain portions are written in C++. The goal is to move *more* of it to Ruby as the compiler and JIT get more intelligent.

cr
 
R

Robert Dober

I would have appreciated a context aware citation, e.g. including my
"correct answer".
Thank you for your comprehension.

R.
 
R

Roger Pack

Short answer: You cant
It's possible, although many language features can't be supported, most
notably eval.

Well, I would disagree slightly. Not impossible, just dang hard to
implement, give what appears to me to be this odd gulf between languages
as being either "statically compiled and typed or not" (though a few
exceptions exist. bytecode emitters and the play framework come to
mind, as well as scala/clojure, so...we're getting there...).

-r
 
M

Martin DeMello

If you really want to explore ruby compilation, you could look into
doing it via scheme. There are several scheme -> native compilers, and
scheme seems like the most rubyish target available. Be warned that it
will be a lot of work.

martin
 
R

Ryan Davis

how can we make a thing that compiles ruby into c++ source code?
does anyone want to help me make one....

What's "awesome" is that not a soul on this thread has asked you a =
single clarifying question...

WHY do you want one and WHAT do you want to do with it?
 
A

Ammar Ali

You just ruined the "awesome" by asking them. Didn't you? Just kidding.

Seriously, I believe these questions are not worth asking. It is 100%
possible in both principle and in practice to convert Ruby code into
another language where the output performs the same functions as the
original code. However, the output will have nothing to do with Ruby
in any way. It will just do the same things that the ruby code did. I
also believe that doing a "round trip", converting back to Ruby, is
also possible.

Before I would even consider asking the OP why and what for, I ask
myself if the benefits of such a tool would justify the immense effort
required to create it. I think the answer is no.

But, and just to get a feel for how much work will be involved, you
can try a few experiments. One interesting one would use Ruby itself
to accomplish part of this goal. Imagine overriding all the methods of
Array and Enumerable with methods that instead of executing generate
the language you are interested in. For example:

class Array
def initialize
$stdout.puts "vector<VALUE> a_#{some_id};"
end

def reverse
$stdout.puts "reverse(a_#{some_id}.begin(), a_#{some_id}.end());"
end

# etc...
end

Of course this is not meant to work, it is meant to illustrate what
might be involved. How would you handle generic values (like ruby's
VALUE struct)... these can hold anything from an int to a map of maps.
Duplicate ruby's? Probably. How would you handle identification (as in
generating identifiers) of the various objects? Do you need multiple
passes to in order to identify which headers you need add #includes
for? Probably.

It's not going to be easy, and I don't think the benefits, if there
are any, will be worth it.

Regards,
Ammar
 
R

Robert Dober

startup time (also there in jruby, btw), and the fact that you have to
add type information, but they may scratch your itch.
I do not think you have to add type information in Clojure, you can,
but the beauty of this is that this is only a last resource in case
performance is not good enough.

Cheers
Robert
 
R

Ryan Davis

Seriously, I believe these questions are not worth asking.

Then why do you bother with this mailing list? You might as well =
unsubscribe now.
It is 100% possible in both principle and in practice to convert Ruby =
code into another language where the output performs the same functions =
as the original code.

I know. I've done it. Twice. See ruby2c for one such example. There is =
another, zenobfuscate, (unreleased) that translates ruby to ruby c =
internals so that you can compile a ruby c extension and ship a binary =
instead of raw source. This may be what the OP wanted, but since nobody =
bothered to ask before answering (in volumes), it looks like they were =
chased off.
However, the output will have nothing to do with Ruby in any way.

This is entirely false and I see absolutely no justification in the rest =
of your mail for such a claim.
 
P

Philip Rhoades

Ryan,


Then why do you bother with this mailing list? You might as well
unsubscribe now.


I know. I've done it. Twice. See ruby2c for one such example. There
is another, zenobfuscate, (unreleased) that translates ruby to ruby c
internals so that you can compile a ruby c extension and ship a
binary instead of raw source. This may be what the OP wanted, but
since nobody bothered to ask before answering (in volumes), it looks
like they were chased off.


This is entirely false and I see absolutely no justification in the
rest of your mail for such a claim.


OK, I have been lurking but will respond now: I have an actual existing
application (C/C++) for population genetics simulations which I would
dearly love to convert to Ruby. It was originally all in C and, as a
learning exercise, I converted parts of it to C++ - but it was such a
pain . . it would have been so pleasant to re-write in Ruby. However I
would have to get resulting code converted back to C or compiled somehow
to get the performance back to something usable. I looked at ruby2c
some time ago but it didn't seem developed enough. I have been watching
Vidar Hokstad's progress:

http://www.hokstad.com/compiler

but that still has some way to go and Ocelot development seems to have
stopped?

Would ZenObfuscate be able to do what I want? - I just had a look at the
website and I see it is an (expensive) commercial product . . pity.

So it appears that there is still no libre software ready for prime time
in terms of being able to write code nicely in Ruby and have some sort
of converted run time that has near C performance . .

Regards,

Phil.
--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
M

Martin DeMello

OK, I have been lurking but will respond now: =A0I have an actual existin= g
application (C/C++) for population genetics simulations which I would dea= rly
love to convert to Ruby. =A0It was originally all in C and, as a learning
exercise, I converted parts of it to C++ - but it was such a pain . . it
would have been so pleasant to re-write in Ruby. =A0However I would have = to
get resulting code converted back to C or compiled somehow to get the
performance back to something usable.

Couldn't you just profile and write the bottlenecks in C? Also, take a
look at OCaml for ruby-like expressiveness with
reasonably-close-to-C++ performance.
So it appears that there is still no libre software ready for prime time = in
terms of being able to write code nicely in Ruby and have some sort of
converted run time that has near C performance . .

I believe projects like ruby2c and zenobfuscate wouldn't get you
near-C performance, since they'd still be doing a lot of what the ruby
runtime is doing under the hood.

martin
 
R

Ryan Davis

I believe projects like ruby2c and zenobfuscate wouldn't get you
near-C performance, since they'd still be doing a lot of what the ruby
runtime is doing under the hood.

That's true of zenobfuscate but not true of ruby2c.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top