PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/fcode-utils-devel/testlogs/testlogs-ppc-linux/TokeErrs/RetStkDep.fth

https://github.com/mwilbur/openbios
Forth | 105 lines | 93 code | 12 blank | 0 comment | 11 complexity | 3e65235bd02f4312d576a520e9c00cd7 MD5 | raw file
  1. \ Return-Stack Depth -- well, "depth" is not exactly it;
  2. \ we're testing detection of imbalance between >R and R> and uses of R@
  3. \ in between.
  4. \ From the ANSI Forth Spec:
  5. \ 3.2.3.3 Return stack
  6. \ . . . . . .
  7. \ A program may use the return stack for temporary storage during the
  8. \ execution of a definition subject to the following restrictions:
  9. \ A program shall not access values on the return stack (using R@,
  10. \ R>, 2R@ or 2R>) that it did not place there using >R or 2>R;
  11. \ A program shall not access from within a do-loop values placed
  12. \ on the return stack before the loop was entered;
  13. \ All values placed on the return stack within a do-loop shall
  14. \ be removed before I, J, LOOP, +LOOP, UNLOOP, or LEAVE is
  15. \ executed;
  16. \ All values placed on the return stack within a definition
  17. \ shall be removed before the definition is terminated
  18. \ or before EXIT is executed.
  19. \ Updated Tue, 18 Jul 2006 at 16:09 PDT by David L. Paktor
  20. [flag] Lower-Case-Token-Names
  21. fcode-version2
  22. headers
  23. \ First, a few primal errors...
  24. ." Primal errors" cr
  25. d# 127 h# 127 dup r> swap r@ -rot >r swap
  26. 3 0 do r@ loop
  27. 3 0 do r> loop
  28. 3 0 do i >r loop
  29. hex
  30. create cold-stone 1c c, ec c, 9e c, a3 c, c0 c, 6e c,
  31. \ Then some legit usages
  32. : legit_one
  33. dup >r
  34. 3 0 do i
  35. cold-stone over ca+ c@ >r
  36. 3 + cold-stone swap ca+ c@ r>
  37. loop
  38. r>
  39. ;
  40. \ Now a tricky one:
  41. : tricky_one
  42. dup >r
  43. over if ." Showing " r> u.
  44. else r> drop ." Don't show"
  45. then
  46. ;
  47. : another_one ( old new -- false | new' true )
  48. >r 0= if r> drop false exit then
  49. dup * r@ / r> + true
  50. ;
  51. ." Now we start getting bad." cr
  52. \ The one that started me down this path...
  53. 0 instance value _str
  54. 0 instance value _len
  55. 0 instance value _num
  56. : PARSE-INTS ( addr len num -- n1 .. nn )
  57. to _num
  58. to _len
  59. to _str
  60. _num 0 ?do
  61. _len if
  62. _str _len [char] , left-parse-string 2swap to _len to _str
  63. $number if 0 then
  64. else
  65. 0
  66. then
  67. >r loop
  68. _num 0 ?do r> loop
  69. ;
  70. . " If this doesn't scare you, it should:" cr
  71. : scattered-errors
  72. 0 >r
  73. _num 0 ?do
  74. _str _len [char] , left-parse-string
  75. 2swap to _len to _str
  76. $number if 0 else r@ 1+ swap >r then
  77. >r i u.
  78. loop
  79. r@ 0 ?do r> i roll loop
  80. ;
  81. ." Now, be very afraid..." cr
  82. 0 instance value where-from
  83. : frayed-knot
  84. where-from 0= if r@ to where-from then
  85. r> drop where-from if exit then
  86. ." What have I done?" cr
  87. ;
  88. : krellboyn
  89. where-from ?dup if >r 0 to where-from then
  90. where-from if exit then
  91. ." I didn't mean it!" cr
  92. ;
  93. fcode-end