home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 4 Part 090 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
-
- We have been working with the words:
-
- CONSTANT VARIABLE CREATE ALLOT DUMP ERASE ! @ C! C@ , and C,
-
- In the last example of L4P080 there were some very interesting
- developments. We did not however provide much in the way of an
- explanation for that example preferring to give you a chance to analyze
- this exciting example on your own.
-
- Let's take a detailed walk through the code as this example provides an
- example of how all of the above words can work together to provide a
- very readable interface to the array data structure.
-
-
- \ Tables - arrays by another name.
- CREATE TABLE 0 , 0 , 0 , 0 , 0 , 0 ,
-
- Here we are creating an array called TABLE This array will hold
- 6 16-bit numbers and they are initialized to zero by the six occurances
- of the sequence 0 ,
-
- It might be appropriate here to say a bit more about comma " , "
- You may have heard of people talk about compilers for languages such as
- BASIC, C, and PASCAL. Forth takes a different approach. There is no
- one compiler for Forth. Instead Forth is made up of a whole lot of
- little compilers.
-
- The simplest of these compilers is comma " , " " , " is Forths 16-bit
- number compiler. Everytime a number is followed by a comma " , " the
- Forth interpreter will compile that number into the dictionary and
- advance the dictionary pointer by 2 bytes. That is why the TABLE array
- above has 6 slots and how each of those slots is initialized to zero.
-
- Forth has other compilers. There is of course C, which compiles bytes.
- CONSTANT compiles constants into the dictionary.
- VARIABLE compiles variables into the dictionary.
- the : ... ; pair compiles colon definitions into the dictionary.
- We will see many more examples of little compilers as we progress and
- even make some or our own.
-
- Another way to create an array of 6 numbers and intialize each entry to
- zero follows:
-
- CREATE TABLE 12 ALLOT \ Here space is alloted but not initialized.
- TABLE 12 ERASE \ Here the 12 bytes or 6 16-bit numbers are
- \ set to zero.
-
- ╓──────────────╖
- ║ Problem 4.11 ║
- ╙──────────────╜
- Make word called CLEAR-TABLE that uses a DO ... LOOP and the store
- operator " ! " to set all six values of the array TABLE to zero.
-
- Well, took awhile but we did get side tracked talking about compilers.
- The varaible MODE is going to be used as flag or signal to tell our
- smart or intelligent word MARBLES what action it should be performing
- when it is executed. Depending upon the value that MARBLES finds stored
- in the varialbe MODE it will perform different operations. We'll
- discuss them later.
-
- VARIABLE MODE
-
- The constants below define fixed offsets into the array TABLE.
- We have used colours for the names of the offsets because our array is
- going to hold the number marbles we have of each colour in our
- collection.
-
- 0 CONSTANT RED 2 CONSTANT BLUE 4 CONSTANT YELLOW
- 6 CONSTANT BLACK 8 CONSTANT WHITE 10 CONSTANT GREEN
-
-
- This group of words is used to set the current function of the word
- MARBLES. The names have been choosen to indicate the function to be
- performed. The variable MODE will carry the message.
-
- : LESS -1 MODE ! ; \ Subtract top of stack from current position.
- : SHOW 0 MODE ! ; \ Display current array position.
- : MORE 1 MODE ! ; \ Add top of stack to current position.
- : ONLY 2 MODE ! ; \ Store top of stack to current array position.
-
- The words below provide a readable way of examining the message that
- was stored in the variable MODE.
-
- : LESS? MODE @ -1 = ;
- : SHOW? MODE @ 0= ;
- : MORE? MODE @ 1 = ;
-
- ╓───────────────╖
- ║ Problem 4.12 ║
- ╙───────────────╜
- Why is there no word ONLY? defined as : ONLY? MODE @ 2 = ;
- What could you do with such a word?
-
- ┌───────────────────────────────────┐
- │ Please move to Lesson 4 Part 100 │
- └───────────────────────────────────┘
-