home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l6p100
< prev
next >
Wrap
Text File
|
1990-07-15
|
3KB
|
80 lines
╔════════════════════════════════════════════════════╗
║ Lesson 6 Part 100 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌────────────────────────────────────────────────┐
│ A short tutorial on COMPILE and [COMPILE] │
└────────────────────────────────────────────────┘
With a quick look at IMMEDIATE LITERAL and ' pronounced "tick"
Along with ( hopefully) a practical application of these words that
will make them clearer. To understand the example you will need to know
the following words from Forth 83 standard.
IMMEDIATE is used right after the definition of a word to declare it
to be IMMEDIATE. An IMMEDIATE word will execute at compile
time instead of being compiled.
LITERAL compiles the stack number in to the dictionary.
LITERAL is an IMMEDIATE word itself.
Example : TEST1 ( -- n ) [ 32 4 * 6 + ] LITERAL ;
Recall, " [ " turns the compiler off and " ] " turns it back on again.
Would be the same as : TEST2 ( -- n ) 134 ;
' ( tick ) Get the cfa of the next word in the input stream.
' is not IMMEDIATE in F-PC ( it was in Fig Forth).
STATE a system variable that is true if the system is
in compile mode and false if the system is in
interpretive mode.
[COMPILE] force the compilation of an IMMEDIATE word when it
would other wise execute.
COMPILE When the word containing COMPILE <xxx> executes
the word <xxx> will be compile into the dictionary.
Now here is the practical example that should help you understand what
is going on or else thoroughly confuse you. A primitive data base
application:
0 CONSTANT QUANTITY 2 CONSTANT COST
CREATE KEYBOARDS 10 , 120 ,
CREATE MONITORS 50 , 295 ,
CREATE MODEMS 15 , 320 ,
: .FIELD ( cfa -- ) >BODY @ + @ . ;
Ops... You also have to know that >BODY takes the cfa obtained by '
and turns it into the words pfa . The pfa of a constant contains the
constants value. You look up @ + and . yourself.
: GET ' .FIELD ;
Now try the following:
KEYBOARDS GET QUANTITY
MONITORS GET COST
MODEMS GET COST
MODEMS GET QUANTITY ( Neat eh?)
But how come the following won't work???
: REPORT ( -- )
CR KEYBOARDS GET QUANTITY KEYBOARDS GET COST
CR MODEMS GET QUANTITY MODEMS GET COST ;
REPORT
It gives funny answers and leaves numbers on the stack. The solution to
the problem is the proper use of [COMPILE] and COMPILE.
┌────────────────────────────────────┐
│ Please Move to Lesson 6 Part 110 │
└────────────────────────────────────┘