r/lua • u/macsipac • Feb 18 '26
Experimental Lua2C
I have been working on a project to turn simple lua code into actual working, compilable C code, and its working! (With static compilation if you have a C compiler, i guess)
It currently supports:
Variables with strings, integers, and floats
Variable manipulation with addition, subtraction, multiplication, and division
Functions with experimental arguments
And Print()
if this gets 20 upvotes ill make it open source WE GOT 20 UPVOTES
Source code: https://github.com/LuaToC/LuaToC
UPDATE 1: Added tables and loops, currently working on os since i want clock,
Update 2: Wait how are theese updates going so fast? Anyway, added experimental os and math libraries, their code inspired by luajit!
Update 3: Minor update, added assert(), optimized print() a bit, and fixed some memory leaks
Update 4: Fixed assert() lol, nearly halfed script size by cleaning up code (1700 lines > 900 lines), fixed more memory leaks.
4
u/topchetoeuwastaken Feb 18 '26
look into the pallene language, made by the same people that made lua.
it is a mostly full superset of lua, compiled to machine code. AFAIK only things its missing are closures and upvalues. i could be mistaken tho
4
u/activeXdiamond Feb 19 '26
This is very interesting. You might also be interested in Nelua.
1
u/BigBossErndog Feb 19 '26
I don't mean to be disrespectful with this question, just genuine curiosity.
What benefits does compiling to C (then needing to compile that again) give over just using LuaJIT or even just the Lua library in C? It seems performant enough that I don't think it would be much of a game changer to compile to C. You just lose the advantages of it being an interpreted language (getting rid of needing to compile in the first place). If you needed C, perhaps it would've been better to just code in C in the first place?
Sounds like an interesting project nonetheless. Good luck!
1
u/AdamTheRedditUser1 Feb 19 '26
you should continue working on it, this is a really cool project
1
u/izuannazrin Feb 20 '26
What lua version you're supporting? And how's the readability of the output C code? (This is a transpiler right?)
1
u/CyberDranik Feb 20 '26
I've added the 20th upvote, go ahead and share the source
2
u/Old_County5271 Feb 19 '26
You have heard of Lua static and terra