Forth: A Weird Programming Language. Here’s Why You Might Love It Anyway.
Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
Forth is an unusual language. It is primarily an interpreter, and has a command-line interface so that it can be used in real time. But it also has the ability to take sets of code and compile it. So it is something of a hybrid.
Because of this, Forth is commonly used on hardware that doesn’t have a traditional operating system. It is also widely used to control machinery, and has been used in space exploration — even on Philae, the probe that landed on comet 67P/Churyumov–Gerasimenko.
Forth was developed by Charles Moore starting in 1968 while doing graphics programming at Mohasco Industries. But the first complete Forth implementation didn’t come until 1971, when Moore created a standalone system for controlling the 11-meter telescope at the US National Radio Astronomy Observatory (NRAO).
Earlier versions of Forth had caught the attention of the programming community. But at this point, it became very popular in the astronomy community. So in 1973, Moore founded FORTH, Inc with his NRAO colleagues Elizabeth Rather and Edward Conklin.
The company ported the program to many different systems. And FORTH, Inc continues to be an important company, providing Forth related products to people in business, academia, and government.
Overview of Forth
There are a number of unique aspects to the Forth programming language. We’ll discuss the most important ones here.
Forth offers many unique features:
Creation of compiled extensions of the language called “words”
Stacks and Reverse Polish Notation
Direct connection between user and hardware.
Reverse Polish Notation
Forth does mathematical calculations using Reverse Polish Notation. This depends upon storing numbers in a stack.
Consider the calculation:
2 + 3. In Forth, this would be done by placing the 2 onto the stack, then placing 3 onto the stack, and then adding them. This would look something like this:
2 3 +.
It seems a little weird, but it is easier for the computer to parse, because this is the way computers actually do calculations. And once you get used to it as a coder, it becomes quite natural.
In addition, there is no need for parentheses. Consider the following two examples. (Note: the backslash character starts a comment.)
2 3 + 5 * This is the same as (2 + 3) * 5 2 3 5 * + This is the same as 2 + 3 * 5
In the first example, 2 is pushed on the stack followed by 3. So 2 is second in the stack and 3 is first. The “+” operator is applied to the first two elements of the stack. This removes these two items from the stack, and leaves 5 (2 + 3) on top of the stack.
Then we push 5 onto the stack. That puts a 5 in the first and second positions of the stack. Finally, we apply the “*” operator, which results in 25 (5 * 5) being placed on the top of the stack.
In the second example, we place 2, 3, and 5 on the stack, so that 5 is in the first position, 3 in the second, and 2 in the third. Then we apply the “*” operator on the top two elements of the stack (5 * 3) resulting in 15 being placed on the top of the stack.
At this point, 15 is in the first position and 2 is in the second position. When the + operator is applied, it leaves 17 (15 + 2) on the top of the stack.
The following program is very simple, but it demonstrates a lot of what goes on in the language.
: OUTMATH Output a mathematical calculation ." We will now calculate: (2 + 3) * 5" CR 2 3 + 5 * ." This equals: " . CR ; OUTMATH
The first thing this program does is define a function named
OUTMATH. The lines that follow it are part of the function up through the line that ends with a semicolon. The first line prints out the text, “We will now calculate: (2 + 3) * 5” and follows it with a carriage return.
The next line performs the calculation we discussed above, resulting in 25 being placed on the top of the stack. The last line of the function outputs, “This equals: ” followed by the value on the top of the stack (represented by “.”) and a carriage return.
Then we simply run the function. So when the program runs, it outputs:
We will now calculate: (2 + 3) * 5 This equals: 25
For most programmers, this is a new way of thinking. But it’s surprising how natural it seems once you start working with it. Here are some tutorials and books that will get you working with the language.
There are lots of online tutorials. Because Forth is strange to non-Forth programmers at the same time that it is self-evident to current users, many of the tutorials gloss over things that can be confusing for beginners. These tutorials don’t have this problem.
Easy Forth: this short Ebook is an excellent introduction. It’s only drawback is that it isn’t big on examples. So check out some of the examples below.
A Beginner’s Guide to Forth: this is another good introduction. It’s a bit more technical, but has more examples.
And So Forth…: this is a very thorough introduction to Forth — almost as good as a book.
It’s often easier to learn a new language by getting a book that can take you step by step through the process. There are a lot of good books about Forth — especially dating back to the 1980s, when Forth really took off. Here is a collection of the best and most important ones.
Discover Forth: Learning and Programming the Forth Language (1982) by Thom Hogan: this is a good and concise introduction to the Forth language.
Starting Forth: An Introduction to the Forth Language and Operating System for Beginners and Professionals (1982) by Leo Brodie: this is a classic and a great way to learn Forth. It is out of print but used copies are not hard to find for little money. It is also online for free. Advanced Forth programmers might want to check out Brodie’s more recent, Thinking Forth.
Forth Lite Tutorial (2014) by Juergen Pintaske: this is a Kindle based tutorial that focuses on examples.
Programming a Problem Oriented Language: Forth — How the Internals Work (2014) by Charles Moore: this is a Kindle version of the book Forth’s creator wrote long ago. It still provides keen insights into the language, but isn’t the best choice to start with.
Forth Programmer’s Handbook (2007) by Elizabeth Rather and Edward Conklin: this is a more advanced book on Forth by the other two Forth pioneers. See also Rather’s book, Forth Application Techniques (2006).
Forth Interest Group: this site has all kinds of interesting information including old historical documents. Unfortunately, it also has a lot of broken links. But it is still worth checking out.
Links to Forth Programs: this is a great collection of real example programs like a spreadsheet and a Sudoku puzzle solver.
Forth Google Group: this is a very active group of Forth users who ask and answer questions.
Forth Subreddit: this is not a terribly active forum, but there are still knowledgeable people on it.
There have been a lot of implementations of Forth over the years. The Forth Interest Group has a long Forth Compilers Page. So if you happen to have a PDP-11 setting around in your garage, you can get a Forth compiler for it! But for more modern systems, here are the more common options.
In the mid-1980s, Dietrich Weineck, Georg Rehfeld, and Klaus Schleisiek developed volksFORTH for 16-bit machines like the 8086 IBM PC and the 6502 Apple, Commodore, and Atari machines.
Eventually, they created a 32-bit version for the (16-bit bus) 68000-based Atari ST. Even though volksFORTH was making progress, the project pretty much died (although it was later restarted).
Bernd Paysan was sufficiently interested in this version of volksFORTH (called turboFORTH by then), however, that he made it the basis of bigFORTH.
Its first version was released for the Atari ST in 1990. By 1995, bigFORTH had been ported to the 386, and by the following year was available on Windows NT, OS/2, and Linux.
At that point, Paysan began work on MINOS. (Technically, it is “MINOS,” but it is normally referred to as simply “MINOS.”)
It is a graphical interface for bigFORTH. It might be easiest to think of it as “Visual bigFORTH” like Visual Basic, because that’s more or less what it is. It was first created for the X-Windows system. But it was soon available for Windows as well.
Development on both bigFORTH and MINOS seem to have stopped around 2010. However, they are both easily available and quite useful (bigFORTH is ANSI compliant). In fact, MINOS is a lot of fun to play with.
If you want to use bigFORTH or even develop for it, you’ll have all the tools you need:
Source Code: this is the code from the last official release on March 22, 2010, Version 2.4.
Debian Package: an easily installed packaged for any Debian-based Linux distribution (eg, Ubuntu, Mint). It installs in /usr/local/bin, and includes MINOS, although it is called xbigforth.
Windows Self-Installing Executable: the same as the Debian package, but for Windows. MINOS is even named xbigforth.exe.
Documentation (PDF): this nearly 300 page book acts as a tutorial and reference, written by JL Bezemer. Much of it is on generic Forth and is what “And So Forth…” is taken from.
bigFORTH Repository: the ultimate destination for all your bigFORTH needs.
Gforth is the GNU implementation of Forth. It has a distinguished pedigree.
It was a combination of bigFORTH and the much older fig-FORTH (see below). But the history of it is twisted, since bigFORTH came from volksFORTH, which was based on fig-FORTH.
Being a GNU project, a great deal of work has been done on it since it started. It is now completely ANSI compliant and has achieved all of its original goals. The most recent version is 0.7.3, with the last entry (by Paysan) on February 25, 2013.
In terms of free Forth implementations, Gforth is the most commonly used. Part of that is just because it is part of the GNU project and its availability. But it’s also because it’s powerful and fast.
Gforth has all the resources you need to put it to whatever use you need:
Source Code: this page provides the Gforth source code from version 0.2.1 up to the current, 0.7.3.
Debian Packages: there are various packages available for the Debian Linux distribution. Note that depending upon the package you choose, you may need to install one or more other packages first.
Windows Self-Installing Executable: this is an earlier (0.7.0) version of Gforth, but a quick way to get started.
Gforth Repository: pretty much everything related to Gforth can be found here.
pbForth does not appear to be supported anymore, but it’s such an interesting implementation of Forth that it is well worth checking out.
As we’ve discussed, Forth has always been associated with hardware control. So it wasn’t at all surprising that a Forth implementation was created for the LEGO MINDSTORMS robotics kit — specifically, the 16-bit RCX that came out in 1998.
It seems never to have been updated to work with the second generation, NXT series, which came out in 2006, nor the current MV3, which came out in 2013.
Although there is no official site for pbForth, it’s still possible to learn about it and even use it. However, there may be a steep learning curve, because there isn’t that much information. And clearly, you would need a LEGO MINDSTORMS RCX (they aren’t that hard to find used).
The NXT uses a completely different (and 32-bit) processor, so pbForth would certainly not work with it. Still, it might be fun. And if you are up to it, you could conceivably port it to second and third generation kits.
What information is available on the normal internet is generally terse and always links to the pbForth Home Page, which has not been in operation since at least 2010. It is only thanks to the Internet Archive that much of this is available.
History of LEGO Robotics: although not about pbForth, it provides a quick overview of LEGO MINDSTORMS products.
Introduction to pbForth (PDF): this 30 page tutorial will get you started with the language.
phForth Source Code: note that pbForth is written in assembly language and Tcl, so you will need to have those tools available.
Windows GUI: this file runs, although getting it to do anything may be more complicated. If you want pbForth for Linux or Mac, you will have to use the source code.
phForth Home Page Circa 2009: this page provides links to quite a bit of interesting information about pbForth, most notably a half dozen sample scripts.
fig-FORTH: this was one of the earliest Forth implementations, created by the Forth Interest Group. It is a 16-bit implementation. It is available for some wonderful old computers like the Data General Eclipse and the Alpha Micro, as well as 8086- and 6502-based computers. The problem is that there are no executable files — just assembly language code. And in most cases, only bitmap PDF files of the code. Actual code seems only available for 8088/8086, PDP-11, and 6800. (The 6800 is an 8-bit processor, so apparently, there was an 8-bit version of fig-FORTH.)
SP-Forth: this is a free Forth implementation for Windows and Linux.
4tH: this is an easy to use Forth implementation that is available for a large range of hardware (including the Raspberry Pi), and includes good documentation.
SwiftForth™: this is the Forth compiler from the original company that brought Forth to the world. It is a commercial product, but it is reasonably priced.
Forth is a fascinating and incredibly useful programming language. That’s especially true if you want to control hardware. Using the resources here, you should be on your way.