I've started reading Thinking Forth. In the book the author mentions a three-line module system with a reference to the proceedings of a Forth conference. Here's a PDF containing a description of the module system starting on page 14, (132 as printed).
Here's the instructions on how to use the three defined words INTERNAL
, EXTERNAL
and MODULE
.
A module is a portion of a program between the words INTERNAL and MODULE. Definitions of constants, variables and routines which are local to the module are written between the words INTERNAL and EXTERNAL. Definitions which are to be used outside the module are written between the words EXTERNAL and MODULE. [Local variables for a routine] are defined between INTERNAL and EXTERNAL. The routine which references them is defined between EXTERNAL and MODULE.
And here's the code itself:
: INTERNAL ( --> ADDR) CURRENT @ @ ;
: EXTERNAL ( --> ADDR) HERE ;
: MODULE( ADDRl ADDR2 --> )PFA LFA ! ;
I'm reading the book for the ideas about how to write software in general, rather than how to program in any particular implementation of Forth, so I'm not familiar with the built-in words used in the code, but I'm curious about this module system. Can someone explain how it works?
I will rephrase the description. A module should look like this:
INTERNAL
... code ...
EXTERNAL
... more code ...
MODULE
The code for implementing this module system assumes the dictionary is a conventional singly linked list. INTERNAL
saves a pointer to the current word, e.g. the one right before INTERNAL
. EXTERNAL
saves a pointer to the word right after EXTERNAL
. MODULE
takes the two pointers, and patches the link field of the word after EXTERNAL
to point to the word before INTERNAL
. In effect, it makes the dictionary skip over all words in between INTERNAL
and EXTERNAL
.
This may not work in a modern Forth, because the words CURRENT
, PFA
, and LFA
are not standardized. And also, HERE
may not be the correct address for the header of the next word.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With