DEFINITIONS

( -- )

Make the compilation word list the same as the first word list in the search order. Specifies that the names of subsequent definitions will be placed in the compilation word list. Subsequent changes in the search order will not affect the compilation word list.

See:

Implementation:

: discard ( x1 ... xn u -- ) \ Drop u+1 stack items
   0 ?DO DROP LOOP
;

: DEFINITIONS ( -- )
   GET-ORDER SWAP SET-CURRENT discard
;

Testing:

T{ ONLY FORTH DEFINITIONS -> }T
T{ GET-CURRENT -> FORTH-WORDLIST }T

T{ GET-ORDER wid2 @ SWAP 1+ SET-ORDER DEFINITIONS GET-CURRENT
-> wid2 @ }T

T{ GET-ORDER -> get-orderlist wid2 @ SWAP 1+ }T
T{ PREVIOUS GET-ORDER -> get-orderlist }T
T{ DEFINITIONS GET-CURRENT -> FORTH-WORDLIST }T

: alsowid2 ALSO GET-ORDER wid2 @ ROT DROP SWAP SET-ORDER ;
alsowid2
: w1 1234 ;
DEFINITIONS : w1 -9876 ; IMMEDIATE

ONLY FORTH
T{ w1 -> 1234 }T
DEFINITIONS
T{ w1 -> 1234 }T
alsowid2
T{ w1 -> -9876 }T
DEFINITIONS T{ w1 -> -9876 }T

ONLY FORTH DEFINITIONS
: so5 DUP IF SWAP EXECUTE THEN ;

T{ S" w1" wid1 @ SEARCH-WORDLIST so5 -> -1  1234 }T
T{ S" w1" wid2 @ SEARCH-WORDLIST so5 ->  1 -9876 }T

: c"w1" C" w1" ;
T{ alsowid2 c"w1" FIND so5 ->  1 -9876 }T
T{ PREVIOUS c"w1" FIND so5 -> -1  1234 }T

ContributeContributions

Maxavatar of Max off-by-one error in reference implementationSuggested reference implementation2020-11-13 18:43:12

After SET-CURRENT consumes the first wordlist, the stack is ( widn ... wid2 n ) i.e. there are n-1 wids to discard.

Thus (if I am not mistaken) the correct reference implementation should be

: DEFINITIONS ( -- )
   GET-ORDER SWAP SET-CURRENT 1- discard
;

AntonErtlavatar of AntonErtl

Correct. An alternative implementation:

: definitions ( -- )
  get-order over set-current set-order ;
Reply New Version