minicomum_general.cmn 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # These are some general functions and tests runnable with minicomum.
  2. printNum: # prints number as unsigned, doesn't pop
  3. ?'
  4. $0
  5. 0
  6. ><
  7. @' # reverse push the chars
  8. $0
  9. 10
  10. %
  11. "0"
  12. +
  13. ><
  14. 10
  15. /
  16. .
  17. ^
  18. @' # print
  19. ->
  20. .
  21. ^
  22. ; # zero
  23. "0" ->
  24. .
  25. .
  26. printNumS: # prints number as signed, doesn't pop
  27. $0 -1 2 / >
  28. ? # negative?
  29. "-" ->
  30. -1 >< - 1 +
  31. .
  32. printNum
  33. .
  34. factR: # computes factorial recursively
  35. ?'
  36. $0 -- factR *
  37. ;
  38. ^ 1
  39. .
  40. .
  41. factI: # computes factorial iteratively
  42. $0 --
  43. @'
  44. ><
  45. $1
  46. *
  47. ><
  48. --
  49. .
  50. ^
  51. .
  52. isPrime: # checks if a number is prime
  53. $0 1 <=
  54. ?
  55. ^ 0 # 0 and 1 are not primes
  56. ;
  57. $0 2 /
  58. @@ # test divisors
  59. $0 1 <=
  60. ? # no divisors greater than 1 found?
  61. ^ ^ 1 !@
  62. .
  63. $1 $1 % 0 =
  64. ? # is divisible?
  65. ^ ^ 0 !@
  66. .
  67. --
  68. .
  69. .
  70. .
  71. 1 2 3 4 5
  72. 0 "stack top: " --> $$ printNum ^ 10 ->
  73. 0 "stack top (should be the same): " --> $$ printNum ^ 10 ->
  74. 0 "factorial of 6 (recursive and iterative): " -->
  75. 6 factR printNum ^ " " ->
  76. 6 factI printNum ^ 10 ->
  77. 0 10 "primes up to 100: " -->
  78. 0 # tested number
  79. @@
  80. $0 100 = ?
  81. !@
  82. .
  83. $0 isPrime ?
  84. $0 printNum ^ 10 ->
  85. .
  86. ++
  87. .
  88. 0 "pointer manipulation: " -->
  89. $$ 3 $+0 $$ $4 -
  90. 4 ?
  91. 0 10 "OK" -->
  92. ;
  93. 0 10 "ERROR" -->
  94. .