aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-03-14 22:43:12 +0100
committerAdrian Kummerlaender2015-03-14 22:43:12 +0100
commit73be8fb60adc4723421e73d38331f793f1d16886 (patch)
tree25d950e94f61dba21eac005329889467bc38288f
parentcb2a79cd818fbfa50e7bdebd090e681a0073f9d5 (diff)
Implemented basic tape log printout in _Turing_ example
* `machine::simulate` now returns a list of all tape states and corresponding head positions * rewrote some of the actual print logic in terms _TypeAsValue_
-rw-r--r--example/turing/src/machine.h34
-rw-r--r--example/turing/turing.cc53
2 files changed, 52 insertions, 35 deletions
diff --git a/example/turing/src/machine.h b/example/turing/src/machine.h
index a30c6e3..4a72d94 100644
--- a/example/turing/src/machine.h
+++ b/example/turing/src/machine.h
@@ -63,19 +63,22 @@ class simulate {
>::template get<Field>;
public:
- using type = tav::Eval<simulate<
- Transition,
- current_state<state::field::NEXT>,
- tape::writeSymbol<
- Position,
- current_state<state::field::WRITE>,
- Tape
- >,
- updatePosition<
- current_state<state::field::MOVE>,
- Position
- >
- >>;
+ using type = tav::Cons<
+ tav::Pair<Position, Tape>,
+ tav::Eval<simulate<
+ Transition,
+ current_state<state::field::NEXT>,
+ tape::writeSymbol<
+ Position,
+ current_state<state::field::WRITE>,
+ Tape
+ >,
+ updatePosition<
+ current_state<state::field::MOVE>,
+ Position
+ >
+ >>
+ >;
};
template <
@@ -84,7 +87,10 @@ template <
typename Position
>
struct simulate<Transition, void, Tape, Position> {
- typedef Tape type;
+ using type = tav::Cons<
+ tav::Pair<Position, Tape>,
+ void
+ >;
};
// (define (run program state tape position)
diff --git a/example/turing/turing.cc b/example/turing/turing.cc
index 070bc16..f015e02 100644
--- a/example/turing/turing.cc
+++ b/example/turing/turing.cc
@@ -47,66 +47,77 @@ using binary_increment = tav::List<
tav::List<tav::Size<2>, tav::Boolean<true>, tav::Boolean<true>, tav::Size<2>, tav::Char<'R'>>
>;
-template <
- typename Initial,
- typename Final
->
-void printStates(const std::string& name) {
- const auto printField = [](const auto x) {
- std::cout << x << ' ';
- };
+const auto printField = [](const auto x) {
+ std::cout << x;
+};
- std::cout << name
- << std::endl
- << " Initial: ";
+template <typename States>
+void printStates() {
+ using position = tav::Car<tav::Head<States>>;
+ using tape = tav::Cdr<tav::Head<States>>;
+ using prefix = tav::MakeList<tav::Size<3>, tav::Char<' '>>;
- tav::runtime::for_each<Initial>(printField);
+ tav::runtime::for_each<tav::Concatenate<prefix, tape>>(printField);
- std::cout << std::endl
- << " Final: ";
+ std::cout << std::endl;
- tav::runtime::for_each<Final>(printField);
+ tav::runtime::for_each<tav::Concatenate<
+ prefix,
+ tav::MakeList<position, tav::Char<' '>>,
+ tav::List<tav::Char<'^'>>
+ >>(printField);
std::cout << std::endl;
+
+ printStates<tav::Tail<States>>();
}
+template <>
+void printStates<void>() { }
+
int main(int, char **) {
// (define mirror_tape (list 1 1 1 0 0 0 0))
using mirror_tape = tav::ListOfType<int, 1, 1, 1, 0, 0, 0, 0>;
+ std::cout << "1. Mirror"
+ << std::endl;
+
printStates<
- mirror_tape,
machine::run<
mirror,
tav::Size<1>,
mirror_tape,
tav::Size<0>
>
- >("1. Mirror");
+ >();
// (define busy_beaver_tape (make-list 13 0))
using busy_beaver_tape = tav::MakeList<tav::Size<13>, tav::Int<0>>;
+ std::cout << "2. Busy Beaver"
+ << std::endl;
+
printStates<
- busy_beaver_tape,
machine::run<
busy_beaver,
tav::Char<'A'>,
busy_beaver_tape,
tav::Size<6>
>
- >("2. Busy Beaver");
+ >();
// (define binary_increment_tape (list #t #f #t #f #t #f))
using binary_increment_tape = tav::ListOfType<bool, 1, 0, 1, 0, 1, 0>;
+ std::cout << "3. Binary Increment"
+ << std::endl;
+
printStates<
- binary_increment_tape,
machine::run<
binary_increment,
tav::Size<0>,
binary_increment_tape,
tav::Size<0>
>
- >("3. Binary Increment");
+ >();
}