home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / forth_32 / struct.4th < prev    next >
Text File  |  1993-03-19  |  5KB  |  187 lines

  1. \ STRUCT.4TH  Arrays Structures Memory management Words  2/08/93 bbm
  2. \ for Forth/2
  3. \ Copyright <c> 1993  BLUE STAR SYSTEMS
  4.  
  5. Echo @  Echo ON
  6. \
  7. \ These words let you create arrays of numbers, structures which group
  8. \   various types of data into one logical entity, and arrays of
  9. \   structures.
  10. Echo !
  11.  
  12. : ALIGN  ( n -- )       \ Align dictionary pointer to n boundary
  13.          HERE  OVER MOD  - ALLOT ;
  14.  
  15.  
  16. \ ARRAYS
  17.  
  18. : CARRAY ( n <name>-- Does: i -- n )   CREATE  HERE  OVER ALLOT
  19.          SWAP 0 FILL  DOES>  + ;
  20.  
  21. : ARRAY  ( n <name>-- Does: i -- n )   CREATE  HERE  OVER W* ALLOT
  22.          SWAP 0 FILL  DOES>  SWAP W* + ;
  23.  
  24. \ Example
  25. \
  26. \ 100 CARRAY XList
  27. \      5 XList C@ .    \ Print value of fifth  element
  28. \ 130 35 XList C!      \ Store a 130 into 35th element
  29. \
  30. \ Same for ARRAY, except use the 32-bit @'s and !'s
  31.  
  32.  
  33.  
  34. ( Memory Structure Definition Creation )
  35.  
  36. : STRUCT      CREATE  0 ( not used ) , HERE      \ Creates structure def.
  37.                       0 ( size     ) , 0  
  38.               DOES>  CREATE  W+ @ ALLOT ;
  39. : FIELD       CREATE OVER ( offset ) ,           \ Create byte-length field
  40.                       DUP ( size   ) , +  
  41.               DOES>  @ + ;
  42. : ENDSTRUCT   SWAP ! ;                           \ Stores structure size
  43.  
  44. : SIZEOF      [COMPILE] ' PFA 9 + @ ;  IMMEDIATE \ Size of STRUCT or FIELD
  45.  
  46. : NEST        [COMPILE] SIZEOF  FIELD ;          \ Use to nest structures
  47.  
  48.  
  49. \ Example 1 - Date Structure for holding system date and time
  50. \
  51. \ STRUCT DateTimeStruct
  52. \    1 Field Hour
  53. \    1 Field Minute
  54. \    1 Field Seconds
  55. \    1 Field Hundreths
  56. \    1 Field Day
  57. \    1 Field Month
  58. \    2 Field Year
  59. \    2 Field TimeZone
  60. \ ENDSTRUCT
  61. \
  62. \
  63. \ Usage Example 
  64. \
  65. \  DateTimeStruct BeginDate      \ Allocate memory for structure
  66. \
  67. \     4 BeginDate Month C!
  68. \     1 BeginDate Day   C!
  69. \  1993 BeginDate Year  W!
  70. \
  71. \  BeginDate SYS$GetDateTime SYSCALL 2DROP
  72. \
  73. \  : DATE.  DUP Day C@ 1 .R  ." /"  DUP Month C@ 2 .R ." /"  Year W@ U. ;
  74. \
  75.  
  76.  
  77.  
  78. ( TO Variable Memory Structure Field Creation )
  79.  
  80. : C+!       ( b addr -- )   TUCK C@ + SWAP C! ;
  81. : <CTODOES> ( addr -- ? )   %TO @ 0 = IF  C@  ELSE
  82.                             %TO @ 0 > IF  C!  ELSE  C+!  THEN THEN
  83.                             0 %TO ! ;
  84. : CHAR        CREATE DUP  , 1  DUP , +  DOES>       @ + <CTODOES> ;
  85.  
  86. : W+!       ( w addr -- )   TUCK W@ + SWAP W! ;
  87. : <WTODOES> ( addr -- ? )   %TO @ 0 = IF  W@  ELSE
  88.                             %TO @ 0 > IF  W!  ELSE  W+!  THEN THEN
  89.                             0 %TO ! ;
  90. : SHORT       CREATE DUP  , 2  DUP , +  DOES>       @ + <WTODOES> ;
  91.  
  92. : INT         CREATE DUP  , W  DUP , +  DOES>       @ +  <TODOES> ;
  93. : INT[]       CREATE OVER , W* DUP , +  DOES> @ SWAP W* + + <TODOES> ;
  94.  
  95. : ARRAYOF   ( size struct_name-- )  [COMPILE] SizeOf
  96.               CREATE  DUP ,  *  HERE OVER  ALLOT
  97.               SWAP 0 FILL  DOES>  TUCK @ * + W+ ;
  98.  
  99.  
  100. \ Example 2 - Data Structures using TO variables
  101. \
  102. \ You can intermix any of:
  103. \
  104. \    FIELD  CHAR  SHORT  INT  and INT[]
  105. \
  106. \ in the same structure definition.  To add to a STRUCT definition use:
  107. \
  108. \ size FIELD <fieldname>  creates a size-byte length field named <fieldname>
  109. \      CHAR  <varname>    creates a byte   TO variable
  110. \      SHORT <varname>    creates a 2-byte TO variable
  111. \      INT   <varname>    creates a 4-byte TO variable
  112. \ size INT[] <varname>    creates an array of 4-byte TO variables
  113. \
  114. \
  115. \ To use the fields, precede each field name by the starting address of the
  116. \ structure.
  117. \
  118. \
  119. \ STRUCT DateTimeStruct
  120. \       CHAR Hour
  121. \       CHAR Minute
  122. \       CHAR Seconds
  123. \       CHAR Hundreths
  124. \       CHAR Day
  125. \       CHAR Month
  126. \      SHORT Year
  127. \      SHORT TimeZone
  128. \ ENDSTRUCT
  129. \
  130. \ DateTimeStruct BeginDate      \ Allocate memory for structure
  131. \
  132. \    4 TO BeginDate Month       \ Store numbers into fields
  133. \    1 TO BeginDate Day
  134. \ 1993 TO BeginDate Year
  135. \
  136. \ BeginDate SYS$GetDateTime SYSCALL 2DROP   \ Pass structure to OS/2 call
  137. \
  138. \ : DATE. ( DateTimeStruct -- )   \ Fetch field contents
  139. \         DUP Month 1 .R  ." /"
  140. \         DUP Day   2 .R  ." /"
  141. \             Year     U. ;
  142.  
  143.  
  144. \ Example 3 - Mixed and Nested Structure
  145. \
  146. \ STRUCT Transaction
  147. \      INT TransID
  148. \     NEST DateTimeStruct TransDate     \ Nests DateTime structure
  149. \     CHAR TransType
  150. \ 14 INT[] Hours[]
  151. \ 80 FIELD TransDesc
  152. \ ENDSTRUCT
  153. \
  154. \
  155. \ Transaction Trans1               \ Create transaction structure
  156. \
  157. \ 1000  TO  Trans1 TransID         \ Store 100 into TransID
  158. \
  159. \ Trans1 TransType EMIT            \ Emits the Transaction Type
  160. \
  161. \ 1993  TO  Trans1 TransDate Year  \ Set Transaction date to 1993
  162. \
  163. \   81  TO  Trans1 5 Hours[]       \ Store 81 into  Hours[5]  of Trans1
  164. \
  165. \ Trans1 5 Hours[] .               \ Display  Hours[5]  of Trans1
  166. \
  167. \ " Overtime OK'd by M.E.D."  Trans1 TransDesc "MOVE  \ Store to TransDesc
  168. \
  169. \ Trans1 TransDesc ".              \ Types contents of TransDesc
  170.  
  171.  
  172. \ Example 4 - Creating an array of structures
  173. \
  174. \
  175. \ 50 ArrayOf Transaction Trans[]      \ Create array of 50 transactions
  176. \
  177. \ 50 0 DO
  178. \    I 100 +  TO  I Trans[] TransID        \ Store unique ID's into TransID's
  179. \    1993     TO  I Trans[] TransDate Year \ Store 1993 into year
  180. \    40       TO  I Trans[] 0 Hours[]      \ Store 40 into Hours[0]
  181. \ LOOP
  182. \
  183. \ 23 Trans[] TransDate Year .      \ Show transaction 23's year
  184. \
  185.  
  186.  
  187.