how can we make a ruby compiler

Discussion in 'Ruby' started by Robin, Oct 1, 2010.

  1. Robin

    Robin Guest

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

     
    Robin, Oct 1, 2010
    #1
    1. Advertising

  2. Robin

    Robert Dober Guest

    On Fri, Oct 1, 2010 at 6:40 AM, Robin <> wrote:
    > how can we make a thing that compiles ruby into c++ source code?
    > does anyone want to help me make one....
    > -r
    >
    >
    >
    >

    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


    --
    The best way to predict the future is to invent it.
    -- Alan Kay
     
    Robert Dober, Oct 1, 2010
    #2
    1. Advertising

  3. 2010/10/1 Robert Dober <>

    > On Fri, Oct 1, 2010 at 6:40 AM, Robin <> wrote:
    > > how can we make a thing that compiles ruby into c++ source code?
    > > does anyone want to help me make one....
    > > -r
    > >
    > >
    > >
    > >

    > 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*

    >
    >
    > --
    > The best way to predict the future is to invent it.
    > -- Alan Kay
    >
     
    Edmond Kachale, Oct 1, 2010
    #3
  4. On 1 Oct 2010, at 05:40, Robin wrote:
    > 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
    ----
    raise ArgumentError unless @reality.responds_to? :reason
     
    Eleanor McHugh, Oct 1, 2010
    #4
  5. Robin

    Roger Pack Guest

    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Oct 1, 2010
    #5
  6. Robin

    Luc Guest

    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.
     
    Luc, Oct 1, 2010
    #6
  7. Robin

    Tony Arcieri Guest

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

    On Thu, Sep 30, 2010 at 11:00 PM, Robert Dober <>wrote:

    > Short answer: You cant
    >


    It's possible, although many language features can't be supported, most
    notably eval.

    See Starkiller, a static type inferencing compiler for Python:

    http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.268&rep=rep1&type=pdf

    --
    Tony Arcieri
    Medioh! A Kudelski Brand
     
    Tony Arcieri, Oct 1, 2010
    #7
  8. Robin

    Chuck Remes Guest

    On Oct 1, 2010, at 11:23 AM, Luc wrote:

    > 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
     
    Chuck Remes, Oct 1, 2010
    #8
  9. Robin

    Robert Dober Guest

    On Fri, Oct 1, 2010 at 6:56 PM, Tony Arcieri <> wrote:

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

    R.
     
    Robert Dober, Oct 1, 2010
    #9
  10. Robin

    Roger Pack Guest

    >> 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Oct 1, 2010
    #10
  11. Robin

    Roger Pack Guest

    Roger Pack, Oct 1, 2010
    #11
  12. Robin

    Intransition Guest

    Intransition, Oct 1, 2010
    #12
  13. 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

    On Fri, Oct 1, 2010 at 10:10 AM, Robin <> wrote:
    > how can we make a thing that compiles ruby into c++ source code?
    > does anyone want to help me make one....
    > -r
    >
    >
    >
    >
     
    Martin DeMello, Oct 1, 2010
    #13
  14. Robin

    Ryan Davis Guest

    On Sep 30, 2010, at 21:40 , Robin wrote:

    > 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?
     
    Ryan Davis, Oct 2, 2010
    #14
  15. Robin

    Ammar Ali Guest

    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



    On Sat, Oct 2, 2010 at 4:52 AM, Ryan Davis <> wrote:
    >
    > On Sep 30, 2010, at 21:40 , Robin wrote:
    >
    >> 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?
    >
    >
    >
     
    Ammar Ali, Oct 2, 2010
    #15
  16. Robin

    Robert Dober Guest

    On Sat, Oct 2, 2010 at 3:29 PM, Roger Pack <> wrote:
    > 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

    --
    The best way to predict the future is to invent it.
    -- Alan Kay
     
    Robert Dober, Oct 2, 2010
    #16
  17. Robin

    Ryan Davis Guest

    On Oct 2, 2010, at 05:36 , Ammar Ali wrote:

    > 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.
     
    Ryan Davis, Oct 2, 2010
    #17
  18. Ryan,


    On 2010-10-03 06:53, Ryan Davis wrote:
    >
    > On Oct 2, 2010, at 05:36 , Ammar Ali wrote:
    >
    >> 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.



    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:
     
    Philip Rhoades, Oct 3, 2010
    #18
  19. On Sun, Oct 3, 2010 at 1:43 PM, Philip Rhoades <> wrote:
    >
    > 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
     
    Martin DeMello, Oct 3, 2010
    #19
  20. Robin

    Ryan Davis Guest

    On Oct 3, 2010, at 10:30 , Martin DeMello wrote:

    > 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.
     
    Ryan Davis, Oct 5, 2010
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,154
  2. Jack Wright
    Replies:
    5
    Views:
    635
    Shiv Kumar
    Jan 19, 2004
  3. Ram
    Replies:
    0
    Views:
    2,878
  4. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    450
    Frederik Hertzum
    May 17, 2004
  5. RickMuller
    Replies:
    4
    Views:
    724
    Alexey Shamrin
    Mar 26, 2005
Loading...

Share This Page