home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l7p060
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
96 lines
╔════════════════════════════════════════════════════╗
║ Lesson 7 Part 060 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌────────────────────────┐
│ CREATE .... DOES> │
└────────────────────────┘
Our adventure into the mysterious, powerful, and captivating
CREATE .... DOES> construct begins.
First let's review how the word CREATE works. CREATE can be used on its
own as follows:
CREATE TABLE
This would create an entry in the FORTH dictionary called TABLE. But
that is all it would do! It just creates an empty shell of a word.
Every word that is used by the Forth system to make entries in the
dictionary, such as VARIABLE CONSTANT : CODE etc etc have the
word CREATE buried in them right near the beginning of their
definitions. When a word created by CREATE is executed it returns the
words " parameter field address " or pfa to the stack. The pfa normally
points to a words data storage area. In the case of the word TABLE
created above it points to the next available dictionary location. The
word TABLE created above is actually quite useless on its own. If
another definition were to be added to the dictionary it would sit in
TABLE's parameter field area. Here is how the standard FORTH word
VARIABLE could be defined using CREATE .
: VARIABLE ( -- ) CREATE 2 ALLOT ;
This definition of VARIABLE would initialize the value of the variable
to garbage which is required by the F83 standard. Most Forth systems
would define VARIABLE as follows:
: VARIABLE ( -- ) CREATE 0 , ;
This definition of VARIABLE would initialize the value of a variable to
0. The comma compiles the 0 into the next available dictionary location.
CREATE is often used to create tables or arrays. You would do this by
ALLOTting some dictionary space or compiling the initial values. For
example:
CREATE TABLE 10 ALLOT creates a TABLE to hold 5 16-numbers. The table
is uninitialized (initial values are garbage).
CREATE TABLE 11 , 22 , 33 , 44 , 55 ,
creates a table to hold 5 16-bit numbers and the table is initialized
to the values 11 22 33 44 and 55.
Now let's see how the CREATE DOES> construct can be used to define a
regular FORTH type CONSTANT. You remember how constant works don't
you.
45 CONSTANT BRICK creates a constant called BRICK which is
initialized to 45 (cents?). When BRICK is executed it puts 45 on the
stack. Just so that we don't get mixed up, the new version of CONSTANT
will be called CON ( it is also shorter to type!)
Here is the definition of our version of CONSTANT called CON
: CON ( n -- )
CREATE , \ This is the creation time stuff.
DOES> @ ; \ This is the run time stuff.
The part of the definition between CON and DOES> describes how to make
CON type constants, ie create a dictionary header and compile the
initial value. The part of the definition between DOES> and ; tells what
is to happen when a CON type constant is executed. Namely, fetch the
value to the parameter stack. Remember that any word created by CON
inherits the properties of CREATE. When a CON type constant executes
pfa or data storage address is pushed to the stack so only the fetch " @
" is required between the DOES> and the ; .
To make a constant with our new defining word CON we have to use it!
Here is how:
45 CON BRICK
This would make a constant call BRICK and initialize it to 45.
The next thing to do is to use the constant called BRICK. For example,
if you had 5 BRICK's they would be worth:
BRICK 5 * .
Please try this for yourself.
┌──────────────────────────────────┐
│ Please Move to Lesson 7 Part 070 │
└──────────────────────────────────┘