home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l7p070
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
99 lines
╔════════════════════════════════════════════════════╗
║ Lesson 7 Part 070 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌──────────────────────────────────────────┐
│ Summary of the CREATE ... DOES> Process │
└──────────────────────────────────────────┘
In summary, there are three aspects to consider when using the
CREATE ... DOES> construct.
1) The process of creating a new defining word or compiler extension
which will create a new class of objects or data structures.
2) The action of using the new defining word to create some instances of
the new objects or data structures.
3) The execution of one or more of the new objects resulting in actions
characteristic of their class.
The word VARIABLE can also be defined using CREATE ... DOES> but
variables are not too exciting. The defining words VAR1 and VAR2
function similarly to the definitions of VARIABLE given earlier.
: VAR1 ( -- )
CREATE 2 ALLOT \ Compile time routine allots space for 16-bit number
DOES> ; \ Run time routine does nothing since CREATE leaves
\ the data storage address.
: VAR2 ( -- )
CREATE 0 , \ Compile time routine initializes variable to zero.
DOES> ; \ Run time routine does nothing as above.
If you have an 8-bit CPU and are often creating variables and constants
in the range 0 - 255 you may appreciate the following space savers.
: BCON ( n -- ) \ n must be between 0 and 255
CREATE C, \ Compile time routine compile only one byte.
DOES> C@ ; \ Run time ... fetches byte stored at compile time.
: BVAR ( -- )
CREATE 1 ALLOT \ Compile time allots only one byte of storage.
DOES> ; \ Run time not required as CREATE leaves pfa.
╓───────────────╖
║ Problem 7.4 ║
╙───────────────╜
Implement VAR1 VAR2 BCON and BVAR and verify that they operate as
advertised. Provide at least one space saving application of BCON.
┌──────────────────────────────────────────┐
│ Creating a VECTOR with CREATE ... DOES> │
└──────────────────────────────────────────┘
CREATE ... DOES> is really the most exciting aspect of Forth. Also at
least initially the most difficult to understand. You see... this
construct can be used to create language. Most other languages do not
allow the user to create new defining words or compiler extensions.
Let's look at one more traditional extension. We are going to create a
data type called VECTOR. We will use VECTOR to create FORCE and
DISTANCE and then compute some WORK using the old Physics definition
WORK = FORCE dot DISTANCE. Here is the defining word VECTOR: This is
step 1 create it.
: VECTOR ( n -- ) \ n is the maximum subscript value.
CREATE 2 * ALLOT \ Compile time allot space for n 16-bit numbers.
DOES> SWAP -1 2 * + ; \ Run time, compute storage address of element.
This is step 2 make some instances of VECTORS.
3 VECTOR DISTANCE
3 VECTOR FORCE
This is step 3 use the instances to something useful:
Assign some values to our vectors...
4 1 DISTANCE ! 6 2 DISTANCE ! 5 3 DISTANCE !
1 1 FORCE ! 2 2 FORCE ! 3 3 FORCE !
The vector DISTANCE = [ 4, 6, 5 ] and FORCE = [ 1, 2, 3 ]
Now here is the definition that will calculate the work.
: WORK ( -- )
0 4 1 DO I DISTANCE @
I FORCE @
* +
LOOP
." The work is " . ;
Executing WORK would give:
WORK <enter> The work is 31 ok
┌───────────────────────────────────┐
│ Please Move to Lesson 7 Part 080 │
└───────────────────────────────────┘