aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-07-04 21:49:48 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-07-04 21:49:48 +0100
commitb4cb8b76e00609e11d65ae8e3eb108a922b81697 (patch)
tree36a1fbf0038f07b7978fc50ae97ca05faccde710
parent8cab52b28709e2e730a973ed9d27068d01651a8e (diff)
downloadperlweeklychallenge-club-b4cb8b76e00609e11d65ae8e3eb108a922b81697.tar.gz
perlweeklychallenge-club-b4cb8b76e00609e11d65ae8e3eb108a922b81697.tar.bz2
perlweeklychallenge-club-b4cb8b76e00609e11d65ae8e3eb108a922b81697.zip
fix to cesil to make resolve a // vs || issue
-rw-r--r--challenge-119/james-smith/cesil/cesil.pl34
1 files 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";