A Forth vocabulary for iteration
I recently wrote a small 16-bit Forth for 8086 PCs running DOS. I built the basic one-liner loop words that can trivially be built with just “branch if zero” and “goto”: begin
, while
, repeat
, until
, again
. But I held off on implementing do
/ loop
at first.
It didn't seem like too much of a hardship. In a previous Forth I'd built, I'd implemented do
/ loop
using the return stack, but it was... ugly. The code to implement it was ugly, the code it generated was ugly (and large!), and I didn't find a lot of places where it was actually much nicer to use than explicit begin
-based loops. I was able to implement an 8086 assembler and a Minesweeper game without bothering to build do
/ loop
. I didn't really miss it, but I had a design percolating in the back of my mind that I wanted to try.
At some point I came across some writing that suggested that Forth had a “loop control stack”. Wouldn't it be nice if I could implement some kind of loop control stack that worked for all kinds of iteration?
The thing I built has blown me away with how flexible, composable, and useful it's turned out to be. It's way more powerful than I was expecting. And the code that leverages it is inevitably much simpler and easier to read.