for my $i (0 .. $#code) {
if ($code[$i]->[0] eq 'nop') {
local $code[$i]->[0] = 'jmp';
- interp(@code);
+ interp(\@code);
} elsif ($code[$i]->[0] eq 'jmp') {
local $code[$i]->[0] = 'nop';
- interp(@code);
+ interp(\@code);
}
}
sub interp {
- my @code = @_;
+ my ($code) = @_;
my $acc = 0;
my $pc = 0;
my %seen;
- while (!$seen{$pc} && $pc != @code) {
+ while (!$seen{$pc} && $pc != @$code) {
$seen{$pc} = 1;
- my ($ins, $arg) = @{ $code[$pc] };
- print "pc=$pc, acc=$acc, $ins $arg\n";
+ my ($ins, $arg) = @{ $code->[$pc] };
+ # print "pc=$pc, acc=$acc, $ins $arg\n";
if ($ins eq 'nop') {
$pc++;
} elsif ($ins eq 'acc') {
$pc += $arg;
}
}
- if ($pc == @code) {
+ if ($pc == @$code) {
print "terminating with acc=$acc.\n";
exit 0;
}
- print "\n";
+ # print "\n";
}