aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-03 13:33:33 +0100
committerGitHub <noreply@github.com>2021-07-03 13:33:33 +0100
commit5d73680dd18c4cba87c1d16a24c93f8f8062e847 (patch)
tree5e3a120858b173fa47b831746e338fd87f5f6a0f
parentce74b51ac90c134d77cdfd57a6115adf105662ec (diff)
parent40a6be70e1e1a6f86920fd0a0c30dbf1aaca8e6d (diff)
downloadperlweeklychallenge-club-5d73680dd18c4cba87c1d16a24c93f8f8062e847.tar.gz
perlweeklychallenge-club-5d73680dd18c4cba87c1d16a24c93f8f8062e847.tar.bz2
perlweeklychallenge-club-5d73680dd18c4cba87c1d16a24c93f8f8062e847.zip
Merge pull request #4399 from drbaggy/master
Added some more CESIL notes and a Perl CESIL interpreter...!
-rw-r--r--challenge-119/james-smith/README.md36
-rw-r--r--challenge-119/james-smith/cesil/cesil.pl32
-rw-r--r--challenge-119/james-smith/cesil/ch-1-extendeded.ces62
3 files changed, 130 insertions, 0 deletions
diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md
index 929869cabc..8747a9bc7c 100644
--- a/challenge-119/james-smith/README.md
+++ b/challenge-119/james-smith/README.md
@@ -89,6 +89,8 @@ with % in it}.
CESIL was designed to teach "machine-code" to Computer science students - so other than the "I/O" commands everything else was at a basic operation level.
+**Note:** Interestingly in just 26 lines of code - all 14 of the instructions are used...!
+
```
LINE
Next IN
@@ -129,6 +131,40 @@ End LINE
54
-1
```
+### Side note... an intepreter for CESIL...
+
+Didn't like the idea of relying on JAVA... so here is a bare bones
+interpreter...
+```perl
+use strict;
+
+$| = 1;
+my( $ptr, @in, %mem, @code, %ptrs, $reg ) = 0;
+
+my %commands = (
+'LINE' ,sub{print "\n"; },
+'IN' ,sub{die '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},
+);
+
+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 ];
+}
+($commands{$code[$ptr][0]}($code[$ptr][1]),$ptr++) for 1..1e6;
+```
# Task 2 - Sequence without 1-on-1
***Write a script to generate sequence starting at 1. Consider the increasing
diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl
new file mode 100644
index 0000000000..bc1f9613aa
--- /dev/null
+++ b/challenge-119/james-smith/cesil/cesil.pl
@@ -0,0 +1,32 @@
+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;
+
+
diff --git a/challenge-119/james-smith/cesil/ch-1-extendeded.ces b/challenge-119/james-smith/cesil/ch-1-extendeded.ces
new file mode 100644
index 0000000000..f011dad073
--- /dev/null
+++ b/challenge-119/james-smith/cesil/ch-1-extendeded.ces
@@ -0,0 +1,62 @@
+ LINE
+ IN
+ STORE success
+ STORE tests
+Next IN
+ JINEG End
+ OUT
+ PRINT " => "
+ STORE a
+ IN
+ STORE c
+ LOAD a
+ DIVIDE 16
+ STORE b
+ MULTIPLY -16
+ ADD a
+ MULTIPLY 16
+ ADD b
+ OUT
+ PRINT " : "
+ SUBTRACT c
+ JIZERO Ok
+ PRINT "-- should be "
+ LOAD c
+ OUT
+ PRINT "?"
+ JUMP Line
+Ok PRINT "OK"
+ LOAD success
+ ADD 1
+ STORE success
+Line LINE
+ LOAD tests
+ ADD 1
+ STORE tests
+ JUMP Next
+End LINE
+ PRINT "TESTS: "
+ LOAD success
+ OUT
+ PRINT " of "
+ LOAD tests
+ OUT
+ PRINT " passed"
+ LINE
+ LINE
+ HALT
+ %
+ 0
+ 240
+ 15
+ 15
+ 240
+ 0
+ 0
+ 255
+ 255
+ 99
+ 54
+ 99
+ 2
+ -1