Created
March 8, 2017 11:40
-
-
Save fergusq/3f1c0a07c21e98785abca2fa4827bde5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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