210b
<< <> >>

Forth programmer at work

: MANY ( -- ) >IN @ STOP? AND >IN ! ;
Wait a minute, STOP? is not a standard Forth word. Let's focus on that first.
Forth file:  ec210.frt
\ MANY TIMES (Albert Nijhof)

FORTH DEFINITIONS   DECIMAL
: STOP? ( -- true/false )
  KEY?
  DUP IF   DROP KEY
           BL OVER = IF DROP KEY THEN
           27 OVER = IF -28 THROW THEN
           BL <>
      THEN ;

) 1000 MS STOP? . [rtn] ?
Gives zero. But enter the same again, and press another key immediately after the [rtn]. Three possibilities:
  1. You've pressed [Esc]: Forth protests and reacts with an error message.
  2. You pressed some other key, not [Esc], not [Space]: Forth prints -1 (TRUE).
  3. You pressed [Space]: Forth pauses, stops all action, and waits for another key. Is this key a [Space] again, then the result is 0 (FALSE). After [Esc] Forth protests, and all other keys produce -1 (TRUE).
This seems terribly complicated, but it's quite natural when you get the hang of it. STOP? (as mentioned, not a standard Forth word) is used a lot in words like WORDS, SEE, DUMP, etc., words that produce text that can run off the screen. With STOP? it is possible to toggle output (temporarily) on and off, or to abort output completely.
: MANY ( -- ) >IN @ STOP? AND >IN ! ;
As long as STOP? produces zero, >IN is made zero: Forth is fooled, an 'thinks' it hasn't yet read the line, starting at the very first character again.

With MANY, interactive loops are possible:
) 888 . MANY DROP [rtn] ?
Utterly useless, but:
) BL [rtn]
) DUP EMIT 1+ MANY DROP [rtn] ?
>>