Content deleted Content added
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5 |
Blush30720 (talk | contribs) Remove erroneous newline |
||
(One intermediate revision by the same user not shown) | |||
Line 2:
{{Redirect|FORTH||Forth (disambiguation)}}
{{Infobox programming language
| name = Forth
| paradigm = [[Concatenative programming language|concatenative]] ([[Stack-oriented programming|stack-based]]), [[Procedural programming|procedural]], [[Reflective programming|reflective]]
| year = {{start date and age|1970}}
| designer = [[Charles H. Moore]]
| typing = Typeless
| implementations = SwiftForth (Forth, Inc.)<br/>[[Gforth]] (GNU Project)<br/>VFX Forth (MicroProcessor Engineering)
| influenced = [[Bitcoin#Transactions|Bitcoin Script]], [[Factor (programming language)|Factor]], [[Joy (programming language)|Joy]], [[RPL (programming language)|RPL]], [[Rebol]], [[STOIC]]
| file_ext = .fs, .fth, .4th, .f, .forth{{cn|date=April 2022}}
| website = {{URL|https://forth-standard.org}}
}}
'''Forth''' is a [[Stack-oriented programming|stack-oriented]] [[programming language]] and interactive [[integrated development environment]] designed by [[Charles H. Moore|Charles H. "Chuck" Moore]] and first used by other programmers in 1970.
Although not an [[acronym]], the language's name in its early years was often spelled in [[all caps|all capital letters]] as ''FORTH''. The FORTH-79 and FORTH-83 implementations, which were not written by Moore, became ''[[de facto]]'' standards, and an official [[technical standard]] of the language was published in 1994 as ANS Forth. A wide range of Forth derivatives existed before and after ANS Forth. The [[free and open-source software]] [[Gforth]] implementation is actively maintained, as are several [[Commercial software|commercially]] supported systems. Forth typically combines a compiler with an integrated command shell, where the user interacts via [[subroutine]]s called ''words''.{{
Efn| There are exceptions, such as Ulrich Hoffmann's preForth<ref name = "preForth }} 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.
Line 24 ⟶ 33:
== 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.
Line 39 ⟶ 49:
== History ==
Forth evolved from [[Charles H. Moore]]'s personal programming system, which had been in continuous development since 1968.<ref name="evolution">{{cite book |last1=Rather |first1=Elizabeth D. |last2=Colburn |first2=Donald R. |last3=Moore |first3=Charles H. |title=History of programming languages---II |chapter=The evolution of Forth |chapter-url=http://www.forth.com/resources/evolution/index.html |editor-first=Thomas J. |editor-last=Bergin |editor2-first=Richard G. |editor2-last=Gibson |publisher=Association for Computing Machinery |date=1996 |isbn=0201895021 |pages=625–670 |doi=10.1145/234286.1057832 |orig-year=1993}}</ref><ref name="WYK4Z">{{Cite web |last=Moore |first=Charles H. |year=1991 |url=http://www.colorforth.com/HOPL.html |title=Forth - The Early Years |access-date=2006-06-03 |url-status=dead |archive-url=https://web.archive.org/web/20060615025259/http://www.colorforth.com/HOPL.html |archive-date=2006-06-15}}</ref> Forth was first exposed to other programmers in the early 1970s, starting with [[Elizabeth Rather]] at the United States [[National Radio Astronomy Observatory]] (NRAO).<ref name="evolution" /> After their work at NRAO, Charles Moore and Elizabeth Rather formed FORTH, Inc. in 1973, refining and porting Forth systems to dozens of other platforms in the next decade.
Line 54 ⟶ 65:
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>
{{ | | 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.
Line 78 ⟶ 101:
[[File:Forthstack3.svg|150px|left]]
<br/>The word <code>+</code> adds the top two values, pushing the sum. <code>CR</code> ([[carriage return]]) starts the output on a new line. Finally, <code>.</code> prints the result. As everything has completed successfully, the Forth system prints <code>OK</code>.<ref name="3yqc9">{{
| Brodie | 1987 | p = 20 }} </ref>{{clear}} Even Forth's structural features are stack-based. For example:
Line 108 ⟶ 136:
==Facilities==
Forth's [[Formal grammar|grammar]] has no official specification. Instead, it is defined by a simple algorithm. The interpreter reads a line of input from the user input device, which is then parsed for a word using spaces as a [[delimiter]]; some systems recognise additional [[Whitespace 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">
{{ | Brodie | 1987 | p = 14 }} </ref> The definition of a new word is started with the word <code>:</code> (colon) and ends with the word <code>;</code> (semi-colon). For example,
Line 114 ⟶ 148:
: X DUP 1+ . . ;
</syntaxhighlight>
will compile the word <code>X</code>, and makes the name findable in the dictionary. When executed by typing <code>10 X</code> at the console this will print <code>11 10</code>.<ref name="compiler">
{{ | Brodie | 1987 | p = 16 }} </ref> Most Forth systems include an [[Assembly language#Assembler|assembler]] to write words using the processor's facilities. Forth assemblers often use a reverse Polish syntax in which the parameters of an instruction precede the instruction. A typical reverse Polish assembler prepares the operands on the stack and the mnemonic copies the whole instruction into memory as the last step. A Forth assembler is by nature a macro assembler, so that it is easy to define an alias for registers according to their role in the Forth system: e.g. "dsp" for the register used as the data stack pointer.<ref name="NAFQu">{{cite web | last = Rodriguez | first = Brad | url = https://www.bradrodriguez.com/papers/6809asm.txt | title = Build Your Own Assembler, Part 2: a 6809 Forth Assembler | access-date = 2023-04-29}}</ref>
Line 150 ⟶ 190:
The "compile time" flag in the name field is set for words with "compile time" behavior. Most simple words execute the same code whether they are typed on a command line, or embedded in code. When compiling these, the compiler simply places code or a threaded pointer to the word.<ref name="compiler" />
The classic examples of compile-time words are the [[control structure]]s such as <code>IF</code> and <code>WHILE</code>.
Almost all of Forth's control structures and almost all of its compiler are implemented as compile-time words.
Apart from some rarely used [[control flow]] words only found in a few implementations, such as the conditional return word {{PreCode|?EXIT}} used in Ulrich Hoffmann's preForth, all of Forth's [[control flow]] words are executed during compilation to compile various combinations of primitive words along with their branch addresses.<ref name = "preForth slides">
{{Cite web
| url = http://www.euroforth.org/ef18/papers/hoffmann-slides.pdf
| title = Ulrich Hoffmann's preForth slides
}}
</ref>
<ref name="preForth">
{{Cite web
| url = http://www.euroforth.org/ef18/papers/hoffmann.pdf
| title = Ulrich Hoffmann's preForth
}}
</ref>
For instance, <code>IF</code> and <code>WHILE</code>, and the words that match with those, set up <code>BRANCH</code> (unconditional branch) and {{PreCode|?BRANCH}} (pop a value off the stack, and branch if it is false). Counted loop [[control flow]] words work similarly but set up combinations of primitive words that work with a counter, and so on. During compilation, the data stack is used to support control structure balancing, nesting, and back-patching of branch addresses. The snippet:
<syntaxhighlight lang="forth">
... DUP 6 < IF DROP 5 ELSE 1 - THEN ...
|