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

  1.  
  2.        ╔═════════════════════════════════════════════════════╗
  3.        ║  Lesson 7 Part 100 F-PC 3.5 Tutorial by Jack Brown  ║
  4.        ╚═════════════════════════════════════════════════════╝
  5.  
  6.            ┌─────────────────────────────────────┐
  7.            │  Defining Multi-Dimensional Arrays  │
  8.            └─────────────────────────────────────┘
  9.  
  10.  
  11. The following definition using CREATE ... DOES> can be used to define
  12. multi dimensional arrays.  It is presented both as a tool to use and
  13. something for you to study.  First here is the definition of the
  14. multi-dimensional array defining word.
  15.  
  16. : ACHECK ( {n items} n -- {n items}  n )  \ Check parameters.
  17.          DUP 1 < OVER 255 > OR
  18.          ABORT" Illegal dimension in array definition."
  19.          DUP 1+ ?ENOUGH ;
  20.  
  21. : *ARRAY  ACHECK
  22.   CREATE
  23.     DUP C,              ( save # of dimensions )
  24.     1 SWAP              ( initialize total size )
  25.     0 DO                ( loop on # of dimensions )
  26.       OVER ,            ( save dimension )
  27.       *                 ( increase total size )
  28.       LOOP
  29.     1 ,                 ( save dummy dimension )
  30.     2 * ALLOT           ( allocate space for words )
  31.   DOES>
  32.     COUNT               ( get # of dimensions )
  33.     0 SWAP              ( initialize offset )
  34.     0 DO                ( loop on # of dimensions )
  35.       >R                ( save offset )
  36.       OVER DUP 0< ABORT" Negative array index."
  37.       OVER @ < 0= ABORT" Array index too large."
  38.       2+ DUP @          ( advance to next dimension )
  39.       ROT R> + *        ( calculate offset so far )
  40.       LOOP
  41.     2 *                 ( double offset for words )
  42.     + 2+ ;              ( calculate element address )
  43.  
  44.  
  45. To define an array, the vector size for each dimension
  46. must be on the stack followed by the total # of dimensions
  47.  
  48.  Example: to define a 3D array name MATRIX with a
  49.  vector length of 5 in each dimension (i.e. x, y, z = 0...4)
  50.  you would execute:
  51.  
  52.         5 5 5 3 *ARRAY MATRIX
  53.  
  54. To get an indexed address into the array, put the coordinates
  55. on the stack followed by the name of the array.
  56.  
  57.  Example: to retrieve the value stored in (x,y,z) = (1,2,3)
  58.  for the array above, you would execute:
  59.         1 2 3 MATRIX @
  60.  to store (x,y,z) = 5 at 3D space x=2,y=3,z=4 do:
  61.  
  62.         5 2 3 4 MATRIX !
  63.  
  64. Now you know why Forth does not have arrays and vectors built in as
  65. part of the language.  If you need them, you add them yourself!
  66.  
  67. ╓───────────────╖
  68. ║  Problem 7.7  ║
  69. ╙───────────────╜
  70. a) Implement *ARRAY and the example MATRIX above.
  71. b) Dump out the contents of the pfa of MATRIX and verify that it
  72.    conforms to what is laid down when it was created with *ARRAY
  73.    ie  Try  ' MATRIX >BODY 16 DUMP
  74. c) Write a word that will fill each of the  125 cells of the *ARRAY
  75.    MATRIX with the sum of its indices.  ie  (1,1,1) = 3  (1,2,3) = 6
  76. d) Write a word that will Display the contents of a *ARRAY .  It
  77.    Call it ADUMP and make its usage:   ADUMP MATRIX
  78.    You might begin the definition with:  : ADUMP  ' >BODY ..... ;
  79.    This will allow you to access the parameter field.
  80.    Use  ADUMP to verify the operation of (c)
  81.  
  82. ┌────────────────────────────────────┐
  83. │  Please Move to Lesson 7 Part 110  │
  84. └────────────────────────────────────┘
  85.  
  86.  
  87.