bitwidth.bf 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. [
  2. This routine is a demonstration of checking for the three cell sizes
  3. that are normal for Brainfuck. The demo code also checks for bugs
  4. that have been noted in various interpreters and compilers.
  5. It should print one of three slight variations of "Hello world" followed
  6. by an exclamation point then the maximum cell value (if it's less than a
  7. few thousand) and a newline.
  8. If the interpreter is broken in some way it can print a lot of other
  9. different strings and frequently causes the interpreter to crash.
  10. It does work correctly with 'bignum' cells.
  11. ]
  12. +>>
  13. +[-<<+>>]
  14. This code runs at pointer offset two and unknown bit width; don't
  15. assume you have more that eight bits
  16. ======= DEMO CODE =======
  17. First just print "Hello"
  18. Notice that I reset the cells despite knowing that they are zero
  19. this is a test for proper functioning of the ability to skip over
  20. a loop that's never executed but isn't actually a comment loop
  21. Secondly there's a NOP movement between the two 'l' characters
  22. Also there's some commented out code afterwards
  23. >[-]<[-]++++++++[->+++++++++<]>.----[--<+++>]<-.+++++++.><.+++.
  24. [-][[-]>[-]+++++++++[<+++++>-]<+...--------------.>++++++++++[<+
  25. ++++>-]<.+++.-------.>+++++++++[<----->-]<.-.>++++++++[<+++++++>
  26. -]<++.-----------.--.-----------.+++++++.----.++++++++++++++.>++
  27. ++++++++[<----->-]<..[-]++++++++++.[-]+++++++[.,]-]
  28. ===== END DEMO CODE =====
  29. <<-
  30. Calculate the value 256 and test if it's zero
  31. If the interpreter errors on overflow this is where it'll happen
  32. ++++++++[>++++++++<-]>[<++++>-]
  33. +<[>-<
  34. Multiply by 256 again to get 65536
  35. [>++++<-]>[<++++++++>-]<[>++++++++<-]
  36. +>[>
  37. Cells should be 32bits at this point
  38. The pointer is at cell two and you can continue your code confident
  39. that there are big cells
  40. ======= DEMO CODE =======
  41. This code rechecks that the test cells are in fact nonzero
  42. If the compiler notices the above is constant but doesn't
  43. properly wrap the values this will generate an incorrect
  44. string
  45. An optimisation barrier; unbalanced loops aren't easy
  46. >+[<]>-<
  47. Print a message
  48. ++>[-]++++++[<+++++++>-]<.------------.[-]
  49. <[>+<[-]]>
  50. ++++++++>[-]++++++++++[<+++++++++++>-]<.--------.+++.------.
  51. --------.[-]
  52. ===== END DEMO CODE =====
  53. <[-]<[-]>] <[>>
  54. Cells should be 16bits at this point
  55. The pointer is at cell two and you can continue your code confident
  56. that there are medium sized cells; you can use all the cells on the
  57. tape but it is recommended that you leave the first two alone
  58. If you need 32bit cells you'll have to use a BF doubler
  59. ======= DEMO CODE =======
  60. Space
  61. ++>[-]+++++[<++++++>-]<.[-]
  62. I'm rechecking that the cells are 16 bits
  63. this condition should always be true
  64. +>>++++[-<<[->++++<]>[-<+>]>]< + <[ >>
  65. Print a message
  66. >[-]++++++++++[<+++++++++++>-]<+++++++++.--------.
  67. +++.------.--------.[-]
  68. <[-]<[-] ] >[> > Dead code here
  69. This should never be executed because it's in an 8bit zone hidden
  70. within a 16bit zone; a really good compiler should delete this
  71. If you see this message you have dead code walking
  72. Print a message
  73. [-]>[-]+++++++++[<++++++++++>-]<.
  74. >++++[<+++++>-]<+.--.-----------.+++++++.----.
  75. [-]
  76. <<[-]]<
  77. ===== END DEMO CODE =====
  78. <<[-]] >[-]< ] >[>
  79. Cells should be 8bits at this point
  80. The pointer is at cell two but you only have 8 bits cells
  81. and it's time to use the really big and slow BF quad encoding
  82. ======= DEMO CODE =======
  83. A broken wrapping check
  84. +++++[>++++<-]>[<+++++++++++++>-]<----[[-]>[-]+++++[<++++++>-]<++.
  85. >+++++[<+++++++>-]<.>++++++[<+++++++>-]<+++++.>++++[<---->-]<-.++.
  86. ++++++++.------.-.[-]]
  87. Space
  88. ++>[-]+++++[<++++++>-]<.[-]
  89. An exponent checker for github user btzy
  90. >++[>++<-]>[<<+>>[-<<[>++++<-]>[<++++>-]>]]<<[>++++[>---<++++]>++.
  91. [<++>+]<.[>+<------]>.+++.[<--->++]<--.[-]<[-]]
  92. Another dead code check
  93. [-]>[-]>[-]<++[>++++++++<-]>[<++++++++>-]<[>++++++++<-]>[<++++++++>-
  94. ]<[<++++++++>-]<[[-]>[-]+++++++++[<++++++++++>-]<.>++++[<+++++>-]<+.
  95. --.-----------.+++++++.----.>>[-]<+++++[>++++++<-]>++.<<[-]]
  96. Print a message
  97. [-] <[>+<[-]]> +++++>[-]+++++++++[<+++++++++>-]<.
  98. >++++[<++++++>-]<.+++.------.--------.
  99. [-]
  100. ===== END DEMO CODE =====
  101. <[-]]<
  102. +[[>]<-] Check unbalanced loops are ok
  103. >>
  104. ======= DEMO CODE =======
  105. Back out and print the last two characters
  106. [<[[<[[<[[<[,]]]<]<]<]<][ Deep nesting non-comment comment loop ]]
  107. Check that an offset of 128 will work
  108. +>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  109. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-[+<-]
  110. And back
  111. +++[->++++++<]>[-<+++++++>]<[->>[>]+[<]<]>>[->]<<<<<<<<<<<<<<<<<<<<<
  112. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  113. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  114. And inside a loop
  115. --[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  116. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>++<<<
  117. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  118. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+]+>----[++
  119. ++>----]-[+<-]
  120. This is a simple multiply loop that looks like it goes off the
  121. start of the tape
  122. +[>]<- [-
  123. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  124. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  125. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  126. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  127. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  128. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  129. ++++
  130. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  131. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  132. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  133. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  134. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  135. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  136. ]
  137. [ Check there are enough cells. This takes 18569597 steps. ]
  138. [
  139. >++++++[<+++>-]<+[>+++++++++<-]>+[[->+>+<<]>>
  140. [-<<+>>]<[<[->>+<<]+>[->>+<<]+[>]<-]<-]<[-<]
  141. ]
  142. This loop is a bug check for handling of nested loops; it goes
  143. round the outer loop twice and the inner loop is skipped on the
  144. first pass but run on the second
  145. BTW: It's unlikely that an optimiser will notice how this works
  146. >
  147. +[>[
  148. Print the exclamation point
  149. [-]+++>
  150. [-]+++++ +#-
  151. [<+++2+++>-]<
  152. .
  153. <[-]>[-]]+<]
  154. <
  155. Clean up any debris
  156. ++++++++[[>]+[<]>-]>[>]<[[-]<]
  157. This is a hard optimisation barrier
  158. It contains several difficult to 'prove' constructions close together
  159. and is likely to prevent almost all forms of optimisation
  160. +[[>]<-[,]+[>]<-[]]
  161. This part finds the actual value that the cell wraps at; even
  162. if it's not one of the standard ones; but it gets bored after
  163. a few thousand: any higher and we print nothing
  164. This has a reasonably deep nested loop and a couple of loops
  165. that have unbalanced pointer movements
  166. Find maxint (if small)
  167. [-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<++++[->>++++>>++++>>++
  168. ++<<<<<<]++++++++++++++>>>>+>>++<<<<<<[->>[->+>[->+>[->+>+[>
  169. >>+<<]>>[-<<+>]<-[<<<[-]<<[-]<<[-]<<[-]>>>[-]>>[-]>>[-]>->+]
  170. <<<]>[-<+>]<<<]>[-<+>]<<<]>[-<+>]<<<]>+>[[-]<->]<[->>>>>>>[-
  171. <<<<<<<<+>>>>>>>>]<<<<<<<]<
  172. The number is only printed if we found the actual maxint
  173. >+<[
  174. Space
  175. >[-]>[-]+++++[<++++++>-]<++.[-]<
  176. Print the number
  177. [[->>+<<]>>[-<++>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[<[-]+>
  178. ->+<[<-]]]]]]]]]]>]<<[>++++++[<++++++++>-]<-.[-]<]]
  179. ]
  180. Check if we should have had a value but didn't
  181. >[
  182. >[-]>[-]++++[<++++++++>-]<[<++++++++>-]>+++[<++++++++>-]<+++++++
  183. [<-------->-]<------->+<[[-]>-<]>[>[-]<[-]++++[->++++++++<]>.+++
  184. +++[-<++>]<.[-->+++<]>++.<++++[>----<-]>.[-]<]<
  185. [-]>[-]++++++++[<++++++++>-]<[>++++<-]+>[<->[-]]<[>[-]<[-]++++[-
  186. >++++++++<]>.---[-<+++>]<.---.--------------.[-->+<]>--.[-]<]
  187. ]<
  188. Clean up any debris
  189. ++++++++[[>]+[<]>-]>[>]<[[-]<]
  190. One last thing: an exclamation point is not a valid BF instruction!
  191. Print the newline
  192. [-]++++++++++.[-]
  193. [
  194. Oh, and now that I can use "!" the string you see should be one of:
  195. Hello World! 255
  196. Hello world! 65535
  197. Hello, world!
  198. And it should be followed by a newline.
  199. ]
  200. ===== END DEMO CODE =====
  201. << Finish at cell zero