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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 4 Part 110  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.  
  6. In the part 10 of lesson 4 we saw how we could write words that could be
  7. used to access, manipulate, enter, display  etc the contents of an
  8. array.  It is very important that you, the Forth programmer, carefully
  9. design the names and functions of the words that access your arrays.
  10. Poor choice of names and function can result in unreadable and
  11. difficult to maintain code.  Forth is one of the few ( possibly only)
  12. language that allows you the freedom to create your own syntax and data
  13. structure interfaces.  If you were not impressed with the MARBLES array
  14. and the wordset used to access and modify its elements you have missed
  15. the point and should go back and study the MARBLES further.
  16.  
  17.                       ┌────────────────────┐
  18.                       │  Personal Stacks.  │
  19.                       └────────────────────┘
  20.  
  21. In order to understand Forth's data stack a little better we are going
  22. to create our own personal stack.  You see Forth's data stack is little
  23. more than an array with a word set for manipulating its elements.
  24. Type in the following definitions into the file PSTACK.SEQ
  25.  
  26. \ Create an array to be used for a 10 number stack.
  27.   CREATE  P-STACK  20 ALLOT  \ Stack will hold 10 16bit numbers.
  28.  
  29.   VARIABLE P-INDEX           \ Holds the value of user stack pointer.
  30.  
  31. \ Clear and initialize the Personal user stack.
  32. : P-CLEAR  ( -- D) ( ?? -- P)
  33.            0 P-INDEX !  P-STACK 20 ERASE ;
  34.  
  35. \ Return number of elements on the user stack to Forth's data stack.
  36. : P-DEPTH  ( -- n  D)   P-INDEX @ 2/ ;
  37.  
  38. \ Increment user stack pointer with error checking.
  39. : P-INC    ( -- D)
  40.            P-INDEX @ 20 =
  41.            IF ." P-OVERFLOW"  P-CLEAR
  42.            ELSE 2 P-INDEX +! THEN ;
  43.  
  44. \ Decrement user stack pointer with error checking.
  45. : P-DEC    ( -- D)
  46.            P-INDEX @ 0=
  47.            IF ." P-UNDERFLOW"
  48.            ELSE -2 P-INDEX +! THEN ;
  49.  
  50. \ Move number from Forth's data stack to the P-stack.
  51. : >P       ( n -- D)  ( -- n P)
  52.            P-INC P-INDEX @ P-STACK + ! ;
  53.  
  54. \ Copy current top number of P-stack to top of Forth's
  55. \ data stack. Note: P-stack is not changed.
  56. : P@       ( --   n D)  ( n  --  n P)
  57.            P-INDEX @ P-STACK + @  ;
  58.  
  59. \ Move number from top of P-stack to top of Forth's
  60. \ data stack. Note: Number is removed from P-stack.
  61. : P>       ( -- n D)  ( n -- P)
  62.            P@ P-DEC ;
  63.  
  64. \ Display all numbers on the P-stack.
  65. : .P       ( -- )
  66.            P-DEPTH ?DUP
  67.            IF 1+ 1 ?DO I 2* P-STACK + @ 8 .R LOOP
  68.            ELSE ." P-STACK EMPTY" THEN ;
  69.  
  70. \ Here are some examples of the execution.
  71. P-CLEAR <enter> ok
  72. .P <enter> P-STACK EMPTY ok
  73. 123 >P <enter> ok
  74. .P  <enter>    123 ok
  75. 456 >P <enter> ok
  76. .P     <enter>   123     456 ok
  77. .S  <enter> Stack Empty.  ok
  78. P@  <enter> ok
  79. .S  <enter> [1]    456  ok
  80. .P  <enter>    123     456 ok
  81. P>  <enter> ok
  82. .S  <enter> [2]    456     456  ok
  83. .P  <enter>    123 ok
  84.  
  85. ╓──────────────╖
  86. ║ Problem 4.18 ║
  87. ╙──────────────╜
  88. Do you understand how the word .P works?  Provide me with a version
  89. that has detailed comments ( a comment for every word or two ).
  90.  
  91. ╓───────────────╖
  92. ║ Problem 4.19  ║
  93. ╙───────────────╜
  94. Personal User Stacks.
  95. Write Forth words for the following user stack operations. They should
  96. leave the data stack unchanged.
  97.  
  98.  PDUP  PDROP PSWAP POVER PROT -PROT PTUCK PNIP 2PDUP 3PDUP 2PSWAP
  99.  2PDROP 2POVER
  100.  
  101. Hint:  : PSWAP ( n m -- m n P)  P> P> SWAP >P >P ;
  102.  
  103. Have we left out any important operators?  Could you make
  104. P+ P- P* and P/ ????
  105.  
  106. ┌────────────────────────────────────┐
  107. │  Please move to Lesson 4 Part 120  │
  108. └────────────────────────────────────┘
  109.