How can I convert Ruby to Lua?
Asked Answered
C

3

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.
Consortium answered 7/11, 2012 at 2:18 Comment(13)
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?Frogfish
@lhf, larger than I'd like :(Consortium
@lhf, I was asking in general to everyone here. Thanks though for not downvoting. It confused me when I say my quesiton at -1Consortium
You could always build a ruby interpreter in Lua...Suggestion
@KevinBallard If he's gonna do that, he might as well just re-write his program in Lua ;)Pretoria
@KevinBallard, Re: Interpreter - how is that any different than converting Ruby to Lua?Consortium
Argh, more downvotes. I wonder why.Consortium
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.Loan
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.Consortium
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! :)Pretoria
@Miguel, thanks for the input. I'll update the question to include some more info.Consortium
IMO re-write, using language-specific paradigms. Everything will be happier.Be
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.Tester
P
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.

Peyton answered 7/11, 2012 at 3:23 Comment(5)
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.Caudill
The people at jruby would like to have a word with you :).Emmalynn
@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.Peyton
@BorisStitnicky, can you answer my 2nd question ... "what are the limitation"? Meaning, what exact subset of function from Ruby would I lose in Lua?Consortium
@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.Peyton
P
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! :)

Pretoria answered 7/11, 2012 at 4:25 Comment(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.Peyton
R
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
Rampant answered 17/9, 2016 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.