-
Notifications
You must be signed in to change notification settings - Fork 1
/
libbrainfunk.cpp
461 lines (412 loc) · 9.85 KB
/
libbrainfunk.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "libbrainfunk.hpp"
#include "ctre.hpp"
using std::string;
using std::string_view;
using std::stringstream;
using std::flush;
using std::cerr;
using std::endl;
using std::vector;
BrainfunkException::BrainfunkException(const string &msg)
{
this->msg = msg;
}
BrainfunkException::~BrainfunkException() throw()
{
this->msg.clear();
}
const char* BrainfunkException::what() const throw()
{
return this->msg.c_str();
}
Brainfunk::Brainfunk(size_t memsize)
{
this->ptr = 0;
try
{
this->memory.resize(memsize);
std::fill(this->memory.begin(), this->memory.end(), 0);
this->bitcode.reserve(memsize);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
}
void Brainfunk::clear()
{
std::fill(this->memory.begin(), this->memory.end(), 0);
this->ptr = 0;
this->bitcode.clear();
}
Brainfunk::~Brainfunk()
{
this->memory.clear();
this->bitcode.clear();
}
ssize_t count_continus(const string_view &text, const string &symbolset)
{
size_t i=0;
ssize_t ctr=0;
assert(symbolset.length() == 2);
while(i < text.length())
{
if(text[i] == symbolset[0])
ctr++;
else if(text[i] == symbolset[1])
ctr--;
i++;
}
return ctr;
}
size_t find_continus(const string_view &text, const string &symbolset, ssize_t &value)
{
size_t i=0;
ssize_t ctr=0;
assert(symbolset.length() == 2);
while(i < text.length())
{
if(text[i] == symbolset[0])
ctr++;
else if(text[i] == symbolset[1])
ctr--;
else
break;
i++;
}
value = ctr;
return i;
}
void count_mul_offset(const string_view &text, vector<memory_t> &mul, vector<offset_t> &offset, size_t lastoffset)
{
mul.emplace_back(count_continus(text, "+-") % 256);
offset.emplace_back(count_continus(text, "><") + lastoffset);
return;
}
void Brainfunk::translate(const string &text)
{
vector<addr_t> stack; // Jump address stack
this->bitcode.clear();
string_view code = text;
if(count_continus(code, "[]") != 0)
{
throw BrainfunkException("Unmatched brackets");
}
addr_t last_pc = 0;
ssize_t value = 0;
while(code.length() > 0)
{
switch(code.front())
{
case '[':
if(auto m = ctre::starts_with<"^\\[(\\-([\\<\\>]+[\\+\\-]+)+[\\<\\>]+|([\\<\\>]+[\\+\\-]+)+[\\<\\>]+\\-)\\]">(code))
{
int mode = 0;
vector<memory_t> mul;
vector<offset_t> offset;
ssize_t pairs = 0;
string_view substr = m.to_view();
/* First we need to validate if it goes back to where it was */
if(count_continus(m.to_view(), "><") != 0)
{
goto bailout; // Not a valid mul-offset loop
}
/* Basically, the text will look either like:
*
* 1. [->>++++>>>>++++++++<<--<<<<]
*
* or
*
* 2. [>>+++++>>>>+++<<---<<<<-]
*/
if (substr[1] == '-')
{
substr.remove_prefix(2);
mode = 1;
}
else
{
substr.remove_prefix(1);
mode = 2;
}
while (auto m = ctre::starts_with<"^[\\>\\<]+[\\+\\-]+">(substr))
{
count_mul_offset(m.to_view(), mul, offset, offset.size() == 0 ? 0 : offset.back());
substr.remove_prefix(m.size());
}
/* Omit the last false pair in mode 2 */
if (mode == 2)
pairs = offset.size() - 1;
else
pairs = offset.size();
assert(pairs > 0);
for (int i = 0; i < pairs; i++)
{
bitcode.emplace_back(Bitcode(_OP_MUL, mul[i], offset[i]));
}
/* Insert a "S 0" to get correct behavior */
bitcode.emplace_back(Bitcode(_OP_S, (memory_t)0));
code.remove_prefix(m.size());
continue;
}
else if(auto m = ctre::starts_with<"^\\[[\\>\\<]+\\]">(code)) // F instruction
{
offset_t offset = count_continus(m.to_view(), "><");
bitcode.emplace_back(Bitcode(_OP_F, offset));
code.remove_prefix(m.size());
continue;
}
else if(auto m = ctre::starts_with<"^\\[[\\+\\-]+\\]">(code)) // S 0
{
auto v = count_continus(m.to_view(), "+-");
if(v % 2 != 1) // is even
{
goto bailout;
}
bitcode.emplace_back(Bitcode(_OP_S, (memory_t)0));
code.remove_prefix(m.size());
continue;
}
// No match, just a normal loop
bailout:
bitcode.emplace_back(Bitcode());
stack.emplace_back(bitcode.size() - 1);
code.remove_prefix(1);
continue;
case ']':
assert(stack.size() > 0);
last_pc = stack.back();
stack.pop_back();
bitcode.emplace_back(Bitcode(_OP_JN, (offset_t)(last_pc - bitcode.size())));
bitcode[last_pc] = Bitcode(_OP_JE, (offset_t)((bitcode.size() - 1) - last_pc));
code.remove_prefix(1);
continue;
case '+':
case '-':
code.remove_prefix(find_continus(code, "+-", value));
bitcode.emplace_back(Bitcode(_OP_A, (memory_t)value));
continue;
case '>':
case '<':
code.remove_prefix(find_continus(code, "><", value));
bitcode.emplace_back(Bitcode(_OP_M, (offset_t)value));
continue;
case '.':
bitcode.emplace_back(Bitcode(_OP_IO, (memory_t)_IO_OUT));
code.remove_prefix(1);
continue;
case ',':
bitcode.emplace_back(Bitcode(_OP_IO, (memory_t)_IO_IN));
code.remove_prefix(1);
continue;
default: // unrecognized characters
code.remove_prefix(1);
continue;
}
}
this->bitcode.emplace_back(Bitcode(_OP_H));
}
void Brainfunk::dump(ostream &os, enum formats format) const
{
if(format == FMT_C)
{
os << "#include <stdio.h>\n"
"#include <stdlib.h>\n"
"#include <stdint.h>\n"
"uint8_t *mem;\n"
"#define MEMSIZE (1<<21)\n"
"#define X(x) /* NOP */\n"
"#define A(x) *mem += x\n"
"#define S(x) *mem = x\n"
"#define MUL(offset, mul) mem[offset] += *mem * mul\n"
"#define F(x) while(*mem != 0) mem += x\n"
"#define M(x) mem += x\n"
"#define JE(x) while(*mem) {\n"
"#define JN(x) }\n"
"#define H() exit(0);\n"
"static inline void IO(uint8_t arg)\n"
"{\n"
" int c = 0;\n"
" switch(arg)\n"
" {\n"
" case 0: /* IN */ c = getchar(); *mem = c == EOF ? 0 : c; break;\n"
" case 1: /* OUT */ putchar(*mem); break;\n"
" }\n"
"}\n"
"int main(void)\n"
"{\n"
" setvbuf(stdin, NULL, _IONBF, 0);\n"
" setvbuf(stdout, NULL, _IONBF, 0);\n"
" mem = (uint8_t *)calloc(sizeof(uint8_t), MEMSIZE) + MEMSIZE/2;\n"
" if(!mem) { puts(\"Out of memory\"); exit(1); }\n\n";
}
for(addr_t pc = 0; pc < bitcode.size(); pc++)
{
if(format == FMT_BIT)
os << pc << ":\t" << this->bitcode[pc].to_string(format) << endl;
else if(format == FMT_C)
os << this->bitcode[pc].to_string(format) << endl;
}
if(format == FMT_C)
{
os << "}" << endl;
}
}
void Brainfunk::run(istream &is, ostream &os)
{
auto codeit = this->bitcode.begin();
std::fill(this->memory.begin(), this->memory.end(), 0);
if(this->bitcode.size() == 0)
return;
while(codeit->execute(this->memory, codeit, this->ptr, is, os));
}
// Bitcode class
Bitcode::Bitcode()
{
this->opcode = _OP_X; // generate invalid opcode by default
return;
}
Bitcode::Bitcode(uint8_t opcode, memory_t operand)
{
if(opcode_type[opcode] != 'I')
{
throw BrainfunkException("Invalid opcode for intermediate instruction");
}
this->opcode = opcode;
this->operand.byte = operand;
}
Bitcode::Bitcode(uint8_t opcode, offset_t operand)
{
if(opcode_type[opcode] != 'O')
{
throw BrainfunkException("Invalid opcode for offset instruction");
}
this->opcode = opcode;
this->operand.offset = operand;
}
Bitcode::Bitcode(uint8_t opcode, memory_t mul, offset_t offset)
{
if(opcode_type[opcode] != 'M')
{
throw BrainfunkException("Invalid opcode for MUL instruction");
}
this->opcode = opcode;
this->operand.dual.mul = mul;
this->operand.dual.offset = offset;
}
Bitcode::Bitcode(uint8_t opcode) // for instructions with no operand
{
if(opcode_type[opcode] != 'N')
{
throw BrainfunkException("Invalid opcode for instruction with no operand");
}
this->opcode = opcode;
}
inline const string Bitcode::to_string(enum formats format) const
{
stringstream operand;
stringstream output;
string opcode_name = opname[this->opcode];
switch(opcode_type[this->opcode])
{
case 'N':
// No operand
operand << "";
break;
case 'O':
// Offset
operand << this->operand.offset;
break;
case 'M':
// Dual Operand
operand << this->operand.dual.offset << ", " << (unsigned short int)this->operand.dual.mul;
break;
case 'I':
// Intermediate byte
operand << (unsigned short int)this->operand.byte;
break;
default:
throw BrainfunkException("Invalid opcode operand type");
break;
}
switch(format)
{
case FMT_BIT:
output << opcode_name << "\t" << operand.str();
break;
case FMT_C:
output << opcode_name << "(" << operand.str() << ");";
break;
}
return output.str();
}
#define wrap(address, size) \
(((address) < (size)) ? (address) : (address) % (size))
inline bool Bitcode::execute(vector<memory_t> &memory, vector<Bitcode>::iterator &codeit, addr_t &ptr, istream &is, ostream &os) const
{
memory_t io_input = 0;
addr_t size = memory.size();
switch(opcode)
{
case _OP_X:
// No operand
throw BrainfunkException("Empty instruction");
break;
case _OP_A:
// Offset
memory[ptr] += operand.byte;
break;
case _OP_S:
// Intermediate byte
memory[ptr] = operand.byte;
break;
case _OP_MUL:
// Dual Operand
memory[wrap(ptr + operand.dual.offset, size)] += memory[ptr] * operand.dual.mul;
break;
case _OP_F:
// Offset
for(; memory[ptr] != 0; ptr = wrap(ptr + operand.offset, size));
break;
case _OP_M:
// Offset
ptr = wrap(ptr + operand.offset, size);
break;
case _OP_JE:
// Jump if equal to 0
if(memory[ptr] == 0)
codeit += operand.offset;
break;
case _OP_JN:
// Jump if not equal to 0
if(memory[ptr] != 0)
codeit += operand.offset;
break;
case _OP_IO:
// 0: Input, 1: Output
switch(operand.byte)
{
case 0:
// Input
is >> std::noskipws >> io_input;
if(is.eof())
io_input = 0;
memory[ptr] = io_input;
break;
case 1:
// Output
os << (char)memory[ptr] << flush;
break;
}
break;
case _OP_H:
// No operand, halt
return false;
break;
default:
throw BrainfunkException("Invalid opcode");
break;
}
codeit++;
return true;
}