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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 2 Part 060  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.           ┌────────────────────────────────────────────┐
  6.           │   Some new words to manipulate the stack.  │
  7.           └────────────────────────────────────────────┘
  8.  
  9. Copy the ith stack item to the top of the stack.
  10. PICK ( ...ni... n1 n0 i -- ...ni...n1 n0 ni )
  11.  
  12. Rotate the ith stack item to the top of the stack.
  13. ROLL ( ...ni... n1 n0 i -- ..._... n1 n0 ni )
  14.  
  15. ROLL is a generalization of ROT.
  16.  
  17. Let's load our outer interpreter MYQUIT and see what these do!
  18. FLOAD MYQUIT  ok
  19. MYQUIT
  20.  Stack Empty. > 11 22 33 44
  21.  [4]     11      22      33      44 > 3 PICK
  22.  [5]     22      33      44      11 > 2 PICK
  23.  [6]     33      44      11      33 > 1 PICK   <--- hey it's OVER
  24.  [7]     44      11      33      11 > 0 PICK   <--- hey it's DUP
  25.  [8]     11      33      11      11 > CLEAR    <--- you got CLEAR?
  26.  Stack Empty. > 11 22 33 44
  27.  [4]     11      22      33      44 > 3 ROLL
  28.  [4]     22      33      44      11 > 2 ROLL   <--- hey it's ROT
  29.  [4]     22      44      11      33 > 1 ROLL   <--- hey it's SWAP
  30.  [4]     22      44      33      11 > 0 ROLL   <--- hey it's nothing.
  31.  [4]     22      44      33      11 >
  32.  
  33. Both PICK and ROLL are zero based indexing.  Consider the stack to be an
  34. array with the 0th array position corresponding to the top of the stack.
  35.  
  36. Note that we could define some of our common stack operators using PICK
  37. and ROLL.
  38.  
  39. : OVER   1 PICK ;         <---  Make these definitions and verify
  40. : DUP    0 PICK ;         <---  that they work like the regular versions
  41. : ROT    2 ROLL ;
  42. : SWAP   1 ROLL ;
  43.  
  44. No one would do this... it's just interesting...  OVER , DUP , ROT , and
  45. SWAP are implemented using machine CODE definitions.
  46.  
  47. More new words. These are not part of the FORTH83 standard but they are
  48. in F-PC and very useful.
  49.  
  50. NIP    ( a b -- b )             Remove second number from the stack.
  51. TUCK   ( a b -- b a b )         Shove copy of top item under second.
  52. 3DUP   ( a b c -- a b c a b c ) Duplicate top three items.
  53.  
  54. ╓─────────────╖
  55. ║ Problem 2.8 ║
  56. ╙─────────────╜
  57. a) Try the above words with MYQUIT running and verify their function
  58.    as we did with PICK and ROLL .
  59.  
  60. b) Write high level colon definitions for NIP TUCK and 3DUP.
  61.    Hint:  : NIP  SWAP DROP ;   Opps... only two left to do!
  62.  
  63. c) Write colon definitions for the following words whose stack effects
  64.    are indicated below.
  65.    SPIN  ( a b c -- c b a )
  66.    HATCH ( a b c -- a b b c )
  67.    SHOVE ( a b c -- c a b c )
  68.  
  69. Let's write an new word to keep TANK_VOLUME company.  Let's write a word
  70. to compute the surface area of the rectangular tank.
  71.  
  72. Surface area = 2lw + 2lh + 2wh.
  73.  
  74. : TANK_AREA  ( l w h   -- )  \ add stack comments for each line
  75.         3DUP            (                            )
  76.         5 ROLL *  2 *   (                            )
  77.         -ROT   *  2 *   (                            )
  78.         +               (                            )
  79.         -ROT *  2 *     (                            )
  80.         +               (                            )
  81.     CR  ." Surface area of tank is " .  ." square" .FT ;
  82.  
  83. Enter TANK_AREA into your file TANKVOL.SEQ , Test it and debug it if you
  84. need help determining the stack effects for each line.
  85.  
  86. ╓──────────────╖
  87. ║  Problem 2.9 ║
  88. ╙──────────────╜
  89. Rectangle area & perimeter
  90. a) Write a word called  .LW  that takes two stack items, a
  91.    length and a width and echoes the values on separate lines
  92.    with identifiers.  Note .LW should leave stack empty.**
  93.  
  94. b) Write a word called AREA that takes two stack items, a
  95.    length and a width, and computes and displays the area of a
  96.    rectangle.  Note  AREA  should leave the stack empty.**
  97.  
  98. c) Write a word call PERIMETER that takes two stack items, a
  99.    length and a width, and computes and displays the perimeter
  100.    of a rectangle.  Note  PERIMETER should leave stack empty.**
  101.  
  102. d) Write a word called RECTANGLE that takes two stack items, a
  103.    length and a width, and performs the functions of a b & c .
  104.    Did you use the   .LW , AREA , and PERIMETER in your
  105.    definition of RECTANGLE?
  106. **  It would be better to say: it consumes its stack parameters.
  107.  
  108. ┌────────────────────────────────────┐
  109. │  Please move to Lesson 2 Part 070  │
  110. └────────────────────────────────────┘
  111.