Skip to content

Instantly share code, notes, and snippets.

@fergusq
Created March 8, 2017 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fergusq/3f1c0a07c21e98785abca2fa4827bde5 to your computer and use it in GitHub Desktop.
Save fergusq/3f1c0a07c21e98785abca2fa4827bde5 to your computer and use it in GitHub Desktop.
my %objs = ();
while (<>) {
s/#.*//;
s/\s//g;
unless (/^$/) {
die "syntax error" unless (/^[0-9]+<(\+|-|>|\[[0-9]+|]|\.)*$/);
my ($name, $code) = split(/</);
$objs{$name} = [0, $code];
}
}
sub interpret {
my ($objnum) = @_;
foreach (split(/(?!\d)/, $objs{"$objnum"}->[1])) {
if (/\+/) {
$objs{$objnum}->[0] += 1;
}
elsif (/-/) {
$objs{$objnum}->[0] -= 1;
}
elsif (/\./) {
print $objs{$objnum}->[0], "\n";
$objs{$objnum}->[0] = 0;
}
elsif (/]/) {
interpret($objs{$objnum}->[0]);
}
elsif (/\[\d+/) {
interpret(substr($_, 1));
}
}
}
interpret 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment