Forth (programming language): Difference between revisions

Jump to navigation Jump to search
imported>Citation bot
Altered template type. | Use this bot. Report bugs. | #UCB_CommandLine
 
imported>Dgpop
: Forth, the language, does not have an official logo.
 
Line 2: Line 2:
{{Redirect|FORTH||Forth (disambiguation)}}
{{Redirect|FORTH||Forth (disambiguation)}}
{{Infobox programming language
{{Infobox programming language
| name = Forth
| name           = Forth
| paradigm = [[Concatenative programming language|concatenative]] ([[Stack-oriented programming|stack-based]]), [[Procedural programming|procedural]], [[Reflective programming|reflective]]
| logo            =
| year = {{start date and age|1970}}
| paradigm       = [[Concatenative programming language|concatenative]] ([[Stack-oriented programming|stack-based]]), [[Procedural programming|procedural]], [[Reflective programming|reflective]]
| designer = [[Charles H. Moore]]
| year           = {{start date and age|1970}}
| typing = Typeless
| designer       = [[Charles H. Moore]]
| typing         = Typeless
| implementations = SwiftForth (Forth, Inc.)<br/>[[Gforth]] (GNU Project)<br/>VFX Forth (MicroProcessor Engineering)
| 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]]
| 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}}
| file_ext       = .fs, .fth, .4th, .f, .forth{{cn|date=April 2022}}
| website = {{URL|https://forth-standard.org}}
| 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''' 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,{{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.
Forth typically combines a compiler with an integrated command shell, where the user interacts via [[subroutine]]s called ''words''.{{
Efn|
There are exceptions, such as Ulrich Hoffmann's preForth<ref name = "preForth slides"/><ref name="preForth"/> and Tom Zimmer's TCOM
}}
 
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.


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 [[Optimizing compiler|optimized]] [[machine code]] like other language compilers.
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 [[Optimizing compiler|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.  


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.
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 created to support Moore's own projects, such as chip design.


== Uses ==
== 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]] [[boot ROM]]s used by [[Apple Inc.|Apple]], [[IBM]], [[Sun Microsystems|Sun]], and [[OLPC XO|OLPC XO-1]] contain a Forth environment.
Forth has a niche in astronomical and space applications<ref name="C4Oss">{{Cite web | url=https://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]] [[boot ROM]]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" />
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 developed  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>
Circa 1982, [[Atari, Inc.]] used an elaborate, self-running 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=https://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 developed  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> The prototype of ''[[Boulder Dash (video game)|Boulder Dash]]'' (1984) was implemented in Forth before being rewritten in 6502 assembly language.<ref>{{cite web |url=https://ataripodcast.libsyn.com/antic-interview-66-peter-liepa-boulder-dash |website=ANTIC The Atari 8-bit Podcast |title=Interview 66 - Peter Liepa, Boulder Dash |date=June 16, 2015}}</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.
[[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: Line 40:
The [[Canon Cat]] (1987) uses Forth for its system programming.
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>
[[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>


== History ==
== 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.
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=https://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.


Moore saw Forth as a successor to compile-link-go [[third-generation programming language]]s, or software for "fourth generation" hardware. He recalls how the name was coined:<ref>{{Cite journal |last=Moore|first=Charles H.|date=August 1980|journal=BYTE Magazine|volume=5|issue=8|pages=82|url=https://archive.org/details/byte-magazine-1980-08/page/n77/mode/2up |title=The Evolution of FORTH, an Unusual Language}}</ref>  
Moore saw Forth as a successor to compile-link-go [[third-generation programming language]]s, or software for "fourth generation" hardware. He recalls how the name was coined:<ref>{{Cite journal |last=Moore|first=Charles H.|date=August 1980|journal=BYTE Magazine|volume=5|issue=8|pages=82|url=https://archive.org/details/byte-magazine-1980-08/page/n77/mode/2up |title=The Evolution of FORTH, an Unusual Language}}</ref>  
Line 48: Line 56:
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>
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}}</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>
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 = https://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>
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==
== Overview ==
{{further|Reverse Polish notation}}
{{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>{{cite book |last=Brodie |first=Leo |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>
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>
{{Cite book
| isbn      = 978-0-13-843079-5
| 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 implicit use of a [[stack (abstract data type)|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.
Forth relies on implicit use of a [[stack (abstract data type)|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 compute the value of the arithmetic expression (25 × 10) + 50 in the following way:
Using RPN, one can compute the value of the arithmetic expression (25 × 10) + 50 in the following way:
<!-- https://pygments.org/docs/lexers/#pygments.lexers.forth.ForthLexer -->
<!-- https://pygments.org/docs/lexers/#pygments.lexers.forth.ForthLexer -->
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  25 10 * 50 + CR .
  25 10 * 50 + CR .
Line 76: Line 96:


[[File:Forthstack3.svg|150px|left]]
[[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">{{harvnb|Brodie|1987|p=20}}</ref>{{clear}}
<br/>The word <code>+</code> takes the top two numbers from the stack, adds them, and puts the sum back on the stack. <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">{{Harvnb
| Brodie
| 1987
| p = 20
}}
</ref>{{clear}}


Even Forth's structural features are stack-based. For example:
Even Forth's structural features are stack-based. For example:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : FLOOR5 ( n -- n' )  DUP 6 < IF DROP 5 ELSE 1 - THEN ;
  : FLOOR5 ( n -- n' )  DUP 6 < IF DROP 5 ELSE 1 - THEN ;
</syntaxhighlight>
</syntaxhighlight>
The colon indicates the beginning of a new definition, in this case a new word (again, ''word'' is the term used for a subroutine) called <code>FLOOR5</code>. The text in parentheses is a comment, advising that this word expects a number on the stack and will return a possibly changed number (on the stack).
The colon indicates the beginning of a new definition, in this case a new word (again, ''word'' is the term used for a subroutine) called <code>FLOOR5</code>. The text in parentheses is a comment, advising that this word expects a number on the stack and will return a possibly changed number (on the stack).


The subroutine uses the following commands: <code>DUP</code> duplicates the number on the stack; <code>6</code> pushes a 6 on top of the stack; <code>&lt;</code> compares the top two numbers on the stack (6 and the <code>DUP</code>ed input), and replaces them with a true-or-false value; <code>IF</code> takes a true-or-false value and chooses to execute commands immediately after it or to skip to the <code>ELSE</code>; <code>DROP</code> discards the value on the stack; <code>5</code> pushes a 5 on top of the stack; and <code>THEN</code> ends the conditional.
The subroutine uses the following commands: <code>DUP</code> duplicates the number on the stack; <code>6</code> pushes a 6 on top of the stack; <code>&lt;</code> compares the top two numbers on the stack (6 and the <code>DUP</code>ed input), and replaces them with a true-or-false value; <code>IF</code> takes a true-or-false value and chooses to execute commands immediately after it or to skip to the <code>ELSE</code>; <code>DROP</code> discards the value on the stack; <code>5</code> pushes a 5 on top of the stack; and <code>THEN</code> ends the conditional.


The <code>FLOOR5</code> word is equivalent to this function written in the [[C (programming language)|C programming language]] using the [[?:|conditional operator]] '?:'
The <code>FLOOR5</code> word is equivalent to this function written in the [[C (programming language)|C programming language]] using the [[Ternary conditional operator|conditional operator]] '?:'
 
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
int floor5(int v) {
int floor5(int v) {
Line 92: Line 120:
}
}
</syntaxhighlight>
</syntaxhighlight>
This function is written more succinctly as:
This function is written more succinctly as:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : FLOOR5 ( n -- n' ) 1- 5 MAX ;
  : FLOOR5 ( n -- n' ) 1- 5 MAX ;
</syntaxhighlight>
</syntaxhighlight>
This can be run as follows:
This can be run as follows:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  1 FLOOR5 CR .
  1 FLOOR5 CR .
Line 103: Line 135:
  7 ok
  7 ok
</syntaxhighlight>
</syntaxhighlight>
First a number (1 or 8) is pushed onto the stack, <code>FLOOR5</code> is called, which pops the number again and pushes the result. <code>CR</code> moves the output to a new line (again, this is only here for readability). Finally, a call to <code>.</code> pops the result and prints.
First a number (1 or 8) is pushed onto the stack, <code>FLOOR5</code> is called, which pops the number again and pushes the result. <code>CR</code> moves the output to a new line (again, this is only here for readability). Finally, a call to <code>.</code> pops the result and prints.


==Facilities==
== 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 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">{{harvnb|Brodie|1987|p=14}}</ref>
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 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">
{{Harvnb
| 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,
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,
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : X DUP 1+ . . ;
  : X DUP 1+ . . ;
</syntaxhighlight>
</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">{{harvnb|Brodie|1987|p=16}}</ref>
 
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">
{{Harvnb
| 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>
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>


=== Operating system, files, and multitasking ===
=== Operating system, files, and multitasking ===
Most Forth systems run under a host operating system such as [[Microsoft Windows]], [[Linux]] or a version of [[Unix]] and use the host operating system's file system for source and data files; the ANSI Forth Standard describes the words used for I/O. All modern Forth systems use normal text files for source, even if they are embedded. An embedded system with a resident compiler gets its source via a serial line.
Most Forth systems run under a host operating system such as [[Microsoft Windows]], [[Linux]] or a version of [[Unix]] and use the host operating system's file system for source and data files; the ANS Forth Standard describes the words used for I/O. All modern Forth systems use normal text files for source, even if they are embedded. An embedded system with a resident compiler gets its source via a serial line.


Classic Forth systems traditionally use neither [[operating system]] nor [[file system]]. Instead of storing code in files, source code is stored in disk blocks written to physical disk addresses. The word <code>BLOCK</code> is employed to translate the number of a 1K-sized block of disk space into the address of a buffer containing the data, which is managed automatically by the Forth system. Block use has become rare since the mid-1990s. In a hosted system those blocks too are allocated in a normal file in any case.
Classic Forth systems traditionally use neither [[operating system]] nor [[file system]]. Instead of storing code in files, source code is stored in disk blocks written to physical disk addresses. The word <code>BLOCK</code> is employed to translate the number of a 1K-sized block of disk space into the address of a buffer containing the data, which is managed automatically by the Forth system. Block use has become rare since the mid-1990s. In a hosted system those blocks too are allocated in a normal file in any case.


[[Computer multitasking|Multitasking]], most commonly [[Computer multitasking#Cooperative multitasking/time-sharing|cooperative]] [[round-robin scheduling]], is normally available (although multitasking words and support are not covered by the ANSI Forth Standard). The word <code>PAUSE</code> is used to save the current task's execution context, to locate the next task, and restore its execution context. Each task has its own stacks, private copies of some control variables and a scratch area. Swapping tasks is simple and efficient; as a result, Forth multitaskers are available even on very simple [[microcontroller]]s, such as the [[Intel MCS-51|Intel 8051]], [[Atmel AVR]], and [[TI MSP430]].<ref name="nxtDb">{{cite web | last = Rodriguez | first = Brad | url = https://www.bradrodriguez.com/papers/8051task.pdf | title = Multitasking 8051 CamelForth | access-date = 2023-04-29}}</ref>
[[Computer multitasking|Multitasking]], most commonly [[Computer multitasking#Cooperative multitasking/time-sharing|cooperative]] [[round-robin scheduling]], is normally available (although multitasking words and support are not covered by the ANS Forth Standard). The word <code>PAUSE</code> is used to save the current task's execution context, to locate the next task, and restore its execution context. Each task has its own stacks, private copies of some control variables and a scratch area. Swapping tasks is simple and efficient; as a result, Forth multitaskers are available even on very simple [[microcontroller]]s, such as the [[Intel MCS-51|Intel 8051]], [[Atmel AVR]], and [[TI MSP430]].<ref name="nxtDb">{{cite web | last = Rodriguez | first = Brad | url = https://www.bradrodriguez.com/papers/8051task.pdf | title = Multitasking 8051 CamelForth | access-date = 2023-04-29}}</ref>


Other non-standard facilities include a mechanism for issuing [[system call|call]]s to the host OS or [[windowing system]]s, and many provide extensions that employ the scheduling provided by the operating system. Typically they have a larger and different set of words from the stand-alone Forth's <code>PAUSE</code> word for task creation, suspension, destruction and modification of priority.
Other non-standard facilities include a mechanism for issuing [[system call|call]]s to the host OS or [[windowing system]]s, and many provide extensions that employ the scheduling provided by the operating system. Typically they have a larger and different set of words from the stand-alone Forth's <code>PAUSE</code> word for task creation, suspension, destruction and modification of priority.


=== Self-compilation and cross compilation ===
=== Self-compilation and cross compilation ===
A full-featured Forth system with all source code will compile itself, a technique commonly called meta-compilation or [[Self-hosting (compilers)|self-hosting]], by Forth programmers (although the term doesn't exactly match [[Meta-Compilation|meta-compilation]] as it is normally defined). The usual method is to redefine the handful of words that place compiled bits into memory. The compiler's words use specially named versions of fetch and store that can be redirected to a buffer area in memory. The buffer area simulates or accesses a memory area beginning at a different address than the code buffer. Such compilers define words to access both the target computer's memory, and the host (compiling) computer's memory.<ref name="Swdke">{{cite web |last=Rodriguez |first=Brad |date=July–August 1995 |url=https://www.bradrodriguez.com/papers/moving8.htm |title=MOVING FORTH, Part 8: CamelForth for the 6809 |access-date=2023-04-29}}</ref>
A full-featured Forth system with all source code will compile itself, a technique commonly called meta-compilation or [[Self-hosting (compilers)|self-hosting]], by Forth programmers (although the term doesn't exactly match [[metacompilation|meta-compilation]] as it is normally defined). The usual method is to redefine the handful of words that place compiled bits into memory. The compiler's words use specially named versions of fetch and store that can be redirected to a buffer area in memory. The buffer area simulates or accesses a memory area beginning at a different address than the code buffer. Such compilers define words to access both the target computer's memory, and the host (compiling) computer's memory.<ref name="Swdke">{{cite web |last=Rodriguez |first=Brad |date=July–August 1995 |url=https://www.bradrodriguez.com/papers/moving8.htm |title=MOVING FORTH, Part 8: CamelForth for the 6809 |access-date=2023-04-29}}</ref>


After the fetch and store operations are redefined for the code space, the compiler, assembler, etc. are recompiled using the new definitions of fetch and store. This effectively reuses all the code of the compiler and interpreter. Then, the Forth system's code is compiled, but this version is stored in the buffer. The buffer in memory is written to disk, and ways are provided to load it temporarily into memory for testing. When the new version appears to work, it is written over the previous version.
After the fetch and store operations are redefined for the code space, the compiler, assembler, etc. are recompiled using the new definitions of fetch and store. This effectively reuses all the code of the compiler and interpreter. Then, the Forth system's code is compiled, but this version is stored in the buffer. The buffer in memory is written to disk, and ways are provided to load it temporarily into memory for testing. When the new version appears to work, it is written over the previous version.


Numerous variations of such compilers exist for different environments. For [[embedded system]]s, the code may instead be written to another computer, a technique known as [[Cross-compilation|cross compilation]], over a serial port or even a single [[Transistor-transistor logic|TTL]] bit, while keeping the word names and other non-executing parts of the dictionary in the original compiling computer. The minimum definitions for such a Forth compiler are the words that fetch and store a byte, and the word that commands a Forth word to be executed. Often the most time-consuming part of writing a remote port is constructing the initial program to implement fetch, store and execute, but many modern microprocessors have integrated debugging features (such as the [[Motorola CPU32]]) that eliminate this task.<ref name="E5iFy">{{cite web | last = Shoebridge | first = Peter | date = 1998-12-21 | url = http://www.zeecube.com/archive/bdm/index.htm | title = Motorola Background Debugging Mode Driver for Windows NT | access-date = 2006-06-19 | url-status = dead | archive-url = https://web.archive.org/web/20070606083244/http://www.zeecube.com/archive/bdm/index.htm | archive-date = 2007-06-06}}</ref>
Numerous variations of such compilers exist for different environments. For [[embedded system]]s, the code may instead be written to another computer, a technique known as [[cross-compilation|cross compilation]], over a serial port or even a single [[transistor–transistor logic|TTL]] bit, while keeping the word names and other non-executing parts of the dictionary in the original compiling computer. The minimum definitions for such a Forth compiler are the words that fetch and store a byte, and the word that commands a Forth word to be executed. Often the most time-consuming part of writing a remote port is constructing the initial program to implement fetch, store and execute, but many modern microprocessors have integrated debugging features (such as the [[Motorola CPU32]]) that eliminate this task.<ref name="E5iFy">{{cite web | last = Shoebridge | first = Peter | date = 1998-12-21 | url = https://www.zeecube.com/archive/bdm/index.htm | title = Motorola Background Debugging Mode Driver for Windows NT | access-date = 2006-06-19 | url-status = dead | archive-url = https://web.archive.org/web/20070606083244/http://www.zeecube.com/archive/bdm/index.htm | archive-date = 2007-06-06}}</ref>


== Structure of the language ==
== Structure of the language ==
The basic data structure of Forth is the "dictionary" which maps "words" to executable code or named data structures. The dictionary is laid out in memory as a tree of [[linked list]]s with the links proceeding from the latest (most recently) defined word to the oldest, until a [[sentinel value]], usually a NULL pointer, is found. A context switch causes a list search to start at a different leaf. A linked list search continues as the branch merges into the main trunk leading eventually back to the sentinel, the root.
The basic data structure of Forth is the "dictionary" which maps "words" to executable code or named data structures. The dictionary is laid out in memory as a tree of [[linked list]]s with the links proceeding from the latest (most recently) defined word to the oldest, until a [[sentinel value]], usually a NULL pointer, is found. A context switch causes a list search to start at a different leaf. A linked list search continues as the branch merges into the main trunk leading eventually back to the sentinel, the root. There can be several dictionaries. In rare cases such as meta-compilation a dictionary might be isolated and stand-alone. The effect resembles that of nesting namespaces and can overload keywords depending on the context.
There can be several dictionaries. In rare cases such as meta-compilation a dictionary might be isolated and stand-alone.
The effect resembles that of nesting namespaces and can overload keywords depending on the context.


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).
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).
Line 143: Line 188:
The exact format of a dictionary entry is not prescribed, and implementations vary.
The exact format of a dictionary entry is not prescribed, and implementations vary.


===Structure of the compiler===
=== 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 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 "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,<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:
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
| 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">
<syntaxhighlight lang="forth">
  ... DUP 6 < IF DROP 5 ELSE 1 - THEN ...
  ... DUP 6 < IF DROP 5 ELSE 1 - THEN ...
</syntaxhighlight>
</syntaxhighlight>
would often be compiled to the following sequence inside a definition:
would often be compiled to the following sequence inside a definition:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  ... DUP LIT 6 < ?BRANCH 5  DROP LIT 5  BRANCH 3  LIT 1 - ...
  ... DUP LIT 6 < ?BRANCH 5  DROP LIT 5  BRANCH 3  LIT 1 - ...
</syntaxhighlight>
</syntaxhighlight>
The numbers after <code>BRANCH</code> represent relative jump addresses. <code>LIT</code> is the primitive word for pushing a "literal" number onto the data stack. (Faster, shorter code would be compiled using pointers to constants instead of <code>LIT</code> and embedded data, if any of the numbers involved have been separately defined as constants. There would be similar changes if yet other words were used instead of constants, and so on.)
The numbers after <code>BRANCH</code> represent relative jump addresses. <code>LIT</code> is the primitive word for pushing a "literal" number onto the data stack. (Faster, shorter code would be compiled using pointers to constants instead of <code>LIT</code> and embedded data, if any of the numbers involved have been separately defined as constants. There would be similar changes if yet other words were used instead of constants, and so on.)


Line 183: Line 244:
<!-- TO DO: Explain PARSE and FIND. -->
<!-- TO DO: Explain PARSE and FIND. -->


===Structure of code===
=== Structure of code ===
In most Forth systems, the body of a code definition consists of either [[machine language]], or some form of [[threaded code]]. The original Forth which follows the informal FIG standard (Forth Interest Group), is a TIL (Threaded Interpretive Language). This is also called indirect-threaded code, but direct-threaded and subroutine threaded Forths have also become popular in modern times. The fastest modern Forths, such as SwiftForth, VFX Forth, and iForth, compile Forth to native machine code.
In most Forth systems, the body of a code definition consists of either [[machine language]], or some form of [[threaded code]]. The original Forth which follows the informal FIG standard (Forth Interest Group), is a TIL (Threaded Interpretive Language). This is also called indirect-threaded code, but direct-threaded and subroutine threaded Forths have also become popular in modern times. The fastest modern Forths, such as SwiftForth, VFX Forth, and iForth, compile Forth to native machine code.


===Data objects===
=== Data objects ===
When a word is a variable or other data object, the CF points to the runtime code associated with the defining word that created it. A defining word has a characteristic "defining behavior" (creating a dictionary entry plus possibly allocating and initializing data space) and also specifies the behavior of an instance of the class of words constructed by this defining word. Examples include:
When a word is a variable or other data object, the CF points to the runtime code associated with the defining word that created it. A defining word has a characteristic "defining behavior" (creating a dictionary entry plus possibly allocating and initializing data space) and also specifies the behavior of an instance of the class of words constructed by this defining word. Examples include:


Line 202: Line 263:
Forth does not enforce consistency of [[data type]] usage; it is the programmer's responsibility to use appropriate operators to fetch and store values or perform other operations on data.
Forth does not enforce consistency of [[data type]] usage; it is the programmer's responsibility to use appropriate operators to fetch and store values or perform other operations on data.


==Examples==
== Examples ==


=== “Hello, World!” ===
=== “Hello, World!” ===
Line 215: Line 276:


A standard Forth system is also an interpreter, and the same output can be obtained by typing the following code fragment into the Forth console:
A standard Forth system is also an interpreter, and the same output can be obtained by typing the following code fragment into the Forth console:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  CR .( Hello, World!)
  CR .( Hello, World!)
</syntaxhighlight>
</syntaxhighlight>
<code>.(</code> (dot-paren) is an immediate word that parses a parenthesis-delimited string and displays it. As with the word <code>."</code> the space character separating <code>.(</code> from <code>Hello, World!</code> is not part of the string.
<code>.(</code> (dot-paren) is an immediate word that parses a parenthesis-delimited string and displays it. As with the word <code>."</code> the space character separating <code>.(</code> from <code>Hello, World!</code> is not part of the string.


Line 224: Line 287:
=== Mixing states of compiling and interpreting ===
=== Mixing states of compiling and interpreting ===
Here is the definition of a word <code>EMIT-Q</code> which when executed emits the single character <code>Q</code>:
Here is the definition of a word <code>EMIT-Q</code> which when executed emits the single character <code>Q</code>:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : EMIT-Q  81 ( the ASCII value for the character 'Q' ) EMIT ;
  : EMIT-Q  81 ( the ASCII value for the character 'Q' ) EMIT ;
</syntaxhighlight>
</syntaxhighlight>
This definition was written to use the [[ASCII]] value of the <code>Q</code> character (81) directly. The text between the parentheses is a comment and is ignored by the compiler. The word <code>EMIT</code> takes a value from the data stack and displays the corresponding character.
This definition was written to use the [[ASCII]] value of the <code>Q</code> character (81) directly. The text between the parentheses is a comment and is ignored by the compiler. The word <code>EMIT</code> takes a value from the data stack and displays the corresponding character.


The following redefinition of <code>EMIT-Q</code> uses the words <code><nowiki>[</nowiki></code> (left-bracket), <code><nowiki>]</nowiki></code> (right-bracket), <code>CHAR</code> and <code>LITERAL</code> to temporarily switch to interpreter state, calculate the ASCII value of the <code>Q</code> character, return to compilation state and append the calculated value to the current colon definition:
The following redefinition of <code>EMIT-Q</code> uses the words <code><nowiki>[</nowiki></code> (left-bracket), <code><nowiki>]</nowiki></code> (right-bracket), <code>CHAR</code> and <code>LITERAL</code> to temporarily switch to interpreter state, calculate the ASCII value of the <code>Q</code> character, return to compilation state and append the calculated value to the current colon definition:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : EMIT-Q  [ CHAR Q ]  LITERAL  EMIT ;
  : EMIT-Q  [ CHAR Q ]  LITERAL  EMIT ;
</syntaxhighlight>
</syntaxhighlight>
The parsing word <code>CHAR</code> takes a space-delimited word as parameter and places the value of its first character on the data stack. The word <code><nowiki>[CHAR]</nowiki></code> is an immediate version of <code>CHAR</code>. Using <code><nowiki>[CHAR]</nowiki></code>, the example definition for <code>EMIT-Q</code> could be rewritten like this:
The parsing word <code>CHAR</code> takes a space-delimited word as parameter and places the value of its first character on the data stack. The word <code><nowiki>[CHAR]</nowiki></code> is an immediate version of <code>CHAR</code>. Using <code><nowiki>[CHAR]</nowiki></code>, the example definition for <code>EMIT-Q</code> could be rewritten like this:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : EMIT-Q  [CHAR] Q  EMIT ; \ Emit the single character 'Q'
  : EMIT-Q  [CHAR] Q  EMIT ; \ Emit the single character 'Q'
Line 240: Line 308:


Both <code>CHAR</code> and <code><nowiki>[CHAR]</nowiki></code> are predefined in {{Not a typo|ANS}} <!-- not a misspelling --> Forth. Using <code>IMMEDIATE</code> and <code>POSTPONE</code>, <code><nowiki>[CHAR]</nowiki></code> could have been defined like this:
Both <code>CHAR</code> and <code><nowiki>[CHAR]</nowiki></code> are predefined in {{Not a typo|ANS}} <!-- not a misspelling --> Forth. Using <code>IMMEDIATE</code> and <code>POSTPONE</code>, <code><nowiki>[CHAR]</nowiki></code> could have been defined like this:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
  : [CHAR]  CHAR  POSTPONE LITERAL ; IMMEDIATE
  : [CHAR]  CHAR  POSTPONE LITERAL ; IMMEDIATE
Line 289: Line 358:


This is one way to test the code:
This is one way to test the code:
<syntaxhighlight lang="forth">
<syntaxhighlight lang="forth">
hex
hex
Line 297: Line 367:
</syntaxhighlight>
</syntaxhighlight>


==Implementations==
== 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".
[https://www.forth.org/bournemouth/jfar/vol2/no1/article1.pdf]{{dead link|date=May 2017|bot=InternetArchiveBot|fix-attempted=yes}}
[https://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.
[https://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=https://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 system]]s. Listed here are some of the systems which conform to the 1994 {{Not a typo|ANS}} <!-- not a misspelling --> Forth standard.
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 system]]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>
* 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]]
* [[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, RISC-V & RP2040)
* [https://home.hccnet.nl/anij/nof/noforth.html noForth], an ANS Forth implementation (as far as possible) for Flash microcontrollers (MSP430, RISC-V & RP2040)
Line 307: Line 406:
* Swift Forth, machine code generating implementation from Forth, Inc.
* Swift Forth, machine code generating implementation from Forth, Inc.
* VFX Forth, optimizing native code Forth
* VFX Forth, optimizing native code Forth
* [https://littlemanstackmachine.org/firth.html Firth], an adaptation of Forth for the Little Man Stack Machine computer.
* [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).
* [https://gitlab.com/higaski/Shi Shi], a fast and tiny embeddable Forth implementation written for the Thumb-2 ISA (ARMv7-M and newer).


Line 322: Line 421:
== External links ==
== External links ==
* [https://forth-standard.org Forth 2012 Standard] official site
* [https://forth-standard.org Forth 2012 Standard] official site
* [http://forth.org/POL.pdf ''Programming a problem-oriented language''] unpublished book by Charles H. Moore (1970)
* [https://forth.org/POL.pdf ''Programming a problem-oriented language''] {{Webarchive|url=https://web.archive.org/web/20250927053119/https://www.forth.org/POL.pdf |date=2025-09-27 }} unpublished book by Charles H. Moore (1970)
* [http://www.euroforth.org/ Annual European Forth Conference] 1985–present
* [http://www.euroforth.org/ Annual European Forth Conference] 1985–present
* [http://www.complang.tuwien.ac.at/projects/forth.html Forth Research] at Institut für Computersprachen  
* [https://www.complang.tuwien.ac.at/projects/forth.html Forth Research] at Institut für Computersprachen  


{{Commons category|Forth (programming language)}}
{{Commons category|Forth (programming language)}}