Clearing stack

Clearing the Stack
There are a few methods to clear the stack.

Clearing the Stack by Error
Most Forths clear the stack when encountering errors.

Pforth
Pforth has the non-portable (ironic, considering its name) way of clearing the stack with 0SP.

10 20 30 40   ok Stack<10> 10 20 30 40 0SP   ok Stack<10>

The Universal Depth Method
Since the previous method is unknown in many Forth, those Forths can use (if they have a depth method - which they should!) can use this very simple word:


 * CLEAR ( [n] --, clears all numbers off the current stack ) begin drop depth 0= until ;

This uses the current depth of the stack, dropping an item if depth returns anything above a zero and exiting elsewise. An example, below:
 * CLEAR ( [n] -- clears all numbers off current stack ) begin drop depth 0= until ;    ok

Stack<10> 1 3 5 7 9   ok Stack<10> 1 3 5 7 9 clear   ok Stack<10>