This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
thegreatstudio's profile picture

C/C++ to Lua

Started by thegreatstudio, 10 June 2013 - 03:37 AM
thegreatstudio #1
Posted 10 June 2013 - 05:37 AM
Do anyone have a program does c/c++ to Lua??
UMayBleed #2
Posted 10 June 2013 - 06:06 AM
That is nearly impossible to do sorry. Lua is actually created in C, Also C uses classes which lua does not use. It could possibly be done, but lua is not meant to be like C. Another thought, C uses imports like System.windows.forms, Lua can't. So that part is not possible either.

Try downloading the source of lua http://www.lua.org/ftp/
then look at it's files, they are .c and .h files.

It would be cool if we could use C in computercraft though.

Also I think this may be in the wrong section.
ElvishJerricco #3
Posted 10 June 2013 - 07:35 AM
That is nearly impossible to do sorry. Lua is actually created in C, Also C uses classes which lua does not use. It could possibly be done, but lua is not meant to be like C. Another thought, C uses imports like System.windows.forms, Lua can't. So that part is not possible either.

Try downloading the source of lua http://www.lua.org/ftp/
then look at it's files, they are .c and .h files.

It would be cool if we could use C in computercraft though.

Also I think this may be in the wrong section.

Actually, those things that you say C uses, are not actually features of C, but features of the libraries used with C. C is just a language. And although there currently exists no way to turn C into lua, it is possible. Not as hard as you'd think, either. In fact, I'm working on making an assembly language for the LuaJ bytecode standard. Once I have that, it shouldn't be hard to create an LLVM backend that compiles to this assembly language.

LLVM (Low Level Virtual Machine) is a three part compiler infrastructure. Front-end, optimizer, back-end. Front-end implements languages. It takes languages like C and compiles them into another language called LLVM IR that is very similar to most assembly languages. The result of that is then optimized. And finally, the final LLVM IR is compiled to a back-end language (usually some assembly language for some platform). And because of how similar LLVM IR is to the idea of assembly, someone writing a back-end doesn't even have to write a full compiler from language to language. Instead, you tell LLVM about your language and LLVM does it for you.

The result is that you can make any language for LLVM, then compile it to any platform. My goal with my lua assembly language (lasm) is to eventually write an LLVM back-end for it, so that you can compile any LLVM language to the LuaJ virtual machine. So you'd be able to use C, C++, Objective-C, and probably a lot of others.

BUT There will be no libraries! Any class, function, variable, or constant you can normally get for any of these languages will not exist. In fact, since Objective-C requires a runtime library, you'd have to write an implementation of that before you could use it for this. But C and C++ should compile fine. And if I do it right, it should be able to call the lua functions and lua functions should be able to call C or C++ functions.
MaHuJa #4
Posted 10 June 2013 - 09:06 AM
Lua is actually created in C […] Try downloading the source of lua http://www.lua.org/ftp/ then look at it's files, they are .c and .h files.
That is the original lua. Computercraft uses LuaJ, a java implementation of the language.
Also C uses classes which lua does not use. […] C uses imports like System.windows.forms,
No it doesn't. C doesn't have "classes" to begin with, and C++ doesn't use .net imports. You may be thinking of C# which is another language altogether, and not either of those discussed.

thegreatstudio:
What exactly are you wanting to convert?
Something you'd write yourself, so as to use C++ to write computercraft programs? Possible if ElvishJerricco finishes that project of his. You'd get none of the speed benefits, since it'd run under the lua runtime anyway. (The performance profile would be very different from native.)
If it's some other application, then it probably has dependencies that would also need to be ported - if that would be at all possible.
thegreatstudio #5
Posted 10 June 2013 - 11:05 AM
Thank you for your great answers! I will download fedora which Linus Thorvald uses it.
Lyqyd #6
Posted 10 June 2013 - 12:46 PM
This is a silly topic.

Locked.