-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainf_ck.h
503 lines (412 loc) · 12.8 KB
/
brainf_ck.h
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#ifndef __PHOENIX_INTRO_H__
#define __PHOENIX_INTRO_H__
//#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <iomanip>
#include <iterator>
#include <string>
#include <deque>
#include <vector>
#include <utility>
#include <boost/lexical_cast.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
using namespace boost::spirit::qi;
using namespace boost::phoenix;
using namespace std;
struct bf_print_range {
size_t start;
size_t end;
};
BOOST_FUSION_ADAPT_STRUCT(bf_print_range
, (size_t, start)
(size_t, end));
// a command string is a sequence of primitive brainf_ck tokens
typedef vector<char> bf_command_string;
/**
* @struct bf_command_sequence
* @brief make initializations and seeks constant time.
* often brainf_ck tokens are repeated many times when a value is initialized or when
* the turing machine seeks a cell which is somewhat far away from the current cell.
* bf_command_sequence offers a way to reduce the this from linear time to constant.
*/
struct bf_command_sequence {
bf_command_sequence() {}
bf_command_sequence(char c, int i)
: m_command(c)
, m_repetitions(i) {}
bf_command_sequence(const vector<char>& v)
: m_command(v[0])
, m_repetitions(v.size()) {}
char m_command;
int m_repetitions;
friend ostream& operator<<(
ostream& os,
const bf_command_sequence& bfcs
);
};
ostream& operator<<(ostream& os, const bf_command_sequence& bfcs);
/**
* @struct bf_clear_cell
* @brief optimized command type for brainf_ck sequence '[-]'.
*/
struct bf_clear_cell {};
ostream& operator<<(ostream& os, const bf_clear_cell& cc);
/**
* @struct bf_transfer_cell
* @brief optimized command type for brainf_ck sequences such as '[->>>+<<<]'.
*/
struct bf_transfer_cell {
bf_transfer_cell() {}
bf_transfer_cell(long x, int y)
: offset(x)
, quantity(y) {}
long offset;
int quantity;
};
ostream& operator<<(ostream& os, const bf_transfer_cell& tc);
/**
* @typedef bf_multi_transfer_cell
* @brief multiple cells can be transferred with the patterns like '[->+>+<<]'
*/
typedef vector<bf_transfer_cell> bf_multi_transfer_cell;
ostream& operator<<(ostream& os, const bf_multi_transfer_cell& tc);
/**
* @typedef bf_known_command
* @brief branching type for optimizable commands
*/
typedef boost::variant<
bf_clear_cell
, bf_transfer_cell
, bf_multi_transfer_cell
> bf_known_command;
/**
* @typedef bf_command_variant
* @brief from the point of view of the @turing_machine_visitor, these are atomic.
*/
typedef boost::variant<
bf_known_command
, bf_command_sequence
> bf_command_variant;
/**
* @struct bf_statement
* @brief statements consist of expressions enclosed in [].
*/
struct bf_statement;
/**
* @typedef bf_expression
* @brief the main non-terminal type
*/
typedef boost::variant<
boost::recursive_wrapper<bf_statement>
, std::vector<bf_command_variant>
, bf_print_range
> bf_expression;
struct bf_statement {
std::vector<bf_expression> stmt;
};
BOOST_FUSION_ADAPT_STRUCT(
bf_statement
, (std::vector<bf_expression>, stmt)
)
/**
* @struct turing_machine
* @brief interprets the stream of commands from the turing_machine_visitor.
*/
struct turing_machine {
typedef std::deque<char> turing_machine_tape;
typedef long cell_pointer;
turing_machine()
: m_data(30000, 0),
m_data_ptr(0) {}
void process_command_sequence(const bf_command_sequence& c);
void process_command(const bf_clear_cell&) {
m_data[m_data_ptr] = 0;
}
void process_command(const bf_transfer_cell& tc) {
char temp = m_data[m_data_ptr];
move_data_ptr(tc.offset);
alter_data(tc.quantity * temp);
move_data_ptr(-tc.offset);
}
bool is_zero() const { return (m_data[m_data_ptr] == 0); }
void alter_data(char a);
void move_data_ptr(long i);
void output_data() const;
void input_data();
void print_state() {
cerr << setw(3) << static_cast<int>(m_data[m_data_ptr]) << " | ";
}
void print_state(size_t pos) {
cerr << setw(3) << static_cast<int>(m_data[pos]) << " | ";
}
void print_state_range(size_t start, size_t end) {
for(size_t i = start ; i <= end ; ++i) {
if(i % 10 == 0)
cout << endl;
print_state(i);
}
cout << endl;
}
turing_machine_tape m_data;
cell_pointer m_data_ptr;
};
/**
* @struct turing_machine_visitor
* @brief visitor dispatching parsed commands.
* ======================
* this structure acts as a dispatch for different command types from the
* brainf*ck input.
*/
struct turing_machine_visitor : boost::static_visitor<> {
turing_machine_visitor(turing_machine& tm)
: m_tm(tm) {}
void operator()(const bf_clear_cell& cc) const {
m_tm.process_command(cc);
}
void operator()(const bf_multi_transfer_cell& mtc) const {
BOOST_FOREACH(bf_transfer_cell tc, mtc) {
m_tm.process_command(tc);
}
m_tm.process_command(bf_clear_cell());
}
void operator()(const bf_transfer_cell& tc) const {
m_tm.process_command(tc);
m_tm.process_command(bf_clear_cell());
}
void operator()(bf_command_sequence& cs) const {
m_tm.process_command_sequence(cs);
}
void operator()(const bf_known_command& kc) const {
boost::apply_visitor(*this, kc);
}
void operator()(const std::vector<bf_command_variant>& v) const {
BOOST_FOREACH(bf_command_variant cv, v) {
boost::apply_visitor(*this, cv);
}
}
void operator()(const bf_statement& s) const {
while(!m_tm.is_zero()) {
BOOST_FOREACH(bf_expression bf_e, s.stmt) {
boost::apply_visitor(*this, bf_e);
}
}
}
void operator()(const bf_print_range& pr) const {
m_tm.print_state_range(pr.start, pr.end);
}
turing_machine& m_tm;
};
/**
* @struct bf_grammar
* @brief parse the raw brainf_ck input.
* ==========
* the grammar parses the input into optimized structures so that we don't have to deal
* directly with the brainf*ck input.
*/
struct bf_grammar
: grammar<bf_command_string::iterator, vector<bf_expression>()> {
bf_grammar()
: bf_grammar::base_type(start) {
start = +expression;
expression %=
command_sequence
| statement
| print_range_phrase
;
statement %= lit('[') >> +expression >> lit(']');
command_sequence %= +command_group;
command_group %= known_command_sequence | command_token;
known_command_sequence %=
clear_cell
| transfer_cell
| multi_transfer_cell
;
command_token =
(
+increment
| +decrement
| +move_right
| +move_left
| +put_output
| +get_input
)
[_val = construct<bf_command_sequence>(boost::spirit::_1)]
;
clear_cell = boost::spirit::qi::string("[-]")[_val = construct<bf_clear_cell>()]
;
transfer_cell %=
transfer_cell_left
| transfer_cell_right
;
transfer_cell_right =
eps[_a = val(0), _b = val(0)] >>
open_bracket >>
decrement >>
+(move_right[_a += val(1)]) >>
+(
increment[_b += val(1)]
| decrement[_b -= val(1)]
) >>
repeat(_a)[move_left] >>
close_bracket[_val = construct<bf_transfer_cell>(_a, _b)]
;
transfer_cell_left =
eps[_a = val(0), _b = val(0)] >>
open_bracket >>
decrement >>
+(move_left[_a += val(1)]) >>
+(
increment[_b += val(1)]
| decrement[_b -= val(1)]
) >>
repeat(_a)[move_right] >>
close_bracket[_val = construct<bf_transfer_cell>(-_a, _b)]
;
multi_transfer_cell =
eps[_a = val(0)] >>
open_bracket >>
decrement >>
multi_transfer_cell_body
[
_a = at_c<0>(boost::spirit::_1)
, _val = at_c<1>(boost::spirit::_1)
] >>
multi_transfer_cell_terminate(_a)
;
multi_transfer_cell_body =
eps[_a = val(0)] >>
+(
multi_transfer_phrase(_a)[
_a = at_c<0>(boost::spirit::_1)
, push_back(
at_c<1>(_val)
, at_c<1>(boost::spirit::_1)
)
]
)[at_c<0>(_val) = _a]
;
multi_transfer_cell_terminate =
multi_transfer_cell_terminate_right(_r1)
| multi_transfer_cell_terminate_left(_r1)
;
multi_transfer_cell_terminate_right =
repeat(-_r1)[move_right] >>
close_bracket
;
multi_transfer_cell_terminate_left =
repeat(_r1)[move_left] >>
close_bracket
;
multi_transfer_phrase =
eps[_a = _r1, _b = val(0)] >>
multi_transfer_seek_phrase[_a += boost::spirit::_1] >>
multi_transfer_modify_phrase[
_b += boost::spirit::_1
, at_c<0>(_val) = _a
, at_c<1>(_val) = construct<bf_transfer_cell>(_a, _b)
]
;
multi_transfer_seek_phrase =
+(
move_right[_val += val(1)]
| move_left[_val -= val(1)]
)
;
multi_transfer_modify_phrase =
+(
increment[_val += val(1)]
| decrement[_val -= val(1)]
)
;
increment %= char_('+');
decrement %= char_('-');
move_right %= char_('>');
move_left %= char_('<');
open_bracket %= char_('[');
close_bracket %= char_(']');
put_output %= char_('.');
get_input %= char_(',');
print_range_phrase %=
print_range_token
>> lit('(') >> int_ >> lit(',') >> int_ >> lit(')');
print_range_token = lit('@');
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(statement);
BOOST_SPIRIT_DEBUG_NODE(command_sequence);
BOOST_SPIRIT_DEBUG_NODE(command_group);
BOOST_SPIRIT_DEBUG_NODE(command_token);
BOOST_SPIRIT_DEBUG_NODE(clear_cell);
BOOST_SPIRIT_DEBUG_NODE(transfer_cell);
BOOST_SPIRIT_DEBUG_NODE(transfer_cell_left);
BOOST_SPIRIT_DEBUG_NODE(transfer_cell_right);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_cell);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_cell_body);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_cell_terminate);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_cell_terminate_right);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_cell_terminate_left);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_phrase);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_seek_phrase);
BOOST_SPIRIT_DEBUG_NODE(multi_transfer_modify_phrase);
BOOST_SPIRIT_DEBUG_NODE(increment);
BOOST_SPIRIT_DEBUG_NODE(decrement);
BOOST_SPIRIT_DEBUG_NODE(move_decide);
BOOST_SPIRIT_DEBUG_NODE(move_right);
BOOST_SPIRIT_DEBUG_NODE(move_left);
BOOST_SPIRIT_DEBUG_NODE(get_input);
BOOST_SPIRIT_DEBUG_NODE(put_output);
}
rule<bf_command_string::iterator, vector<bf_expression>()> start;
rule<bf_command_string::iterator, bf_expression()> expression;
rule<bf_command_string::iterator, bf_statement()> statement;
rule<bf_command_string::iterator, vector<bf_command_variant>()> command_sequence;
rule<bf_command_string::iterator, bf_command_variant()> command_group;
rule<bf_command_string::iterator, bf_command_sequence()> command_token;
rule<bf_command_string::iterator, bf_known_command()> known_command_sequence;
rule<bf_command_string::iterator, bf_clear_cell()> clear_cell;
rule<bf_command_string::iterator, bf_transfer_cell()> transfer_cell;
rule<bf_command_string::iterator, bf_transfer_cell(), locals<long, int> > transfer_cell_left;
rule<bf_command_string::iterator, bf_transfer_cell(), locals<long, int> > transfer_cell_right;
rule<bf_command_string::iterator, bf_multi_transfer_cell(), locals<long> > multi_transfer_cell;
rule<
bf_command_string::iterator,
boost::tuple<long, bf_multi_transfer_cell>(),
locals<long>
> multi_transfer_cell_body;
rule<
bf_command_string::iterator,
void(long)
> multi_transfer_cell_terminate;
rule<
bf_command_string::iterator,
void(long)
> multi_transfer_cell_terminate_right;
rule<
bf_command_string::iterator,
void(long)
> multi_transfer_cell_terminate_left;
rule<
bf_command_string::iterator,
boost::tuple<long, bf_transfer_cell>(long),
locals<long, int>
> multi_transfer_phrase;
rule<bf_command_string::iterator, long(), locals<long> > multi_transfer_seek_phrase;
rule<bf_command_string::iterator, int(), locals<int> > multi_transfer_modify_phrase;
rule<bf_command_string::iterator, char()> increment;
rule<bf_command_string::iterator, char()> decrement;
rule<bf_command_string::iterator, char()> move_left;
rule<bf_command_string::iterator, char()> move_right;
rule<bf_command_string::iterator, char()> move_decide;
rule<bf_command_string::iterator, char()> open_bracket;
rule<bf_command_string::iterator, char()> close_bracket;
rule<bf_command_string::iterator, char()> get_input;
rule<bf_command_string::iterator, char()> put_output;
rule<bf_command_string::iterator, bf_print_range()> print_range_phrase;
rule<bf_command_string::iterator, void()> print_range_token;
};
#endif