The Forth Programming Language...

Table of Contents

last revision
16 November 2011, 10:44am
book quality
just begun

Forth is generally considered to be an obsolete programming language which has a brief period of glory in the early eighties. Nevertheless there are many remarkable features about forth which are not found in other languages or computer systems. Forth can be implemented in a tiny size (maybe 30K), and can be used as an operating system for embedded systems. Forth, in a sense is not even a language because it has no syntax (apart from words separated by space characters). This booklet is oriented to using forth on a linux computer.

Philosophy And Ideas ‹↑›

The philosophy behind forth is an extremely small and efficient interpreter and language based on a 2 stack virtual machine and a dictionary of 'words' or functions.

Installation ‹↑›

see what software is available for the forth language on debian linux

 apt-cache search forth | less

install the gforth system on a debian/ubuntu linux

 sudo apt-get install gforth

see what files were installed as part of the gforth system

 dpkg -L gforth

Openfirmware ‹↑›

Apple Mac computers at one stage used a forth based system for booting. This was the 'openfirmware' system which uses 'fcode' which is an ans compliant version of forth.

install fcode utilities on linux

 sudo apt-get install fcode-utils

The Virtual Machine ‹↑›

The forth language is based on a virtual machine. This machine can be implemented in a very efficient manner. The machine has a data stack and a return stack. The return stack is used for holding the return address of functions as well as for temporary storage.

Dictionary ‹↑›

Forth words are stored in a dictionary in computer memory. This dictionary has a particular structure, which may vary slightly from one implementation to another.

Implementation Methods ‹↑›

www: direct
threaded
www: indirect
threaded
www: subroutine
threaded Each call to a word is implemented as a subroutine
www: token
threaded

Standard Words ‹↑›

This section documents common or traditional forth words

bye exit the forth interpreter ! stores the value in the memory location @ fetches the value at the memory location create creates a new dictionary entry ' <word> looks up the word in the dictionary and prints something like 'ok' if found or 'Undefined word' if not dump dumps the contents of memory locations words show all words currently defined in the system variable

Data Stack Words ‹↑›

 .s
display the contents of the data stack
 depth
show how many items are on the data stack

set the text input buffer location to just above the stack

 sp0 @ 'tib !

Return Stack Words ‹↑›

 >r
push the top value of the data stack onto the return stack
 r>
push the top value of the return stack onto the data stack

 rp0 @ rp!

Input Buffer Words ‹↑›

 >in
leave on the stack the position in the input buffer

 blk
the input source block value turn the block value off (set input to keyboard) eg: blk off

Parsing Words ‹↑›

www: word
(s: delimiter - address) get a word from the input stream
get the next word and print it
 word this count type

Interpreting Words ‹↑›

 quit
the main forth interpreter an example quit loop
 : quit blk off begin rp0 @ rp! query interpret state @ not
       if ." ok" then again ;

 evaluate
execute code stored in a text string
 tib
terminal input buffer, where text typed by the user is stored
 rp0
 query
get 80 characters into the text input buffer

Documenting Code ‹↑›

Basic Code ‹↑›

Print a single star (s:- ) 42 is the ASCII code for * : star 42 emit ;

print n stars. loop n times (0 up to n-1) and execute star (s: n -- ) : stars 0 do star loop ;

: square ( n -- ) \ print an n-line square of stars dup 0 do \ loop n times, keeping (dup-licating) n on the stack dup stars cr \ each time, print n stars then print cr loop drop ; \ after loop is done, drop the n from the stack

: triangle ( n -- ) \ print an n-line triangle 1 + 1 do \ loop n times from 1 to n (instead of 0 to n-1) i stars cr \ this time use the inner loop index i loop ;

: tower ( n -- ) \ print a "tower" with an base of size n dup \ dup-licate n (since it is used twice below) 1 - triangle \ print a triangle 1 size smaller than n square ; \ print a square base of size n

Maths ‹↑›

find the hypotenuse of a right triangle with sides x, y

 : magnitude (x y—vector magnitude) dup * swap dup * + sqrt ;

Output ‹↑›

documenting words
\ - start a one line comment
( ) - start and end a comment in brackets

Loops ‹↑›

print 20 star characters

 20 0 [do] 42 emit [loop]

The words 'do' and 'loop' can only be used in the compile context (within a colon ':' definition). Use [do] and [loop] in interpret mode

Variables ‹↑›

add variables a and b and store in c

 a @ b @ + c !
The c equivalent of this is: c=a+b;

define a new variable 'x'

 variable x

define a variable and store the value '3' in it

 variable x 3 x !

Compiling ‹↑›

http://www.forth.org/lost-at-c.html the forth compile flow chart

Gforth ‹↑›

Gforth does not seem very helpful or simple to use.

output words
. - pop a number from the stack and print
emit - print the character on the stack
www: stack
underflow - The word just executed expected one or more values to be on the data stack

Implementations ‹↑›

error messages

New Forth Blueprint ‹↑›

The aim of this booklet is to document the forth language and also provide a blue print for a new small forth with the following features:

Universal word names; using a dot notation to represent hierarchies of forth words. eg 'core.!' This concept will take the place of 'wordlists' or loadable dictionaries in traditional forths.

Case sensitive words with standard words defined in lower case

A 'byte code' or token threaded implentation of the forth language with a seperate 'virtual machine dictionary' which will define all the words which form part of the virtual machine.

A 'bootstrap' forth where only the most primitive words are defined as assembler macros, and everything else is defined using forth

A 'literate' forth, which allows code to be documented nicely, with the text stored in the dictionary (if practical) or else in some external storage

Dictionary will look like

prefixes: a draw.line b core

a.lineto|...

The system is to associate a character with each word prefix (in dot notation) and then use that character to 'disambiguate' each word within the dictionary.

All words (except assembler macro primitives) stored in a text file in the format name.name1 : word1 def def ; : word2 def def ... ;

name2.name3 : word3 def def ; : word4 def def ... ;

This utf8 text file contains prefixes followed by lists of words which use that prefix. eg name.name1.word1 is one fully qualified word name

People ‹↑›

This section talks about some people who have been important in the forth world

www: charles
moore The inventor of forth.
www: elizabeth
rather An early user and documenter of forth. Has written some books on the subject
www: leo
brodie Author of 'Starting Forth' (1981)
www: ward
cunningham The inventor of the 'wiki' and forth enthusiast

Web ‹↑›

www: http://c2.com/cgi/wiki?ForthLanguage
a good list of forth links on the webs first wiki
www: http://home.iae.nl/users/mhx/sf.html
online edition of 'starting forth'
DOCUMENT-NOTES:
forth implementations
gforth - a gnu forth
yforth - a small ansi c forth (~27K) not actively developed