-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf.cpp
211 lines (191 loc) · 5.47 KB
/
bf.cpp
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
#include "bf.hpp"
#include <string.h>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
void Brainfuck::reset() {
if (Brainfuck::program) {
free(Brainfuck::program);
Brainfuck::program = NULL;
}
progIndex = 0;
if (Brainfuck::input) {
free(Brainfuck::input);
Brainfuck::input = NULL;
}
inIndex = 0;
memset(memory, 0, sizeof(memory));
memIndex = 0;
memLen = 1;
}
void Brainfuck::reset(unsigned progLen, wchar_t *program, unsigned inLen, void *input) {
if (Brainfuck::program) free(Brainfuck::program);
if (progLen == 0 || !program) {
Brainfuck::program = NULL;
Brainfuck::progLen = 0;
} else {
Brainfuck::program = program;
Brainfuck::progLen = progLen;
}
progIndex = 0;
if (Brainfuck::input) free(Brainfuck::input);
if (inLen == 0 || !input) {
Brainfuck::input = NULL;
Brainfuck::inLen = 0;
} else {
Brainfuck::input = (unsigned char *)input;
Brainfuck::inLen = inLen;
}
inIndex = 0;
memset(memory, 0, sizeof(memory));
memIndex = 0;
memLen = 1;
}
void Brainfuck::setBehavior(enum noinput_t noInput, bool wrapInt, bool signedness, bool debug) {
Brainfuck::noInput = noInput;
Brainfuck::wrapInt = wrapInt;
Brainfuck::signedness = signedness;
Brainfuck::debug = debug;
}
enum Brainfuck::result_t Brainfuck::next(unsigned char *output, bool *didOutput) {
enum result_t result = RESULT_RUN;
*didOutput = false;
if (progIndex >= progLen) {
return RESULT_FIN;
}
switch (program[progIndex]) {
case L'>':
if (memIndex != 65535) {
++memIndex;
if (memIndex == memLen) ++memLen;
} else {
wsprintfW(lastError, L"%u: Instruction '>' used when the memory pointer is 65535.", progIndex);
return RESULT_ERR;
}
break;
case L'<':
if (memIndex != 0) {
--memIndex;
} else {
wsprintfW(lastError, L"%u: Instruction '<' used when the memory pointer is 0.", progIndex);
return RESULT_ERR;
}
break;
// Wrap around is guaranteed for an unsigned integer.
case L'+':
if (wrapInt) {
++memory[memIndex];
} else if (signedness) {
if (memory[memIndex] != 0x7F) {
++memory[memIndex];
} else {
wsprintfW(lastError, L"%u: Instruction '+' used when the pointed memory is 127.", progIndex);
return RESULT_ERR;
}
} else {
if (memory[memIndex] != 0xFF) {
++memory[memIndex];
} else {
wsprintfW(lastError, L"%u: Instruction '+' used when the pointed memory is 255.", progIndex);
return RESULT_ERR;
}
}
break;
// Wrap around is guaranteed for an unsigned integer.
case L'-':
if (wrapInt) {
--memory[memIndex];
} else if (signedness) {
if (memory[memIndex] != 0x80) {
--memory[memIndex];
} else {
wsprintfW(lastError, L"%u: Instruction '-' used when the pointed memory is -128.", progIndex);
return RESULT_ERR;
}
} else {
if (memory[memIndex] != 0x00) {
--memory[memIndex];
} else {
wsprintfW(lastError, L"%u: Instruction '-' used when the pointed memory is 0.", progIndex);
return RESULT_ERR;
}
}
break;
case L'.':
*output = memory[memIndex];
*didOutput = true;
break;
case L',':
if (inIndex >= inLen) {
if (noInput == NOINPUT_ZERO) {
memory[memIndex] = 0;
} else if (noInput == NOINPUT_FF) {
memory[memIndex] = 255;
} else {
wsprintfW(lastError, L"%u: Instruction ',' used when the input stream is empty.", progIndex);
return RESULT_ERR;
}
} else {
memory[memIndex] = input[inIndex];
++inIndex;
}
break;
case L'[':
if (memory[memIndex] == 0) {
if (progIndex == progLen - 1) {
wsprintfW(lastError, L"%u: No matching closing bracket.", progIndex);
return RESULT_ERR;
}
unsigned nextIndex = progIndex + 1, ketCnt = 0;
while (true) {
if (program[nextIndex] == L']') {
if (ketCnt == 0) {
break; // match
} else {
--ketCnt; // recursive bracket
}
}
if (program[nextIndex] == L'[') ketCnt++;
if (nextIndex == progLen - 1) {
wsprintfW(lastError, L"%u: No matching closing bracket.", progIndex);
return RESULT_ERR;
}
++nextIndex;
}
progIndex = nextIndex;
}
break;
case L']':
if (memory[memIndex] != 0) {
if (progIndex == 0) {
wsprintfW(lastError, L"%u: No matching opening bracket.", progIndex);
return RESULT_ERR;
}
unsigned nextIndex = progIndex - 1, braCnt = 0;
while (true) {
if (program[nextIndex] == L'[') {
if (braCnt == 0) {
break; // match
} else {
--braCnt; // recursive bracket
}
}
if (program[nextIndex] == L']') braCnt++;
if (nextIndex == 0) {
wsprintfW(lastError, L"%lu: No matching opening bracket.", (unsigned long)progIndex);
return RESULT_ERR;
}
--nextIndex;
}
progIndex = nextIndex;
}
break;
// Extended spec for debugging (breakpoint).
case L'@':
if (debug) result = RESULT_BREAK;
break;
}
++progIndex;
return result;
}