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)
(section)
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!
==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.
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)