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

  1. \ Lesson 6 Part 10 ( F-PC 3.5 Tutorial by Jack Brown )
  2. COMMENT:
  3. A short tutorial on the difference between:
  4.  
  5.             COMPILE   and  [COMPILE]
  6.  
  7. With a quick look at IMMEDIATE   LITERAL  and  '  pronounced "tick"
  8.  
  9. Along with ( hopefully) a practical application of these words that
  10. will make them clearer. To understand the example you will need to know
  11. the following words from Forth 83 standard.
  12.  
  13. IMMEDIATE   is used right after the definition of a word to declare it
  14.             to be IMMEDIATE. An IMMEDIATE word will execute at compile
  15.             time instead of being compiled.
  16.  
  17. LITERAL     compiles the stack number in to the dictionary.
  18.             LITERAL  is an IMMEDIATE word itself.
  19.  
  20. Example   : TEST1  ( -- n ) [ 32 4 * 6 + ]  LITERAL ;
  21.  
  22. Recall, " [ " turns the compiler off and " ] " turns it back on again.
  23.  
  24. Would be the same as   : TEST2  ( -- n ) 134 ;
  25.  
  26.  
  27. ' ( tick )  Get the cfa of the next word in the input stream.
  28.             '  is not IMMEDIATE in F-PC ( it was in Fig Forth).
  29.  
  30. STATE       a system variable that is true if the system is
  31.             in compile mode and false if the system is in
  32.             interpretive mode.
  33.  
  34. [COMPILE]   force the compilation of an IMMEDIATE word when it
  35.             would other wise execute.
  36.  
  37. COMPILE     When the word containing  COMPILE <xxx>  executes
  38.             the word <xxx> will be compile into the dictionary.
  39.  
  40. Now here is the practical example that should help you understand what
  41. is going on or else thoroughly confuse you. A primitive data base
  42. application:
  43. COMMENT;
  44.             0 CONSTANT QUANTITY     2 CONSTANT COST
  45. CREATE KEYBOARDS          10 ,                 120 ,
  46. CREATE MONITORS           50 ,                 295 ,
  47. CREATE MODEMS             15 ,                 320 ,
  48.  
  49. : .FIELD ( cfa -- )  >BODY @ + @ . ;
  50. COMMENT:
  51. Ops... You also have to know that >BODY takes the cfa obtained by  '
  52. and turns it into the words pfa .  The pfa of a constant contains the
  53. constants value.  You look up @ +  and . yourself.
  54. COMMENT;
  55. : GET ' .FIELD ;
  56.  
  57. \ Now try the following:
  58.  
  59. \ KEYBOARDS GET QUANTITY
  60. \ MONITORS  GET COST
  61. \ MODEMS    GET COST
  62. \ MODEMS    GET QUANTITY  \  ( Neat eh?)
  63.  
  64. \ But how come the following won't work???
  65.  
  66. : REPORT  ( -- )
  67.           CR  KEYBOARDS GET QUANTITY  KEYBOARDS GET COST
  68.           CR  MODEMS    GET QUANTITY  MODEMS    GET COST  ;
  69.  
  70. \ REPORT
  71.  
  72. \ It gives funny answers and leaves numbers on the stack. The solution to
  73. \ the problem is the proper use of [COMPILE] and  COMPILE.
  74.  
  75. ( Please Move to Lesson 6 Part 11 )
  76.