8

Is there any way to automatically convert Ruby code to Lua? My question is:

  1. What kinds of tools exist for this purpose?
  2. What are their limitations?

One primary goal of this is to be able to bypass the Ruby interpreter when I execute the (converted to) Lua program. I don't mind if the Ruby interpreter needs to be invoked during the conversion process to Lua, but I don't want to have to rely on the Ruby interpreter when I run the newly generated Lua code.


UPDATE:

I've done quite a bit research into this topic and I personally am not finding a solution (which is why I'm asking the question here on StackOverflow).

In case anyone is curious, I've looked deeply into:

  • Ruby-Lua, though all this does is allow you to call Lua from Ruby
  • RLua, this allows you to run Ruby code from Lua and vise versa but it's not translating Ruby to Lua and it still invokes the Ruby interpreter on program run.
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
nickb
  • 9,140
  • 11
  • 39
  • 48
  • Ruby and Lua are probably quite different semantically and so an automatic translation is not simple. How big is the program you want to convert? – lhf Nov 07 '12 at 02:20
  • @lhf, larger than I'd like :( – nickb Nov 07 '12 at 02:22
  • @lhf, I was asking in general to everyone here. Thanks though for not downvoting. It confused me when I say my quesiton at -1 – nickb Nov 07 '12 at 02:25
  • You could always build a ruby interpreter in Lua... – Lily Ballard Nov 07 '12 at 02:25
  • @KevinBallard If he's gonna do that, he might as well just re-write his program in Lua ;) – Miguel Nov 07 '12 at 02:26
  • @KevinBallard, Re: Interpreter - how is that any different than converting Ruby to Lua? – nickb Nov 07 '12 at 02:26
  • Argh, more downvotes. I wonder why. – nickb Nov 07 '12 at 02:28
  • 2
    I'm just reading this and your question does sound like you have not invested any time searching for a solution. Some people doesn't like that. Maybe that's the cause of the downvote. – madth3 Nov 07 '12 at 02:30
  • 1
    Unfortunately, I've searched quite a bit and am not finding any meaningful solution. I could also link to what I've already looked at and reviewed ... though I've always favored brevity in questions than long winded ones. – nickb Nov 07 '12 at 02:32
  • 1
    Concerning the downvotes: I +1'd. Not everyone likes direct and open-ended questions, list style questions, and many times they get closed. I find this sad. Many of the best questions on this site are of this kind, and they're extremely helpful! :) – Miguel Nov 07 '12 at 02:35
  • @Miguel, thanks for the input. I'll update the question to include some more info. – nickb Nov 07 '12 at 02:38
  • 1
    IMO re-write, using language-specific paradigms. Everything will be happier. – Dave Newton Nov 07 '12 at 03:54
  • Note that a real transpiler would need to come with its own runtime library, because the modules lua ships with are rather lean (by design), ruby has a lot more things from the get go in its standard library. – Cubic Jan 09 '15 at 19:44

3 Answers3

7

I'm sorry if this is not the answer you want to hear, but there is no practical way to do it, pal. I'm not going to say Ruby is richer than Lua, as I haven't studied the latter enough to say that, but from what I know and what I see,

  1. There is no way anyone would have bothered writing Ruby compiler to Lua and similar languages.
  2. There is no way writing such compiler would take you shorter time than rewriting your program manually.
  3. The only language Ruby community cares about compiling Ruby into, is Ruby bytecode.

One could go on an on on what the possibilities are and are not of compiling high level languages into one another, but the bottom line for you is: Give up early. Stop searching and start rewriting your progs manually.

Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74
  • agree. language features differ. that's why conversion would have you loose advantages and introduce disadvantages. if you'd like pure lua, write pure lua. – glasz Nov 07 '12 at 04:33
  • 4
    The people at jruby would like to have a word with you :). – sunnyrjuneja Nov 07 '12 at 06:32
  • `@glasz`: You are speaking from my heart. `@Sunny Juneja`: Did I say a dirty word? Apologies. By Ruby bytecode, I meant 'bytecode on either YARV or JVM or whatever other VM'. But when on JVM, I'd personally go for rewriting Ruby manually into Scala, which is very much like typed Ruby. – Boris Stitnicky Nov 07 '12 at 20:22
  • @BorisStitnicky, can you answer my 2nd question ... "what are the limitation"? Meaning, what exact subset of function from Ruby would I lose in Lua? – nickb Nov 11 '12 at 15:29
  • @nickb: Sorry, I cannot. The purpose of my answer was to discourage you from seeking an automated solution. If you _still_ must do it, then I am not the one who knows how to do it, or what limitaions would it introduce. But like `@glasz` said, some limitations are inherent to the differences in the design of the two languages. – Boris Stitnicky Dec 20 '12 at 17:27
2

Note: I agree with everyone else here, that you really should just re-write your program from scratch. This answer is just an idea, that I hope someone will look into really.

There are already a few Ruby-to-X converters that could you could bootstrap. The good ones already have a lexer & parsers, so you'd only need to work with the backend. If you choose a language similar to Lua then you could simplify things even more.

For example, there are a few Ruby-to-Javascript implementations, like RubyJS. Putting aside metamethods, self syntactic sugar, library differences, and a few semantic differences here and there, Lua could be seen as a subset of Javascript with a more verbose syntax.

With that in mind, you could modify most of the backend generator by changing a few { }s here and there:

// some random js
while(x < 56 && y == 40) {
  print('Hello World!');
}

 

-- What you could make it generate:
while x < 56 and y == 40 do
  print 'Hello World!'
end

Perhaps there may be a few times when it relies upon Javascript specific features, but even then, most of them could be emulated.

// Javascript has a === operator that enforces type equality
'5' === 5 // FALSE!

 

-- Lua lacks this, but it's easy to imitate
(type('5') == type(5)) and ('5' == 5)

No, honestly, this wouldn't be an easy project, but I think it's something definetly worth investing the time in. I hope this helps! :)

Miguel
  • 1,966
  • 2
  • 18
  • 32
  • 1
    I know that there will be always market for this, with snake oils on it. By this, I do not mean to offend RubyJS. They admit themselves – citing 'RubyJS' site: "With RubyJS you can transform a subset of Ruby into Javascript code." *Subset* is the keyword. It may achieve significant success, but in the end, will be like Wine on X: Plenty of programs run on it, with the exception of the one you really need. Still, +1 to you for the effort. – Boris Stitnicky Nov 07 '12 at 20:26
0

Universal-transpiler is a library for SWI-Prolog that can convert a small subset of Ruby into Lua and several other languages.

This is an example of its usage in Prolog, with Ruby source code as its input:

:- use_module(library(transpiler)).
:- set_prolog_flag(double_quotes,chars).
:- initialization(main).

main :- 
    translate("def add(a,b) return a + b end def squared(a) return a*a end def add_exclamation_point(parameter) return parameter + \"!\" end",'ruby','lua',X),
    atom_chars(Y,X),
    writeln(Y).

...and this is the Lua source code that it generates:

function add(a,b) 
        return a..b
end 
function squared(a) 
        return a*a
end 
function add_exclamation_point(parameter) 
        return parameter.."!"
end
Anderson Green
  • 30,230
  • 67
  • 195
  • 328