From 73be8fb60adc4723421e73d38331f793f1d16886 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 14 Mar 2015 22:43:12 +0100 Subject: 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_ --- example/turing/src/machine.h | 34 ++++++++++++++++------------ example/turing/turing.cc | 53 ++++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 35 deletions(-) (limited to 'example') 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; public: - using type = tav::Eval, - tape::writeSymbol< - Position, - current_state, - Tape - >, - updatePosition< - current_state, - Position - > - >>; + using type = tav::Cons< + tav::Pair, + tav::Eval, + tape::writeSymbol< + Position, + current_state, + Tape + >, + updatePosition< + current_state, + Position + > + >> + >; }; template < @@ -84,7 +87,10 @@ template < typename Position > struct simulate { - typedef Tape type; + using type = tav::Cons< + tav::Pair, + 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::Boolean, tav::Boolean, 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 +void printStates() { + using position = tav::Car>; + using tape = tav::Cdr>; + using prefix = tav::MakeList, tav::Char<' '>>; - tav::runtime::for_each(printField); + tav::runtime::for_each>(printField); - std::cout << std::endl - << " Final: "; + std::cout << std::endl; - tav::runtime::for_each(printField); + tav::runtime::for_each>, + tav::List> + >>(printField); std::cout << std::endl; + + printStates>(); } +template <> +void printStates() { } + int main(int, char **) { // (define mirror_tape (list 1 1 1 0 0 0 0)) using mirror_tape = tav::ListOfType; + 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::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; + 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"); + >(); } -- cgit v1.2.3