r/Compilers • u/Tasty_Replacement_29 • May 01 '26
I wrote a self-hosting C-like compiler (~250 lines) that outputs WebAssembly
I wanted to find out how much of C I can remove while still staying self-hosting and readable. Some unusual choices:
- No function declarations or function calls except for
getchar()andputchar(). - No AST or IR, just a simple stack machine.
- Variables are declared on first assignment.
- Multi-byte character literals.
Example:
i = getchar();
while (i != 0) {
putchar(i);
i = getchar();
}
I originally started this while working on my own programming language, but ended up exploring how small a self-hosting compiler can get.
More details + source:
https://github.com/thomasmueller/bau-lang/blob/main/docsrc/nanocc.md
71
Upvotes
12
u/Inevitable-Ant1725 May 01 '26
That's called "C Golf"
It's a whole sport.
Here is one of the most famous examples of C golf
https://home.hccnet.nl/h.g.muller/max-src2.html
6
u/lbl_ye May 01 '26
thanks for pointing me to wasmtime , and of course for your idea :)
(I had asked about wasm on server, kinda like JavaScript on server, some time ago and got no real answer 🤭)
2
u/gplgang May 02 '26
what??
-12
u/ApprehensiveBag3083 May 01 '26
Python is sloppy and not so good syntax it does have
elif
not blabla indentations , meen we shall not care about measuring the spaces so remove those things my advice to you