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
.S
to
list the contents of the stack.
For example, type the following and press Enter.
2 5
.S
+ - * /
SWAP ROT DUP OVER TUCK DROP NIP
3.14e0
3.14e
1e-9
.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 wordF.
F+ F- F* F/
3.14e 6.28e f+ f.
FSWAP FROT FDUP FOVER FDROP