Here is a way to create temporary, or shadow, words in Forth. The temporary words can not be used in normal Forth words. They can be used to help build normal Forth words and data structures and removed when no longer needed because they are defined higher up in memory.
A helper word to exchange memory contents is needed.
// TEST RUN
TEMPS
: RAMP
256 0
DO
255 I - C,
LOOP ;
NORMAL
CREATE DESCEND
TEMPORARY // INCLUDE TEMPORARY
// IN SEARCH ORDER
RAMP
: NOTHING ;
FORGET NOTHING
A nothing word is created at the end. When NOTHING is forgotten, all the words in the TEMPORARY vocabulary will be forgotten, without forgetting DESCEND , because they are all above HERE .
Last edited by JimBoyd on Wed Jul 16, 2025 12:58 pm, edited 2 times in total.
There is a way to avoid placing the temporary words ( or shadow words) in a different vocabulary. The word PRUNE will unlink any words which are higher than HERE .
: PRUNE ( -- )
CURRENT @
BEGIN
DUP HERE TRIM
@ DUP [ HERE ] LITERAL U<
UNTIL
DROP ;
TRIM is a word in my Forth which trims the VOC-LINK and each vocabulary. It follows the chain of links at the supplied address until it finds one which is less than the supplied limit.
: TRIM ( ADR LIMIT -- )
OVER
BEGIN
@ 2DUP SWAP U<
UNTIL
NIP SWAP! ;
PRUNE follows the link field chain in the CURRENT vocabulary until an address less than somewhere in itself is reached. The assumption is that no shadow words will be defined before PRUNE is defined. TRIM is used on each of these links to make sure each link points to an address less than HERE .
Shadow words can be defined in the same vocabulary as normal Forth words. Since a shadow word is defined entirely in higher memory, pruning them is just a matter of changing the necessary link fields.
Another change will make the shadow word lexicon more efficient and smaller. EXCHANGE is overly general. It can be replaced with SWITCH .