onion: Stack language compiled to lua
17 points by veqq
17 points by veqq
Why compile to Lua instead of a bytecode interpreter written in Lua?
Because compiling to lua results in a much faster code, and I want to use Onion in games. A longer term goal that I got side-tracked from was to get Onion added to TIC-80 as a Forth-like language for it, because having -a- Forthlike for TIC-80 seems like it’d be a really good fit.
Onion’s specific strategy of tracking stack effects and allocating local variables for them also is markedly faster in performance than strategies like using a global stack (like Equinox does), at least when running in Lua. Onion, for all intents and purposes, is basically the same speed as Lua. I don’t blame folks for -not- going this direction, though, building a stack effect checker and compiler took a decent amount of effort to get right
It -does- come with some costs, mostly around needing certain stack effects in some places. Also, Onion is unforthlike in that you can’t write immediate words at the moment.
Hi, I made this last year while I was unemployed. Happy to answer any questions!
That’s pretty cool, with a neat solution for calling lua functions while succinctly specifying their stack effects.
- Currently: Onion compiler in lua
- TODO: Onion compiler in Onion
Intuitively I don’t like the idea of self hosted languages, even though languages (and language creators) I admire choose to do it. It seems kind of risky, like abandoning a real reliable implementation, and putting oneself in a precarious spot going forward.
I readily admit I’m a fool and the folks who do this are very smart. But if anyone wants to explain to me why it’s a good idea, please set me straight here.
Nobody forces you to abandon the maintenance of a bootstrap compiler once you have a self-hosting compiler working, and maintaining two implementations of a spec in parallel is often easier than it sounds.
Onion creator here! The reason I wanted to self host Onion is that it lets me retarget Onion to languages like Javascript, and maybe even GDScript without having to rewrite the entire compiler in either language. Right now, I’m not actively developing Onion (I have a different project I’m pretty excited about), however, so that’s kinda on hold.
There’s prior art in Compile-to-Lua languages self hosting, Fennel has been self hosted for a while now. Also, the compilation output of Onion is decently readable as far as such things go, which means that verifying output of various tests is decently straightforward.
It gives you a real-life (if admittedly, tautologically, contrived) use case for the language that already has a ground truth implementation for comparison (not just for looking for bugs, but comparing performance and output code size, like with zig and C++). If the compiler compiled with the compiler can produce identical outputs, you can be reasonably sure the pieces exercised work well. And it forces you to flesh out a bunch of standard library functions.
On top of what people have said, you invent a language because you like to use it, because it has something you like, probably not found in other languages. It’s rather pointless to having to maintain a compiler in another language, and not being able to take advantage (and pleasure) of your own invention.
Of course this only applies to general purpose languages.