cache crab

Becoming a Zigster

I heard that Andrew Kelley got so fed up of malloc that he went and made Zig. I don't know if that's true or a rumour, would be quick to settle, but I choose to believe that because that is exactly what made me start designing my own language. Procrastinating and doing Ziglings out of curiosity made me realise I don't need to design a language - this language is it.

Zig was mentioned to me in the past, especially when I was in my Rust era, but I didn't look into it as it was before 1.0. That spooked me because I didn't want to spend time learning features that could be scrapped - look at old Rust and how it used sigils, green threads, GC - unrecognisable to modern Rust; I was scared Zig would do something similar. Man how I was wrong.

Ziglings

I decided to learn Zig on a whim. I was fond of Rustlings before, so I decided to do Ziglings. No further thought was put into it, I just cloned the repo, installed the Zig extension in the Zed editor, forgot to install the LSP, and began zigging it up.

Learning Zig with Ziglings was easy. It actually scares me how easy it was to grasp and pick up. Remember how I was designing my own language? Zig made the same decisions but executes it beautifully with a better syntax and import system. The choices are reasonable, practical, and very much adhere to the language's goal of empowering us to maintain robust, optimal and reusable software. I've never been hooked on a language this quickly before - it makes me feel like a wizard! Huge huge thanks to @ludwigABAP for setting me on this path.

I finished Ziglings in a day, taking only a few breaks in between sessions that lasted hours, and began working on my first Zig project - a brainfuck interpreter.

Brainfuck

View this project on Github

Not long ago I wanted to experiment with a different brainfuck instruction representation and its effect on performance. Wrote it in C, and, ofcourse, it hanged somewhere (yes, I know, skill issue). Literally gave up and didn't try to debug it. I chose to build it from scratch in Zig, and this time I used unit tests. Holy fucking shit. I love unit tests so much now. It's saved me so much pain and the frequent tests passing reward kept me motivated to finish the project. TDD = ADHD cure?

While writing tests I decided to experiment with the comptime feature, which reduced the lines of code needed significantly. comptime is awesome, I view it as a metaprogramming feature with no headaches and minimal syntax - I would never try to do anything advanced with C++ templates but I'm always tempted to see how far I can push Zig's comptime - more on this later.

For this project I decided to use the LSP but it didn't work a significant portion of the time so I also switched it off when I didn't want any distractions. I was doing TDD so why not bundle syntax errors into the same verification cycle?

Because I was doing a more advanced representation instead of just interpreting, I needed to make a parser. This was also partly why I chose this project - I'm no stranger to making parsers and making one would be great to learn new control flow patterns that don't exist in C. I absolutely love love orelse, catch and try. At first glance I thought these features were similar to Rust's Monads but turns out they're much better - what comes after orelse or catch aren't closures, they're actual control flow blocks so you can break from a loop or return from the function, all from the same line. It's been optimised for developer wizardry and shit eating grins while coding.

Overall, I'm very happy with the zig experience and how this project turned out. I still haven't touched build.zig - I will learn the build system the first time I want to use a dependency.

I'm still learning Zig and so any feedback on the code would be greatly appreciated! You can take a look at the code here.

My Next Project

I don't know yet what my next project will be (have a few ideas but need the right moment to execute effectively), and maybe this counts, but for now I'm going to be experimenting with comptime. A lot. I'm thinking of implementing something equivalent to TinyGrad but in Zig, where the memory layouts and automatic differentiation are implemented at comp time (@compileError() for shape mismatch!) and it should compile down to no-copy SIMD code that can run on embedded platforms. It will be a lot of experimentation and not much building for now, but I think I will learn an incredible amount from it - especially because I haven't implemented auto-diff before.

I'm also going to do my best to start streaming ASAP. Maybe you'll catch me experimenting on this there, maybe you'll be a contributor.

- @cachecrab