www.delorie.com/gnu/docs/gforth/gforth_92.html   search  
 
Buy GNU books!


Gforth Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.8.4 Arbitrary control structures

ANS Forth permits and supports using control structures in a non-nested way. Information about incomplete control structures is stored on the control-flow stack. This stack may be implemented on the Forth data stack, and this is what we have done in Gforth.

An orig entry represents an unresolved forward branch, a dest entry represents a backward branch target. A few words are the basis for building any control structure possible (except control structures that need storage, like calls, coroutines, and backtracking).

doc-if doc-ahead doc-then doc-begin doc-until doc-again doc-cs-pick doc-cs-roll

The Standard words CS-PICK and CS-ROLL allow you to manipulate the control-flow stack in a portable way. Without them, you would need to know how many stack items are occupied by a control-flow entry (many systems use one cell. In Gforth they currently take three, but this may change in the future).

Some standard control structure words are built from these words:

doc-else doc-while doc-repeat

Gforth adds some more control-structure words:

doc-endif doc-?dup-if doc-?dup-0=-if

Counted loop words constitute a separate group of words:

doc-?do doc-+do doc-u+do doc--do doc-u-do doc-do doc-for doc-loop doc-+loop doc--loop doc-next doc-leave doc-?leave doc-unloop doc-done

The standard does not allow using CS-PICK and CS-ROLL on do-sys. Gforth allows it, but it's your job to ensure that for every ?DO etc. there is exactly one UNLOOP on any path through the definition (LOOP etc. compile an UNLOOP on the fall-through path). Also, you have to ensure that all LEAVEs are resolved (by using one of the loop-ending words or DONE).

Another group of control structure words are:

doc-case doc-endcase doc-of doc-endof

case-sys and of-sys cannot be processed using CS-PICK and CS-ROLL.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003