PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/hrc/forth/full/fth.misc2.fth

https://github.com/mediogre/colorite
Forth | 232 lines | 203 code | 29 blank | 0 comment | 9 complexity | ea784ab7fedb57f01d872dcd0da90bed MD5 | raw file
  1. \ @(#) misc2.fth 98/01/26 1.2
  2. \ Utilities for PForth extracted from HMSL
  3. \
  4. \ Author: Phil Burk
  5. \ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
  6. \
  7. \ The pForth software code is dedicated to the public domain,
  8. \ and any third party may reproduce, distribute and modify
  9. \ the pForth software code or any derivative works thereof
  10. \ without any compensation or license. The pForth software
  11. \ code is provided on an "as is" basis without any warranty
  12. \ of any kind, including, without limitation, the implied
  13. \ warranties of merchantability and fitness for a particular
  14. \ purpose and their equivalents under the laws of any jurisdiction.
  15. \
  16. \ 00001 9/14/92 Added call, 'c w->s
  17. \ 00002 11/23/92 Moved redef of : to loadcom.fth
  18. anew task-misc2.fth
  19. : 'N ( <name> -- , make 'n state smart )
  20. bl word find
  21. IF
  22. state @
  23. IF namebase - ( make nfa relocatable )
  24. [compile] literal ( store nfa of word to be compiled )
  25. compile namebase+
  26. THEN
  27. THEN
  28. ; IMMEDIATE
  29. : ?LITERAL ( n -- , do literal if compiling )
  30. state @
  31. IF [compile] literal
  32. THEN
  33. ;
  34. : 'c ( <name> -- xt , state sensitive ' )
  35. ' ?literal
  36. ; immediate
  37. variable if-debug
  38. decimal
  39. create msec-delay 1000 , ( default for SUN )
  40. : msec ( #msecs -- )
  41. 0
  42. do msec-delay @ 0
  43. do loop
  44. loop
  45. ;
  46. : SHIFT ( val n -- val<<n )
  47. dup 0<
  48. IF negate arshift
  49. ELSE lshift
  50. THEN
  51. ;
  52. variable rand-seed here rand-seed !
  53. : random ( -- random_number )
  54. rand-seed @
  55. 31421 * 6927 +
  56. 65535 and dup rand-seed !
  57. ;
  58. : choose ( range -- random_number , in range )
  59. random * -16 shift
  60. ;
  61. : wchoose ( hi lo -- random_number )
  62. tuck - choose +
  63. ;
  64. \ sort top two items on stack.
  65. : 2sort ( a b -- a<b | b<a , largest on top of stack)
  66. 2dup >
  67. if swap
  68. then
  69. ;
  70. \ sort top two items on stack.
  71. : -2sort ( a b -- a>b | b>a , smallest on top of stack)
  72. 2dup <
  73. if swap
  74. then
  75. ;
  76. : barray ( #bytes -- ) ( index -- addr )
  77. create allot
  78. does> +
  79. ;
  80. : warray ( #words -- ) ( index -- addr )
  81. create 2* allot
  82. does> swap 2* +
  83. ;
  84. : array ( #cells -- ) ( index -- addr )
  85. create cell* allot
  86. does> swap cell* +
  87. ;
  88. : .bin ( n -- , print in binary )
  89. base @ binary swap . base !
  90. ;
  91. : .dec ( n -- )
  92. base @ decimal swap . base !
  93. ;
  94. : .hex ( n -- )
  95. base @ hex swap . base !
  96. ;
  97. : B->S ( c -- c' , sign extend byte )
  98. dup $ 80 and
  99. IF
  100. $ FFFFFF00 or
  101. ELSE
  102. $ 000000FF and
  103. THEN
  104. ;
  105. : W->S ( 16bit-signed -- 32bit-signed )
  106. dup $ 8000 and
  107. if
  108. $ FFFF0000 or
  109. ELSE
  110. $ 0000FFFF and
  111. then
  112. ;
  113. : WITHIN { n1 n2 n3 -- flag }
  114. n2 n3 <=
  115. IF
  116. n2 n1 <=
  117. n1 n3 < AND
  118. ELSE
  119. n2 n1 <=
  120. n1 n3 < OR
  121. THEN
  122. ;
  123. : MOVE ( src dst num -- )
  124. >r 2dup - 0<
  125. IF
  126. r> CMOVE>
  127. ELSE
  128. r> CMOVE
  129. THEN
  130. ;
  131. : ERASE ( caddr num -- )
  132. dup 0>
  133. IF
  134. 0 fill
  135. ELSE
  136. 2drop
  137. THEN
  138. ;
  139. : BLANK ( addr u -- , set memory to blank )
  140. DUP 0>
  141. IF
  142. BL FILL
  143. ELSE
  144. 2DROP
  145. THEN
  146. ;
  147. \ Obsolete but included for CORE EXT word set.
  148. : QUERY REFILL DROP ;
  149. VARIABLE SPAN
  150. : EXPECT accept span ! ;
  151. : TIB source drop ;
  152. : UNUSED ( -- unused , dictionary space )
  153. CODELIMIT HERE -
  154. ;
  155. : MAP ( -- , dump interesting dictionary info )
  156. ." Code Segment" cr
  157. ." CODEBASE = " codebase .hex cr
  158. ." HERE = " here .hex cr
  159. ." CODELIMIT = " codelimit .hex cr
  160. ." Compiled Code Size = " here codebase - . cr
  161. ." CODE-SIZE = " code-size @ . cr
  162. ." Code Room UNUSED = " UNUSED . cr
  163. ." Name Segment" cr
  164. ." NAMEBASE = " namebase .hex cr
  165. ." HEADERS-PTR @ = " headers-ptr @ .hex cr
  166. ." NAMELIMIT = " namelimit .hex cr
  167. ." CONTEXT @ = " context @ .hex cr
  168. ." LATEST = " latest .hex ." = " latest id. cr
  169. ." Compiled Name size = " headers-ptr @ namebase - . cr
  170. ." HEADERS-SIZE = " headers-size @ . cr
  171. ." Name Room Left = " namelimit headers-ptr @ - . cr
  172. ;
  173. \ Search for substring S2 in S1
  174. : SEARCH { addr1 cnt1 addr2 cnt2 | addr3 cnt3 flag -- addr3 cnt3 flag }
  175. \ ." Search for " addr2 cnt2 type ." in " addr1 cnt1 type cr
  176. \ if true, s1 contains s2 at addr3 with cnt3 chars remaining
  177. \ if false, s3 = s1
  178. addr1 -> addr3
  179. cnt1 -> cnt3
  180. cnt1 cnt2 < not
  181. IF
  182. cnt1 cnt2 - 1+ 0
  183. DO
  184. true -> flag
  185. cnt2 0
  186. ?DO
  187. addr2 i chars + c@
  188. addr1 i j + chars + c@ <> \ mismatch?
  189. IF
  190. false -> flag
  191. LEAVE
  192. THEN
  193. LOOP
  194. flag
  195. IF
  196. addr1 i chars + -> addr3
  197. cnt1 i - -> cnt3
  198. LEAVE
  199. THEN
  200. LOOP
  201. THEN
  202. addr3 cnt3 flag
  203. ;