6.10.10.4 The gory details of CREATE..DOES>

DOES> ( compilation colon-sys1 – colon-sys2  ) core “does”

This means that you need not use CREATE and DOES> in the same definition; you can put the DOES>-part in a separate definition. This allows us to, e.g., select among different DOES>-parts:

: does1 
DOES> ( ... -- ... )
    code1 ;

: does2
DOES> ( ... -- ... )
    code2 ;

: def-word ( ... -- ... )
    create ...
    IF
       does1
    ELSE
       does2
    ENDIF ;

In this example, the selection of whether to use does1 or does2 is made at definition-time; at the time that the child word is CREATEd.

Note that the property of does> to end the definition makes it necessary to introduce extra definitions does1 and does2. You can avoid that with set-does>:

: def-word ( ... -- ... )
    create ...
    IF
       [: code1 ;] set-does>
    ELSE
       [: code2 ;] set-does>
    ENDIF ;

In a standard program you can apply a DOES>-part only if the last word was defined with CREATE. In Gforth, the DOES>-part will override the behaviour of the last word defined in any case. In a standard program, you can use DOES> only in a colon definition. In Gforth, you can also use it in interpretation state, in a kind of one-shot mode; for example:

CREATE name ( ... -- ... )
  initialization
DOES>
  code ;

is equivalent to the standard:

:noname
DOES>
    code ;
CREATE name EXECUTE ( ... -- ... )
    initialization

Gforth also supports quotations in interpreted code, and quotations save and restore the current definition, so you can also write the example above also as:

CREATE name ( ... -- ... )
  initialization
[: code ;] set-does>
set-does> ( xt –  ) gforth-1.0 “set-does>”

Changes the current word such that it pushes its body address and then executes xt. Also changes the compile, implementation accordingly. Call set-optimizer afterwards if you want a more efficient implementation.

>body ( xt – a_addr  ) core “to-body”

Get the address of the body of the word represented by xt (the address of the word’s data field).