Cookbook (cook·book) a book containing recipes and other information about the preparation and cooking of food.
The way I cook in the Forth programming language. Let me cook!.
This cookbook is using pforth forth implementation.
All Forth Word Capitalize
Souce 1
git clone https://github.com/philburk/pforth.git
cd pforth
cmake -S . -B build -G Ninja
cmake --build build
./fth/pforth_standalone
CR is EMIT newline
." It's Forth" CR
. ( N -- , print number on top of stack )
DUP ( n -- n n , DUPlicate top of stack )
SWAP ( a b -- b a , swap top two items on stack )
OVER ( a b -- a b a , copy second item on stack )
DROP ( a -- , remove item from the stack )
ROT ( a b c -- b c a , ROTate third item to top )
NIP ( a b -- b , remove second item from stack )
TUCK ( a b -- b a b , copy top item to third position )
?DUP ( n -- n n | 0 , duplicate only if non-zero, '|' means OR )
-ROT ( a b c -- c a b , rotate top to third position )
2SWAP ( a b c d -- c d a b , swap pairs )
2OVER ( a b c d -- a b c d a b , leapfrog pair )
2DUP ( a b -- a b a b , duplicate pair )
2DROP ( a b -- , remove pair )
PICK ( ... v3 v2 v1 v0 N -- ... v3 v2 v1 v0 vN )
: SQUARE ( N -- N*N , calculate square )
DUP *
;
In forth FALSE is 0 and TRUE is -1 or any number that not zero is TRUE
ELSE is Optional
: GT4? ( -- , check if greater than 4 )
DUP 4 >
IF ." YES"
ELSE ." NO"
THEN
;
: PRINT.NUM ( -- , print a number )
CASE
0 OF ." ZERO" ENDOF
1 OF ." ONE" ENDOF
2 OF ." TWO" ENDOF
3 OF ." THREE" ENDOF
4 OF ." FORTH" ENDOF
DUP . ." WHO?"
ENDCACE
;
: COUNT.UP ( N -- , print number 0 to N )
0
BEGIN
DUP .
1 +
2DUP <
UNTIL
2DROP
;
I
is Special (Optional)
: COUNT.UP ( N -- , print number 0 to N )
1 + 0
DO
I .
LOOP
;
in DO … LOOP you can also LEAVE
: COUNT.UP ( N -- , print number 0 no N )
1 + 0
BEGIN
2DUP >
WHILE
DUP .
1 +
REPEAT
2DROP
;
CHAR ( address -- address' , add the size of one character )
COUNT ( $addr -- addr #bytes , extract string information )
TYPE ( addr #bytes -- , output characters at addr )
ACCEPT ( addr maxbytes -- numbytes , input text, save at address )
S and C is add string to stack, but C add null termination
: GREET ( -- , print friendly )
S" Hello Friend" TYPE
;
: GREET ( -- , print friendly )
C" Hello Friend" COUNT TYPE
;
HEX, DECIMAL & BINARY for changing base
6 BINARY
6 DECIMAL
6 HEX
also can change any BASE, BASE is variable
7 BASE !
Forth Tutorial by https://www.softsynth.com/pforth/pf_tut.php ↩