FORTH

From OLPC
Revision as of 23:45, 21 October 2006 by Mokurai (talk | contribs) (New article)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

FORTH is a terse, stack-oriented, interpreted/semi-compiled language using reverse Polish notation, like Hewlett-Packard calculators. Identifiers, called words, are composed of any sequence of printing characters. Words are separated by any whitespace characters, including space, tab, and new line. The word . prints the item on top of the stack.

For example, the line

2 3 + .

starts by putting 2 on the stack, leaving 3 + . to interpret.

The next step is to put 3 on the stack, giving 3 2, with + . left to interpret.

Then + adds the two top elements of the stack and puts the result back, so that the stack contains just 5, and only . is left.

Finally, . removes the 5 from the stack and prints it

5 OK

The OK signifies that the line is finished, and FORTH is ready for the next command.

The design goals for FORTH included minimum program size; readability by experienced users, though not always by novices; and extremely high productivity.

We do not have room here to explain the language properly. See references below.

Free Software versions of FORTH include gforth, kforth, pforth, and yforth. Source code is of course available for each of these.

FORTH for education

FORTH is one of the best programming languages for learning how to build languages. The FORTH kernel was originally about 8K of highly modular code. Students who know an assembly language can read and understand the code for FORTH for that processor in a few weeks. The rest of the FORTH system is written in highly modular FORTH.

Learning FORTH internals is almost the same as learning Virtual Machine architecture. The primary technical difference is that VMs use byte codes (token-threaded code), while compiled FORTH consists mainly of sequences of addresses of subroutines (address-threaded code). Token-threaded code is shorter, but takes longer to interpret than address-threaded code.

External links

Forth Interest Group

Wikipedia: FORTH

Forth, a programming language for real programmers

FORTH tutorials