From b4cb8b76e00609e11d65ae8e3eb108a922b81697 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sun, 4 Jul 2021 21:49:48 +0100 Subject: fix to cesil to make resolve a // vs || issue --- challenge-119/james-smith/cesil/cesil.pl | 34 +++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl index 25e79e8982..b9a06902e9 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 [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}, @@ -40,16 +35,15 @@ 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; -die "\n** Exited without HALT\n" if $ptr >= @code; + while--$MAX&& $ptr<@code; +die "\n** Exited without HALT\n"; -- cgit From d89a71d3440d66adbd6f5a5ecdef74aab131467c Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sun, 4 Jul 2021 21:53:26 +0100 Subject: x --- challenge-119/james-smith/README.md | 36 ++++++++++++++---------------------- 1 file 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 -- cgit From c54f9e34cbdc96cd2453d99a920c679384b23fc7 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sun, 4 Jul 2021 21:56:42 +0100 Subject: tidied --- challenge-119/james-smith/cesil/cesil.pl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl index b9a06902e9..64b0355de7 100644 --- a/challenge-119/james-smith/cesil/cesil.pl +++ b/challenge-119/james-smith/cesil/cesil.pl @@ -10,7 +10,7 @@ my %messages = ( 'l','Unknown pointer ','m','Unitialized memory at '); ## Support functions -sub _e { die sprintf "\n** %s%s [cmd %s - line %d]\n", +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'} @@ -35,10 +35,10 @@ 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//'']; } -- cgit From e9f9c49b72008b480b3715a447681a1a0d354432 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sun, 4 Jul 2021 21:57:25 +0100 Subject: tidied --- challenge-119/james-smith/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md index d8bbf62fde..11f1a24015 100644 --- a/challenge-119/james-smith/README.md +++ b/challenge-119/james-smith/README.md @@ -150,7 +150,7 @@ my %messages = ( 'l','Unknown pointer ','m','Unitialized memory at '); ## Support functions -sub _e { die sprintf "\n** %s%s [cmd %s - line %d]\n", +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'} @@ -175,10 +175,10 @@ 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//'']; } -- cgit