pnut 🥜
Well, there wasn’t going to be a Drop today, but I just had to blast this insanely cool project out to y’all.
pnut

Pnut is the brainchild of some clever folks who thought, “Hey, why not compile C to shell?” It takes your C99 code and spits out shell scripts that even your Bash-phobic colleague can (sort of) read. It’s not magic, but it’s pretty close.
My brain is quite literally broken right now because of it.
Why would anyone want this????
Good question!
Portable shell scripts in C-style can let us write code once in C and run it anywhere with just a POSIX shell, eliminating the frustrations of shell compatibility issues. Pnut supports bootstrapping, making it possible to build a compiler that can even compile itself, providing a robust foundation for your development needs.
For C developers who dread traditional shell scripting, Pnut serves as a cheat code, enabling you to avoid the complexities, nuances, and often-times frustrating syntax of shell scripting while still generating functional scripts. The output generated by Pnut is (mostly) readable, which makes it easy to understand without the need for a decoder ring.
Pnut-generated scripts also integrate smoothly with existing scripts as they’re, well, just plain POXSIX shell scripts.
There are plenty of examples in the repo (the README is great, too).
Because it can compile itself (I’m still wrapping my head around that, as I’m still stuck on the “compiles to POSIX shell” bit), you can start with pnut.sh, and — theoretically — bootstrap your way up to clang.
Of course, Pnut isn’t all guns and roses. It def has some quirks:
- floating-point numbers? Unsigned integers?
#nope gotoandswitchfallthrough?#nope- local variables?
#nope
Also, the preprocessor sometimes loses its way on complex tasks, and aggregate types (e.g., arrays and structs) can be problematic.
Even if you do not choose to rely on it in “production”, it’s still a bonkers cool tool that may be a nice diversion.
FIN
Remember, you can follow and interact with the full text of The Daily Drop’s free posts on Mastodon via @dailydrop.hrbrmstr.dev@dailydrop.hrbrmstr.dev ☮️
Leave a comment