Forth (programming language): Difference between revisions

Content deleted Content added
Format citations
Remove erroneous newline
 
Line 190:
The "compile time" flag in the name field is set for words with "compile time" behavior. Most simple words execute the same code whether they are typed on a command line, or embedded in code. When compiling these, the compiler simply places code or a threaded pointer to the word.<ref name="compiler" />
 
The classic examples of compile-time words are the [[control structure]]s such as <code>IF</code> and <code>WHILE</code>.
Almost all of Forth's control structures and almost all of its compiler are implemented as compile-time words.
Apart from some rarely used [[control flow]] words only found in a few implementations, such as the conditional return word {{PreCode|?EXIT}} used in Ulrich Hoffmann's preForth, all of Forth's [[control flow]] words are executed during compilation to compile various combinations of primitive words along with their branch addresses.<ref name = "preForth slides">
{{Cite web
| url = http://www.euroforth.org/ef18/papers/hoffmann-slides.pdf
Line 202 ⟶ 204:
}}
</ref>
all of Forth's [[control flow]] words are executed during compilation to compile various combinations of primitive words along with their branch addresses. For instance, <code>IF</code> and <code>WHILE</code>, and the words that match with those, set up <code>BRANCH</code> (unconditional branch) and {{PreCode|?BRANCH}} (pop a value off the stack, and branch if it is false). Counted loop [[control flow]] words work similarly but set up combinations of primitive words that work with a counter, and so on. During compilation, the data stack is used to support control structure balancing, nesting, and back-patching of branch addresses. The snippet:
<syntaxhighlight lang="forth">
... DUP 6 < IF DROP 5 ELSE 1 - THEN ...