Forth (programming language): Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Altered template type. | Use this bot. Report bugs. | Suggested by Headbomb | Linked from Wikipedia:WikiProject_Academic_Journals/Journals_cited_by_Wikipedia/Sandbox | #UCB_webform_linked 215/591
Remove erroneous newline
 
(33 intermediate revisions by 18 users not shown)
Line 2:
{{Redirect|FORTH||Forth (disambiguation)}}
{{Infobox programming language
| name = Forth
| paradigm = [[Concatenative programming language|concatenative]] ([[Stack-oriented programming|stack-based]]), [[Procedural programming|procedural]], [[Reflective programming|reflective]]
| year = {{start date and age|1970}}
| designer = [[Charles H. Moore]]
| typing = Typeless
| implementations = SwiftForth (Forth, Inc.)<br/>[[Gforth]] (GNU Project)<br/>VFX Forth (MicroProcessor Engineering)
| influenced = [[Bitcoin#Transactions|Bitcoin Script]], [[Factor (programming language)|Factor]], [[Joy (programming language)|Joy]], [[RPL (programming language)|RPL]], [[Rebol]], [[STOIC]]
| file_ext = .fs, .fth, .4th, .f, .forth{{cn|date=April 2022}}
| website = {{URL|https://forth-standard.org}}
}}
 
'''Forth''' is a [[Stack-oriented programming|stack-oriented]] [[programming language]] and interactive [[integrated development environment]] designed by [[Charles H. Moore|Charles H. "Chuck" Moore]] and first used by other programmers in 1970.
Although not an [[acronym]], the language's name in its early years was often spelled in [[all caps|all capital letters]] as ''FORTH''.
The FORTH-79 and FORTH-83 implementations, which were not written by Moore, became ''[[de facto]]'' standards, and an official [[technical standard]] of the language was published in 1994 as ANS Forth.
A wide range of Forth derivatives existed before and after ANS Forth.
The [[free and open-source software]] [[Gforth]] implementation is actively maintained, as are several [[Commercial software|commercially]] supported systems.
 
Forth typically combines a compiler with an integrated command shell, where the user interacts via [[subroutine]]s called ''words''.{{
Forth typically combines a compiler with an integrated command shell,{{efn|There are exceptions, such as Ulrich Hoffmann's preForth [http://www.euroforth.org/ef18/papers/hoffmann-slides.pdf][http://www.euroforth.org/ef18/papers/hoffmann.pdf] and Tom Zimmer's TCOM}} where the user interacts via [[subroutine]]s called ''words''. Words can be defined, tested, redefined, and debugged without recompiling or restarting the whole program. All syntactic elements, including variables, operators, and control flow, are defined as words. A [[stack (abstract data type)|stack]] is used to pass parameters between words, leading to a [[Reverse Polish Notation]] style.
Efn|
There are exceptions, such as Ulrich Hoffmann's preForth<ref name = "preForth slides"/><ref name="preForth"/> and Tom Zimmer's TCOM
}}
 
Forth typically combines a compiler with an integrated command shell,{{efn|There are exceptions, such as Ulrich Hoffmann's preForth [http://www.euroforth.org/ef18/papers/hoffmann-slides.pdf][http://www.euroforth.org/ef18/papers/hoffmann.pdf] and Tom Zimmer's TCOM}} where the user interacts via [[subroutine]]s called ''words''. Words can be defined, tested, redefined, and debugged without recompiling or restarting the whole program. All syntactic elements, including variables, operators, and control flow, are defined as words. A [[stack (abstract data type)|stack]] is used to pass parameters between words, leading to a [[Reverse Polish Notationnotation]] style.
For much of Forth's existence, the standard technique was to compile to [[threaded code]], which can be interpreted faster than [[bytecode]]. One of the early benefits of Forth was size: an entire development environment—including compiler, editor, and user programs—could fit in memory on an 8-bit or similarly limited system. No longer constrained by space, there are modern implementations that generate [[Compiler optimization|optimized]] [[machine code]] like other language compilers. The relative simplicity of creating a basic Forth system has led to many personal and proprietary variants, such as the custom Forth used to implement the bestselling 1986 video game ''[[Starflight]]'' from [[Electronic Arts]].<ref name="maher">{{cite web|last1=Maher|first1=Jimmy|title=Starflight|url=https://www.filfre.net/2014/10/starflight/|website=The Digital Antiquarian|date=October 28, 2014|access-date=April 29, 2023}}</ref>
 
For much of Forth's existence, the standard technique was to compile to [[threaded code]], which can be interpreted faster than [[bytecode]]. One of the early benefits of Forth was size: an entire development environment—including compiler, editor, and user programs—could fit in memory on an 8-bit or similarly limited system. No longer constrained by space, there are modern implementations that generate [[CompilerOptimizing optimizationcompiler|optimized]] [[machine code]] like other language compilers. The relative simplicity of creating a basic Forth system has led to many personal and proprietary variants, such as the custom Forth used to implement the bestselling 1986 video game ''[[Starflight]]'' from [[Electronic Arts]].<ref name="maher">{{cite web|last1=Maher|first1=Jimmy|title=Starflight|url=https://www.filfre.net/2014/10/starflight/|website=The Digital Antiquarian|date=October 28, 2014|access-date=April 29, 2023}}</ref>
Forth is used in the [[Open Firmware]] [[boot loader]], in [[spaceflight]] applications<ref name="oSWRm">[https://web.archive.org/web/20101024223709/http://forth.gsfc.nasa.gov/ NASA applications of Forth] (original NASA server no longer running, copy from archive.org)</ref> such as the [[Philae (spacecraft)|Philae spacecraft]],<ref name="zYmBe">{{cite web|title=Intersil's RTX processors and Forth software controlled the successful Philae landing|url=https://mpeforth.com/press/MPE_PR_From_Telescope_to_Comet_2014_11_13.pdf |access-date=April 29, 2023|website=MicroProcessor Engineering Limited|date=October 13, 2014}}</ref><ref name="sgEMh">{{cite web|title=Here comes Philae! Powered by an RTX2010|url=https://www.cpushack.com/2014/11/12/here-comes-philae-powered-by-an-rtx2010/|website=The CPU Shack Museum|date=October 12, 2014|access-date=April 29, 2023}}</ref> and in other embedded systems which involve interaction with hardware.
 
The relative simplicity of creating a basic Forth system has led to many personal and proprietary variants, such as the custom Forth used to implement the bestselling 1986 video game ''[[Starflight]]'' from [[Electronic Arts]].<ref name="maher">{{cite web|last1=Maher|first1=Jimmy|title=Starflight|url=https://www.filfre.net/2014/10/starflight/|website=The Digital Antiquarian|date=October 28, 2014|access-date=April 29, 2023}}</ref> Forth is used in the [[Open Firmware]] [[boot loader]], in [[spaceflight]] applications<ref name="oSWRm">[https://web.archive.org/web/20101024223709/http://forth.gsfc.nasa.gov/ NASA applications of Forth] (original NASA server no longer running, copy from archive.org)</ref> such as the [[Philae (spacecraft)|''Philae'' spacecraft]],<ref name="zYmBe">{{cite web|title=Intersil's RTX processors and Forth software controlled the successful Philae landing|url=https://mpeforth.com/press/MPE_PR_From_Telescope_to_Comet_2014_11_13.pdf |access-date=April 29, 2023|website=MicroProcessor Engineering Limited|date=October 13, 2014|archive-date=April 29, 2023|archive-url=https://web.archive.org/web/20230429195754/https://mpeforth.com/press/MPE_PR_From_Telescope_to_Comet_2014_11_13.pdf|url-status=dead}}</ref><ref name="sgEMh">{{cite web|title=Here comes Philae! Powered by an RTX2010|url=https://www.cpushack.com/2014/11/12/here-comes-philae-powered-by-an-rtx2010/|website=The CPU Shack Museum|date=October 12, 2014|access-date=April 29, 2023}}</ref> and in other embedded systems which involve interaction with hardware.
Moore developed a series of microprocessors for executing compiled Forth-like code directly and experimented with smaller languages based on Forth concepts, including cmForth and [[colorForth]]. Most of these languages were designed to support Moore's own projects, such as chip design.
 
Beginning in the early 1980s, Moore developed a series of microprocessors for executing compiled Forth-like code directly and experimented with smaller languages based on Forth concepts, including cmForth and [[colorForth]]. Most of these languages were designed to support Moore's own projects, such as chip design.
 
== Uses ==
 
Forth has a niche in astronomical and space applications<ref name="C4Oss">{{Cite web | url=http://forth.gsfc.nasa.gov | title=Space Related Applications of Forth | access-date=2007-09-04 | url-status=dead | archive-url=https://web.archive.org/web/20101024223709/http://forth.gsfc.nasa.gov/ | archive-date=2010-10-24}}</ref> as well as a history in [[embedded system]]s. The [[Open Firmware]] [[booting|boot ROMsROM]]s used by [[Apple Inc.|Apple]], [[IBM]], [[Sun Microsystems|Sun]], and [[OLPC XO|OLPC XO-1]] contain a Forth environment.
 
Forth has often been used to bring up new hardware. Forth was the first [[Resident monitor|resident software]] on the new [[Intel 8086]] chip in 1978, and MacFORTH was the first resident development system for the [[Macintosh 128K]] in 1984.<ref name="evolution" />
 
Circa 1982, [[Atari, Inc.]] used an elaborate animated demo written in Forth to showcase capabilities of the [[Atari 8-bit computers]] in department stores.<ref>{{cite web |title=Atari In-Store Demonstration Program |url=http://www.atarimania.com/demo-atari-400-800-xl-xe-in-store-demonstration-program_19329.html |website=Atari Mania}}</ref> [[Electronic Arts]] published multiple video games in the 1980s that were written in Forth, including ''[[Worms?]]'' (1983),<ref>{{cite web |last1=Maynard |first1=David S. |title=David Maynard: Software Artist |url=https://www.software-artist.com}}</ref> ''[[Adventure Construction Set]]'' (1984),<ref>{{cite podcast |url=https://appletimewarp.libsyn.com/episode-9-stuart-smith |title=Time Warp: Episode 9 - Stuart Smith |host=John Romero |date=2020-09-23 |time=29:02}}</ref> ''[[Amnesia_(1986_video_game)|Amnesia]]'' (1986),<ref>{{cite book |last=Aycock |first=John |year=2023 |title=Amnesia Remembered: Reverse Engineering a Digital Artifact |url=https://www.berghahnbooks.com/title/AycockAmnesia |publisher=Berghahn Books |page=79 |isbn=978-1800738676}}</ref> ''[[Starflight]]'' (1986),<ref name=maher/> and ''[[Lords of Conquest]]'' (1986). Robot coding game ''[[ChipWits]]'' (1984) was writtendeveloped in MacFORTH. for the [[Macintosh 128K]], <ref>{{cite web |last1=Sharp |first1=Doug |title=FORTH Programming Language (Going FORTH) |date=8 April 2023 |url=https://chipwits.com/2023/04/08/forth-programming-language-going-forth/}}</ref> while the [[Commodore 64]] port was written with SuperForth 64.<ref>{{cite web| url=https://chipwits.com/2025/02/06/back-to-1985-how-we-remade-chipwits-for-the-c64/ |title=Back to 1985: How We Remade ChipWits for the C64 |last=Rochat |first=Jan |date=February 6, 2025}}</ref>
 
[[Ashton-Tate#RapidFile|Ashton-Tate's RapidFile]] (1986), a flat-file database program, and VP-Planner<ref>{{cite web |title=FORTH GETS ITS OWN SPECIAL INTEREST GROUP |url=https://techmonitor.ai/technology/forth_gets_its_own_special_interest_group |website= Tech Monitor|date=6 February 1989 }}</ref> from [[Paperback Software International]] (1983), a spreadsheet program competing with [[Lotus 1-2-3]], were written in Forth.
Line 34 ⟶ 44:
The [[Canon Cat]] (1987) uses Forth for its system programming.
 
[[Rockwell International|Rockwell]] produced single-chip microcomputers with resident Forth kernels: the R65F11 and R65F12. ASYST was a Forth expansion for measuring and controlling on PCs.<ref name="Rwirj">Campbell et al, "Up and Running with Asyst 2.0", MacMillan Software Co., 1987</ref>
 
ASYST was a Forth expansion for measuring and controlling on PCs.<ref name="Rwirj">Campbell et al, "Up and Running with Asyst 2.0", MacMillan Software Co., 1987</ref>
The Forth programming language was used in the production of ''[[Star Wars (film)|Star Wars: Episode IV &ndash; A New Hope]]''.{{citation needed|date=July 2024}} Specifically, it was used to control the Dystraflex motion control system, which was created by [[John Dykstra]] and his team at [[Industrial Light & Magic]]. This system was instrumental in creating the complex and precise camera movements required for the space battle scenes and the trench run sequences on the [[Death Star]].
 
The Dystraflex system allowed for the pre-programming of camera movements, which could then be repeated exactly for multiple takes. This was crucial for combining live-action footage with model shots and other visual effects. Forth was chosen for its efficiency and its ability to handle real-time processing required for these intricate tasks.
 
== History ==
 
Forth evolved from [[Charles H. Moore]]'s personal programming system, which had been in continuous development since 1968.<ref name="evolution">{{cite book |last1=Rather |first1=Elizabeth D. |last2=Colburn |first2=Donald R. |last3=Moore |first3=Charles H. |title=History of programming languages---II |chapter=The evolution of Forth |chapter-url=http://www.forth.com/resources/evolution/index.html |editor-first=Thomas J. |editor-last=Bergin |editor2-first=Richard G. |editor2-last=Gibson |publisher=Association for Computing Machinery |date=1996 |isbn=0201895021 |pages=625–670 |doi=10.1145/234286.1057832 |orig-year=1993}}</ref><ref name="WYK4Z">{{Cite web |last=Moore |first=Charles H. |year=1991 |url=http://www.colorforth.com/HOPL.html |title=Forth - The Early Years |access-date=2006-06-03 |url-status=dead |archive-url=https://web.archive.org/web/20060615025259/http://www.colorforth.com/HOPL.html |archive-date=2006-06-15}}</ref> Forth was first exposed to other programmers in the early 1970s, starting with [[Elizabeth Rather]] at the United States [[National Radio Astronomy Observatory]] (NRAO).<ref name="evolution" /> After their work at NRAO, Charles Moore and Elizabeth Rather formed FORTH, Inc. in 1973, refining and porting Forth systems to dozens of other platforms in the next decade.
 
Line 52 ⟶ 61:
Forth was popular in the early 1980s,<ref name="1AvuY">{{Citation | title=The Forth Language | journal=BYTE Magazine |volume=5 |issue=8 | year=1980 | url=https://archive.org/details/byte-magazine-1980-08/}}</ref> because it was well suited to the limited memory of [[microcomputer]]s. The ease of implementing the language led to many implementations.<ref name="family-tree">{{Cite web | url=https://www.complang.tuwien.ac.at/forth/family-tree/ | title=Forth family tree and timeline | author=M. Anton Ertl}}</ref> The [[Jupiter Ace|Jupiter ACE]] home computer has Forth in its [[Read-only memory|ROM]]-resident operating system. Insoft GraFORTH is a version of Forth with graphics extensions for the Apple II.<ref name="gWyzb">{{Cite web |last1=Lutus |first1=Paul |title=GraFORTH Language Manual |url=https://archive.org/details/graforth1 |website=archive.org |publisher=Insoft |date=1982}}</ref>
 
Common practice was codified in the de facto standards FORTH-79<ref name="qyrcN">{{Cite web | url=https://www.physics.wisc.edu/~lmaurer/forth/Forth-79.pdf | title=The Forth-79 Standard | access-date=2023-04-29}}</ref> and FORTH-83<ref name="BQ2oi">{{Cite web | url=https://forth.sourceforge.net/standard/fst83/ | title=The Forth-83 Standard | access-date=2023-04-29 | archive-date=2023-04-05 | archive-url=https://web.archive.org/web/20230405001333/https://forth.sourceforge.net/standard/fst83/ | url-status=dead }}</ref> in the years 1979 and 1983, respectively. These standards were unified by [[American National Standards Institute|ANSI]] in 1994, commonly referred to as {{Not a typo|ANS}} <!-- not a misspelling --> Forth.<ref name="IGDIN">{{Cite web | publisher = ANSI technical committee X3J14 | date = 24 March 1994 | url = https://www.taygeta.com/forth/dpans.html | title = Programming Languages: Forth | access-date = 2006-06-03}}</ref><ref name="KojQK">{{Cite web | publisher = Quartus Handheld Software | date = 13 September 2005 | url = http://quartus.net/files/PalmOS/Forth/Docs/stdref.pdf | title = Standard Forth (ANSI INCITS 215-1994) Reference | access-date = 2023-04-29}}</ref>
 
As of 2018, the source for the original 1130 version of FORTH has been recovered, and is now being updated to run on a restored or emulated 1130 system.<ref name="BtuRy">{{Cite web |last1=Claunch |first1=Carl |title=Restoring the original source code for FORTH on the IBM 1130 |url=https://rescue1130.blogspot.com/2018/03/restoring-original-source-code-for.html |website=rescue1130 |access-date=July 30, 2018|date=2018-03-02}}</ref>
 
== Overview ==
 
{{further|Reverse Polish notation}}
 
Forth emphasizes the use of small, simple functions called ''words''. Words for bigger tasks call upon many smaller words that each accomplish a distinct sub-task. A large Forth program is a hierarchy of words. These words, being distinct modules that communicate implicitly via a stack mechanism, can be prototyped, built and tested independently. The highest level of Forth code may resemble an English-language description of the application. Forth has been called a ''meta-application language'': a language that can be used to create [[Domain-specific language|problem-oriented languages]].<ref>
{{citeCite book
|last=Brodie |first=Leoisbn |title=Starting Forth |edition=2nd |year=1987 |publisher=Prentice-Hall |isbn=978-0-13-843079-5 |url=https://www.forth.com/starting-forth/index.html}}</ref>
| edition = 2nd
| last = Brodie
| first = Leo
| publisher = Prentice-Hall
| title = Starting Forth
| url = https://www.forth.com/starting-forth/index.html
| year = 1987
}}
</ref>
 
Forth relies on explicitimplicit use of a [[stack (abstract data structuretype)|data stack]] and [[reverse Polish notation]] which is commonly used in calculators from [[Hewlett-Packard]]. In RPN, the operator is placed after its operands, as opposed to the more common [[infix notation]] where the operator is placed between its operands. Postfix notation makes the language easier to parse and extend; Forth's flexibility makes a static [[Backus-Naur form|BNF]] grammar inappropriate, and it does not have a monolithic compiler. Extending the compiler only requires writing a new word, instead of modifying a grammar and changing the underlying implementation.
 
Using RPN, one can getcompute the resultvalue of the mathematicalarithmetic expression <code>(25 *× 10) + 50)</code> thisin the following way:
<!-- https://pygments.org/docs/lexers/#pygments.lexers.forth.ForthLexer -->
<syntaxhighlight lang="forth">
Line 74 ⟶ 95:
 
[[File:Forthstack1 5.svg|150px|left]]
<br/>The word <code>*</code> takes the top two numbers from the stack, multiplies them, and puts the product back on the stack.{{clear}}
 
[[File:Forthstack2.svg|150px|left]]
Line 80 ⟶ 101:
 
[[File:Forthstack3.svg|150px|left]]
<br/>The word <code>+</code> adds the top two values, pushing the sum. <code>CR</code> ([[carriage return]]) starts the output on a new line. Finally, <code>.</code> prints the result. As everything has completed successfully, the Forth system prints <code>OK</code>.<ref name="3yqc9">{{harvnbHarvnb
| Brodie
| 1987
| p = 20
}}
</ref>{{clear}}
 
Even Forth's structural features are stack-based. For example:
Line 110 ⟶ 136:
 
==Facilities==
Forth's [[Formal grammar|grammar]] has no official specification. Instead, it is defined by a simple algorithm. The interpreter reads a line of input from the user input device, which is then parsed for a word using spaces as a [[delimiter]]; some systems recognise additional [[Whitespace (computer science)character|whitespace]] characters. When the interpreter finds a word, it looks the word up in the ''dictionary''. If the word is found, the interpreter executes the code associated with the word, and then returns to parse the rest of the input stream. If the word isn't found, the word is assumed to be a number and an attempt is made to convert it into a number and push it on the stack; if successful, the interpreter continues parsing the input stream. Otherwise, if both the lookup and the number conversion fail, the interpreter prints the word followed by an error message indicating that the word is not recognised, flushes the input stream, and waits for new user input.<ref name="6dQ7P">
{{harvnbHarvnb
| Brodie
| 1987
| p = 14
}}
</ref>
 
The definition of a new word is started with the word <code>:</code> (colon) and ends with the word <code>;</code> (semi-colon). For example,
Line 116 ⟶ 148:
: X DUP 1+ . . ;
</syntaxhighlight>
will compile the word <code>X</code>, and makes the name findable in the dictionary. When executed by typing <code>10 X</code> at the console this will print <code>11 10</code>.<ref name="compiler">
{{harvnbHarvnb
| Brodie
| 1987
| p = 16
}}
</ref>
 
Most Forth systems include an [[Assembly language#Assembler|assembler]] to write words using the processor's facilities. Forth assemblers often use a reverse Polish syntax in which the parameters of an instruction precede the instruction. A typical reverse Polish assembler prepares the operands on the stack and the mnemonic copies the whole instruction into memory as the last step. A Forth assembler is by nature a macro assembler, so that it is easy to define an alias for registers according to their role in the Forth system: e.g. "dsp" for the register used as the data stack pointer.<ref name="NAFQu">{{cite web | last = Rodriguez | first = Brad | url = https://www.bradrodriguez.com/papers/6809asm.txt | title = Build Your Own Assembler, Part 2: a 6809 Forth Assembler | access-date = 2023-04-29}}</ref>
Line 143 ⟶ 181:
A defined word generally consists of ''head'' and ''body'' with the head consisting of the ''name field'' (NF) and the ''link field'' (LF), and body consisting of the ''code field'' (CF) and the ''parameter field'' (PF).
 
Head and body of a dictionary entry are treated separately because they may not be contiguous. For example, when a Forth program is recompiled for a new platform, the head may remain on the compiling computer, while the body goes to the new platform. In some environments (such as [[embedded system]]s) the heads occupy memory unnecessarily. However, some cross-compilers may put heads in the target if the target itself is expected to support an interactive Forth.<ref name="lx5fY">{{cite journal |last=Martin |first=Harold M. |date=March 1991 |title=Developing a tethered Forth model |journal=ACM SigforthSIGFORTH Newsletter |volume=2 |issue=3 |pages=17–19 |publisher=ACM Press |doi=10.1145/122089.122091 |doi-access=free | s2cid=26362015}}</ref>
 
The exact format of a dictionary entry is not prescribed, and implementations vary.
 
===Structure of the compiler===
The compiler itself is not a monolithic program. It consists of Forth words visible to the system, and usable by a programmer. This allows a programmer to change the compiler's words for special purposes. Compilation in traditional Forth systems is straightforward and does not involve building and optimizing an abstract representation of the code. (Some newer Forth compilers use more elaborate compilation methods, as common in other languages.)
 
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>.
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,<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 {{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:
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
| title = Ulrich Hoffmann's preForth slides
}}
</ref>
<ref name="preForth">
{{Cite web
| url = http://www.euroforth.org/ef18/papers/hoffmann.pdf
| title = Ulrich Hoffmann's preForth
}}
</ref>
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 ...
Line 169 ⟶ 221:
The interpreter state can be changed manually with the words <code><nowiki>[</nowiki></code> (left-bracket) and <code><nowiki>]</nowiki></code> (right-bracket) which enter interpretation state or compilation state, respectively. These words can be used with the word <code>LITERAL</code> to calculate a value during a compilation and to insert the calculated value into the current colon definition. <code>LITERAL</code> has the compilation semantics to take an object from the data stack and to append semantics to the current colon definition to place that object on the data stack.
 
In {{Not a typo|ANS}} <!-- not a misspelling --> Forth, the current state of the interpreter can be read from the [[flag (computingprogramming)|flag]] <code>STATE</code> which contains the value true when in compilation state and false otherwise. This allows the implementation of so-called ''state-smart words'' with behavior that changes according to the current state of the interpreter.
 
==== Immediate words ====
Line 300 ⟶ 352:
2C F9 4C EE DC 5 test \ output should be: F1 38 29 C9 DE
</syntaxhighlight>
 
== Forth engines ==
 
A processor designed to support a specific programming language is called a language "engine".<ref name="dumse">Randy M. Dumse.
"The R65F11 and F68K Single-Chip Forth Computers".
[http://www.forth.org/bournemouth/jfar/vol2/no1/article1.pdf]{{dead link|date=May 2017|bot=InternetArchiveBot|fix-attempted=yes}}
[http://soton.mpeforth.com/flag/jfar/vol2/no1/article1.pdf] {{Webarchive|url=https://web.archive.org/web/20141202144044/http://soton.mpeforth.com/flag/jfar/vol2/no1/article1.pdf|date=2014-12-02}}
1984.</ref>
 
Forth engines are hardware platforms specifically designed to support developing and running programs written in Forth.
(Likewise, [[Lisp machine]]s were specifically designed to support developing and running programs written in Lisp, the [[Pascal MicroEngine]] was specifically designed to support developing and running programs written in Pascal, etc.).
 
The first commercially available single-chip Forth engine was the Rockwell R65F11<ref name="dumse" />,
a chip that includes a Forth kernel in ROM,
an [[MOS Technology 6502#Variations and derivatives | enhanced 6502]], SRAM, and various interface circuits that previously required peripheral chips.<ref name="dumse" /><ref>
Ed Schmauch.
[http://www.forth.org/bournemouth/jfar/vol4/no2/article48.pdf "A Computerized Corrosion Monitoring System"]{{dead link|date=May 2017 |bot=InternetArchiveBot |fix-attempted=yes}}.
1986.
</ref><ref>Lawrence P. Forsley.
[https://books.google.com/books?id=Yx8YAQAAMAAJ "Embedded systems: 1990 Rochester Forth Conference: June 12 – 16th, 1990 University of Rochester"] {{Webarchive|url=https://web.archive.org/web/20150325214054/http://books.google.com/books?id=Yx8YAQAAMAAJ |date=2015-03-25}}.
p. 51.</ref><ref>Rockwell.
[http://www.smallestplcoftheworld.org/RSC-FORTH_User's_Manual.pdf "RSC-Forth User's Manual"] {{Webarchive|url=https://web.archive.org/web/20131207015149/http://smallestplcoftheworld.org/RSC-FORTH_User%27s_Manual.pdf |date=2013-12-07}}.
1983.</ref><ref>{{cite web
|title=Rockwell R65F11 R65F12 Forth Based Microcomputers
|url=http://archive.6502.org/datasheets/rockwell_r65f11_r65f12_forth_microcomputers.pdf
|date=June 1987
|access-date=28 Apr 2020
|archive-date=4 August 2020
|archive-url=https://web.archive.org/web/20200804035007/http://archive.6502.org/datasheets/rockwell_r65f11_r65f12_forth_microcomputers.pdf
|url-status=live
}}</ref>
 
Many other commercial CPUs (Harris RTX-2000, Novix NC4016, F21, MARC4, KimKlone, etc.) and many [[homebrew CPU]]s
(My4TH, J1, H2, Mark 1 FORTH Computer, etc.)
are specifically designed to run Forth.
Typically they implement common Forth primitives such as the "Forth NEXT" as single instructions.
 
==Implementations==
Because Forth is simple to implement and has no standard reference implementation, there are numerous versions of the language. In addition to supporting the standard varieties of desktop computer systems ([[POSIX]], [[Microsoft Windows]], [[macOS]]), many of these Forth systems also target a variety of [[embedded systemssystem]]s. Listed here are some of the systems which conform to the 1994 {{Not a typo|ANS}} <!-- not a misspelling --> Forth standard.
* ASYST, a Forth-like system for data collection and analysis <ref>{{cite journal |last1=Hary |first1=David |first2=Koichi |last2=Oshio |first3=Steven D. |last3=Flanagan|title=The ASYST Software for Scientific Computing |journal=Science |volume=236 |issue=4805 |date=1987 |pages=1128–32 |jstor=1699106 |pmid=17799670 |doi=10.1126/science.236.4805.1128 |bibcode=1987Sci...236.1128H |s2cid=30463062}}</ref><ref name="Rwirj" />
* [[Gforth]], a portable {{Not a typo|ANS}} Forth implementation from the [[GNU Project]]
* [https://home.hccnet.nl/anij/nof/noforth.html noForth], an ANS Forth implementation (as far as possible) for Flash microcontrollers (MSP430, & RiscRISC-V & RP2040)
* [[Open Firmware]], a [[bootloader]] and [[Firmwarefirmware]] standard based on {{Not a typo|ANS}} Forth
* [[pForth]], portable Forth written in C
* SP-Forth, {{Not a typo|ANS}} Forth implementation from the Russian Forth Interest Group (RuFIG)
Line 312 ⟶ 400:
* VFX Forth, optimizing native code Forth
* [https://littlemanstackmachine.org/firth.html Firth], an adaptation of Forth for the Little Man Stack Machine computer.
* [https://gitlab.com/higaski/Shi Shi], a fast and tiny embeddable Forth implementation written for the Thumb-2 ISA (ARMv7-M and newer).
 
== See also ==
* [[:Category:Video games written in Forth|Video games written in Forth]]
* [[RTX2010]], a CPU that runs Forth natively