diff options
| author | James Smith <baggy@baggy.me.uk> | 2021-07-03 13:04:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-03 13:04:08 +0100 |
| commit | ab738c29fa72f10f66ebd3fdc46a28ac88fa2e77 (patch) | |
| tree | 53a3c065eda85ed790a4aef318384699a2e324a4 | |
| parent | 5e37fb5eb893b70de52be7da6e8bfc1d402610d4 (diff) | |
| download | perlweeklychallenge-club-ab738c29fa72f10f66ebd3fdc46a28ac88fa2e77.tar.gz perlweeklychallenge-club-ab738c29fa72f10f66ebd3fdc46a28ac88fa2e77.tar.bz2 perlweeklychallenge-club-ab738c29fa72f10f66ebd3fdc46a28ac88fa2e77.zip | |
Update README.md
| -rw-r--r-- | challenge-119/james-smith/README.md | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md index 7323a629a3..9937a6296d 100644 --- a/challenge-119/james-smith/README.md +++ b/challenge-119/james-smith/README.md @@ -131,6 +131,42 @@ End LINE 54 -1 ``` +### Side note... an intepreter for CESIL... + +Didn't like the idea of relying on JAVASCRIPT... so here is a bare bones +interpreter... +```perl +use strict; + +$| = 1; +my( $ptr, @in, %mem, @code, %ptrs, $reg ) = 0; + +while(<>) { + (@in = map { 0+$_ } <> ) && last if m{^ {8}%}; + ($ptrs{$1},$_)=(scalar @code,$2) if m{^(\S{1,7})\s+(.*)}; + push @code, [ split m{\s+}, s{^\s+}{}r =~ s{\s+$}{}r, 2 ]; +} + +my %commands = ( + 'LINE' => sub { print "\n"; }, + 'IN' => sub { die "RUN OUT OF INPUT" unless @in; $reg = shift @in; }, + 'OUT' => sub { print $reg }, + 'PRINT' => sub { print $_[0] =~ s{^"}{}r =~ s{"$}{}r; }, + 'STORE' => sub { $mem{$_[0]} = $reg; }, + 'LOAD' => sub { $reg = $mem{$_[0]}; }, + 'HALT' => sub { exit; }, + 'JINEG' => sub { $ptr = $ptrs{$_[0]} - 1 if $reg < 0; }, + 'JIZERO' => sub { $ptr = $ptrs{$_[0]} - 1 if $reg == 0; }, + 'JUMP' => sub { $ptr = $ptrs{$_[0]} - 1; }, + 'ADD' => sub { $reg += ( $_[0]=~m{^-?\d+$} ? $_[0] : $mem{$_[0]} ) }, + 'SUBTRACT' => sub { $reg -= ( $_[0]=~m{^-?\d+$} ? $_[0] : $mem{$_[0]} ) }, + 'MULTIPLY' => sub { $reg *= ( $_[0]=~m{^-?\d+$} ? $_[0] : $mem{$_[0]} ) }, + 'DIVIDE' => sub { $reg /= ( $_[0]=~m{^-?\d+$} ? $_[0] : $mem{$_[0]} ) ; $reg = int $reg; }, +); + +## Execution loop... +(&{ $commands{$code[$ptr][0]} }( $code[$ptr][1] ),$ptr++) foreach 1..1e6; +``` # Task 2 - Sequence without 1-on-1 ***Write a script to generate sequence starting at 1. Consider the increasing |
