PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/fth/see.fth

https://github.com/cataska/pforth
Forth | 218 lines | 191 code | 27 blank | 0 comment | 3 complexity | 80c696ad213b8ea6f7983fbbe2f9b33c MD5 | raw file
  1. \ @(#) see.fth 98/01/26 1.4
  2. \ SEE ( <name> -- , disassemble pForth word )
  3. \
  4. \ Copyright 1996 Phil Burk
  5. ' file? >code rfence a!
  6. anew task-see.fth
  7. : .XT ( xt -- , print execution tokens name )
  8. >name
  9. dup c@ flag_immediate and
  10. IF
  11. ." POSTPONE "
  12. THEN
  13. id. space
  14. ;
  15. \ dictionary may be defined as byte code or cell code
  16. 0 constant BYTE_CODE
  17. BYTE_CODE [IF]
  18. : CODE@ ( addr -- xt , fetch from code space ) C@ ;
  19. 1 constant CODE_CELL
  20. .( BYTE_CODE not implemented) abort
  21. [ELSE]
  22. : CODE@ ( addr -- xt , fetch from code space ) @ ;
  23. CELL constant CODE_CELL
  24. [THEN]
  25. private{
  26. 0 value see_level \ level of conditional imdentation
  27. 0 value see_addr \ address of next token
  28. 0 value see_out
  29. : SEE.INDENT.BY ( -- n )
  30. see_level 1+ 1 max 4 *
  31. ;
  32. : SEE.CR
  33. >newline
  34. see_addr ." ( ".hex ." )"
  35. see.indent.by spaces
  36. 0 -> see_out
  37. ;
  38. : SEE.NEWLINE
  39. see_out 0>
  40. IF see.cr
  41. THEN
  42. ;
  43. : SEE.CR?
  44. see_out 6 >
  45. IF
  46. see.newline
  47. THEN
  48. ;
  49. : SEE.OUT+
  50. 1 +-> see_out
  51. ;
  52. : SEE.ADVANCE
  53. code_cell +-> see_addr
  54. ;
  55. : SEE.GET.INLINE ( -- n )
  56. see_addr @
  57. ;
  58. : SEE.GET.TARGET ( -- branch-target-addr )
  59. see_addr @ see_addr +
  60. ;
  61. : SEE.SHOW.LIT ( -- )
  62. see.get.inline .
  63. see.advance
  64. see.out+
  65. ;
  66. exists? F* [IF]
  67. : SEE.SHOW.FLIT ( -- )
  68. see_addr f@ f.
  69. 1 floats +-> see_addr
  70. see.out+
  71. ;
  72. [THEN]
  73. : SEE.SHOW.ALIT ( -- )
  74. see.get.inline >name id. space
  75. see.advance
  76. see.out+
  77. ;
  78. : SEE.SHOW.STRING ( -- )
  79. see_addr count 2dup + aligned -> see_addr type
  80. see.out+
  81. ;
  82. : SEE.SHOW.TARGET ( -- )
  83. see.get.target .hex see.advance
  84. ;
  85. : SEE.BRANCH ( -- addr | , handle branch )
  86. -1 +-> see_level
  87. see.newline
  88. see.get.inline 0>
  89. IF \ forward branch
  90. ." ELSE "
  91. see.get.target \ calculate address of target
  92. 1 +-> see_level
  93. nip \ remove old address for THEN
  94. ELSE
  95. ." REPEAT " see.get.target .hex
  96. drop \ remove old address for THEN
  97. THEN
  98. see.advance
  99. see.cr
  100. ;
  101. : SEE.0BRANCH ( -- addr | , handle 0branch )
  102. see.newline
  103. see.get.inline 0>
  104. IF \ forward branch
  105. ." IF or WHILE "
  106. see.get.target \ calculate adress of target
  107. 1 +-> see_level
  108. ELSE
  109. ." UNTIL=>" see.get.target .hex
  110. THEN
  111. see.advance
  112. see.cr
  113. ;
  114. : SEE.XT { xt -- }
  115. xt
  116. CASE
  117. 0 OF see_level 0> IF ." EXIT " see.out+ ELSE ." ;" 0 -> see_addr THEN ENDOF
  118. ['] (LITERAL) OF see.show.lit ENDOF
  119. ['] (ALITERAL) OF see.show.alit ENDOF
  120. [ exists? (FLITERAL) [IF] ]
  121. ['] (FLITERAL) OF see.show.flit ENDOF
  122. [ [THEN] ]
  123. ['] BRANCH OF see.branch ENDOF
  124. ['] 0BRANCH OF see.0branch ENDOF
  125. ['] (LOOP) OF -1 +-> see_level see.newline ." LOOP " see.advance see.cr ENDOF
  126. ['] (+LOOP) OF -1 +-> see_level see.newline ." +LOOP" see.advance see.cr ENDOF
  127. ['] (DO) OF see.newline ." DO" 1 +-> see_level see.cr ENDOF
  128. ['] (?DO) OF see.newline ." ?DO " see.advance 1 +-> see_level see.cr ENDOF
  129. ['] (.") OF .' ." ' see.show.string .' " ' ENDOF
  130. ['] (C") OF .' C" ' see.show.string .' " ' ENDOF
  131. ['] (S") OF .' S" ' see.show.string .' " ' ENDOF
  132. see.cr? xt .xt see.out+
  133. ENDCASE
  134. ;
  135. : (SEE) { cfa | xt -- }
  136. 0 -> see_level
  137. cfa -> see_addr
  138. see.cr
  139. 0 \ fake address for THEN handler
  140. BEGIN
  141. see_addr code@ -> xt
  142. BEGIN
  143. dup see_addr ( >newline .s ) =
  144. WHILE
  145. -1 +-> see_level see.newline
  146. ." THEN " see.cr
  147. drop
  148. REPEAT
  149. CODE_CELL +-> see_addr
  150. xt see.xt
  151. see_addr 0=
  152. UNTIL
  153. cr
  154. 0= not abort" SEE conditional analyser nesting failed!"
  155. ;
  156. }PRIVATE
  157. : SEE ( <name> -- , disassemble )
  158. '
  159. dup ['] FIRST_COLON >
  160. IF
  161. >code (see)
  162. ELSE
  163. >name id.
  164. ." is primitive defined in 'C' kernel." cr
  165. THEN
  166. ;
  167. PRIVATIZE
  168. 0 [IF]
  169. : SEE.JOKE
  170. dup swap drop
  171. ;
  172. : SEE.IF
  173. IF
  174. ." hello" cr
  175. ELSE
  176. ." bye" cr
  177. THEN
  178. see.joke
  179. ;
  180. : SEE.DO
  181. 4 0
  182. DO
  183. i . cr
  184. LOOP
  185. ;
  186. : SEE."
  187. ." Here are some strings." cr
  188. c" Forth string." count type cr
  189. s" Addr/Cnt string" type cr
  190. ;
  191. [THEN]