Forth by Examples
Forth by Examples
Forth by Examples
FORTH by examples iv
Annexes xvi
Lessons & Links . . . . . . . . . . . . . . . . . . . . . . . . . xvi
Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Some FORTH implementations . . . . . . . . . . . . . . . . xvii
FORTH for microcontrollers . . . . . . . . . . . . . . . . . . xviii
Some more exotic FORTH implementations . . . . . . . . . xix
Practical uses . . . . . . . . . . . . . . . . . . . . . . . . . . xix
ii
TABLE DES MATIÈRES
iii
FORTH by examples
FORTH by examples
https://gitlab.com/garvalf/forth-is-fun
iv
Memento and examples
All examples will display the basic core words in capital letters.
Some FORTH might accept both commands in lower and upper
case letters, while some others might behave differently.
Forth basics
First of all:
\ is for a comment.
: test_cr
CR CR CR
;
SPACE
SPACES ( n – ) is for n spaces
v
FORTH by examples
: test_spaces
10 SPACES
;
PAGE clears the screen and resets the cursor position to top left
corner
. is for displaying the top value on the stack (consuming it)
." is for appening some text into the console (use a space after
it!). " The text output ends after the second double-quote
Arithmetics are in RPN (reverse polish notation), with integer
numbers.
You can use + , - , * and /
: test_arithmetics
8 5 + 3 - 2 / 3 * .
\ 15 ok
;
vi
FORTH BASICS
vii
FORTH by examples
: test_min_max
4 12 2DUP 2DUP
." between " . ." and " . CR
MIN . ." is the smallest and "
MAX . ." is the biggest"
\ between 12 and 4
\ 4 is the smallest and 12 is the biggest ok
;
: test_negate_abs
5 -2 * ABS .S
\ 10
NEGATE .
\ -10 ok
;
: test_emit
68 EMIT
\ D ok
;
: test_string
." Hello World "
;
viii
CONDITIONS AND LOOPS
: test_see
see test_emit
\ : test_emit #68
\ emit ; ok
;
: test_if ( n -- )
12 < IF ." This number was lower than 12" THEN
CR ." This part will always be displayed."
;
: test_if_else ( n -- )
12 < IF
." This number was lower than 12."
ELSE
." This number was higher than 12 or equal to it."
THEN
CR ." This part will always be displayed."
;
DO and LOOP are for definite loops. I is the index of the current
loop and J is the second index in the case of two imbricated loops.
There is also K as a third index.
: test_loop_definite ( -- ) \ loop from 1 to 12
ix
FORTH by examples
: test_+loop_definite
130 1 DO ." looping " I . 8 +LOOP CR ." End of the
loop"
;
: test_loop_leave
." TODO "
;
BEGIN and AGAIN are for endless loops. They can be exited with
EXIT though (or ctrl+c)
: test_loop_again
0 BEGIN 1 + DUP .
DUP 500000 = IF DROP EXIT THEN
AGAIN
;
x
VARIABLES AND ARRAYS
VARIABLE Apples
: test_variables
5 Apples !
Apples @ .
\ 5 ok
Apples @ 1 + Apples !
Apples~?
\ 6 ok
;
VALUE like a variable VALUE takes an initial value, and the created
word puts the value directly on the stack like CONSTANT. The value
can still be changed using TO. Word definitions in many Forths using
VALUE’s will be smaller, because they just need to reference the
created word and not !.
xi
FORTH by examples
xii
NEW DEFINITIONS
VARIABLE Peaches
: test_move
0 Peaches !
test_plusstore
CR ." There are " Peaches~? ." peaches"
CR ." but now we move..."
Apples Peaches 1 MOVE
CR ." There are " Apples~? ." apples"
CR ." There are " Peaches~? ." peaches"
;
New definitions
: test_define_does>
myC
\ mya 67 in ascii is C ok
;
xiii
FORTH by examples
SYNONYM can also create an alias for a word, it’s a standard but not
always implemented
SYNONYM ensuite THEN not included by default
should behave the same as:
: ensuite POSTPONE THEN ; IMMEDIATE
DEFER creates a placeholder for a word name which will be defined
later. IS will define such a word whose definition will start with
:NONAME instead of :
DEFER test_defer
Some more definitions can be set here. You can reference
test_defer now
: test_defer_bis
." Here is the result of the addition of 3 + 3: "
test_defer
;
:NONAME
CR
3 3 + . CR
." It was the defered definition"
xiv
STRINGS
;
IS test_defer
Strings
PAD is an address used to store temporary data ACCEPT ( addr n1 –
n2) receives a string of n1 characters )
variable myVar variable myVarLen
: testInput
." type a number (4 characters max):"
PAD 4 ACCEPT myVarLen !
PAD myVarLen @ s>number? drop drop myVar !
." You have typed: " myVar @ .
;
Tools
DUMP ( addr n1 – ) displays the n1 lines of content from an adress
xv
FORTH by examples
Annexes
xvi
STANDARDS
Standards
• Forth 79 standard
◦ https://www.complang.tuwien.ac.at/forth/fth79std/FORTH-
79.TXT
• Forth 83 standard
◦ https://www.complang.tuwien.ac.at/forth/fth83std/FORTH83.TXT
• Forth 94 ANS standard
◦ http://www.forth.org/svfig/Win32Forth/DPANS94.txt
• Forth 2012 standard
◦ http://forth200x.org/documents/forth-2012.pdf
• Forth 200x Standard liste core bib
◦ https://forth-standard.org/standard/core
• UF Forth glossary
◦ https://gitlab.com/b2495/uf/-/blob/master/GLOSSARY
https://gforth.org/manual/Word-Index.html gforth index
xvii
FORTH by examples
xviii
SOME MORE EXOTIC FORTH IMPLEMENTATIONS
Practical uses
• GameBoy development kits: https://gbforth.org/
https://github.com/Reinboar/pixelforth
• Gforth sdl 2 binding : https://github.com/JeremiahCheatham/Gforth-
SDL2-Bindings/
xix
FORTH by examples
TODO
Words to add in the examples:
RECURSE IMMEDIATE R> >R R@ ] [ LITERAL AND OR 0< 0=
0> 1+ 1- 2+ 2- 2/ 2* +! KEY ’ EXECUTE CELL CREATE QUIT CHAR
[CHAR]
xx