Acme-Turing
view release on metacpan or search on metacpan
my ($hkey, $hentry) = @_;
Carp::croak("No entry defined") unless defined($hentry);
$self->{'spec'}{$hkey} = $hentry;
return;
}
# Initialize the tape.
sub init_tape {
my $self = shift;
my ($startpos, @symbols) = @_;
my @Tape = @{$self->{'tape'}};
Carp::croak("Start position $startpos is not on tape")
if $startpos < 0 || $startpos > $#Tape;
my $i;
for ($i = 0; $i < @symbols ; $i++) {
$self->{'tape'}[$startpos + $i] = $symbols[$i];
}
return;
}
# Step the machine to the next state. The next state is returned.
sub step {
my $self = shift;
# $ps = previous state. $tp = tape position. $ts = tape symbol.
my $ps = $self->{'cur_state'};
my $tp = $self->{'tape_pos'};
=head1 NAME
Acme::Turing - Turing machine emulation
=head1 SYNOPSIS
use Acme::Turing;
$machine = Acme::Turing->new(steps=>$steps);
$machine->add_spec($conditions, $actions);
$machine->init_tape($startpos, LIST...);
$machine->step();
$machine->print_tape($L, $R);
$machine->run();
=head1 DESCRIPTION
This module gives you the methods needed to emulate a Turing machine
in Perl.
Why? Because we can.
'PX, L:5' Write 'X', move tape backward, enter state 5
'L, P1:Q' Move tape backward, write '1', enter state Q
':STOP' Do not write, don't move the tape, go to STOP
There are two reserved states: START and STOP. The machine always
begins in state START and stops when it enters state STOP.
=item B<init_tape> STARTPOS LIST
Writes symbols to the tape. You may use this method to initialize
the tape before starting the machine. STARTPOS is the position of
the tape to start writing in; there may be any number of symbols
after that.
=item B<step>
After the machine has been specified, this method will execute
one instruction (one state transition) on the machine.
It returns the resulting state of the machine, which is also stored
in C<$machine-E<gt>{cur_state}>.
=item B<run> L R
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
################## We start with some black magic to print on failure.
BEGIN { $| = 1; print "1..4\n"; }
END {print "not ok 1\n" unless $loaded;}
use Acme::Turing;
$loaded = 1;
print "ok 1\n";
################## End of black magic.
my $failed = 0;
( run in 0.268 second using v1.01-cache-2.11-cpan-0d8aa00de5b )