diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-07-04 21:53:26 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-07-04 21:53:26 +0100 |
| commit | d89a71d3440d66adbd6f5a5ecdef74aab131467c (patch) | |
| tree | 0565608575cb1e53878b7707d303870bb5621091 | |
| parent | b4cb8b76e00609e11d65ae8e3eb108a922b81697 (diff) | |
| download | perlweeklychallenge-club-d89a71d3440d66adbd6f5a5ecdef74aab131467c.tar.gz perlweeklychallenge-club-d89a71d3440d66adbd6f5a5ecdef74aab131467c.tar.bz2 perlweeklychallenge-club-d89a71d3440d66adbd6f5a5ecdef74aab131467c.zip | |
x
| -rw-r--r-- | challenge-119/james-smith/README.md | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md index 84f389fefc..d8bbf62fde 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 [cmd %s - line %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}%}; + ((@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" + die "\n## Unknown command [cmd $cmd - line ",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 |
