πForth cheatsheet
- tags
- help
see <word>to show word definition.Sdisplay stack without destroying it
- I/O
.print numberemitshow number as char." Hello"print string βHelloβ (space required after.")
- Functions
: <funcname> <body...> ;define function
- Variables
variable <varname>create variable<varname>pushes variable address onto stack<varname> @fetch from variable<value> <varname> !store to variable2variable <varname>create double-length variable
- Constants
<value> CONSTANT <constname>create constant<constname>pushes value of the constant to the stack
- Values (CORE EXTENSION)
<value> VALUE <valname>creates value and initializes it<valname>push value to the stack<value> TO <valname>re-assign value
- Locals (LOCALS EXTENSION)
- local variables
- locals act as values (not variables)
LOCALS| a b c ... |example
: v+ ( a b c d -- a+c b+d) LOCALS| d c b a | a c + b d + ;
- Return stack (rstack)
>Rstore to rstackR>retrieve from rstackR@copy from rstack
- Memory
@=/!= fetch/store numbersD@=/=D!fetch/store double numbersC@=/=C!fetch/store chars (chars are always positive)CREATE <name>makes a new dictionary entryHEREreturn address of the next available space13 CELLS ALLOTallocate 13 cells (kind of array) (used as increate x 10 cells allot)
- Control flow
: test 0 = invert IF cr ." Not zero!" THEN ;if-then: truth cr 0 = IF ." false" ELSE ." true" THEN ;if-then-elserecursecall itselfBEGIN xxx ( -- flag) UNTILloop until flag is trueBEGIN xxx ( -- flag) WHILE yyy REPEATloop until flag is false (execute yyy only if flag is true for given iteration)<to> <from> DO ... LOOP(ican be used to get iteration number)<to> <from> DO ... <step> +LOOP?DOskip looping if<to>and<from>are equal
- create
CREATEis a component of the compiler, whose function is to make a new dictionary entry with a given name (the next name in the input stream): VARIABLE CREATE 1 CELLS ALLOT ;definition of variable- or
: VARIABLE CREATE 0 , ; ,put top of stack to the dictionary pointer and increment dictionary pointer: CONSTANT CREATE , DOES> @ ;definition of constant (it is usually implemented in machine code for speed, though)DOES>: : CREATE ] DOES> doLIST ;definition of:: ; next [ ; IMMEDIATEdefinition of;[switch to interpret mode,]switch to compile mode
Backlinks
- π Forth