Skip to content

Commit 666cbb6

Browse files
committed
Turing tape printer.
1 parent 7dd5b7c commit 666cbb6

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

turing-machine/main.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1+
#include "common/string.hpp"
2+
#include "common/printer.hpp"
3+
14
#include "tape.hpp"
25
#include "machine.hpp"
36

47
using namespace meta::turing;
58

6-
template <int i> struct I {};
7-
8-
using set_cursor_1 = meta::partial<set_cursor, I<1>>;
9-
using set_cursor_0 = meta::partial<set_cursor, I<0>>;
10-
9+
//helper functions
10+
using set_cursor_1 = meta::partial<set_cursor, meta::chr<'1'>>;
11+
using set_cursor_0 = meta::partial<set_cursor, meta::chr<'0'>>;
1112
using is_nil = meta::partial<meta::is_same_l, meta::nil>;
1213

13-
struct q1;
14-
struct q2;
14+
int main() {
1515

16-
struct q1 {
17-
using transitions = meta::list<
18-
transition<is_nil, meta::list<set_cursor_1, rshift>, q2>>;
19-
};
16+
//prints sequence 1010101010....
17+
struct q1; struct q2;
2018

21-
struct q2 {
22-
using transitions = meta::list<
23-
transition<is_nil, meta::list<set_cursor_0, rshift>, q1>>;
24-
};
19+
struct q1 {
20+
using transitions = meta::list<
21+
transition<is_nil, meta::list<set_cursor_1, rshift>, q2>>;
22+
};
2523

26-
using test1 = eval<tape<>, q1, 10>;
24+
struct q2 {
25+
using transitions = meta::list<
26+
transition<is_nil, meta::list<set_cursor_0, rshift>, q1>>;
27+
};
2728

29+
using initial_tape = tape<>;
30+
using final_tape = eval<initial_tape, q1, 50>;
31+
tape_printer<final_tape>::print();
32+
33+
meta::newline_printer::print();
2834

29-
int main() {
3035
return 0;
3136
}

turing-machine/tape.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "common/list.hpp"
44
#include "common/lambda.hpp"
5+
#include "common/printer.hpp"
56

67
namespace meta {
78
namespace turing {
@@ -12,6 +13,8 @@ template <typename cursor = nil,
1213
typename default_cursor = nil>
1314
struct tape {
1415
using get = cursor;
16+
using r = right;
17+
using l = left;
1518

1619
using lshift = tape<peek<left, default_cursor>,
1720
pop<left>,
@@ -42,5 +45,20 @@ template <typename t>
4245
using _rshift = typename t::rshift;
4346
using rshift = lambda<_rshift>;
4447

48+
template <typename t>
49+
struct _rewind {
50+
using result = typename _rewind<typename t::lshift>::result;
51+
};
52+
53+
template <typename cursor, typename right>
54+
struct _rewind<tape<cursor, list<>, right>> {
55+
using result = tape<cursor, list<>, right>;
56+
};
57+
58+
CREATE_ALIAS(rewind);
59+
60+
template <typename t>
61+
using tape_printer = printer<typename rewind<t>::lshift::r>;
62+
4563
}
4664
}

0 commit comments

Comments
 (0)