home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l7p110
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
92 lines
╔═════════════════════════════════════════════════════╗
║ Lesson 7 Part 110 F-PC 3.5 Tutorial by Jack Brown ║
╚═════════════════════════════════════════════════════╝
┌───────────────────────────────────────────────────┐
│ Using CREATE ... DOES> to Define Smart Variables │
└───────────────────────────────────────────────────┘
Most beginning Forth programmers soon tire of all the fetches " @ "
and stores " ! " required in application. Why not they ask, can't we
have variables work the way the do in BASIC?? When we type a variables
name we get the value of the variable, when we want to give a variable
a new value we will use an assignment operator. The answer is or course
that you can make you own variables that behave any way you like.
Implementation of variables that work like BASIC variables with an
assignment operator are called by various names by Forth People. Common
terms " smart variables " and the " TO concept " are used to describe the
ideas implemented below.
The idea is to keep track of whether a variable should be fetching or
storing by means of a regular type variable that contains a flag. In
the implementation below the name of the flag is STORE? The idea is
this: If the run time routine of SMART-VARIABLE detects that STORE?
contains a false flag then it will fetch the value of the variable. If
the run time routine detects that the value of the variable STORE? is
true flag then it will store the top stack number in the variable.
The default value of the variable STORE? is false, ie the normal mode
of operation is fetching. To change the mode of operation we make an
assignment operator that puts at true flag in STORE? The name of the
assignment operator could be anything that makes sense, = =: -> etc,
Just to be difficult the example uses => for the assignment operator.
Here is your SMART-VARIABLE :
\ Template for creating new compilers:
\ : {compiler name}
\ CREATE {compile time code}
\ DOES> {run time code} ;
\ At run time the pfa of the created word is put on the stack.
VARIABLE STORE?
: => ( -- ) STORE? ON ; \ Set STORE? to true.
: SMART-VARIABLE
CREATE 0 , \ Compile time action.
DOES> STORE? @ STORE? OFF
IF ! ELSE @ THEN ;
Usage: SMART-VARIABLE JACK
JACK . <enter> 0 ok
123 => JACK <enter> ok
JACK . <enter> 123 ok
┌───────────────────────────────────────────────────────┐
│ VECTOR with both compile and run time error checking │
└───────────────────────────────────────────────────────┘
\ Create a one dimensional vector of n storage cells
\ Usage: VECTOR {name} ( n -- )
\ Later: {name} ( index -- adr )
: COMPILE-VECTOR
DUP , \ Compile n, maximum subscript.
0 DO 0 , LOOP ; \ Initialize vector to zeros.
: RUN-VECTOR ( index pfa -- addr )
TUCK @ OVER \ pfa index n index
<= OVER 0< OR \ pfa index flag
ABORT" Subscript out of range." \ Error message
1+ 2* + ; \ Compute address of ith element.
: VECTOR ( n -- )
DUP 1 < OVER 256 > OR ABORT" Dimension out of range."
CREATE COMPILE-VECTOR
DOES> RUN-VECTOR ;
╓───────────────╖
║ Problem 7.8 ║
╙───────────────╜
Implement and test the above version of VECTOR.
┌───────────────────────────────────┐
│ Please Move to Lesson 7 Part 120 │
└───────────────────────────────────┘