Type
kforth
to start the program. Upon startup, kForth will inform
you that it is ready to accept input by displaying
Ready!
You may type commands, a sequence of words, and
press Enter. kForth will respond with the prompt
ok
after it finishes executing each line of input.
To illustrate, try typing the following
2 5 + .
and press Enter. kForth will respond with
7 ok
You may now enter another sequence of words.
One particularly useful word to know is
bye
kForth will respond by saying
Goodbye
and exiting. Finally, note that kForth is not case
sensitive --- you may enter words in lower case
or upper case.
The word
words
will display a list of currently defined words in the
dictionary. You may define your own words by
typing them at the kForth prompt. For example, a word that
counts from one to ten and displays each number counted may be
defined by entering
: count_to_ten 10 0 do i 1+ . loop ;
The symbols ":" and ";" are very important --- they indicate to
the kForth compiler the beginning and ending of the definition of
the word, called count_to_ten
in this example. kForth will
display the prompt ok after the new word has been
compiled into the dictionary.
You can verify that our newly defined word has been added to
the dictionary by using words
. Now,
execute the word by typing
count_to_ten
and pressing Enter. kForth will display the output
1 2 3 4 5 6 7 8 9 10 ok
If you are entering a definition that requires
several lines of typing, the ok prompt will not be
displayed until the end of the definition has been entered,
i.e. until the compiler encounters a semicolon.
Although you can write Forth programs this way, it is much easier
to create the definitions in a separate source file and then load
them into kForth by issuing the command
include
filename
For example, the definition of count_to_ten
could
have been entered into a plain text file called prog1.4th.
Once kForth has been started, you can simply issue the
command
include prog1
kForth will read the input from the specified file as though
it was being entered from the keyboard. You may have noticed
that the full filename was not entered in the include
command. If no extension is specified, the file is
assumed to have an extension of .4th.
You may also load a source file upon startup of kForth by typing
kforth
filename
Forth provides a memory region called the stack
in which data may be placed and operated upon by defined words.
You may enter numbers onto the stack simply by typing them
and pressing Enter. You can use the word .S
to
list the contents of the stack.
For example, type the following and press Enter.
2 5
You have placed two numbers onto the stack. Now, type
.S
and press Enter. kForth will respond by listing the items
on the stack:
+ - * /
SWAP ROT DUP OVER TUCK DROP NIP
Each stack cell holds a single integer number. You may
also place real numbers, also known as floating point numbers,
onto the stack. These must be entered in a special way known as
exponential notation. For example, to enter
the real number 3.14 onto the stack, type
3.14e0
and press Enter. The zero following the 'e' indicates
the power of ten that is multiplied to the number (10 raised to
the zero power is equal to 1). If the exponent is zero, as in
this example, the entry can be shortened to simply
3.14e
Exponential format allows you to enter very small and very large numbers
easily. To enter the fractional number representing one-billionth,
0.000000001, you may type
1e-9
and press Enter.
When you enter a floating point number onto the stack and list
the stack using .S
, you will see two integer numbers
printed instead of one real number. A floating point number occupies
two stack cells instead of one, and .S
lists the contents
of each cell as though it were a single integer. You may print the
floating point number occupying the top two cells of the stack with
the word
F.
Use the words
F+ F- F* F/
to perform arithmetic on floating point numbers which have been
entered onto the stack. Example:
3.14e 6.28e f+ f.
will print the result 9.42. Words to manipulate floating
point numbers on the stack include
FSWAP FROT FDUP FOVER FDROP