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