Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Forth (programming language)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Stack-based programming language}} {{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,{{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. 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. 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]] [[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" /> [[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 written in MacFORTH.<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> [[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. 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> == 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. 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> {{quote|At [[Mohawk Industries|Mohasco]] ["in the late 1960s"] I also worked directly on an [[IBM 1130]] interfaced with an [[IBM 2250]] graphics display. The 1130 was a very important computer: it had the first cartridge disk, as well as a card reader, a card punch (as backup for the disk), and a console typewriter. The 1130 let the programmer, for the first time, totally control the computer interactively.}} {{quote|FORTH first appeared as an entity on that 1130. It was called F-O-R-T-H, a five-letter abbreviation of FOURTH, standing for fourth-generation computer language. That was the day, you may remember, of third-generation computers and I was going to leapfrog. But because FORTH ran on the 1130 (which permitted only five-character identifiers), the name was shortened.}} FORTH, Inc.'s microFORTH was developed for the [[Intel 8080]], [[Motorola 6800]], [[Zilog Z80]], and [[RCA 1802]] microprocessors, starting in 1976. MicroFORTH was later used by hobbyists to generate Forth systems for other architectures, such as the [[MOS Technology 6502|6502]] in 1978. The Forth Interest Group was formed in 1978.<ref>{{cite web |title=ANS 1994 Specification, Annex C ("Perspective") |url=https://www.taygeta.com/forth/dpansc.htm |website=taygeta.com}}</ref> It promoted and distributed its own version of the language, FIG-Forth, for most makes of home computer. 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> 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>{{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 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: <!-- https://pygments.org/docs/lexers/#pygments.lexers.forth.ForthLexer --> <syntaxhighlight lang="forth"> 25 10 * 50 + CR . 300 ok </syntaxhighlight> [[File:Stack1.svg|150px|left]] First the numbers 25 and 10 are put on the stack.{{clear}} [[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]] Then the number 50 is placed on the stack.{{clear}} [[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}} Even Forth's structural features are stack-based. For example: <syntaxhighlight lang="forth"> : FLOOR5 ( n -- n' ) DUP 6 < IF DROP 5 ELSE 1 - THEN ; </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 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><</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]] '?:' <syntaxhighlight lang="c"> int floor5(int v) { return (v < 6) ? 5 : (v - 1); } </syntaxhighlight> This function is written more succinctly as: <syntaxhighlight lang="forth"> : FLOOR5 ( n -- n' ) 1- 5 MAX ; </syntaxhighlight> This can be run as follows: <syntaxhighlight lang="forth"> 1 FLOOR5 CR . 5 ok 8 FLOOR5 CR . 7 ok </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. ==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> 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"> : 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">{{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> === 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. 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> 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 === 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> 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> == 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. 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). 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 Sigforth Newsletter |volume=2 |issue=3 |pages=17–19 |publisher=ACM Press |doi=10.1145/122089.122091| 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>. 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: <syntaxhighlight lang="forth"> ... DUP 6 < IF DROP 5 ELSE 1 - THEN ... </syntaxhighlight> would often be compiled to the following sequence inside a definition: <syntaxhighlight lang="forth"> ... DUP LIT 6 < ?BRANCH 5 DROP LIT 5 BRANCH 3 LIT 1 - ... </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.) ==== Compilation state and interpretation state ==== The word <code>:</code> (colon) parses a name as a parameter, creates a dictionary entry (a ''colon definition'') and enters compilation state. The interpreter continues to read space-delimited words from the user input device. If a word is found, the interpreter executes the ''compilation semantics'' associated with the word, instead of the ''interpretation semantics''. The default compilation semantics of a word are to append its interpretation semantics to the current definition.<ref name="compiler" /> The word <code>;</code> (semi-colon) finishes the current definition and returns to interpretation state. It is an example of a word whose compilation semantics differ from the default. The interpretation semantics of <code>;</code> (semi-colon), most control flow words, and several other words are undefined in {{Not a typo|ANS}} <!-- not a misspelling --> Forth, meaning that they must only be used inside of definitions and not on the interactive command line.<ref name="compiler" /> 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 (programming)|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 ==== The word <code>IMMEDIATE</code> marks the most recent colon definition as an ''immediate word'', effectively replacing its compilation semantics with its interpretation semantics.<ref name="odMGJ">{{harvnb|Brodie|1987|p=273}}</ref> Immediate words are normally executed during compilation, not compiled, but this can be overridden by the programmer in either state. <code>;</code> is an example of an immediate word. In {{Not a typo|ANS}} <!-- not a misspelling --> Forth, the word <code>POSTPONE</code> takes a name as a parameter and appends the compilation semantics of the named word to the current definition even if the word was marked immediate. Forth-83 defined separate words <code>COMPILE</code> and <code>[COMPILE]</code> to force the compilation of non-immediate and immediate words, respectively. Instead of reserving space for an Immediate flag in every definition, some implementations of Forth use an Immediates Dictionary which is checked first when in compile mode. ==== Unnamed words and execution tokens ==== In {{Not a typo|ANS}} <!-- not a misspelling --> Forth, unnamed words can be defined with the word <code>:NONAME</code> which compiles the following words up to the next <code>;</code> (semi-colon) and leaves an ''execution token'' on the data stack. The execution token provides an opaque handle for the compiled semantics, similar to the [[function pointer]]s of the [[C (programming language)|C programming language]]. Execution tokens can be stored in variables. The word <code>EXECUTE</code> takes an execution token from the data stack and performs the associated semantics. The word <code>COMPILE,</code> (compile-comma) takes an execution token from the data stack and appends the associated semantics to the current definition. The word <code><nowiki>'</nowiki></code> (tick) takes the name of a word as a parameter and returns the execution token associated with that word on the data stack. In interpretation state, <code>' RANDOM-WORD EXECUTE</code> is equivalent to <code>RANDOM-WORD</code>.<ref name="iUYBX">{{harvnb|Brodie|1987|p=199}}</ref> ==== Parsing words and comments ==== The words <code>:</code> (colon), <code>POSTPONE</code>, <code><nowiki>'</nowiki></code> (tick) are examples of ''parsing words'' that take their arguments from the user input device instead of the data stack. Another example is the word <code>(</code> (paren) which reads and ignores the following words up to and including the next right parenthesis and is used to place comments in a colon definition. Similarly, the word <code>\</code> (backslash) is used for comments that continue to the end of the current line. To be parsed correctly, <code>(</code> (paren) and <code>\</code> (backslash) must be separated by whitespace from the following comment text. <!-- TO DO: Explain PARSE and FIND. --> ===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. ===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: ;<code>VARIABLE</code> :Names an uninitialized, one-cell memory location. Instance behavior of a <code>VARIABLE</code> returns its address on the stack. ;<code>CONSTANT</code> :Names a value (specified as an argument to <code>CONSTANT</code>). Instance behavior returns the value. ;<code>CREATE</code> :Names a location; space may be allocated at this location, or it can be set to contain a string or other initialized value. Instance behavior returns the address of the beginning of this space. Forth also provides a facility by which a programmer can define new application-specific defining words, specifying both a custom defining behavior and instance behavior. Some examples include circular buffers, named bits on an I/O port, and automatically indexed arrays. Data objects defined by these and similar words are global in scope. The function provided by local variables in other languages is provided by the data stack in Forth (although Forth also has real local variables). Forth programming style uses very few named data objects compared with other languages; typically such data objects are used to contain data which is used by a number of words or tasks (in a multitasked implementation).<ref name="ceEJ3">{{cite book | title = {{harvnb|Brodie|1987}} | pages = 241 | chapter = Under The Hood | quote = To summarize, there are three kinds of variables: System variables contain values used by the entire Forth system. User variables contain values that are unique for each task, even though the definitions can be used by all tasks in the system. Regular variables can be accessible either system-wide or within a single task only, depending upon whether they are defined within <code>OPERATOR</code> or within a private task.}}</ref> 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== === “Hello, World!” === <syntaxhighlight lang="forth"> : HELLO ( -- ) CR ." Hello, World!" ; </syntaxhighlight> HELLO <cr> Hello, World! The word <code>CR</code> (Carriage Return) causes the output following <code>CR</code> to be displayed on a new line. The parsing word <code>."</code> (dot-quote) reads a double-quote delimited string and appends code to the current definition so that the parsed string will be displayed upon execution. The space character separating the word <code>."</code> from the string <code>Hello, World!</code> is not included as part of the string. It is needed so that the parser recognizes <code>."</code> as a Forth word. 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"> CR .( Hello, World!) </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. The word <code>CR</code> comes before the text to print. By convention, the Forth interpreter does not start output on a new line. Also by convention, the interpreter waits for input at the end of the previous line, after an <code>ok</code> prompt. There is no implied "flush-buffer" action in Forth's <code>CR</code>, as sometimes is in other programming languages. === 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>: <syntaxhighlight lang="forth"> : EMIT-Q 81 ( the ASCII value for the character 'Q' ) EMIT ; </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. 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"> : EMIT-Q [ CHAR Q ] LITERAL EMIT ; </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: <syntaxhighlight lang="forth"> : EMIT-Q [CHAR] Q EMIT ; \ Emit the single character 'Q' </syntaxhighlight> This definition used <code>\</code> (backslash) for the describing comment. 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"> : [CHAR] CHAR POSTPONE LITERAL ; IMMEDIATE </syntaxhighlight> === RC4 cipher program === In 1987, [[Ron Rivest]] developed the [[RC4]] cipher-system for RSA Data Security, Inc. Its description follows: {{Blockquote|We have an array of 256 bytes, all different. Every time the array is used it changes by swapping two bytes. The swaps are controlled by counters ''i'' and ''j'', each initially 0. To get a new ''i'', add 1. To get a new ''j'', add the array byte at the new ''i''. Exchange the array bytes at ''i'' and ''j''. The code is the array byte at the sum of the array bytes at ''i'' and ''j''. This is XORed with a byte of the plaintext to encrypt, or the ciphertext to decrypt. The array is initialized by first setting it to 0 through 255. Then step through it using ''i'' and ''j'', getting the new ''j'' by adding to it the array byte at ''i'' and a key byte, and swapping the array bytes at ''i'' and ''j''. Finally, ''i'' and ''j'' are set to 0. All additions are modulo 256.}} The following Standard Forth version uses Core and Core Extension words only. <syntaxhighlight lang="forth"> 0 value ii 0 value jj 0 value KeyAddr 0 value KeyLen create SArray 256 allot \ state array of 256 bytes : KeyArray KeyLen mod KeyAddr ; : get_byte + c@ ; : set_byte + c! ; : as_byte 255 and ; : reset_ij 0 TO ii 0 TO jj ; : i_update 1 + as_byte TO ii ; : j_update ii SArray get_byte + as_byte TO jj ; : swap_s_ij jj SArray get_byte ii SArray get_byte jj SArray set_byte ii SArray set_byte ; : rc4_init ( KeyAddr KeyLen -- ) 256 min TO KeyLen TO KeyAddr 256 0 DO i i SArray set_byte LOOP reset_ij BEGIN ii KeyArray get_byte jj + j_update swap_s_ij ii 255 < WHILE ii i_update REPEAT reset_ij ; : rc4_byte ii i_update jj j_update swap_s_ij ii SArray get_byte jj SArray get_byte + as_byte SArray get_byte xor ; </syntaxhighlight> This is one way to test the code: <syntaxhighlight lang="forth"> hex create AKey 61 c, 8A c, 63 c, D2 c, FB c, : test cr 0 DO rc4_byte . LOOP cr ; AKey 5 rc4_init 2C F9 4C EE DC 5 test \ output should be: F1 38 29 C9 DE </syntaxhighlight> ==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. * 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> * [[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) * [[Open Firmware]], a [[bootloader]] and [[firmware]] 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) * Swift Forth, machine code generating implementation from Forth, Inc. * 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 == Notes == {{Notelist}} == References == {{Reflist}} == External links == * [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) * [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 {{Commons category|Forth (programming language)}} {{wikibooks|Forth}} {{Programming languages}} {{Authority control}} {{DEFAULTSORT:Forth (Programming Language)}} [[Category:Forth programming language family]] [[Category:Concatenative programming languages]] [[Category:Stack-based virtual machines]] [[Category:Systems programming languages]] [[Category:Programming languages created in 1970]] [[Category:Extensible syntax programming languages]] [[Category:Programming languages with an ISO standard]] [[Category:Programming languages]] [[Category:1970 software]] [[Category:Articles with example C code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Authority control
(
edit
)
Template:Blockquote
(
edit
)
Template:Citation
(
edit
)
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite podcast
(
edit
)
Template:Cite web
(
edit
)
Template:Clear
(
edit
)
Template:Comma separated entries
(
edit
)
Template:Commons category
(
edit
)
Template:Efn
(
edit
)
Template:Further
(
edit
)
Template:Harvnb
(
edit
)
Template:Infobox programming language
(
edit
)
Template:Main other
(
edit
)
Template:Not a typo
(
edit
)
Template:Notelist
(
edit
)
Template:PreCode
(
edit
)
Template:Programming languages
(
edit
)
Template:Quote
(
edit
)
Template:Redirect
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Wikibooks
(
edit
)