Skip to content

Instantly share code, notes, and snippets.

@euhmeuh
Created April 13, 2018 15:14
Show Gist options
  • Save euhmeuh/7d6d5f7db342f86844bcf9dca47ee45b to your computer and use it in GitHub Desktop.
Save euhmeuh/7d6d5f7db342f86844bcf9dca47ee45b to your computer and use it in GitHub Desktop.
Forth Cheatsheet
\ Forth
\ stack operations cheat sheet
0 [if]
================================================================
GFORTH STANDARDS
================================================================
WORD SIGNATURE EQUIVALENT CODE
================================================================
dup ( a -- a a ) 0 pick
?dup ( a -- a a | 0 ) dup if dup endif
drop ( a -- )
swap ( a b -- b a ) 1 roll
over ( a b -- a b a ) 1 pick
rot ( a b c -- c a b ) 2 roll
-rot ( a b c -- b c a ) rot rot
nip ( a b -- b ) swap drop
tuck ( a b -- b a b ) swap over
>r ( a -- R: a )
r> ( R: a -- a )
r@ ( R: a -- a R: a )
rdrop ( R: a -- )
2dup ( a b -- a b a b ) over over
2drop ( a b -- ) drop drop
2swap ( a b c d -- c d a b )
2over ( a b c d -- a b c d a b )
2rot ( a b c d e f -- e f a b c d )
2nip ( a b c d -- c d )
2tuck ( a b c d -- c d a b c d )
2>r ( a b -- R: a b )
2r> ( R: a b -- a b )
2r@ ( R: a b -- a b R: a b )
2rdrop ( R: a b -- )
pick ( x0...xn n -- x0...xn x0 )
roll ( x0...xn n -- x1...xn x0 ) dup 0<= if drop else
swap >r 1- recurse r>
swap endif
depth ( x1...xn -- x1...xn n )
.s ( [s] -- [s] )
================================================================
USEFUL ADDITIONS
================================================================
WORD SIGNATURE EQUIVALENT CODE
================================================================
cons% ( -- cons% ) struct
cell% field cons-head
cell% field cons-tail
end-struct cons%
cons ( tail head -- cons% ) cons% %allot dup >l 2! l>
list ( x0...xn -- cons% )
[then]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment