chapter four

4 Structuring programs, from REPL to webpage

 

This chapter covers

  • Compile-time parsing and processing
  • Patterns for debugging comptime code
  • Using errors to separate code concerns
  • Practical TCP and HTTP, and how to leverage them with the standard library
  • Ziggy patterns including iterators and generic interfaces

Word games are one of my favorite ways to relax, although I think I take them too seriously to do much relaxing. They are also a fun programming challenge. Words are messy. It feels like there are more exceptions to the rules than there are words that fit the rules. It’s even difficult to pin down which sequences of syllables are words and which are not. For logic-brained computer-heads like us, this will always be a challenge — and that’s also why it’s fun!

In this chapter, we’ll write a word ladder game. A word ladder is a series of words of the same length that each differ in one letter to the last. Given a randomized starting word, how long can you make your ladder?

We’ll start from a simple REPL, using idiomatic Zig to optimize both developer and user experience. Then we’ll see firsthand how modular and expressive Zig code makes it trivial to take our REPL and turn it into a full-fledged Web application!

4.1 Climbing the ladder

4.1.1 Thinking in comptime

4.1.2 Comptime caveats

4.1.3 Executing our plan

4.1.4 Talk to me

4.1.5 Core logic

4.1.6 Check it

4.2 Wilda’s want for words

4.2.1 Connecting the web

4.2.2 A poor conversationalist

4.2.3 How do I HTTP?

4.2.4 The HTTP cherry on top

4.2.5 Modularity makes it easy

4.3 Next steps

4.4 Summary