Forth has an almost non-existent syntax. Spaces and carriage returns are used to delimit tokens, and all other characters can be legally used in variable names and word definitions.
Some symbols have commonly accepted meanings. An example of this is the @
(at symbol), which is used for words that perform memory fetches.
The table below serves as a quick reference to commonly used symbols in the language. It is intended for programmers new to Forth.
Errata and additions: If you have additions or corrections to contribute, please leave a comment or send me a message. Special thanks to Lars Brinkhoff for numerous updates and additions to the list via /r/forth.
Symbol | Hint | Meaning(s) |
---|---|---|
@ , EXAMPLE@ |
fetch | Retrieves the value of a memory address and places it on the top of the stack. |
! , EXAMPLE! |
store | Stores a value into a memory address. |
+! , EXAMPLE+! |
store | Increments the value of a memory address. |
' , 'EXAMPLE |
tick | Fetches the memory address (execution token or “XT”) of the word to the right of it. Variables containing this symbol hint at the fact they store an XT. For example, ' foo will fetch the memory address of the word foo . |
[ ,] , [EXAMPLE] |
brackets | Brings the compiler into and out of immediate mode. |
(PARENTHESIZED_WORD) |
parenthesis | Implement low-level details for an unparenthesized version. Usually are not meant to be used directly. Example: (LOOP) . |
( ,) |
stack effect comments | A form of comments. Typically in the form of ( precondition -- postcondtion) . More information |
$ |
dollar sign | Two uses: 1. Numbers starting with a $ are written in hex notation ($AF0 ). 2. Variable names with a dollar sign typically indicate a counted string. |
0x10 |
zero X | Hex numbers. Example: 0X1A . |
& |
ampersand | Decimal numbers. Example: &123 |
# |
pound | Decimal numbers. Example: #456 |
% |
percent | Binary numbers. Example: %101 |
'a or 'a' |
ASCII | Placing ' before (and optionally, after) an ASCII char will push the ASCII code onto the stack. |
: |
colon | Begin a new dictionary definition. Puts the interpreter into compilation mode and starts a new definition in the dictionary. Similar to a function or method declaration in other languages. The name of the defined word follows the : . |
; |
semicolon | Finish the current definition and leave compilation mode. |
EXAMPLE? |
question mark | Words used to test conditions (in an IF statement, for example) will often start or end with a ? |
?EXAMPLE |
“Optionally do EXAMPLE ” |
|
#EXAMPLE |
Number of examples | |
EXAMPLE# |
Example number | |
/EXAMPLE |
Size of example (“(bytes) per example”). | |
. , .EXAMPLE |
dot | Indicates that a word will print results to STDOUT. On embedded systems, these words will often transmit data to a serial line. |
fEXAMPLE |
float | Starting or ending a word with an f hints that it works on floating point numbers. Example: f+ , f* |
{ ,} |
Curly braces | Used to denote local variables. |
" and EXAMPLE" |
quote | Words with quote symbols hint that the word performs a string literal operation. For example, the ." in ." Hello! prints Hello! to STDOUT. The trailing space after " is significant. |
, |
comma | Push the top of the stack into the next available storage slot in the dictionary. |
2EXAMPLE , 3EXAMPLE |
1. Given the word EXAMPLE , 2EXAMPLE would perform EXAMPLE twice. 3EXAMPLE performs the word three times. 2. Some words, such as 2SWAP , use the the number to indicate how many cells the word operates on. |
|
R> , >R |
Transfers the top of the data stack to and from the return stack, respectively. | |
DEXAMPLE |
Indicates that the word operates on double numeric types. | |
I , J |
I represents the count of the current loop. When nesting two loops, J can be used as the index of the inner loop. |