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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 4 Part 090  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.  
  6. We have been working with the words:
  7.  
  8. CONSTANT VARIABLE CREATE ALLOT DUMP ERASE ! @  C! C@  , and C,
  9.  
  10. In the last example of L4P080 there were some very interesting
  11. developments.  We did not however provide much in the way of an
  12. explanation for that example preferring to give you a chance to analyze
  13. this exciting example on your own.
  14.  
  15. Let's take a detailed walk through the code as this example provides an
  16. example of how all of the above words can work together to provide a
  17. very readable interface to the array data structure.
  18.  
  19.  
  20. \ Tables  -  arrays by another name.
  21. CREATE TABLE   0 , 0 , 0 , 0 , 0 , 0 ,
  22.  
  23. Here we are creating an array called TABLE  This array will hold
  24. 6 16-bit numbers and they are initialized to zero by the six occurances
  25. of the sequence 0 ,
  26.  
  27. It might be appropriate here to say a bit more about  comma " , "
  28. You may have heard of people talk about compilers for languages such as
  29. BASIC, C, and PASCAL.  Forth takes a different approach.  There is no
  30. one compiler for Forth.  Instead Forth is made up of a whole lot of
  31. little compilers.
  32.  
  33. The simplest of these compilers is comma " , " " , " is Forths 16-bit
  34. number compiler.  Everytime a number is followed by a comma " , " the
  35. Forth interpreter will compile that number into the dictionary and
  36. advance the dictionary pointer by 2 bytes.  That is why the TABLE array
  37. above has 6 slots and how each of those slots is initialized to zero.
  38.  
  39. Forth has other compilers.  There is of course C, which compiles bytes.
  40. CONSTANT compiles constants into the dictionary.
  41. VARIABLE compiles variables into the dictionary.
  42. the : ... ; pair compiles colon definitions into the dictionary.
  43. We will see many more examples of little compilers as we progress and
  44. even make some or our own.
  45.  
  46. Another way to create an array  of 6 numbers and intialize each entry to
  47. zero follows:
  48.  
  49. CREATE TABLE 12 ALLOT   \ Here space is alloted but not initialized.
  50. TABLE 12 ERASE          \ Here the 12 bytes or 6 16-bit numbers are
  51.                         \ set to zero.
  52.  
  53. ╓──────────────╖
  54. ║ Problem 4.11 ║
  55. ╙──────────────╜
  56. Make word called CLEAR-TABLE that uses a DO ... LOOP and the store
  57. operator " ! " to set all six values of the array TABLE to zero.
  58.  
  59. Well, took awhile but we did get side tracked talking about compilers.
  60. The varaible MODE is going to be used as flag or signal to tell our
  61. smart or intelligent word MARBLES what action it should be performing
  62. when it is executed.  Depending upon the value that MARBLES finds stored
  63. in the varialbe MODE it will perform different operations.  We'll
  64. discuss them later.
  65.  
  66.  VARIABLE MODE
  67.  
  68. The constants below define fixed offsets into the array TABLE.
  69. We have used colours for the names of the offsets because our array is
  70. going to hold the number marbles we have of each colour in our
  71. collection.
  72.  
  73.  0 CONSTANT RED         2 CONSTANT BLUE     4 CONSTANT YELLOW
  74.  6 CONSTANT BLACK       8 CONSTANT WHITE   10 CONSTANT GREEN
  75.  
  76.  
  77. This group of words is used to set the current function of the word
  78. MARBLES.  The names have been choosen to indicate the function to be
  79. performed.  The variable MODE will carry the message.
  80.  
  81. : LESS -1  MODE !  ;     \ Subtract top of stack from current position.
  82. : SHOW  0  MODE !  ;     \ Display current array position.
  83. : MORE  1  MODE !  ;     \ Add top of stack to current position.
  84. : ONLY  2  MODE !  ;     \ Store top of stack to current array position.
  85.  
  86. The words below provide a readable way of examining the message that
  87. was stored in the variable MODE.
  88.  
  89. : LESS?  MODE @ -1 = ;
  90. : SHOW?  MODE @  0=  ;
  91. : MORE?  MODE @  1 = ;
  92.  
  93. ╓───────────────╖
  94. ║ Problem 4.12  ║
  95. ╙───────────────╜
  96. Why is there no word ONLY? defined as : ONLY? MODE @ 2 = ;
  97. What could you do with such a word?
  98.  
  99. ┌───────────────────────────────────┐
  100. │  Please move to Lesson 4 Part 100 │
  101. └───────────────────────────────────┘
  102.