Forth (programming language): Difference between revisions

Content deleted Content added
Ternary operators are a class of operators. C only has one ternary operator, the conditional operator.
Structure of the compiler: format code as code
Line 145:
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 <code>?EXIT</code> used in Ulrich Hoffmann's preForth,<ref name="preForth slides">[http://www.euroforth.org/ef18/papers/hoffmann-slides.pdf Ulrich Hoffmann's preForth slides]</ref><ref name="preForth">[http://www.euroforth.org/ef18/papers/hoffmann.pdf Ulrich Hoffmann's preForth]</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 <code>?BRANCH</code> (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 ...