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

  1.  
  2.        ╔═════════════════════════════════════════════════════╗
  3.        ║  Lesson 7 Part 120 F-PC 3.5 Tutorial by Jack Brown  ║
  4.        ╚═════════════════════════════════════════════════════╝
  5.  
  6.         ┌───────────────────────────────────────────────────┐
  7.         │ Magic Variables or Object Orientated Programming  │
  8.         └───────────────────────────────────────────────────┘
  9.  
  10. From smart variables we move on to MAGIC-VARIABLEs.  All you AI type
  11. people who are waiting for an affordable SMALLTALK can now switch to
  12. Forth and implement your own data structures and objects that pass and
  13. receive messages.  This is actually a primitive implementation but the
  14. idea is simple.  We simply extend the meaning of the STORE? variable of
  15. the preceding example.  We might as well change its name at the same
  16. time, let's call the variable MESSAGE .   What is going to happen is
  17. that our MAGIC-VARIABLES run time routine will examine the contents of
  18. MESSAGE and then do the appropriate thing.  Here is the code for
  19.  
  20. \ MAGIC-VARIABLES:
  21. \  Magic Variables.
  22.    VARIABLE  MESSAGE
  23.  
  24. : FETCH    0 MESSAGE ! ;
  25. : =>       1 MESSAGE ! ;
  26. : DISPLAY  2 MESSAGE ! ;
  27. : SOUND    3 MESSAGE ! ;
  28. : PLOT     4 MESSAGE ! ;
  29. : CLEAR    5 MESSAGE ! ;
  30. : INC      6 MESSAGE ! ;
  31. : DEC      7 MESSAGE ! ;
  32.  
  33. : WAIT     5000 0 DO I DROP LOOP ;
  34.  
  35. :  COMPILE-MAGIC-VARIABLE  ( --  -- )
  36.         0 ,  ;
  37.  
  38. :  RUN-MAGIC-VARIABLE   ( val|--  val|-- )
  39.         MESSAGE @  MESSAGE OFF
  40.         CASE
  41.         0 OF  @         ENDOF
  42.         1 OF  !         ENDOF
  43.         2 OF  @ .       ENDOF
  44.         3 OF     @ 0 ?DO BEEP    WAIT  LOOP  ENDOF
  45.         5 OF  OFF       ENDOF
  46.         6 OF  1 SWAP +! ENDOF
  47.         7 OF  -1 SWAP +! ENDOF
  48.         4 OF  CR @ 0 ?DO ASCII * EMIT  LOOP  ENDOF
  49.           DROP
  50.         ENDCASE ;
  51.  
  52. : MAGIC-VARIABLE
  53.         CREATE  COMPILE-MAGIC-VARIABLE
  54.         DOES>   RUN-MAGIC-VARIABLE ;
  55.  
  56. Note if you don't have a CASE statement in you FORTH you will have to
  57. use nested IF statements or write your own CASE statement.
  58. MAGIC-VARIABLE examples:
  59.  
  60.     MAGIC-VARIABLE PIG  <enter> ok
  61.     DISPLAY PIG <enter> 0  ok
  62.     45 => PIG  <enter> ok
  63.     DISPLAY PIG <enter> 45  ok
  64.     5 => PIG PIG . <enter> 5  ok
  65.     PLOT PIG <enter>
  66.     ***** ok
  67.     INC PIG DISPLAY PIG <enter> 6  ok
  68.     CLEAR PIG DISPLAY PIG <enter> 0  ok
  69.  
  70. ╓────────────────╖
  71. ║  Problem  7.9  ║
  72. ╙────────────────╜
  73. Implement and verify the operation of Magic variables.
  74.  
  75. ┌───────────────────────────────────┐
  76. │  Please Move to Lesson 7 Part 130 │
  77. └───────────────────────────────────┘
  78.  
  79.  
  80.