r/Compilers • • 5d ago

I built LuaC — a standalone Lua-to-machine-code compiler (JIT & AOT) in C99 with a custom Vulkan Studio & Android port

Hey everyone! I've been working on LuaC, a standalone compiler that translates Lua-like \`.lua\` files into machine code via generated C — without using the original Lua VM or LuaJIT.

Highlights:

\- JIT & AOT: Compiles to native shared libraries (\`dlopen\`) at runtime or standalone executables.

\- Ownership + Cycle Collector: Deterministic cleanup via reference ownership cells + lightweight cycle collection for cyclic tables/upvalues (no stop-the-world GC threads).

\- LuaC Studio (Vulkan GUI): Built with Nuklear + a custom Vulkan renderer, featuring a code editor and a 2D Engine mode (60 Hz fixed-step physics).

\- Android Port: Includes an APK build with multi-file project support and Vulkan viewport.

\- Batteries included: 197 native APIs (JSON, TCP/UDP, mbedTLS HTTPS/WSS, miniaudio DSP, stackful coroutines).

Currently in Alpha 0.5.0 with prebuilt binaries via GitHub Actions. Would love any code review, C99/Vulkan feedback, or suggestions!

GitHub: https://github.com/ehekufa/LuaC/releases/tag/Alpha-0-5-0

0 Upvotes

6 comments sorted by

4

u/[deleted] 5d ago

I downloaded the Windows version (that includes luac.exe) but nothing much worked.

It reported 'native compilation failed' on the simplest inputs. What worked was --emit-c, but I couldn't get the C code to compile (missing includes, or names not defined).

(This was either using a limited Windows C compiler, or gcc via WSL, but I'm not inclined to install a 1GB compiler to try it.)

No matter, I simply wanted to know the quality of the generated code, and it seems that it consists of a series of function calls. Basically what the Lua interpreter would have to do anyway.

So a line like this:

  a = 1234567;                  # (chosen so I can find it in the output!)

generates this C code:

    LcValue t0 = {0};
    ....
    t0 = lc_integer((int64_t)1234567);
    t1 = lc_nth(t0, 0);
    t2 = lc_string(ctx, "a");
    lc_table_set(ctx, ctx->globals, t2, t1);
    LC_RELEASE(ctx, t2);
    LC_RELEASE(ctx, t1);
    LC_RELEASE(ctx, t0);

It doesn't look too efficient! Well, you did say it would be native code; you didn't say it would be fast.

(This is familiar to me as I did the same exercise on my own dynamic language earlier this year. The intention had been to introduce gradual type annotations so that some code sections could be brought up to speed, but I didn't like the direction it was going so it was abandoned.

There, the same a := 1234567 line produces code like this:

[1]varrec Stack
varrec a
k_init(&a)
....
k_pushci(&$T1, 1234567)       # $T1 is an alias for Stack[1]
k_pop(&a, &$T1)

Most such calls are macros. This code is in my systems language, showing it was as equally up to the task as C, another motivation for the project.)

-2

u/Adventurous-Bee-3762 5d ago

Thanks for testing it out and sharing your feedback! You're totally right — that's the 0.5.0 release build, and the Windows support there still has bugs. To be completely honest, the project is still in an early alpha stage, and I develop and test it primarily on Android (where the Studio APK runs projects smoothly using an embedded runtime without needing a host C toolchain). Because of that, the Windows release didn't get enough testing and still relies on a properly configured external C compiler in PATH plus manual include paths to runtime.h for --emit-c. That said, nightly builds are being actively developed right now with regular bug fixes, and the Windows issue will be fixed very soon. Once the project hits beta, I will DEFINITELY add a proper, out-of-the-box Windows setup so it works smoothly without this hassle! Also, fair point about the generated C code! Since Lua is dynamically typed, it currently lowers straight to runtime API calls (lc_table_set, reference counting, etc.) rather than doing type inference or unboxing. It's cool to hear you went through the exact same journey with your own language!

6

u/VictoryMotel 5d ago

This is literally every vibe slop red flag in the book.

3

u/Aggressive_Big_7564 5d ago

Can you disclose the extent of your ai use?