Revision 2017-03-02
Background
Forth word EXIT is used to
immediately terminate execution of the definition in which it
appears and to return to the
calling function. EXIT is
typically found within conditional statements. An example is ELSE elimination - a technique
used in Forth to improve efficiency. It works as follows.
A definition is found to terminate with an IF ELSE THEN statement:
: WORD1 ... ( flag) IF A ELSE B THEN ;
It can be rewritten thus:
: WORD2 ... ( flag) IF A EXIT THEN B ;
The advantage of the latter is faster execution of A since the unconditional jump
represented by ELSE is eliminated.
Code size is also reduced.
END
The sequence EXIT THEN occurs
so frequently in Forth that it can outnumber other control
structures such as
BEGIN WHILE REPEAT . An
examination of source files supplied with several well-known
Forth systems revealed
counts in the hundreds. Given the large numbers, consideration
was given as to whether a new control flow word might
replace the sequence EXIT THEN -
the object being simpler and clearer Forth code.
Similar optimizations already exist in Forth, for example REPEAT , which replaces the sequence AGAIN THEN . The
function of THEN in this instance
is to inform the compiler where to direct the branch from WHILE. From a language
perspective only one word is necessary
REPEAT to describe the action at run-time. The same is
true of EXIT THEN.
THEN may be hidden since it
exists only for the benefit of the compiler at compile-time. END is the suggested word to
replace EXIT THEN.
The name END was formerly used
as a synonym for UNTIL in Fig-FORTH
however even in the day its use was
exceptional. Importantly no current Forth system appears to use END making it available for the
purpose proposed here.
Using END the previous definition would be written:
: WORD2 ... ( flag) IF A END B ;
Occasionally conditional code may contain an EXIT that is not directly followed by THEN making substitution with
END problematic e.g.
( flag) IF A EXIT ELSE B THEN
Notice however use of ELSE here
is redundant as it generates a jump that is never executed. Since
it serves no purpose
the ELSE may be removed and the
function then reduces to:
( flag) IF A END B
Other Uses
An important use of END is in CASE statements where it can be used
to perform the equivalent of ELSE elimination.
Consider the following code fragment taken from a commercial
application:
... BEGIN 0 KEY $20 OR CASE [CHAR] n OF ... 1- ENDOF [CHAR] s OF ... 1- ENDOF [CHAR] r OF ... 1- ENDOF [CHAR] f OF ... 1- ENDOF ENDCASE UNTIL ;
With an appropriate implementation of OF it may be replaced with:
... BEGIN KEY $20 OR [CHAR] n OF ... END [CHAR] s OF ... END [CHAR] r OF ... END [CHAR] f OF ... END DROP AGAIN ;
By replacing ENDOF with END the need to maintain a flag and
code to effect an exit is eliminated resulting in simpler
more efficient code.
The Future of EXIT
The consequence of replacing EXIT
THEN with END is a
dramatic reduction in the use of EXIT.
Such will be the
impact that the very appearance of Forth code will change. Apart
from a few specialized uses, EXIT will
largely
disappear from Forth programs. Such a change should not be seen
as a revolution, but rather a stepping stone in Forth's
evolution.
Conclusion
The benefit of replacing EXIT THEN in
Forth source with END is simpler
and more readable code. Other word pairs
ending with THEN may also be
found in Forth but none occur in such numbers as EXIT THEN . Use of END within
Case statements as a short-cut exit mechanism is both desirable
and significant, avoiding the need for specialized CASE
words.
Implementation
Sample definitions
: END POSTPONE EXIT POSTPONE THEN ; IMMEDIATE
: END S" EXIT THEN" EVALUATE ; IMMEDIATE
Top Home Forth
Page updated: 2017-03-02