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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 6 Part 110  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.  
  6. Wow....  the word GET only works when you execute it directly...  It
  7. doesn't work when you use it in a definition. This is because GET gets
  8. compiled and the ' does not try to do its job of picking up the cfa of
  9. the next word in the input stream until the word REPORT is executed.  By
  10. then it is too late. Here is a version that will work in a definition.
  11. But to make it we will have to use the words [COMPILE] and COMPILE
  12.  
  13. : GET  ' [COMPILE] LITERAL  COMPILE .FIELD ;  IMMEDIATE
  14.  
  15. This version of get is IMMEDIATE  so that when you compile the word
  16. REPORT from the last message it executes immediately gets the cfa of the
  17. word following and compiles it ( that's what the LITERAL does remember)
  18. then we compile the word .FIELD.  ( .FIELD gets compiled every time we
  19. use GET )
  20.  
  21. Make sure you redefine the word REPORT from the previous message and
  22. then try it!!  Also decompile it by using SEE  REPORT   ( in F-PC  to
  23. see what really is happening )
  24.  
  25. Now try using you new GET in direct execution mode like we did with the
  26. first one.  ie try the following :
  27.  
  28. MONITORS GET COST   MONITORS GET QUANTITY
  29.  
  30. Horrors... this version does not work when try to use it in interpretive
  31. mode.  Wouldn't it be nice to have a GET that works both in compile mode
  32. and interpretive mode?? here is the solution:
  33.  
  34. : GET ( -- )
  35.      '  STATE @
  36.      IF    [COMPILE] LITERAL
  37.            COMPILE .FIELD
  38.      ELSE  .FIELD
  39.      THEN ;
  40. IMMEDIATE
  41.  
  42. This version of GET is said to be "state smart"  because it checks the
  43. system variable state and then does the correct thing when compiling.
  44. "state smart"  words must be IMMEDIATE.
  45.  
  46. By the way.... state smart words were banned from the FORTH 83 standard.
  47.  
  48. ."   is not state smart in FORTH 83 ( it was in fig FORTH and FORTH79)
  49. and so it can only be used inside a definition.  If you have a version
  50. of fig FORTH look up the definition of ."  and you will see that it uses
  51. the same ideas that were illustrated in the word GET above.
  52.  
  53. One more time....  If you are only reading this you will not not grasp
  54. what is really going on.  Capture this message and take the time to
  55. figure exactly what is going on here.  I guarantee that mastery of these
  56. two words will really pay off in your understanding of how the FORTH
  57. compiler works.
  58.  
  59. Happy   COMPILEing  or should I say [COMPILE]ing
  60.  
  61. ╓──────────────╖
  62. ║ Problem 6.14 ║
  63. ╙──────────────╜
  64. Write a STATE smart version of ." that can be used both inside and
  65. outside a colon definition.  Hint:  Look at the F-PC definitions of .(
  66. and ." and combine the two actions in a way similar to what we did with
  67. the two functions required for GET.
  68.  
  69. ╓───────────────╖
  70. ║ Problem 6.15  ║
  71. ╙───────────────╜
  72. The ANS Forth Standard is going to replace the the words COMPILE and
  73. [COMPILE] with the single new word POSTPONE .  Check out the ANS Forth
  74. Basis document and give state smart definition of GET and  ." that use
  75. POSTPONE .
  76.  
  77. ┌────────────────────────────────────┐
  78. │   Please Move to Lesson 6 Part 120 │
  79. └────────────────────────────────────┘
  80.