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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║  Lesson 7 Part 060 F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.                      ┌────────────────────────┐
  6.                      │   CREATE ....  DOES>   │
  7.                      └────────────────────────┘
  8.  
  9. Our adventure into the mysterious, powerful, and captivating
  10.  
  11.     CREATE  ....   DOES>       construct begins.
  12.  
  13. First let's review how the word CREATE works.  CREATE can be used on its
  14. own as follows:
  15.  
  16.              CREATE  TABLE
  17.  
  18. This would create an entry in the FORTH dictionary called TABLE.  But
  19. that is all it would do!  It just creates an empty shell of a word.
  20. Every word that is used by the Forth system to make entries in the
  21. dictionary, such as VARIABLE  CONSTANT   :   CODE   etc etc   have the
  22. word CREATE buried in them right near the beginning of their
  23. definitions.  When a word created by CREATE is executed it returns the
  24. words " parameter field address " or pfa to the stack.  The pfa normally
  25. points to a words data storage area. In the case of the word TABLE
  26. created above it points to the next available dictionary location.  The
  27. word TABLE  created above is actually quite useless on its own.   If
  28. another  definition were to be added to the dictionary it would sit in
  29. TABLE's parameter field area.  Here is how the standard FORTH word
  30. VARIABLE could be defined using CREATE .
  31.  
  32.   :  VARIABLE  ( -- )  CREATE  2 ALLOT  ;
  33.  
  34. This definition of VARIABLE would initialize the value of the variable
  35. to garbage which is required by the F83 standard.  Most Forth systems
  36. would define VARIABLE as follows:
  37.  
  38.   :  VARIABLE  ( -- )  CREATE 0 ,      ;
  39.  
  40. This definition of VARIABLE would initialize the value of a variable to
  41. 0. The comma compiles the 0 into the next available dictionary location.
  42.  
  43. CREATE is often used to create tables or arrays.  You would do this by
  44. ALLOTting some dictionary space or compiling the initial values. For
  45. example:
  46.  
  47. CREATE TABLE 10 ALLOT    creates a TABLE to hold 5 16-numbers. The table
  48.                          is uninitialized (initial values are garbage).
  49. CREATE TABLE 11 , 22 , 33 , 44 , 55 ,
  50.    creates a table to hold 5 16-bit numbers and the table is initialized
  51.    to  the values 11 22 33 44 and 55.
  52.  
  53. Now let's see how the  CREATE  DOES> construct can be used to define a
  54. regular FORTH  type  CONSTANT.  You remember how constant works don't
  55. you.
  56.  
  57.   45  CONSTANT  BRICK     creates a constant called BRICK which is
  58.  initialized to 45 (cents?).  When BRICK is executed it puts 45 on the
  59.  stack.  Just so that we don't get mixed up, the new version of CONSTANT
  60.  will be called   CON  ( it is also shorter to type!)
  61.  
  62. Here is the definition of our version of CONSTANT called CON
  63.  
  64.   :  CON    ( n    -- )
  65.      CREATE    ,         \  This is the creation time stuff.
  66.      DOES>     @   ;     \  This is the run time stuff.
  67.  
  68. The part of the definition between CON and DOES> describes how to make
  69. CON type constants, ie  create a dictionary header and compile the
  70. initial value. The part of the definition between DOES> and  ; tells what
  71. is to happen when a CON type constant is executed.  Namely,  fetch the
  72. value to the parameter stack.  Remember that any word created by CON
  73. inherits the properties of CREATE.  When a CON type constant executes
  74. pfa or data storage address is pushed to the stack so only the fetch " @
  75. " is required between the DOES> and the  ;  .
  76.  
  77. To make a constant with our new defining word  CON   we have to use it!
  78. Here is how:
  79.  
  80.      45  CON  BRICK
  81.  
  82. This would make a constant call BRICK and initialize it to 45.
  83.  
  84. The next thing to do is to use the constant called BRICK.  For example,
  85. if you had 5 BRICK's they would be worth:
  86.  
  87.      BRICK  5   *   .
  88.  
  89. Please try this for yourself.
  90.  
  91. ┌──────────────────────────────────┐
  92. │ Please Move to Lesson 7 Part 070 │
  93. └──────────────────────────────────┘
  94.  
  95.  
  96.