Is there any way to automatically convert Ruby code to Lua? My question is:
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:
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?
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?
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.
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,
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.
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! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With