Forth Notes

Forth is the coolest programming language I know of, it feels sort of like Lisp, except it's more low level like C. Like Lisp, Forth has basically no syntax and everything revolves around a simple core data structure, in this case a stack. Like C, Forth works very closely with the hardware and doesn't manage memory for you.

Tables of Forth Words

Words that don't manipulate the stack.

Word Notation Explanation
: ; ( -- ) : defines a new word up to ;
cr ( -- ) Output a newline (Carriage Return)
." " ( -- ) ." outputs a string up to the next "

Stack Manipulation Words.

Word Notation Explanation
. ( n -- ) Move the top element of the stack to the console and append a space
emit ( c -- ) Move the top number on the stack to the console as an ascii character, but don't append a space
dup ( n -- n n ) Duplicate the top element of the stack
drop ( n -- ) Remove the top element from the stack
swap ( n1 n2 -- n2 n1 ) Swap the topmost elements of the stack
over ( n1 n2 -- n1 n2 n1 ) Duplicate the second element of the
rot ( n1 n2 n3 -- n2 n3 n1 ) Move the third element of the stack to

Boolean Words

Forth doesn't have a boolean type. Instead it treats 0 as falsy and everything else as truthy. -1 is the canonical truthy value, which feels a little weird to me coming from C.

Word Notation Explanation
= ( n1 n2 – ) Are the top two numbers in the stack equal?
< ( n1 n2 – ) Is the top number in the stack greater than the second number?
> ( n1 n2 – ) Is the top number in the stack less than the second number?

Control-Flow Words

Word Notation Explanation
if then    
if else then    
do loop    
begin until    

Non-Word Types: Variables, Constants, Arrays

External links

Viewable With Any Browser Join the Fediverse!!! 100% mothra-compatible Powered by FreeBSD This site made with GNU Emacs

Last Modified: 2021-05-28 Fri 11:18