home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l7p110 < prev    next >
Text File  |  1990-07-15  |  4KB  |  92 lines

  1.  
  2.        ╔═════════════════════════════════════════════════════╗
  3.        ║  Lesson 7 Part 110 F-PC 3.5 Tutorial by Jack Brown  ║
  4.        ╚═════════════════════════════════════════════════════╝
  5.  
  6.         ┌───────────────────────────────────────────────────┐
  7.         │ Using CREATE ... DOES> to Define Smart Variables  │
  8.         └───────────────────────────────────────────────────┘
  9.  
  10.  
  11. Most beginning Forth programmers soon tire of all the  fetches  " @ "
  12. and stores " ! " required in application.  Why not they ask, can't we
  13. have variables work the way the do in BASIC??  When we type a variables
  14. name we get the value of the variable,  when we want to give a variable
  15. a new value we will use an assignment operator.  The answer is or course
  16. that you can make you own variables that behave any way you like.
  17.      
  18. Implementation of variables that work like BASIC variables with an
  19. assignment operator are called by various names by Forth People.  Common
  20. terms  " smart variables " and the " TO concept " are used to describe the
  21. ideas implemented below.
  22.     
  23. The idea is to keep track of whether a variable should be fetching or
  24. storing by means of a regular type variable that contains a flag.  In
  25. the implementation below the name of the flag is STORE?  The idea is
  26. this: If the run time routine of SMART-VARIABLE detects that STORE?
  27. contains a false flag then it will fetch the value of the variable.  If
  28. the run time routine detects that the value of the variable STORE? is
  29. true flag then it will store the top stack number in the variable.
  30.     
  31. The default value of the variable STORE? is false, ie  the normal mode
  32. of operation is fetching. To change the mode of operation we make an
  33. assignment operator that puts at true flag in STORE?  The name of the
  34. assignment operator could be anything that makes sense,  =  =:  ->  etc,
  35. Just to be difficult the example uses => for the assignment operator.
  36. Here is your SMART-VARIABLE :
  37.  
  38. \ Template for creating new compilers:
  39. \ : {compiler name}
  40. \       CREATE  {compile time code}
  41. \       DOES>   {run time code}  ;
  42. \ At run time the pfa of the created word is put on the stack.
  43.  
  44.   VARIABLE STORE?
  45.  
  46. : =>    ( -- )  STORE? ON ;   \ Set STORE? to true.
  47.  
  48. : SMART-VARIABLE
  49.         CREATE  0  ,            \ Compile time action.
  50.         DOES>   STORE? @  STORE? OFF
  51.                 IF    !  ELSE  @ THEN  ;
  52.  
  53. Usage:   SMART-VARIABLE  JACK
  54.          JACK  . <enter>  0     ok
  55.  123 =>  JACK    <enter>  ok
  56.          JACK  . <enter>  123   ok
  57.  
  58.     ┌───────────────────────────────────────────────────────┐
  59.     │ VECTOR with both compile and run time error checking  │
  60.     └───────────────────────────────────────────────────────┘
  61.  
  62. \ Create a one dimensional vector of n storage cells
  63. \ Usage:         VECTOR  {name}   ( n -- )
  64. \ Later:         {name}           ( index -- adr )
  65.  
  66. : COMPILE-VECTOR
  67.         DUP ,                 \ Compile n, maximum subscript.
  68.         0 DO 0 , LOOP   ;     \ Initialize vector to zeros.
  69.  
  70. : RUN-VECTOR     ( index pfa -- addr )
  71.         TUCK @  OVER           \ pfa index n index
  72.         <= OVER 0< OR          \ pfa index flag
  73.         ABORT" Subscript out of range."  \ Error message
  74.         1+  2* + ;             \ Compute address of ith element.
  75.  
  76. : VECTOR   ( n -- )
  77.         DUP 1 < OVER 256 > OR  ABORT" Dimension out of range."
  78.         CREATE  COMPILE-VECTOR
  79.         DOES>   RUN-VECTOR ;
  80.  
  81. ╓───────────────╖
  82. ║  Problem 7.8  ║
  83. ╙───────────────╜
  84. Implement and test the above version of VECTOR.
  85.  
  86. ┌───────────────────────────────────┐
  87. │  Please Move to Lesson 7 Part 120 │
  88. └───────────────────────────────────┘
  89.  
  90.  
  91.  
  92.