Dynamic Words

Elsewhere I can be found:
icychkn@protonmail.com
github/icychkn
youtube/icychkn

Implementation

: :do def c< begin c< jump c@ 0 >c ; : :set create swp >c does @ ! ; : :dynamic :do :set ;

Why

Allows for further factoring by separating structure from behavior, and both from context.

I had a need for words to change behavior during runtime and before their execution, with unique behavior for each context.

Description

Dynamic words change meaning when context does. One limited example of this existing in Forths is words being compiled instead of interpreted during the compiler context:

41 emit : A 41 emit ;

However, this behavior isn't embedded into the words themselves, existing only within the compiler itself. They do not change behavior after compilation.

The method used here relies on a single level of indirection, with the dynamic word jumping to the actual behavior. It costs an additional call during runtime, but allows other words to change the behavior being pointed to with only a single store.

Examples

Generic Printing

:dynamic . ->. : number literal num ->. ; : letter literal emit ->. ; 41 dup number . letter .
00000041 A

Generic apply to the values 41, 42, and 43.

:dynamic . ->. : triple 41 . 42 . 43 . ; : add literal + ->. ; : letter literal emit ->. ; add 0 triple num letter triple
000000C6 ABC