PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/fth/misc1.fth

https://github.com/cataska/pforth
Forth | 176 lines | 154 code | 22 blank | 0 comment | 2 complexity | 1d527f74d4d2e1d9ddc151710a3520d1 MD5 | raw file
  1. \ @(#) misc1.fth 98/01/26 1.2
  2. \ miscellaneous words
  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. anew task-misc1.fth
  16. decimal
  17. : >> rshift ;
  18. : << lshift ;
  19. : CELL* ( n -- n*cell ) 2 lshift ;
  20. : (WARNING") ( flag $message -- )
  21. swap
  22. IF count type
  23. ELSE drop
  24. THEN
  25. ;
  26. : WARNING" ( flag <message> -- , print warning if true. )
  27. [compile] " ( compile message )
  28. state @
  29. IF compile (warning")
  30. ELSE (warning")
  31. THEN
  32. ; IMMEDIATE
  33. : (ABORT") ( flag $message -- )
  34. swap
  35. IF count type cr abort
  36. ELSE drop
  37. THEN
  38. ;
  39. : ABORT" ( flag <message> -- , print warning if true. )
  40. [compile] " ( compile message )
  41. state @
  42. IF compile (abort")
  43. ELSE (abort")
  44. THEN
  45. ; IMMEDIATE
  46. : ?PAUSE ( -- , Pause if key hit. )
  47. ?terminal
  48. IF key drop cr ." Hit space to continue, any other key to abort:"
  49. key dup emit BL = not abort" Terminated"
  50. THEN
  51. ;
  52. 60 constant #cols
  53. : CR? ( -- , do CR if near end )
  54. OUT @ #cols 16 - 10 max >
  55. IF cr
  56. THEN
  57. ;
  58. : CLS ( -- clear screen )
  59. 40 0 do cr loop
  60. ;
  61. : PAGE ( -- , clear screen, compatible with Brodie )
  62. cls
  63. ;
  64. : $ ( <number> -- N , convert next number as hex )
  65. base @ hex
  66. 32 lword number? num_type_single = not
  67. abort" Not a single number!"
  68. swap base !
  69. state @
  70. IF [compile] literal
  71. THEN
  72. ; immediate
  73. : .HX ( nibble -- )
  74. dup 9 >
  75. IF $ 37
  76. ELSE $ 30
  77. THEN + emit
  78. ;
  79. variable TAB-WIDTH 8 TAB-WIDTH !
  80. : TAB ( -- , tab over to next stop )
  81. out @ tab-width @ mod
  82. tab-width @ swap - spaces
  83. ;
  84. \ Vocabulary listing
  85. : WORDS ( -- )
  86. 0 latest
  87. BEGIN dup 0<>
  88. WHILE dup id. tab cr? ?pause
  89. prevname
  90. swap 1+ swap
  91. REPEAT drop
  92. cr . ." words" cr
  93. ;
  94. : VLIST words ;
  95. variable CLOSEST-NFA
  96. variable CLOSEST-XT
  97. : >NAME ( xt -- nfa , scans dictionary for closest nfa, SLOW! )
  98. 0 closest-nfa !
  99. 0 closest-xt !
  100. latest
  101. BEGIN dup 0<>
  102. IF ( -- addr nfa ) 2dup name> ( addr nfa addr xt ) <
  103. IF true ( addr below this cfa, can't be it)
  104. ELSE ( -- addr nfa )
  105. 2dup name> ( addr nfa addr xt ) =
  106. IF ( found it ! ) dup closest-nfa ! false
  107. ELSE dup name> closest-xt @ >
  108. IF dup closest-nfa ! dup name> closest-xt !
  109. THEN
  110. true
  111. THEN
  112. THEN
  113. ELSE false
  114. THEN
  115. WHILE
  116. prevname
  117. REPEAT ( -- cfa nfa )
  118. 2drop
  119. closest-nfa @
  120. ;
  121. : @EXECUTE ( addr -- , execute if non-zero )
  122. x@ ?dup
  123. IF execute
  124. THEN
  125. ;
  126. : TOLOWER ( char -- char_lower )
  127. dup ascii [ <
  128. IF dup ascii @ >
  129. IF ascii A - ascii a +
  130. THEN
  131. THEN
  132. ;
  133. : EVALUATE ( i*x c-addr num -- j*x , evaluate string of Forth )
  134. \ save current input state and switch to passed in string
  135. source >r >r
  136. set-source
  137. -1 push-source-id
  138. >in @ >r
  139. 0 >in !
  140. \ interpret the string
  141. interpret
  142. \ restore input state
  143. pop-source-id drop
  144. r> >in !
  145. r> r> set-source
  146. ;
  147. : \S ( -- , comment out rest of file )
  148. source-id
  149. IF
  150. BEGIN \ using REFILL is safer than popping SOURCE-ID
  151. refill 0=
  152. UNTIL
  153. THEN
  154. ;