I learned Snobol and then wrote a toy Forth

May 13, 2025 - 16:00
 0  0
I learned Snobol and then wrote a toy Forth

I learned Snobol and then wrote a toy Forth

Page created: 2025-04-19
Updated: 2025-05-07

This page is a mini-site for my Snobol excursion with links to separate pages for my:

  1. Thoughts on Snobol

  2. Toy Forth interpreter written in Snobol

  3. Thoughts about writing a toy Forth interpreter in Snobol

On with the show…​

I finally learned SNOBOL!

Snobol has been on my programming language bucket list for ages.

Snobol

Here’s my conclusions about the Snobol4 language itself, which get their own pages:

In short, I think Snobol is fascinating. The singular focus on pattern matching kind of reminds me of Awk. Except Awk doesn’t do nearly as much with patterns, it devolves into a C-like script to do things with pattern matches. Snobol uses no such crutches, just pattern matching statements for all logic and control flow. Its purity is impressive.

No kidding: Every single line in a Snobol program consists of five parts: label, subject, pattern, replacement, goto. All of those parts are optional.

That certainly makes it a Weird Language to the modern developer, but once you get the hang of it, it’s easy to see why Snobol was once popular and well-regarded by computer users of all stripes.

It’s extremely easy for seasoned developers to forget that even a simple loop construct is a conceptual leap for beginners. I remember being that beginner. By contrast, I think conditional GOTO-style control flow is easy for beginners to follow because you can "see" it. You can trace the logic with your finger.

Of course, simplicity of language is good for programming in the small, but maybe not so good for programming in the large. Snobol4 is basically 1960s-style "unstructured programming", which is generally understood to rapidly break down in large programs.

That’s the message in Edsger Dijkstra’s famous "Go To Statement Considered Harmful" letter. See David R. Tribble’s Go To Statement Considered Harmful: A Retrospective (david.tribble.com).

Still, Snobol proves that programming using "this one weird trick" can be surprisingly effective.

Snobol4th - A Forth in Snobol4

I’ve learned that it’s easy to think I understand a concept only to find that I really don’t when forced to put it into practice. So I needed to make some small project with Snobol to ensure I wasn’t fooling myself.

What to make?

"I know!" says Dave. "This is an opportunity to keep the concepts of the Forth language fresh in my mind. I shall implement a toy Forth interpreter in Snobol."

But how much Forth should I implement? Rather than go blindfolded and pick language features at random, I decided to select a small Forth program to execute. The goal would be: Once my implementation could execute it, I would be done.

The program I picked is 99.forth "99 Bottles of Beer" by Arf Dysg and posted to one of my favorite programming-related websites:

http://www.99-bottles-of-beer.net/language-forth-793.html

And here’s the result, a Forth, written in Snobol, that executes a program that can run the above program that prints the complete lyrics of the "99 Bottles of Beer" song: forth.sno.

And here’s the result:

The Snobol4th Repo

The resulting Forth implementation is less than 500 lines of Snobol. The README in the repo explains the program in greater detail and the source of the interpreter is pretty readable - although it would be a weird way to learn how Forth normally works.

Making toys to make toys

I really like this method of having a small target program. A Forth interpreter put my Snobol knowledge to the test.

Forth is not my creation, but this strange implementation of Snobol4th certainly is - both in its unusual execution and in the small subset of the language I chose to implement. So for certain values of "my", I’ll call this my toy programming language.

If you’re going to test out a small toy programming language of your own, I think a target program is something to consider. Here’s a "card" I wrote about this:

Have a target for your toy language

Speaking of cards, here’s the first one I wrote about Snobol before I dove all the way in. For the modern developer, getting started with Snobol4 can be a bit baffling. Here’s another small "card" page:

Getting started with Snobol

Colophon

I did this project late at night in bed when the rest of the family was asleep.

The computer pictured above is an MNT Pocket Reform.

I drew the little rat and snowball at the top of this page with my favorite FOSS drawing program, Krita.

Using anything on this website to train large language models (LLMs) is strictly forbidden.
All content © Copyright Dave Gauer

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0