Search code examples
forthgforth

How do I control where new Forth words will be compiled?


Is there a way to change what HERE points to, or some other way to make sure that the next definition I compile will end up in some special location? Or can I copy a definition to somewhere else once it's made? Ideally I'd want a solution in ANS Forth, but something Gforth-specific is good enough.


Solution

  • You can use ALLOT to move the data space pointer in both directions:

    : here! ( a -- )   here - allot ;
    

    As for gforth, this simpler version should work:

    : here!   dp ! ;
    

    Having said this, HERE isn't necessarily where new words will be compiled. HERE points to data space, whereas definitions are written to name space and code space. However, in a traditional design like Gforth, the three are a single contiguous region. See DPANS94 3.3.