Acme-Brainfuck

 view release on metacpan or  search on metacpan

lib/Acme/Brainfuck.pm  view on Meta::CPAN

our $p = 0;
our @m = (); 

# The basic Brainfuck instructions.  Extras will be added in import().
our $ops = '+-<>,.[]'; 

# Whether or not we accept extra instructions.
our $verbose = 0;

# print out filtered text?
our $debug = 0;

sub import()
{
    shift;
    foreach (@_)
    {
	if (/^verbose$/)
	{
	    $ops .= '~#';
	    $verbose = 1;
	}
	if (/^debug$/)
	{
	    $debug = 1;
	}
    }
}

FILTER_ONLY code => sub
{
    my $ret = $_;
    while ($ret =~ /\s ([\Q$ops\E]+) \s/gsx)
    {
	my $code = $1;

lib/Acme/Brainfuck.pm  view on Meta::CPAN

	{
	    $code =~
		s/~/\$Acme::Brainfuck::p = 0;\@Acme::Brainfuck::m = (); /g;
	    $code =~
		s/\#/print STDERR sprintf\('\$p = %d \$m[\$p]= %d', \$Acme::Brainfuck::p, P\), "\\n"; /g;
	}
	$code =~ s/P/\$Acme::Brainfuck::m\[\$Acme::Brainfuck::p\]/g;
	substr($ret, $at, $len, $code);
    }
    $_ = $ret;
    print $_ if $debug;
};

1;

__END__

=pod

=head1 NAME

lib/Acme/Brainfuck.pm  view on Meta::CPAN


=item # Peek

Prints the values of the memory pointer and the current memory cell to 
STDERR.  See also L</"Debugging"> below.

=back

=head2 Debugging

By using the I<debug> pragma like this:
 
use Acme::Brainfuck qw/debug/;

you can dump out the generated perl code.  (Caution: it is not pretty.)
The key to understanding it is that the memory pointer is represented by 
I<$p>, and the memory array by I<@m>  Therefore the  value of the current 
memory cell is I<$m[$p]>.

=head1 RETURN VALUE

Each sequence of Brainfuck instructions becomes a Perl block and returns the 
value of the current memory cell.



( run in 1.096 second using v1.01-cache-2.11-cpan-49f99fa48dc )