home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l6p110.seq < prev    next >
Text File  |  1990-04-26  |  3KB  |  71 lines

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