---
title: Forth (programming language)
slug: forth-programming-language
qid: 
snapshot: enwiki-001378025929
content_hash: 
license: CC BY-SA 4.0
source: https://en.wikipedia.org/w/index.php?oldid=1378025929
tags: [116467647, 126706833, 130386609, 20161863, 20284182, 21493616, 22930027, 22973933, 23118434, 2317679, 23418218, 23603337, 23837581, 24043274, 24623845, 24745122, 28269686, 32693616, 3471506, 37645845, 39178665, 50998678, 51915991]
---

# Forth (programming language)

> [!note] Provenance
> Snapshot `enwiki-001378025929` · content root `…`
> Derived from <https://en.wikipedia.org/w/index.php?oldid=1378025929> · Licensed CC BY-SA 4.0

Forth is a stack-oriented programming language and interactive integrated development environment designed by 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 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 commercially supported systems. Forth typically combines a compiler with an integrated command shell, where the user interacts via subroutines 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 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.…

Forth is a stack-oriented programming language and interactive integrated development environment designed by 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 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 commercially supported systems. Forth typically combines a compiler with an integrated command shell, where the user interacts via subroutines 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 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 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. Forth is used in the Open Firmware boot loader, in spaceflight applications such as the Philae spacecraft, 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 created to support Moore's own projects, such as chip design. 25 10 * 50 + CR . 300 ok First the numbers 25 and 10 are put on the stack. The word * takes the top two numbers from the stack, multiplies them, and puts the product back on the stack. Then the number 50 is placed on the stack. The word + takes the top two numbers from the stack, adds them, and puts the sum back on the stack. CR (carriage return) starts the output on a new line. Finally, . prints the result. As everything has completed successfully, the Forth system prints OK . Even Forth's structural features are stack-based. For example: : FLOOR5 ( n -- n' ) DUP 6 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 FLOOR5 . 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: DUP duplicates the number on the stack; 6 pushes a 6 on top of the stack; &lt; compares the top two numbers on the stack (6 and the DUP ed input), and replaces them with a true-or-false value; IF takes a true-or-false value and chooses to execute commands immediately after it or to skip to the ELSE ; DROP discards the value on the stack; 5 pushes a 5 on top of the stack; and THEN ends the conditional. The FLOOR5 word is equivalent to this function written in the C programming language using the conditional operator '?:' int floor5(int v) { return (v This function is written more succinctly as: : FLOOR5 ( n -- n' ) 1- 5 MAX ; This can be run as follows: 1 FLOOR5 CR . 5 ok 8 FLOOR5 CR . 7 ok First a number (1 or 8) is pushed onto the stack, FLOOR5 is called, which pops the number again and pushes the result. CR moves the output to a new line (again, this is only here for readability). Finally, a call to . pops the result and prints. : X DUP 1+ . . ; will compile the word X , and makes the name findable in the dictionary. When executed by typing 10 X at the console this will print 11 10 . Most Forth systems include an 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. ... DUP 6 would often be compiled to the following sequence inside a definition: ... DUP LIT 6 The numbers after BRANCH represent relative jump addresses. LIT 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 LIT 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.) : HELLO ( -- ) CR ." Hello, World!" ; HELLO &lt;cr&gt; Hello, World! The word CR (Carriage Return) causes the output following CR to be displayed on a new line. The parsing word ." (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 ." from the string Hello, World! is not included as part of the string. It is needed so that the parser recognizes ." 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: CR .( Hello, World!) .( (dot-paren) is an immediate word that parses a parenthesis-delimited string and displays it. As with the word ." the space character separating .( from Hello, World! is not part of the string. The word CR 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 ok prompt. There is no implied "flush-buffer" action in Forth's CR , as sometimes is in other programming languages. : EMIT-Q 81 ( the ASCII value for the character 'Q' ) EMIT ; This definition was written to use the ASCII value of the Q character (81) directly. The text between the parentheses is a comment and is ignored by the compiler. The word EMIT takes a value from the data stack and displays the corresponding character. The following redefinition of EMIT-Q uses the words [ (left-bracket), ] (right-bracket), CHAR and LITERAL to temporarily switch to interpreter state, calculate the ASCII value of the Q character, return to compilation state and append the calculated value to the current colon definition: : EMIT-Q [ CHAR Q ] LITERAL EMIT ; The parsing word CHAR takes a space-delimited word as parameter and places the value of its first character on the data stack. The word [CHAR] is an immediate version of CHAR . Using [CHAR] , the example definition for EMIT-Q could be rewritten like this: : EMIT-Q [CHAR] Q EMIT ; \ Emit the single character 'Q' This definition used \ (backslash) for the describing comment. Both CHAR and [CHAR] are predefined in Forth. Using IMMEDIATE and POSTPONE , [CHAR] could have been defined like this: : [CHAR] CHAR POSTPONE LITERAL ; IMMEDIATE 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 This is one way to test the code: 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
