Forth uses white space for delimiters. All other printable characters can be the name of a word (e.g. function or subroutine) in Forth.
Forth words generally take their arguments from an implicit data stack and return the results to that same stack. For example ...
1 2 + .pushed a 1 onto the stack, then pushes a 2 onto the stack. It then executes the + word which adds the top two stack entries (and removes them), then pushes the result back on. The . word will print the top number in the stack.
To define a new word, use a colon, the new word name, a list of words and a semi-colon to terminate the definition... so
: add1-and-print 1 + . ; ( n -- )will add a 1 to whatever is on the stack and print the result. The net change in the stack is that it is one element shorter and the stack comment in parenthesis expresses that. A stack comment is a before and after snapshot of the top of the stack.
Variables in Forth are implemented as addresses. "variable x" will create a word named "x" that leaves its address on the stack. Use
@ ( addr -- n ) To fetch a value\n ! ( n addr -- ) To store a valueOk, here's our function written out long hand ...
: ? ( addr -- ) \\ Define a word named ?\n @ \\ Fetch the value stored at the given address\n . \\ Print the value at the top of the stack\n ; \\ Terminate the definition\n\n \\ Now we can write ...\n\n variable x 10 x ! \\ Create a variable x and store a 10 there\n x ? \\ Print the value of xThat's it. : ? @ . ; defines a function that prints the value of a variable.