SMETANA To Infinity!/brainfuck
Jump to navigation
Jump to search
A tape of 1-bit memory cells can be implemented like this:
# We assume that steps 1 through 6 are laid out like this: # # 1 through 3: The desired return location for an operation returning... # 1: ...no result. # 2: ...a result of "0". # 3: ...a result of "1". # # 4: Go to (the entry point for flipping the current cell). # 5: Go to (the entry point for moving left on the tape). # 6: Go to (the entry point for moving right on the tape). # 7: Go to (the entry point for testing the current cell). # Entry point for flipping the current cell. # We assume that step 1 is the desired return location. Step 100n + 200. Swap step 100n + 220 with step 100n + 221. Step 100n + 201. Go to step 1. # Entry point for moving left on the tape. Step 100n + 202. Swap step 4 with step 100n + 222. Step 100n + 203. Swap step 5 with step 100n + 223. Step 100n + 204. Swap step 6 with step 100n + 224. Step 100n + 205. Swap step 7 with step 100n + 225. Step 100n + 206. Swap step 100n + 122 with step 4. Step 100n + 207. Swap step 100n + 123 with step 5. Step 100n + 208. Swap step 100n + 124 with step 6. Step 100n + 209. Swap step 100n + 125 with step 7. Step 100n + 210. Go to step 1. # Entry point for moving right on the tape. Step 100n + 211. Swap step 4 with step 100n + 222. Step 100n + 212. Swap step 5 with step 100n + 223. Step 100n + 213. Swap step 6 with step 100n + 224. Step 100n + 214. Swap step 7 with step 100n + 225. Step 100n + 215. Swap step 100n + 322 with step 4. Step 100n + 216. Swap step 100n + 323 with step 5. Step 100n + 217. Swap step 100n + 324 with step 6. Step 100n + 218. Swap step 100n + 325 with step 7. Step 100n + 219. Go to step 1. # Storage location for the bit which is currently active. # Also the entry point for testing the current cell. Step 100n + 220. Go to step 2. # Storage location for the bit which is currently NOT active. Step 100n + 221. Go to step 3. # Storage location for the instruction which is to be placed at step 4, # when this cell is the current cell. Step 100n + 222. Go to step 100n + 200. # Storage location for the instruction for step 5. Step 100n + 223. Go to step 100n + 202. # Storage location for the instruction for step 6. Step 100n + 224. Go to step 100n + 211. # Storage location for the instruction for step 7. Step 100n + 225. Go to step 100n + 220.
Then a brainfuck instruction meaning "flip the current cell" could be implemented like this:
# Entry point for the instruction. # First, place step 54 at step 1, so that control will return back here. Step 50. Swap step 54 with step 1. # Then go to step 4, which flips the bit and jumps back to 1, # which jumps back to 52. Step 51. Go to step 4. # Then put step 1 back in its original place. Step 52. Swap step 1 with step 54. # Finally, jump to the next instruction. Step 53. Go to step 55. # Storage location for the jump back to step 52. Step 54. Go to step 52.
Instructions for moving left and right would look identical, but with Step 51 mentioning step 5 or 6 instead of 4.
An instruction for a conditional jump to another place could be implemented like this:
# Entry point for the instruction. # First, place steps 69 and 70 at steps 2 and 3, # so that control will return back to us. Step 60. Swap step 69 with step 2. Step 61. Swap step 70 with step 3. # Then go to step 7, which tests the bit and jumps back to 2 or 3. Step 62. Go to step 7. # Landing point if it was 0. # First, put steps 2 and 3 back in their original places. Step 63. Swap step 2 with step 69. Step 64. Swap step 3 with step 70. # Then jump to the next instruction. Step 65. Go to step 71. # Landing point if it was 1. # Again, first put steps 2 and 3 back in their original places. Step 66. Swap step 2 with step 69. Step 67. Swap step 3 with step 70. # Then jump off to some remote instruction. Step 68. Go to step 80. # Storage location for the jump to step 63. Step 69. Go to step 63. # Storage location for the jump to step 66. Step 70. Go to step 66.
All of this together provides the mechanisms needed to translate a 1-bit brainfuck program into SMETANA To Infinity!.