Where to start to program in FORTH language

published: 9 May 2019 / updated 15 May 2020

Lire cette page en français

 


There is no more effective solution to learn a computer language than to get started without delay by having the most suitable tool.

FORTH exists in a multitude of forms and versions.

Gforth

To learn FORTH, we chose the Gforth version, available here:

  http://www.complang.tuwien.ac.at/forth/gforth/

You arrive on a very basic page. Extract from this page:

extract home page complang.tuwien.ac.at/forth/gforth/

In this list of files, two are essential to get started:

You can also access the official GIT repository for Gforth here:

  https://savannah.gnu.org/git/?group=gforth

On this repository, updates are available very regularly.

Gforth installation on Windows

On the complang.tuwien.ac.at site, download the file gforth-0.7.0.exe.

Once this file is downloaded, just run it. Follow the installation instructions.

Once installed, to launch Gforth, launch the Windows quick menu and search for Gforth:

launch of Gforth

Select Gforth

Gforth is launched very quickly, in a window similar to a system command window:

Gforth home screen

Yes. You expected something more sexy! But this sobriety hides a monster of incomparable power.

Because the FORTH language is the faster programming language after pure machine language.

First manipulations under Gforth

If you press the ENTER key on the keyboard, you will see ok.

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
  ok
  ok
  ok

Type words and press the ENTER key. Gforth will display all words from the dictionary. Here, just an extract:

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
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
.lib-error c-source-file-execute notype-execute c-source-file
init-c-source-file lib-handle c-tmp-library-name c-named-library-name

Compile our first word:

: hi ." Hello friends" ;

Let's break down what you just did:

  1. the word : initializes the compilation of a new word
  2. the word hi is the new word that will be added to the dictionary
  3. the string . "Hello friends" corresponds to what the created word will do
  4. the word ; stops the compilation.

Let's type words again. We find at the beginning of the dictionary:

: salut ." Bonjour les amis" ;  ok
words
hi 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:

Our new word hi is at the top of the Gforth dictionary.

To execute this new word, just type hi and then press the ENTER key:

  ok
hi Hello friends ok

Some elementary rules in FORTH

In FORTH, each word or number is separated by at least one space character. If he there are several space characters, FORTH takes into account only one space character. Example:

2 3 + . 5  ok
2   3   +   . 5  ok

In FORTH, no character is reserved to mark a word class of variable, constant type, etc. Any sequence of characters not separated by a space character is a word.

If we try to execute a word that is not defined in the dictionary, we have a runtime error:

kisses-friends
:8: Undefined word
>>>kisses-friends<<<
Backtrace:
$7FA54EB4 throw
$7FA61628 no.extensions
$7FA5502C interpreter-notfound1

Yes, the word kisses-friends has not been compiled. We cannot therefore execute.

FORTH: interpreter or compiler?

FORTH is both. It is the word : which marks the beginning of the compilation of a new word. And it is the word ; which marks the end of compilation of this new word. Example:

: cube ( n --- n)
    dup dup * *  ;

The number of lines of code does not matter. We might as well have written this:

: cube dup dup * *  ;

Or like this:

: cube ( n --- n)   \ cube elevation of an integer 
    dup             \ first duplication of the integer 
    dup             \ second duplication of the integer 
    *               \ first multiplication between two duplicate integers, 
                    \ leave the integer square on the stack 
    *               \ second multiplication of the integer squared with the integer 
; 

You can copy/paste this code and have it compiled by Gforth.

Everything goes through the stack

In FORTH, all values go through a data stack. Before compiling a word, you may be tempted to execute a sequence of operations step by step. The word .s allows you to see what is on the stack without altering the content. Example:

4 .s <1> 4  ok
dup .s <2> 4 4  ok
dup .s <3> 4 4 4  ok
* .s <2> 4 16  ok
* .s <1> 64  ok

Here, the value between < and >, example <3>, indicates that there has three values on the stack, followed by those values. The rightmost value corresponds to the value located at the top of the stack.