test.cmn 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # general testing program for comun, tests most of its features
  2. 0 10 "testing:" -->
  3. exitFunc: # tests the exit command
  4. 0
  5. 1 ?
  6. !.
  7. .
  8. ^ 1
  9. .
  10. unusedFunc: # function that won't be called, basic test of optimization
  11. 1 2 3
  12. ?
  13. --
  14. ;
  15. ++
  16. .
  17. .
  18. myFunc: # just some function
  19. 1 2 3
  20. ?
  21. 0 "abc" -->
  22. ;
  23. !.
  24. .
  25. .
  26. fact: # recursive factorial
  27. ?'
  28. $0 -- fact *
  29. ;
  30. ^ 1
  31. .
  32. .
  33. ##0 # push 0
  34. ?
  35. ~abc # sneakily declare pointer in non-executed branch, shouldn't matter
  36. $4 # this might crash the program (if stack top is near 0 now)
  37. 0 10 "ERROR (a)" -->
  38. !.
  39. .
  40. $$ 2 $+0 $$ $4 - # stack top now: 4
  41. $' $$ >< - +
  42. 10 != ?
  43. 0 10 "ERROR (b)" -->
  44. !.
  45. .
  46. lateFunc # call a function that will be defined later, pushes 10
  47. "#" # comment in string, should be pushed
  48. # "abc" # # string in comment, shouldn't be pushed
  49. +
  50. # stack now: 45
  51. # let's now try out most operations:
  52. +xf +b011 - * 11 / -d02 // 50 + -10 4 %% + 1000 >< / -b100 +x3 != + $0 -1 << +
  53. +b111 |! 1 + 0 1 2 ?? +
  54. # 48 is here # 1 2 3 4 $ # push the 48
  55. 1 |< # multiply by 2
  56. 96 != ?
  57. 0 10 "ERROR (c)" -->
  58. !.
  59. .
  60. # let's try some complicated control structures:
  61. 1
  62. ?
  63. # here
  64. 1 2 =
  65. ?
  66. # not here
  67. @
  68. @
  69. 0 10 "ERROR (d)" -->
  70. !.
  71. .
  72. !@
  73. .
  74. ++
  75. ;
  76. # here
  77. 7
  78. @@
  79. ?'
  80. ;
  81. @'
  82. !@
  83. .
  84. !@
  85. .
  86. --
  87. .
  88. 0 = ?'
  89. 3 +
  90. .
  91. .
  92. ;
  93. # not here
  94. ++
  95. @
  96. ?
  97. ++
  98. ?
  99. ?
  100. ++
  101. .
  102. .
  103. .
  104. @
  105. <-
  106. ++
  107. .
  108. .
  109. .
  110. 4 != ?
  111. 0 10 "ERROR (e)" -->
  112. !.
  113. .
  114. exitFunc
  115. ?
  116. 0 10 "ERROR (f)" -->
  117. !.
  118. .
  119. # test pointers now:
  120. ~var0:0
  121. ~var20:20
  122. ~stBackup
  123. ~16
  124. ~var20 # same name but in different type env.
  125. ~0
  126. $0>stBackup
  127. 13 $:abc # abc is declared above
  128. $var20>var0
  129. 20 $abc $:var0
  130. $>var20
  131. 30 $:var20
  132. $var20>0
  133. + +
  134. $stBackup>0
  135. $stBackup=0 +
  136. $var0
  137. $1=0 # should push 2
  138. +
  139. 45 != ?
  140. 0 10 "ERROR (g)" -->
  141. !.
  142. .
  143. 5 fact 120 != ? # test recursion
  144. 0 10 "ERROR (h)" -->
  145. !.
  146. .
  147. # test simple goto:
  148. >skipLabel
  149. 0 10 "ERROR (i)" -->
  150. !.
  151. ~:skipLabel
  152. # test type environments:
  153. 55
  154. >8
  155. ~8
  156. $0 2 * ++ 3 * # should be 77
  157. >16
  158. ~16
  159. $0 10 / # should be 7
  160. >32
  161. ~32
  162. $0 $0 * # should be 49
  163. >0
  164. ~0
  165. 0 + # these operations do nothing (test optimizations)
  166. 1 *
  167. 1 /
  168. 1 //
  169. 0 -
  170. 0 |
  171. 49 != ?
  172. 0 10 "ERROR (j)" -->
  173. !.
  174. .
  175. 0 10 "ALL SEEMS OK" -->
  176. lateFunc:
  177. 10
  178. .