/hmsl/fth/utils.fth

https://github.com/philburk/hmsl · Forth · 156 lines · 134 code · 22 blank · 0 comment · 2 complexity · 2e850be2c28d78f461af080ca0b20445 MD5 · raw file

  1. \ @(#) utils.fth 96/06/11 1.1
  2. \ General Utilities to support JForth & HMSL
  3. \ These utilities are useful words which are not likely to be
  4. \ supported by a typical Forth. Words which some Forths support
  5. \ but some not, should be defined in XXX_BASE.
  6. \
  7. \ Author: Phil Burk
  8. \ Copyright 1986
  9. \
  10. \ MOD: PLB 11/9/86 Add SERVICE.TASKS/16
  11. \ MOD: PLB 3/2/87 Use abort" in stack.check.
  12. \ MOD: PLB 4/29/87 Remove include? , change V: to VARIABLE
  13. \ MOD: PLB 9/3/87 Add DEBUG.TYPE
  14. \ MOD: PLB 5/17/91 Merged with ho:more_utils
  15. ANEW TASK-UTILS
  16. VARIABLE IF-DEBUG ( debug trace flag )
  17. VARIABLE IF-TESTING ( flag for loading test code )
  18. : DEBUG.TYPE ( $string -- , type if debugging )
  19. if-debug @
  20. IF >newline count type space
  21. ELSE drop
  22. THEN
  23. ;
  24. : ?MORE ( count -- flag , pause every 20, true if "Q")
  25. 20 mod 0= dup
  26. IF drop
  27. ." Q to quit, <CR> to continue ----" CR
  28. KEY ascii q =
  29. THEN
  30. ;
  31. \ Stack depth checking , useful for catching leftovers --------
  32. VARIABLE STACK-HOLD
  33. : STACK.MARK ( -- , record depth of stack )
  34. depth stack-hold !
  35. ;
  36. : STACK.CHECK ( -- , check to make sure stack hasn't been damaged )
  37. depth stack-hold @ = NOT
  38. IF ." Old stack depth = " stack-hold @ .
  39. .s
  40. true abort" STACK.CHECK - Change in stack depth!"
  41. THEN
  42. ;
  43. : $EQUAL ( $string1 $string2 -- true_if_= , case insens. )
  44. >r count
  45. r> count 2 pick =
  46. IF text=?
  47. ELSE
  48. 2drop drop false
  49. THEN
  50. ;
  51. hex
  52. : NFA.MOVE ( nfa addr -- , copy name field to address and fix like string )
  53. >r count 1f and ( n+1 c , remove immediate bit )
  54. dup r@ c! ( set length at pad )
  55. r> 1+ rot rot 0 ( a+1 n+1 c 0 )
  56. ?DO
  57. 2dup c@ 7f and ( remove flags from characters )
  58. swap c!
  59. 1+ swap 1+ swap ( advance )
  60. LOOP 2drop
  61. ;
  62. : NFA->$ ( nfa -- $string , copy to pad )
  63. pad nfa.move pad
  64. ;
  65. decimal
  66. \ Assistance for debugging.
  67. : BREAK ( -- , dump stack and allow abort )
  68. .s cr ." BREAK - Enter A to abort" cr
  69. key toupper ascii A =
  70. IF abort THEN
  71. ;
  72. : BREAK" ( xxxx" -- , give message and break )
  73. [compile] ."
  74. compile break
  75. ; immediate
  76. \ ?terminal that only happens so often to avoid slowing down system
  77. V: ?term-count
  78. : ?TERMINAL/64 ( -- key? , true if key pressed, sometimes )
  79. ?term-count @ dup
  80. 1+ 63 AND ?term-count !
  81. 0= IF ?terminal
  82. ELSE false
  83. THEN
  84. ;
  85. : ?TERMINAL/8 ( -- key? , true if key pressed, sometimes )
  86. ?term-count @ dup
  87. 1+ 7 AND ?term-count !
  88. 0= IF ?terminal
  89. ELSE false
  90. THEN
  91. ;
  92. \ Range checking and clipping tools.
  93. : INRANGE? ( n lo hi -- flag , Is LO <= N <= HI ? )
  94. 2 pick <
  95. IF 2drop false
  96. ELSE >=
  97. THEN
  98. ;
  99. : CLIPTO ( n lo hi -- nclipped , clip N to range )
  100. >r max r> min
  101. ;
  102. : BAD.CHAR? ( CHAR -- FLAG , true if non printing)
  103. 32 126 inrange? not
  104. ;
  105. : SAFE.EMIT ( char -- , emit if safe or '.' )
  106. dup bad.char?
  107. IF drop ascii . emit
  108. ELSE emit
  109. THEN
  110. ;
  111. : BAD.STR? ( addr count -- , scan string for bad chars)
  112. 0
  113. ?DO dup i + c@ bad.char?
  114. IF cr dup i + dup h. c@ h.
  115. THEN
  116. LOOP drop
  117. ;
  118. : Y/N ( -- , ask for key )
  119. BEGIN
  120. ." (y/n) " key dup emit tolower
  121. dup [char] y = over [char] n = or 0=
  122. WHILE drop cr
  123. REPEAT [char] y =
  124. ;
  125. : Y/N/Q ( -- true_if_y , ask for key , abort on 'Q')
  126. BEGIN
  127. ." (y/n/q) " key dup emit tolower
  128. dup [char] q =
  129. IF cr abort
  130. THEN
  131. dup [char] y = over [char] n = or 0=
  132. WHILE drop cr
  133. REPEAT [char] y =
  134. ;