.s
- Show the stack+
, -
, *
, mod
- Math operators/mod
- performs both / and moddrop
and 2drop
- drop a stack item (once / twice)dup
- duplicate a stack itemrot
- rotate the stacknip
- Removes the 2nd to last item on the stacktuck
- duplicates the 2nd to last item on the stack to the front of the stacks" file.fs" included
- Loads file.fs into memorygforth code.fs tests.fs -e bye
- Load a file and exit if something goes wrong (instead of the command line)(
and \
- Comments. Comments are significant in Forth, so keep a space between them.By convention the comment after the name of a definition describes the stack effect: The part in front of the '--' describes the state of the stack before the execution of
the definition, i.e., the parameters that are passed into the colon definition; the part behind the '--' is the state of the stack after the execution of the definition,
i.e., the results of the definition. The stack comment only shows the top stack items that the definition accesses and/or changes.
You should put a correct stack effect on every definition, even if it is just ( -- ). You should also add some descriptive comment to more complicated words (I usually do
this in the lines following :). If you don't do this, your code becomes unreadable (because you have to work through every definition before you can understand any).
n
- signed integeru
- unsigned integerc
- characterf
- Boolean flags, i.e. false or true.a-addr
,a-
Cell-aligned addressc-addr
,c-
- Char-aligned address (note that a Char may have two bytes in Windows NT)xt
- Execution token, same size as Cellw,x
- Cell, can contain an integer or an address. It usually takes 32, 64 or 16 bits (depending on your platform and Forth system). A cell is more commonly known as a machine word, but the term word already means something different in Forth.d
- signed double-cell integerud
- unsigned double-cell integerr
- Float (on the FP stack): squared ( n -- n^2 ) \ The parenthesis are just a convention to explain the function.
dup * ;
see
- 'decompiles' a word to see source.Forth doesn't do any type checking whatsoever. It's not a big deal though, but you should be aware of it.
(none)
- signed integeru
- unsigned integerc
- characterd
- signed double-cell integerud
, du
- unsigned double-cell integer2
- two cells (not-necessarily double-cell numbers)m
, um
- mixed single-cell and double-cell operationsf
- floating-point (note that in stack comments 'f' represents flags, and 'r' represents FP numbers).Curly braces in a word definition allow you to create local variables in the definition.
: swap { a b -- b a }
b a ;
Conditionals can only be used within a colon definition.
: abs ( n1 -- +n2 )
dup 0 < if
negate
endif ;
NOTE: In Forth, endif
is less commonly used. Typically, the word then
is used. But that's really confusing for non-forth programmers.
Another example:
: min ( n1 n2 -- n )
2dup < if
drop
else
nip
endif;
true
is often represented by -1
(a 11111111 byte)false
is 0true
begin
does nothing at run-time, again
jumps back to begin
. : endless ( -- )
0 begin
dup . 1+
again ;
endless
use leave
to get out of a loop and exit
to leave the definition.
Use recurse
to call the word being defined.
Aside from the main stack ('the stack'), there is also a 'return stack'. Typically, the return stack is used to store temporary variables. Miscounting items on the return stack usually causes a crash
>r
Pushes data stack element onto return stack (data -> return)r>
Move from return stack to data stack (return -> data)r@
pushes a copy of the top of the return stack on the data stackvariable v
initialize variable 'v'v
Push the address onto the stack (not the value)5 v !
Set the value of 'v' to 5.v @
Push the value of 'v' to the stackv 2 cells
"Grab the contents of 2 cells of memory, starting at the location of variable v"create v2 20 cells allot
Allocate 20 cells of memory and store the starting address to variable v2Write floats with scientific notation.
1.23e
-> 1.23
1e0
-> 1.0
Most Forth implementations create a seperate stack for FP operations. This includes Gforth. Usually, you can just prefix data stack operators with an 'f' to operate in the float stack. Eg: f.
to pop off the fp stack. Other operators include f+
f-
f*
f/
f**
f@
and f!
.