Contents
Introduction
Chapter 1
SETTING UP THE ACE
Chapter 2
SETTING UP THE ACE
Chapter 3
LOADING PROGRAMS FROM TAPE
Chapter 4
DEFINING NEW WORDS
Chapter 5
SIMPLE ARITHMETIC
Chapter 6
DEFINING NEW ARITHMETIC WORDS
Chapter 7
ALTERING WORD DEFINITIONS
Chapter 8
WORDS THAT ARE REALLY NUMBERS
Chapter 9
MAKING DECISIONS
Chapter 10
REPEATING
Chapter 11
SOUND
Chapter 12
THE CHARACTER SET
Chapter 13
PLOTTING GRAPHS
Chapter 14
SAVING PROGRAMS ON TAPE
Chapter 15
FRACTIONS AND DECIMAL POINTS
Chapter 16
READING THE KEYBOARD
Chapter 17
OTHER WAYS OF COUNTING
Chapter 18
BOOLEAN OPERATIONS
Chapter 19
MORE ADVANCED ARITHMETIC
Chapter 20
INSIDE THE DICTIONARY
Chapter 21
STRINGS AND ARRAYS
Chapter 22
VOCABULARIES
Chapter 23
INSIDE COLON DEFINITIONS
Chapter 24
HOW THE MEMORY IS LAID OUT
Chapter 25
MACHINE CODE
Chapter 26
EXTENDING THE ACE
Appendix A
QUICK GUIDE FOR 'FORTH' ENTHUSIASTS
Appendix B
ERRORS
Appendix C
THE JUPITER ACE - FOR REFERENCE
Appendix D
QUICK GUIDE FOR 'FORTH' ENTHUSIASTS
INDEX
|
Chapter 4
DEFINING NEW WORDS
When you do VLIST, you see a list of all the words that the computer already knows
about - its dictionary. When you first switch on these are the words that are built into the Ace,
but the dictionary isn't final because you can define your own words. This is
the process of writing a computer program, or telling the computer how to do
something new.
As a (not very practical) example, suppose you want to teach the computer a new
word BILL, which is to mean 'Do VLIST twice'. You do this using two special words,
: (colon) and ; (semicolon), like this:
: BILL
VLIST VLIST
;
: is a word telling the computer that you're going to define a new word. First will come its name (BILL) and then the definition saying how to execute BILL.
So, type in : (and ENTER) ... oops! Sorry, I forgot to tell you that : needs the name
of the new word straight away, there and then. Otherwise it says ERROR 6 — you can
look up the various error numbers in Appendix B at the back of the manual, where
you can find out what went wrong. If you ever get ERROR when you're half way
through defining a new word, then you have to start all over again from :.
All right, this time type in
: BILL
↑
remember the space
When you press ENTER the computer doesn't say OK, but that's just to remind
you that you're in the middle of a definition. At least it doesn't say ERROR.
Next comes the central part of the definition, saying what the computer is to do
when you use BILL: it is to do VLIST twice. Type in
VLIST VLIST
and ENTER. Again, there's no OK. Also, thankfully, there's no long list of words
printed up - the computer knows it's in the middle of a definition, so VLIST doesn't
need to be executed.
Finally, ; means, 'The definition is finished. Now you know what BILL means', so
type in ; and ENTER. This time the computer will print OK.
Now the computer knows the new word BILL, and you can prove this in two ways.
First, if you use VLIST, you'll see that BILL has appeared at the beginning of the dictionary .
Second, if you type in BILL, the computer will execute it will do VLIST twice.
If you type in BILL once too often for your patience, and get depressed at seeing
the dictionary yet again, press BREAK (shifted SPACE). The computer will stop,
saying ERROR 3. If you want to interrupt the computer when its in the middle of
something, BREAK nearly always works. What's more - unlike pulling the plug out -
it doesn't destroy the words you've defined. In some circumstances, for instance
when the computer is using the tape recorder, unshifted SPACE also acts as BREAK.
BILL is a moronically useless word and nobody would normally bother to define it.
But you will soon see that the same partnership of : and ; can be used to define
tremendously powerful words, so make sure you understand them. Remember, to
define a new word, you need
first, :
second, and on the same line as : followed by a space, the name of the new word
third, the definition of the new word (which shows how the new word is made up
from old ones)
and fourth, ;
This is called a colon definition, because it uses : (there are other sorts of definition
as well).
I had you defining BILL on three separate lines, so that I could explain it all as we
went along. In practice, you'd type it all in at once, as
: BILL VLIST VLIST ;
↖ ↑ ↗ ↗
Spaces
This is quite permissible. Also, in practice you'd use a more suggestive name --something
like 2VLISTS.
Here's a construction that can liven up word definitions; in fact it can only be used
in word definitions. It enables the word to print out a message when it is executed,
and consists of the word ." (followed by a space), then the message, then the
character ". (SYMBOL SHIFT P.) ." is pronounced dot-quote. It's often used in
conjunction with a word CR (Carriage Return), which makes the next message start
on a new line. Here's an example:
: BEN
CR ." Aah bobbop tipop weed ."
;
Note - if you forget the second ", you get ? to give you a chance to put it in.
Remember that ? means, 'Do you want to change this at all?'
Summary
FORTH words :, ;, CR and ."
BREAK
ERROR messages
Exercises
1. Type in
." Hello!"
As we said, you can only use ." within a word definition. (Look up Error 4
in Appendix B.)
CR doesn't suffer from this disability. If you type in
CR CR CR CR CR
you can see it forcing a new line each time.
2. Define some words like
: FOOTBALL
CR ." Hamilton Academicals boot-boys"
CR ." rule"
;
and
: FARMING
CR ." Shaggy sheep wool"
;
Amuse your friends by getting them to type in FOOTBALL and FARMING. See
their eyes light up with glee!
[Top] | [Back] | [Next]
|