Introduction for non-forthers

If you are new to forth, here are a few things you should absolutely know before you try to read forth programs.

1.   Words

Forth code consists of words (commands). Words appear as strings separated by one or more spaces. This means that forth does not know compound expressions like
print(12x+y)/3
In forth such an expression is written as a series of independent commands separated by spaces:
x 12 *  y +  3 /  .
The dot prints the result.

The main forth rule is:
First the data, then the operation on the data.

Numbers, constants, variables and operations as  +   *   /   and  .  (dot) are all independent commands, even when they are one character words. They are not part of an expression and can appear in any order because there is no syntax (and consequently no syntax error ;). Of course the result will be different when you change the word order.

2.   Comment

\ This is comment
'\' is a word. It (the backslash) simply ignores the rest of the line.

x 12 * ( 12x )   y + ( 12x+y )   3 /   .
'(' is a word, it ignores text until ')' appears. Yes, it is indeed the word '(' itself that does this, not a compiler or an interpreter.

3.   Defining new words in forth

\ this a colon definition:
: FORMULA1   x 12 * y + 3 /  ;
':' starts the definition of a new word.
The following word FORMULA1 will be its name.
The words  x   12   *   y   +   3   /  are not executed. They are compiled in the definition.
';' finishes the definition.
Now there is a new forth word  FORMULA1 . When you type its name and press 'enter' the  x   12   *   y   +   3   /  will be executed.

\ this is a definition in assembler:
code LED-ON ...assembly... next end-code
code  and  next end-code  behave like the pair ':' and ';'. The difference is that code definitions are written in assembler.

When you type a forth word and press 'enter' the word will be executed, but while defining a new definition the words will be compiled in the definition for later execution.

Documentation: When a new word is defined, it is usually followed by an
( in -- out ) diagram that symbolically shows the data 'eaten' by the word and the data produced by the word:
*  ( x y -- x*y )
.  ( x -- )

4.   if then   begin until   begin while repeat

These words are a bit tricky in forth. You might expect:
IF rain THEN umbrella ..      wrong in forth!

Remember:
First the data, then the operation on the data.

rain? IF umbrella ELSE no-umbrella THEN ..
rain?  (a rain detector) produces a flag for the decision maker  IF  (zero=false=no, non-zero=true=yes). When the flag is zero  IF  jumps to no-umbrella just after  ELSE  otherwise it does nothing.

BEGIN umbrella rain-stopped? UNTIL no-umbrella ..
On zero  UNTIL  jumps back to  BEGIN 

BEGIN rain? WHILE umbrella REPEAT no-umbrella ..
On zero  WHILE  jumps to ... just after  REPEAT  and quits the loop.