Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert Ruby to Lua?

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.
like image 263
nickb Avatar asked Nov 07 '12 02:11

nickb


People also ask

Is ruby similar to Lua?

Lua is easier to embed, but Ruby has a larger library, and better syntax (in my opinion). The problem is, there is no easy way I found to use Ruby as scripting language with C++, whereas it's very easy with Lua. Toughs about this?

Is Lua faster than ruby?

What can lua do that ruby cannot? Fit in a smaller region of memory, use less memory, and do more work per unit of time. In other words, it's smaller and faster, which makes it much more desirable for gaming and embedded use. Why should I code in lua over ruby?

Is ruby a compiled language?

Ruby is a compiled language in much the same way that Java is. While ruby is not compiled down to native machine code, it is compiled into a set of bytecode instructions that are interpreted by a virtual machine.


2 Answers

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.

like image 84
Boris Stitnicky Avatar answered Sep 23 '22 18:09

Boris Stitnicky


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! :)

like image 29
Miguel Avatar answered Sep 20 '22 18:09

Miguel