Forth Lesson 0: Difference between revisions

From OLPC
Jump to navigation Jump to search
m (Reverted edits by 199.245.188.252 (Talk); changed back to last version by RafaelOrtiz)
(minor edit for grammar)
 
(One intermediate revision by one other user not shown)
Line 21: Line 21:
asdf foo jello @W#$%^,T/%$ 1a2qw2 gibbet
asdf foo jello @W#$%^,T/%$ 1a2qw2 gibbet


That's 6 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.)
That's 6 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, though most Forth implementations limit valid word names to 31 or fewer characters.


== Left to right execution ==
== Left to right execution ==

Latest revision as of 16:22, 25 June 2008

Mitch Bradley's Forth and
Open Firmware Lessons:

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.

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 an 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.

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 6 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, though most Forth implementations limit valid word names to 31 or fewer characters.

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.

Next Lesson