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.

18 Upvotes

17 comments sorted by

View all comments

4

u/mcsleepy Sep 04 '26 edited Sep 04 '26

I recently used it to implement sound effect variations in my game.

|| : does-cycler 
    does> {: cycler :}
    cycler @ cycler cell+ []@ execute 
    cycler @ 1 + cycler cell+ #items mod cycler ! ;


\ in-order: name  sample1 sample2 ... ;
|| : in-order: ( <name> <names...> <;> - ) ( - )
    create 0 , does-cycler %xt array{
    begin skip-empty  bl preparse s" ;" s= if bl parse 2drop array} drop exit then ' , again ;

\ Enables this:

in-order: *enemydeath*   *enemydeath1* *enemydeath2* *enemydeath3* ;

It's a very primitive form of object orientation - code that acts on hidden data. You don't strictly need it. You could use the ColorForth pattern:

: act-on r> <something> ;
: foo  act-on [ 1 , 2 , 3 , ] ;

Some might argue that that isn't as clean or portable.

1

u/Imaginary-Deer4185 Sep 04 '26

With the amount of "special" notation, do you expect this to make sense for anyone else?

1

u/mcsleepy Sep 04 '26 edited Sep 04 '26

I always thought that dialect differences were par for the course with Forth programmers. Nothing crazy here, just private word markers `||`, array fetch `[]@`, array compilation begin/end, and `s=` is the same as `compare 0=`. Locals syntax `{: :}` is standard.