diff options
| -rw-r--r-- | challenge-119/james-smith/README.md | 40 | ||||
| -rw-r--r-- | challenge-119/james-smith/cesil/cesil.pl | 38 |
2 files changed, 32 insertions, 46 deletions
diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md index 84f389fefc..11f1a24015 100644 --- a/challenge-119/james-smith/README.md +++ b/challenge-119/james-smith/README.md @@ -138,40 +138,35 @@ interpreter... This uses a dispatch table to execute the commands - a list of "anonymous" subroutines stored in a hash. ```perl - use strict; use warnings; -$| = 1; # Set auto-flush... ## Initialize state -my($MAX_LOOPS, $ptr, $reg, @in, %mem, @code, %ptrs) = (1e6,0,0); +my($MAX,$ptr,$reg,@in,%mem,@code,%ptrs)=(1e6,0,0); ## Define error messages my %messages = ( - 'i' => 'No further input', - 'd' => 'Division by zero ', - 'm' => 'Unitialized memory at ', - 'l' => 'Unknown pointer ', -); +'i','No further input','d','Division by zero ', +'l','Unknown pointer ','m','Unitialized memory at '); ## Support functions -sub _err { die sprintf "\n** %s%s [cmd %s - line %d]\n", - $messages{$_[0]}, $code[$ptr][1], $code[$ptr][0], 1+$ptr; } -sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_err 'l'; } -sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_err 'm'; } +sub _e { die sprintf "\n** %s%s [%s @ %d]\n", + $messages{$_[0]},@{$code[$ptr]}[1,0],1+$ptr} +sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_e 'l'} +sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_e 'm'} ## Command dispatch table my %commands = ( 'LINE' ,sub{print "\n"}, 'OUT' ,sub{print $reg}, 'PRINT' ,sub{print s/^"//r=~s/"$//r}, -'IN' ,sub{@in?($reg=shift@in):_err 'i'}, +'IN' ,sub{@in?($reg=shift@in):_e 'i'}, 'STORE' ,sub{$mem{$_}=$reg}, 'LOAD' ,sub{$reg=_v}, 'ADD' ,sub{$reg+=_v}, 'SUBTRACT',sub{$reg-=_v}, 'MULTIPLY',sub{$reg*=_v}, -'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_err 'd'}, +'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_e 'd'}, 'JINEG' ,sub{_j if $reg<0}, 'JIZERO' ,sub{_j if !$reg}, 'JUMP' ,sub{_j}, @@ -180,20 +175,17 @@ my %commands = ( ## Parser loop while(<>) { - ((@in = map {/^\s+-?\d+\s*$/?0+$_:()} <> ),last) if m{^ {8}%}; - ($ptrs{$1},$_)=(scalar @code,$2) if m{^(\S{1,7})\s+(.*)}; - my($cmd,$data) = split m{\s+}, s{^\s+}{}r=~s{\s+$}{}r, 2; - die "\n** Unknown command [cmd $cmd - line ",1+@code,"]\n" + ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/; + ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/; + my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2; + die "\n## Unknown command [$cmd @ ",1+@code,"]\n" unless exists $commands{$cmd}; - push @code, [$cmd,$data||'']; + push @code, [$cmd,$data//'']; } - ## Execution loop ($commands{$code[$ptr][0]}($_=$code[$ptr][1]),$ptr++) - while --$MAX_LOOPS && $ptr<@code; - -## Error if did not exit with HALT.. -die "\n** Exited without HALT\n" if $ptr >= @code; + while--$MAX&& $ptr<@code; +die "\n** Exited without HALT\n"; ``` # Task 2 - Sequence without 1-on-1 diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl index 25e79e8982..64b0355de7 100644 --- a/challenge-119/james-smith/cesil/cesil.pl +++ b/challenge-119/james-smith/cesil/cesil.pl @@ -1,37 +1,32 @@ use strict; use warnings; -# Set auto-flush... -$| = 1; ## Initialize state -my($MAX_LOOPS, $ptr, $reg, @in, %mem, @code, %ptrs) = (1e6,0,0); +my($MAX,$ptr,$reg,@in,%mem,@code,%ptrs)=(1e6,0,0); ## Define error messages my %messages = ( - 'i' => 'No further input', - 'd' => 'Division by zero ', - 'm' => 'Unitialized memory at ', - 'l' => 'Unknown pointer ', -); +'i','No further input','d','Division by zero ', +'l','Unknown pointer ','m','Unitialized memory at '); ## Support functions -sub _err { die sprintf "\n** %s%s [cmd %s - line %d]\n", - $messages{$_[0]}, $code[$ptr][1], $code[$ptr][0], 1+$ptr; } -sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_err 'l'; } -sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_err 'm'; } +sub _e { die sprintf "\n** %s%s [%s @ %d]\n", + $messages{$_[0]},@{$code[$ptr]}[1,0],1+$ptr} +sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_e 'l'} +sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_e 'm'} ## Command dispatch table my %commands = ( 'LINE' ,sub{print "\n"}, 'OUT' ,sub{print $reg}, 'PRINT' ,sub{print s/^"//r=~s/"$//r}, -'IN' ,sub{@in?($reg=shift@in):_err 'i'}, +'IN' ,sub{@in?($reg=shift@in):_e 'i'}, 'STORE' ,sub{$mem{$_}=$reg}, 'LOAD' ,sub{$reg=_v}, 'ADD' ,sub{$reg+=_v}, 'SUBTRACT',sub{$reg-=_v}, 'MULTIPLY',sub{$reg*=_v}, -'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_err 'd'}, +'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_e 'd'}, 'JINEG' ,sub{_j if $reg<0}, 'JIZERO' ,sub{_j if !$reg}, 'JUMP' ,sub{_j}, @@ -40,16 +35,15 @@ my %commands = ( ## Parser loop while(<>) { - ((@in = map {/^\s+-?\d+\s*$/?0+$_:()} <> ),last) if m{^ {8}%}; - ($ptrs{$1},$_)=(scalar @code,$2) if m{^(\S{1,7})\s+(.*)}; - my($cmd,$data) = split m{\s+}, s{^\s+}{}r=~s{\s+$}{}r, 2; - die "\n** Unknown command [cmd $cmd - line ",1+@code,"]\n" + ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/; + ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/; + my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2; + die "\n## Unknown command [$cmd @ ",1+@code,"]\n" unless exists $commands{$cmd}; - push @code, [$cmd,$data||'']; + push @code, [$cmd,$data//'']; } - ## Execution loop ($commands{$code[$ptr][0]}($_=$code[$ptr][1]),$ptr++) - while --$MAX_LOOPS && $ptr<@code; -die "\n** Exited without HALT\n" if $ptr >= @code; + while--$MAX&& $ptr<@code; +die "\n** Exited without HALT\n"; |
