Daily Forth Lesson. Edition 0.

Mitch Bradley wmb at firmworks.com
Wed Dec 20 17:28:23 EST 2006


This is the first in what I plan as a continuing series of lessons about 
Forth and Open Firmware.  Each lesson will be very brief.

Lesson 0:

Point 1: Why Bother?

Forth is weird compared to most popular computer languages.  Until you 
learn how, it is hard to read because it is not based on the syntax of 
algebraic expressions.

But it is worth learning because a running Forth system gives you a 
extraordinary degree of low-level control over the system. Unlike most 
other programming environments that put up walls to hide or block access 
to "unauthorized" things, Forth makes it easy to get at anything, at any 
level from low to high.

Point 2: Forth syntax.

Here is syntactically-valid line of Forth code:

this is a test 123 456

Don't try to guess what it does; in fact it doesn't necessarily actually 
work, because some of the symbols might not be defined.  But it is 
syntactically valid.  It consists of 6 words, "this" "is" "a" "test" 
"123" "456".  Words are separated by white space - spaces, tabs, and 
newlines.  In most cases, spaces and newlines are the same.

Another syntactically valid line:

asdf  foo  jello  @W#$%^,T/%$  1a2qw2   gibbet

That's 5 words.  One of them is pretty strange, consisting mostly of 
punctuation, but it is a word nevertheless.  Any string of printing 
characters is a word.  (Most Forth implementations limit valid word 
names to 31 or fewer characters.)

Point 3: Left to right execution

The Forth interpreter is very simple.  It parses the next word (i.e. it 
skips whitespace, then collects characters until it sees another 
whitespace character) and executes it.

That is it in a nutshell.  So if you are trying to understand a Forth 
program in detail, you have to look at each word in turn and work out 
what it does.  That sounds simple, but it will trip you up if you insist 
on looking for algebra.  Just go left to right, one word at a time.

With practice, you will learn enough of the Forth vocabulary (the 
meanings of standard words) so that you can see what is going on at a 
glance, without having to puzzle out each individual word.  It is just 
like learning to read - it is tedious until you get the basic vocabulary 
down, then it is easy.

Thus endeth the lesson.




More information about the Devel mailing list