Onion: A Stack-Based Language Emerges for the Lua Ecosystem
Share this article
Source: yumaikas/onion on GitHub
Stack-based programming languages like Forth have long fascinated developers with their minimalist elegance and conceptual purity. Now, a new contender named Onion is bringing this paradigm to Lua's versatile ecosystem. Designed as a Forth-like language with deliberate concessions for Lua compatibility, Onion introduces a clever innovation: a "subject stack" that enables fluid object interactions within its stack-oriented architecture.
The Stack Reimagined for Lua
At its core, Onion operates like traditional stack languages—data flows through push/pop operations using postfix notation (e.g., 3 5 + instead of 3+5). But its true differentiation lies in the subject stack, which allows natural object method chaining akin to object.method().anotherMethod() in OOP languages. This hybrid approach maintains stack-based efficiency while accommodating Lua's table-based object model.
Practical Execution
Onion targets LuaJIT or Lua 5.2+, compiling down to Lua 5.1-compatible output (without polyfills). Developers can quickly experiment via CLI:
# Compile Onion to Lua
lua cli.lua --compile demo.fth out.lua
# Execute directly
lua cli.lua --exec demo.fth
The toolkit includes diagnostics like --lex for token inspection and --comptest for output preview, easing the learning curve. For newcomers, the Onion in Y minutes guide and Quick Reference provide rapid onboarding.
Why This Matters
Stack languages excel in embedded systems and DSLs but often require sacrificing ecosystem support. By compiling to Lua—a language embedded everywhere from game engines to network routers—Onion offers stack semantics without isolation. The subject stack elegantly addresses a key friction point: object manipulation. As creator yumaikas notes, it enables "a very pleasant stack-based version of object interaction," merging paradigms rather than forcing a choice.
The Self-Hosting Horizon
The project's roadmap reveals ambitious maturity goals: while the compiler is currently written in Lua, plans call for self-hosting—rewriting the compiler in Onion itself. This milestone would validate the language's robustness and serve as the ultimate stress test.
A New Flavor for Lua Developers
For Lua veterans, Onion offers fresh syntactic possibilities and mental models. For stack-language enthusiasts, it provides immediate access to Lua's rich libraries and deployment environments. As both communities explore this fusion, Onion could inspire new approaches to balancing expressiveness with pragmatism—proving that even established ecosystems have room for radical reinterpretation.