Search This Site Full Web

What is Forth?

The only problem we Forthers have is that each time someone tells us he has found something new, we answer: "Oh, I know three guys, none of them has a CS degree, and each one invented this independently 10 years ago, but they had a much better approach than you". He'll hate you for that.
Bernd Paysan

People have often dreamed of programming computers with a natural language such as English, but the problem of ambiguity remains insuperable. Forth, like English, is made up of words separated by spaces but it has a simpler syntax, made possible by the use of a stack. Unlike other computer languages there are no lists of parameters enclosed in brackets. Instead of saying 2+2 or +(2,2) we say 2 2 +. Each word stands on its own. When we say + the computer does not have to wait to discover what to add, because it is already there, on the stack. The noun always comes before the verb that acts on it, as is natural in English whenever urgent commands are issued.

Programming consists of defining new words, using those the computer already knows. Forth encourages short definitions which can be tested as soon as they are written. In that way, when a bug appears, you can be pretty sure it is in the last word you defined, not somewhere in several pages of code.

A trivial example:

The word . prints the number on the stack

: DOUBLE 2 * ; defines a word which doubles the number on the stack.

3 DOUBLE DOUBLE . prints 12

Since the entire language structure is embodied in words, the application programmer can "extend" Forth to add new operators, program constructs, or data types at will. The Forth "core" includes operators for integers, addresses, characters, and boolean values; string and floating-point operators may be optionally added. Forth has been called "a language for writing languages" - by defining new words you create the language that best describes the problem in hand. Indeed, most of a Forth system is itself written in Forth, and the essential core is so small and simple that it is perfectly possible for an amateur to write their own Forth.
 


A Newcomer's View

TOP

As a complete newbie to Forth experienced in several other languages I can say that:

Forth is a unique language.

It is based on two types of data structures: dictionaries and stacks.

You use stacks to push numeric data on them and have them processed. Neat side-effect: that way you can use RPN for all your operations, you don't need no temporary variables yada yada. Short, you don't need parentheses. Gosh!

The dictionary (or several of them, called modules, vocabularies or wordlists) are linked lists of words. A word is a named piece of memory that contains at least a pointer to the next word and a pointer to some code. You can define words and allocate memory to them to have pointers, data or code stored in them at your heart's content. That way, defining "subroutines" as well as close-to-arbitrary data structures is possible.

A Forth system (that's the terminology because it is more than just an interpreter or compiler) swallows source code consisting of definitions and compiles them into words. When you call some word from the source file or from an interactive session with the system, it is executed. Executing a word means that the system mainly follows pointers to code and keeps addresses on a return stack. At the lowest level, these pointers point to assembly implementations of basic words.

The kernel of a Forth system may well be not bigger than 8 KB.

Forth systems normally contain in addition:

As you can see, with a Forth system you don't need an OS anymore. That doesn't prevent people from writing Forth systems for all major and especially for a LOT of "minor" platforms. Forth is a language used very much in embedded systems programming as well as a fast, clean, consistent general-purpose language.

Hope this helps.

All you gurus: tell me if I got something wrong up there, please.

Matthias Warkus

mailto:mawa@iname.com
http://www.angelfire.com/ny/mawaspace/


FAQs on Forth

TOP

Discourse on all manner of Forth-related topics can be found in the lively comp.lang.forth newsgroup. The comp.lang.forth FAQ is published in seven parts, which are updated monthly. The seven parts are:

General questions Books and Periodicals Online resources The ANS Standard
Forth systems  Forth groups & organizations Forth vendors 

These FAQs are intended to be a brief overview of the tools and information available for the new FORTHer.


Further Reading

TOP
The Evolution of Forth
A History of Forth from the 1970s - by its inventors
Stories
About some modern Forth projects - including the Space Shuttle
The Advantages of Forth
An essay by Brad Rodriguez
Why do they call it Reverse Polish Notation?
A little bit of History
StackFlow
An Introductory Tutorial by Gordon Charlton (33k Zipped HTM file)
Levels of Forth
A Brief Overview of various types of Forth - by Glen Haydon
Julian Noble's Forth Primer
(zipped files, not HTML, 1184K)
Stack Machines
An on-line book by Phil Koopman describing a number of CPU designs which are optimised for Forth
Open Firmware
A Forth derivative which has been adopted by Sun Microsystems, HP, Apple, IBM, and others as the official language for writing bootstrap and driver firmware.
The Forth Research Page
Links to Papers, Conferences, Bibliographies, and a lot more

HOME Site Map TOP