Autres articles / Other articles

No word FORGET in FlashForth

published: 8 December 2019 / updated 8 December 2019

Lire cette page en français

 


No word FORGET

The FlashForth version for ARDUINO does not have the word FORGET.

However, the word FORGET appears from the very beginning of the FORTH language in almost all standards: 79-Standard, 83-Standard ...

So why is not it in FlashForth?

In fact, the most recent standard, forth200x, has declared the word FORGET as obsolete. It may still remain in recent versions of FORTH as a concession for existing implementations to old standards.

Use of the word FORGET

Here is how the word FORGET is used on FORTH language versions including this word.

Let's take a series of definitions:

: >gray ( n -- n' ) 
    dup 2/ xor ;    \ n' = n xor ( décalage logique vers la droite 1x ) 
 
: gray> ( n -- n ) 
    0  1 31 lshift  ( -- g b mask ) 
    begin 
        >r          \ sauve mask sur pile retour 
        2dup 2/ xor 
        r@ and or 
        r> 1 rshift 
        dup 0= 
    until 
    drop nip ;      \ nettoie pile de données en laissant le résultat 
 
: test 
    2 base !        \ sélectionne base binaire 
    32 0 do 
        cr I  dup 5 .r ."  ==> "    \ affiche valeurs (binaire) 
        >gray dup 5 .r ."  ==> "    \ justifiés à droite sur 5 caractères 
        gray>     5 .r 
    loop 
    decimal ;         \ remet en décimal 

How to remove the word test?

With FORGET, just type:

FORGET test 

If we then type WORDS, we will only find the definitions >gray and gray>.

The word FORGET only affects newly defined words. He has no action on pre-defined words in the dictionary.

The word FORGET allows you to delete some of the definitions:

: w1 ;  ok 
: w2 ;  ok 
: w3 ;  ok 
: w4 ;  ok 
: w5 ;  ok 
words 
w5 w4 w3 w2 w1 ..... 
forget w3 
words 
w2 w1 ..... 

The forget w3 sequence removes from the dictionary the word w3 and all the words defined after him, ie w4 and w5.

If the word FORGET is convenient in the development phase, it does a risk of confusion by accidentally deleting dictionary fields sensitive. A vectorized execution word can be destroyed by its vector. Execution of this word can then profoundly disrupt FORTH.

When one develops in FORTH language, one conceives before all sets of words by layers functional. On ARDUINO it will be for example:

There is an elegant way to mark these different layers with the word marker.

Marking functional layers with the word marker

Forth allows you to forget the words and all that has been attributed in the dictionary after these. You have to use a marker created with marker:

-gray 
marker -gray 
: >gray ( n -- n' ) 
    dup 2/ xor ; 
: gray> ( n -- n ) 
    0  1 31 lshift  ( -- g b mask ) 
    begin 
        >r 2dup 2/ xor  r@ and or  r> 1 rshift dup 0= 
    until 
    drop nip ; 

We find in the dictionary these words:

words 
gray> >gray -gray 

It may seem surprising to invoke this word -gray before it is defined. Moreover, at the first compilation, this word causes an error message but does not prevent the compilation:

-gray -gray ? 
marker -gray  ok<#,ram> 
: >gray ( n -- n' ) 
    dup 2/ xor ; 
\ ...etc....... 

You can manually type -gray and all the words defined after -gray will be removed from the dictionary, -gray included.

But it's even more interesting when we recompile our code. Because the first word found by the FORTH interpreter will be -gray :

-gray  ok<#,ram> 
marker -gray  ok<#,ram> 
: >gray ( n -- n' ) 
    dup 2/ xor ;  ok<#,ram> 

At the first compilation we had -gray -gray?.

At the next compilation, we have -gray ok <#, ram>.

We can recompile as many times as necessary our FORTH code. Everytime the interpreter will encounter the -gray, the memory space taken by all previously defined words after -gray will be removed.

This freed space will be reused by the new definitions.

Example of marking a management layer of the SPI interface

Find complete explanations on the programming of the SPI interface here:
  The SPI interface of ARDUINO boards

Find the complete listing of the programming of this SPI interface here:
  Words to drive the SPI module on the ATmega2560

In the listing spi-base-avr-v2.txt find this at the beginning of the source code:

-spi-base 
marker -spi-base 

The dictionary can be reset to its original state by typing empty. All words compiled will be removed from the dictionary. We will find the dictionary in its initial state.

Then, when setting the code managing the 8x8 LEDs display, we acritit at the beginning of the code source this:

-8x8 
marker -8x8 

From this point, we are left with two markers: -spi-base and -8x8 . If we execute -8x8 , everything that has been compiled after this word will be forgotten. But everything that has been compiled before this word will be compiled.

Here we have a considerable advantage over other programming languages: the ability to manage the definitions compiled by functional layers!

If we compile several functional layers, in our case the codes listed after -spi-base and -8x8, deleting the definitions will be executed in the order LIFO (Last In First Out). Clearly, if we execute -spi-base, all definitions compiled after -spi-base will be removed from the dictionary, including definitions compiled after -8x8.

Let's go back to our listing spi-base-avr-v2.txt.

This layer can be compiled after another layer of software, even followed other software layers. If you embed markers at the beginning of each software layer, it will be easy to selectively recompile a software layer while it is being debugged.