Forth: Immediacy and postponement

When reading a Forth tutorial, a lot of time is usually spent explaining RPN and
the data and return stack, which are extremely easy to understand for most
people having been exposed to some sort of STEM subjects. Then, examples pop up
with words like "IMMEDIATE" and "POSTPONE" which it seems the author thinks the
reader finds just as easy, while using jargon not used outside Forth
programming. This is my attempt at writing the explanation I wish I had found a
long time ago, written so I can better my own understanding.


    1.0 Compilation

Forth words are very much like sub-routines, but just as the Forth machine has
two modes, interpretation and compilation, Forth words may be thought of as
having interpretation semantics and compilation semantics. The literal "5" will
during interpretation have the value 5 pushed on to the data stack, but during
compilation, it will simply have code which will push 5 appended to whatever
word is currently being compiled. This is the case of most words, with one major
exception being control flow words.


    1.1 Control flow

Control flow words execute during compilation, pushing and popping references to
and from the control flow stack (which may or may not be implemented using the
data stack). A word like "IF" has no standards defined interpretation semantics
at all. "IF" will as well as appending the "IF" test to the current word, push
to the control flow stack a forward reference for where it's going to jump. That
forward reference can then be resolved by an "ELSE" or "THEN" later in the
source code. Words which execute during compilation, are called immediate.


    2.0 IMMEDIATE

Set the compilation semantics of the last defined word to be its interpretation
semantics.

GForth has a very good explanation:

http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Interpretation-and-Compilation-Semantics-and-Immediacy-Tutorial.html#Interpretation-and-Compilation-Semantics-and-Immediacy-Tutorial


    3.0 POSTPONE

Compile a word's compilation semantics instead of its interpretation semantics.

Again, GForth has a very good explanation:

http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/POSTPONE-Tutorial.html#POSTPONE-Tutorial


    4.0 Putting it together

"POSTPONE" compiles whatever the next word would have done during compilation
into the current word. "IMMEDIATE" makes the last defined word behave during
compilation as it does during interpretation.


    4.1 An example

Let us assume there exists an ALGOL fandom and define:

    : FI POSTPONE THEN ; IMMEDIATE

In other words, when running "FI", run the compilation semantics of "THEN", and
by the way, let "FI" be a word where the interpretation semantics becomes the
compilation semantics. So, "FI" is now a word which during compilation behaves
exactly as "THEN". Using "SEE" on a word containing "FI" will only show the
"THEN" which has been compiled into that word.

Edited 20210910T190453Z, 20210911T123750Z.

Steinar Knutsen, 20231001T094944Z, 79378A74