Getting started FORTH

published: 6 May 2019 / updated 27 April 2020

Lire cette page en français

 

First commands

The first command you will try is WORDS. This word causes all the words to be displayed FORTH available in current vocabulary. Type WORDS and press the key marked RETURN (or RET or ENTER), you see on the display:

WORDS
disasm disassembler base-addr show-name default-32bit default-16bit
default-16bit? col w@ (D.) MAXCOUNTED SPCS SPCS-MAX maxstring
dffield: sffield: ffield: 2field: field: cfield: end-structure
begin-structure +field init-libcc end-c-library c-library c-library-name
c-library-incomplete clear-libs c-function c-function-rt c-function-ft
link-wrapper-function compile-wrapper-function1 compile-wrapper-function

This is the result displayed using gForth . We have only put the first lines here. This result may vary depending on the version of the Forth language you are using.

There are many words defined in the FORTH dictionary. It is not necessary to all know. Many of these words are primitive functions of high-level words.

The words you see scrolling when running the WORDS command belong to the dictionary FORTH language. The dictionary is divided into several vocabularies, the main vocabulary being the FORTH vocabulary.

If you type VOCS, you will see the list of different vocabularies FORTH language:

vocs 
disassembler libcc-types assembler esc-sequences new-locals-wl locals-types locals 
environment-wordlist Root Forth 
ok

Another word, PAGE, causes the video screen to be cleared.

With gForth, words containing alphabetic characters in lowercase characters or capital letters: WORDS or words or WoRdS will always have the same effect.

BE CAREFUL: some versions of FORTH, in particular those for Arduino are sensitive for scrap. Clearly, we can only type words and not WORDS with these versions

The word BYE takes you out of gForth.

We can type several words on the same line, respecting the syntax of the language.

A FORTH word consists of the only ASCII characters between 33 and 127 inclusive and the size valid words is between 1 and 31 characters maximum, separation spaces and carriage return not included.

Example:

* ?DUP >R # @ VOCABULARY IN-META

are all FORTH words.

When you create your own definitions, names as wacky as the ones below will be fully valid, provided that they mean something to you and are defined:
  () "aeiouy" AND-NOW-GO-SLEEPING #@[]@#

Only imperative: no space character in the word itself. The space character is the one and only character separating words and numbers in FORTH.

When typing a command line from the keyboard, the words FORTH and their parameters are separated by at least one space.

Remember this simple and only FORTH lexical rule: words are separated by spaces. If there is more than one space character between two words, only the first space character will be taken into account. account by the interpreter.

If you type a word not found in the FORTH dictionary, an error message is displayed:

TTT
:1: Undefined word
>>>TTT<<<
Backtrace:
$7FA54EB4 throw
$7FA61628 no.extensions
$7FA5502C interpreter-notfound1

Numbers, although not defined in the dictionary, are correctly interpreted. Any number typed in the input stream is stored in a memory area called DATA STACK or parametric stack. Example: 65 EMIT displays the capital letter A.

The numerical value 65 is placed on the data stack and corresponds to the ASCII code of a character displayable or control. This value is then processed by the word following this number, EMIT in our example.

Many FORTH words accept one or more parameters as input and often stack the result on the data stack.

In this sense, a word FORTH is comparable to a procedure or a function. FORTH does not distinguish between a word defined in machine code within its dictionary and a word defined in a more advanced form. For you well imbibe this concept, approach each new word like a black box taking into account for the moment exclusively parameters which are possibly processed and rendered:

The operating mode immediately executing the commands is called INTERPRETATION (the other mode being COMPILATION) because all orders are immediately executed. Several commands can be typed in the same line:
  PAGE s" this is a line of text" TYPE

First compilation

FORTH can compile new commands. In this, FORTH sets itself apart from all other compilers such as PASCAL, C, COBOL, FORTRAN to name only the best known. Regarding FORTH:

A compilation action begins with the word : (colon) and ends with the word ; (semicolon). The word : is followed by the name of the news command to be defined. For example, either define the word ERASE which will delete the screen, type: : ERASE PAGE ; and press Return:

Gforth 0.7.0, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
: EFFACE PAGE ;  ok    

This time, the screen has not been cleared because PAGE is located in a current definition compilation. The new word ERASE is now part of the FORTH dictionary, which we can easily verify by running WORDS:

WORDS
ERASE disasm disassembler base-addr show-name default-32bit
default-16bit default-16bit? col w@ (D.) MAXCOUNTED SPCS SPCS-MAX

We can now execute ERASE. Its execution has the same effect as PAGE.

It's very practical, a language that can be redefined as easily!

: IS ;
: TO-ADD + ;
: AND ;
: TO-DISPLAY . ;

We can now execute
  IS 3 AND 5 TO-ADD AND TO-DISPLAY
displays the sum of 3 and 5.

Almost natural language! In this example, the words IS and AND are supposed to do nothing, which they do very well ...

All the words thus compiled are added to the dictionary and are now part of the language. FORTH in the same way as any primitive already defined.

Edit source codes

FORTH source codes can be edited with any text editor in Windows or Linux.

There is no specific FORTH source code editor.

With gForth, source files have the extension fs:

FORTH source files

In Windows, right-click on the file of your choice and select open with, then select the appropriate text editor.