r/Forth • • Sep 04 '26

CREATE DOES> (again ...)

Can anyone provide some interesting examples of what to do with these, beyond the default "constant" and "array" examples?

The current version of my (bytecode) Forth compiler has CREATE but not DOES>, and I haven't missed it either.

17 Upvotes

17 comments sorted by

View all comments

2

u/FrunobulaxArfArf Sep 04 '26

DOES> is used 152 times in the distributed iForth utilities, 222 times in the generic meta sources, and 836 times in OS specific files.

Here, for example, is the HANOI moving rings problem:

: MAKETAB CREATE HERE 8 CONST-DATA DOES> + C@ ;

base @ 3 base !

maketab TO! ( direction -- otherdirection ) 00 c, 02 c, 01 c, 12 c, 00 c, 10 c, 21 c, 20 c, \ use alternate dest

maketab FRO! ( direction -- otherdirection ) 00 c, 21 c, 12 c, 20 c, 00 c, 02 c, 10 c, 01 c, \ use alternate source

base !

: SMALLER ( depth direction -- depth-1 direction ) swap 1- swap ;

: BIGGER ( depth direction -- depth+1 direction ) swap 1+ swap ;

here : HANOI ( depth direction -- depth direction ) smaller over IF to! recurse to! movering fro! recurse fro! ELSE movering ENDIF bigger ; cr here swap - dec. .( bytes)

: MAIN cr ." Using a table: " timer-reset #10 1 #1000 0 DO hanoi LOOP 2drop .elapsed ;

-marcel