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