Skip to content

Instantly share code, notes, and snippets.

@sinkovsky
Created March 26, 2013 17:16
Show Gist options
  • Save sinkovsky/5247251 to your computer and use it in GitHub Desktop.
Save sinkovsky/5247251 to your computer and use it in GitHub Desktop.
!perl
use strict;
use warnings;
my $ptr = 0;
my @tape = (0) x 5000;
my $op_idx = 0;
my @cmds;
my $ops = {
'>' => sub { $ptr++; },
'<' => sub { $ptr--; },
'+' => sub { $tape[$ptr]++; },
'-' => sub { $tape[$ptr]--; },
'.' => sub { print chr($tape[$ptr]) },
',' => sub {},
'[' => sub { if ($tape[$ptr] == 0) { $op_idx++ while ($cmds[$op_idx] ne ']') } },
']' => sub { if ($tape[$ptr] != 0) { $op_idx-- while ($cmds[$op_idx] ne '[') } }
};
my $input;
while (<>) {
chomp;
$input .= $_;
}
@cmds = split //, $input;
use Data::Dumper;
print Dumper(scalar @cmds);
while ($op_idx != scalar @cmds - 1) {
my $op = $cmds[$op_idx];
$ops->{$op}->();
$op_idx++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment