Implementing a Forth

3 days ago 3

Page created: 2025-05-28

Updated: 2025-05-30

drawing of chuck using a DEC PDP-11 computer by dave gauer in krita

I’ve made a couple Forths. Not a single one of them are "finished software" intended for industrial use, but all of them taught me something about Forth, concatenative programming, and how computers work in general.

Sure, I’ve done my time in that place where points are free and the stacks always need twiddling. But what about you?

You’re thinking about making a Forth yourself, aren’t you? Of course you are. The call is irresistable. I know that.

The only thing holding you back is uncertaintly.

You’re thinking, where do I start? How much Forth should I make? How do I know when I’m done?

(Here, all of it, and you don’t.)

But here are three things to consider.

1. Consider porting an existing Forth

The first Forth I made was a port of JONESFORTH by Richard WM Jones. I found it through…​

…​which lead me to…​

my gold on gray logo for nasmjf

The beauty of many Forths is that they contain a core Forth written in a host language (or machine code) and then the rest of the language features are implemented in…​Forth!

Knowing when I had completed my port was easy - I was done when my ported assembly half of JonesForth could run the Forth half of JonesForth.

Here’s the Forth half of JonesForth: jonesforth.f

I think porting software is a pretty amazing way to learn about a program and especially a language implementation. Here’s a card: Porting Software. (At the moment, it’s somewhat redundant with the page you’re reading, but it will probably get a lot more interesting when I do my next port, whatever that ends up being. Hint: probably a Sed.)

2. Consider making an ultra-tiny Forth core

I think one of the most interesting questions in the Forth universe is how small can its core be?

Which is to say, how few words do you need to implement in a host language (or raw machine code, as we’ll see) before you can bootstrap the rest of the language in the Forth itself?

(The most popular one I’ve seen is SUBLEQ, which stands for "SUBtract, branch if Less than/EQual to zero". And since there’s only one, you typically omit it and your program is just the operands for the implied instruction. The "language", then, has no operations and feels to me like the computing equivalent of John Cage’s musical stunt 4'33" (wikipedia.org).)

So I guess a Forth with one word is all you need.

Therefore, it’s not a question of, "How few words would be possible?" but rather, "How few words would be interesting?"

The answer will be personal to you.

The Forth-eV Wiki’s page on "Minimal Word Set" is a great place to start:

In addition to the word set, you can also constrain the total file weight of your core Forth. (Or lines of source or some other arbitary limitation.)

Some really intriguing minimal Forths I’ve encountered:

PlanckForth

I love the hexdump visualization of Koichi Nakamura’s PlanckForth:

binary layout of planckforth as taken from the repo

You’re looking at a complete working Forth implementation in the form of a hand-written ELF binary in less than 1,000 bytes. The rest of PlanckForth is written in PlanckForth to implement a "normal" Forth.

SmithForth

David Smith’s SmithForth is another 1,000 byte, hand-written machine code Forth that bootstraps a more complete Forth.

"My goal with SmithForth is not to stop writing machine code early, but to start writing Forth early."

I love tiny Forths!

sectorforth

Cesar Blum’s sectorforth is even smaller, at 512 bytes (the size of a boot sector).

Like other ultra-tiny Forths, it starts with just a handful of Forth primitives with which to bootstrap higher-level constructs.

The included 01-helloworld.f example is 200 lines of well-commented Forth source which starts with making numbers, implements a core of standard Forth words, and then prints "Hello world".

I imagine this is how Ents talk.

"As soon as the whole company was assembled, standing in a wide circle round Treebeard, a curious and unintelligible conversation began." --JRR Tolkein, The Two Towers

milliForth

512 bytes too much for you? How about 336? By my count, fuzzballcat’s milliFORTH implements 11 words and it can do anything, just, you know, painfully. The "Hello World" is 53 lines of pain.

StoneKnifeForth

Not just a hand-written Forth binary, but also a metacircular compiler, Kragen Javier Sitaker’s StoneKnifeForth source comes to a total of 1,902 essential bytes.

"Surprisingly, the language that results is still almost bearable to write a compiler in, although it definitely has the flavor of an assembler."

Many Forths are also compilers and/or assemblers, but rare is the program that can compile its own source in less than 2Kb.

"Three Instruction" Forth

Frank Sergeant’s 3-instruction Forth for an early 1990s Mototorla chip is possibily the smallest of them all?

I love the description:

"How many instructions does it take to make a Forth for target development work? Does memory grow on trees? Does the cost of the development system come out of your own pocket? A 3- instruction Forth makes Forth affordable for target systems with very limited memory. It can be brought up quickly on strange new hardware. You don’t have to do without Forth because of memory or time limitations. It only takes 66 bytes for the Motorola MC68HC11. Full source is provided."

How can you not want to learn about that? I mean, 66 bytes. What?!

"If you do have extra RAM available on the system then you can use the 3-instruction Forth to build and test a full Forth, and then use it."

3. Consider a tiny target program

meow5 kitty logo is black with two bright teal eyes and a pink nose

My "extremely concatenative" Forth-like, Meow5 is literally named after the target program it was designed to run.

When Meow5 was done, it could run this program:

def meow "Meow!\n" print ; def meow5 meow meow meow meow meow ; meow5

(Yup, that’s it! Needless to say, the target program is not what makes Meow5 unique.)

Drawing of a little rat on a snowball with a heart drawn by Dave Gauer in Krita

For an even funnier example, my Forth written in the string manipulation language from the 1960s, SNOBOL4, was complete when it could run this "99 Bottles of Beer on the Wall" song generator program:

When Snobol4th was done, it could run this program:

( ********************************************************* 99 Bottles of Beer Example by Arf Dysg http://www.99-bottles-of-beer.net/language-forth-793.html Lightly modified by Dave Gauer: * +LOOP changed to -LOOP * Explicit SPACE added ********************************************************* ) : MANY ( n) ?DUP IF . ELSE ." No more " THEN ; ( the number) : BOTTLES ( n) ." bottle" 1 - IF ." s" THEN ; ( handle plural) : BEER ( n) CR DUP MANY BOTTLES SPACE ." of beer" ; : WALL SPACE ." on the wall" ; : DRINK CR ." Take one down and pass it around." ; : BUY CR ." Go to the store and buy some more." ; : ANOTHER ( n-n) ?DUP IF DRINK 1- ELSE BUY 99 THEN ; : VERSE ( n) DUP BEER WALL DUP BEER ANOTHER BEER WALL CR ; : VERSES ( n) 0 SWAP DO I VERSE 1 -LOOP ; 99 VERSES

Resources

If you’re going to implement a Forth, there are some really high-quality resources out there. Reading the source of JonesForth is a great place to start (there’s a complete copy in my NasmJF repo linked above).

Another one that everybody links to because it’s so good is Brad Rodriguez’s Moving Forth series from The Computer Journal magazine. Start with Part 1: https://www.bradrodriguez.com/papers/moving1.htm

Finally, the book Threaded Interpretive Languages by R.G. Loeliger.

cover image of TIL book

I’ve used "TIL" a couple times as reference for the definitions of core words. And it’s just a really cool little hardback book that feels good in the hand.

Happy hacking!

Read Entire Article