blib/lib/Language/SNUSP.pm

filter: Statementstmt 21.0% 31 / 147 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 14.8% 4 / 27 Podpod 0.0% 0 / 7 totaltotal 12.5% 35 / 278 CC 44 SCAR 43.7
CC
44
Cov
13%
run_debug
242.5
get_options
90.5
run
42.0
CRAP 79.0
↑ Summary
1 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Language::SNUSP;
3 2 our $VERSION = '0.0.15';
4  
5 2 my $input = ''; # SNUSP input
6 2 my $code = ''; # 2D code matrix
7 2 my $width = 1; # 2D code width
8 2 my $pos = 0; # 2D code execution pointer
9 2 my $max = 0; # Maximum pos value (length of code)
10 2 my $dir = 1; # Execution direction:
11 # 1=right -1=left $width=down -$width=up
12 2 my @args = (); # Program input list
13 2 my @data = (0); # Data slots
14 2 my $index = 0; # Data slot index
15 2 my @stack = (); # Subroutine call stack
16 2 my $count = 0; # Execution counter
17  
18 # I/O handlers
19 0 ▶my $put = sub { print shift };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 0 ▶my $get = sub { substr shift(@args), 0, 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21  
22 # SNUSP opcode handler lookup table.
23 my %ops = (
24 0 ▶ '>' => sub { $data[++$index] ||= 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$data[++$index] ||= 0
l!l
00
MC/DC0%0/1
$data[++$index] ||= 0
$data[++$index]
Truth table0%0/2
A
A= $data[++$index]
Aresult
11
00
25 0 ▶ '<' => sub { --$index >= 0 or $dir = 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
--$index >= 0 or $dir = 0 0 0 0
26 0 ▶ '+' => sub { ++$data[$index] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 ▶ '-' => sub { --$data[$index] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 ▶ ',' => sub { $data[$index] = ord $get->() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 ▶ '.' => sub { $put->(chr $data[$index]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 ▶ '/' => sub { $dir = -$width / $dir },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 ▶ '\\' => sub { $dir = $width / $dir },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 ▶ '!' => sub { $pos += $dir },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ '?' => sub { $pos += $dir if $data[$index] == 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $data[$index] == 0 0 0 0
34 0 ▶ '@' => sub { push @stack, [ $pos + $dir, $dir ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 ▶ '#' => sub { @stack ? ($pos, $dir) = @{pop @stack} : $dir = 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@stack ? : 0 0 0
36 0 ▶ "\n" => sub { $dir = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 2 );
38  
39 # Runtime flags
40 2 my $file; # Input SNUSP file
41 2 my $trace = 0; # Run with trace execution
42 2 my $debug = 0; # Run with 2D Curses debugger
43  
44 0 ▶sub run {
Subroutine run: not called CC=6 SCAR=37.4
Pod undocumented
45 0 my ($class, @args) = @_;
46 0 $class->get_options(@args);
47  
48 0 ▶ open my $fh, '<', $file or die "Can't open '$file' for input.\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't open '${file}' for input.\n" 0 0 0
49 0 $input = do { local $/; <$fh> };
50 0 close $fh;
51  
52 0 for ($input =~ /^.*\n/gm) {
53 0 $code .= $_;
54 0 ▶ $width = length if length > $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $width 0 0 0
55 }
56 0 $code =~ s/^.*/$& . ' ' x ($width - length $&) . "\n"/gem;
57 0 $max = length($code) - 1;
58 0 $width += 2;
59 0 $pos = $code =~ /\$/ * $-[0];
60  
61 0 ▶ $trace ? run_trace() :
Branch: 2 branches
BranchTrueFalseTotal
$trace ? : 0 0 0
$debug ? : 0 0 0
62 $debug ? run_debug() :
63 run_normal();
64  
65 0 exit $data[$index];
66 }
67  
68 0 ▶sub run_normal {
Subroutine run_normal: not called CC=5 SCAR=34.0
Pod undocumented
69 0 ▶ while ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
70 0 ▶ if (my $op = $ops{substr $code, $pos, 1}) { &$op }
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = $ops{substr $code, $pos, 1}) 0 0 0
71 0 $pos += $dir;
72 0 ▶ last if $pos < 0 or $pos > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos > $max 0 0 0
Condition0%0/3
$pos < 0 or $pos > $max
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos > $max
$pos < 0 $pos > $max
Truth table0%0/3
A or B
A= $pos < 0B= $pos > $max
ABresult
1X1
011
000
73 }
74 }
75  
76 0 ▶sub run_trace {
Subroutine run_trace: not called CC=6 SCAR=37.4
Pod undocumented
77 0 ▶ while ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
78 0 my $char = substr $code, $pos, 1;
79 0 $count++;
80 0 print trace_line() . "\n";
81 0 ▶ if (my $op = $ops{$char}) { &$op }
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = $ops{$char}) 0 0 0
82 0 $pos += $dir;
83 0 ▶ last if $pos < 0 or $pos > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos > $max 0 0 0
Condition0%0/3
$pos < 0 or $pos > $max
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos > $max
$pos < 0 $pos > $max
Truth table0%0/3
A or B
A= $pos < 0B= $pos > $max
ABresult
1X1
011
000
84 0 ▶ print "\n" if $char eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "." 0 0 0
85 }
86 }
87  
88 0 ▶sub run_debug {
Subroutine run_debug: not called CC=16 SCAR=54.9
Pod undocumented
89 0 require Curses; Curses->import;
90 0 require Term::ReadKey; Term::ReadKey->import;
91  
92 0 initscr();
93 0 ReadMode(3);
94  
95 0 my $y = 0;
96 0 addstr(
97 $y++, 0,
98 "(n)ext (SPACE)stop/start (+)faster (-)slower (q)uit",
99 );
100 0 my $top = ++$y;
101 0 ▶ addstr($y++, 0, $&) while $code =~ /.+/g;
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /.+/g and do { addstr($y++, 0, $&) } 0 0 0
102  
103 0 my $key = '';
104 0 my $sleep = 0.1;
105 0 my $pause = 1;
106  
107 0 my $out = '';
108 0 ▶ $put = sub { $out .= shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
109  
110 0 while(1) {
111 0 ▶ if ($dir and (not $pause or $key eq "n")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and !$pause || $key eq "n") 0 0 0
Condition0%0/6
$dir and !$pause || $key eq "n"
!ll&&!rl&&r
000
not $pause or $key eq "n"
l!l&&r!l&&!r
000
MC/DC0%0/3
$dir and !$pause || $key eq "n"
$dir not $pause $key eq "n"
Truth table0%0/4
A and not(B) or C
A= $dirB= not $pauseC= $key eq "n"
ABCresult
0XX0
1000
11X1
1011
112 0 $count++;
113 0 ▶ if (my $op = $ops{substr $code, $pos, 1}) { &$op }
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = $ops{substr $code, $pos, 1}) 0 0 0
114 0 ▶ last if $pos < 0 or $pos > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos > $max 0 0 0
Condition0%0/3
$pos < 0 or $pos > $max
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos > $max
$pos < 0 $pos > $max
Truth table0%0/3
A or B
A= $pos < 0B= $pos > $max
ABresult
1X1
011
000
115 0 $pos += $dir;
116 0 ▶ $pause = 1 if $dir == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dir == 0 0 0 0
117 }
118  
119 {
120 0 addstr($top - 1, 0, trace_line());
121 0 addstr($y, 0, $out);
122 0 clrtoeol();
123 0 move(int($pos / $width) + $top, $pos % $width);
124 0 refresh();
125 }
126  
127 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 0 ▶ $key = ReadKey($pause ? 0 : $sleep);
Branch: 1 branch
BranchTrueFalseTotal
$pause ? : 0 0 0
129 0 ▶ if ($key =~ /^[\+\=]$/) {$sleep -= 0.01 if $sleep > 0.011}
Branch: 6 branches
BranchTrueFalseTotal
if ($key =~ /^[\+\=]$/) { } 0 0 0
elsif ($key eq '-') { } 0 0 0
elsif ($key eq ' ') { } 0 0 0
elsif ($key eq 'n') { } 0 0 0
elsif ($key eq 'q') { } 0 0 0
if $sleep > "0.011" 0 0 0
130 0 elsif ($key eq '-') {$sleep += 0.01}
131 0 elsif ($key eq ' ') {$pause = not $pause}
132 0 elsif ($key eq 'n') {$pause = 1}
133 0 elsif ($key eq 'q') {last}
134 }
135 0 ReadMode(0);
136 0 endwin();
137 }
138  
139 0 ▶sub trace_line {
Subroutine trace_line: not called CC=2 SCAR=17.9
Pod undocumented
140 0 my $n = 0;
141 my $display = join '', map {
142 0 ▶ $n++ == $index ? "[$_] " : "$_ "
Branch: 1 branch
BranchTrueFalseTotal
$n++ == $index ? : 0 0 0
143 } @data;
144 0 return "$count) \@${\scalar @stack} < $display>";
145 }
146  
147 0 ▶sub get_options {
Subroutine get_options: not called CC=10 SCAR=45.1
Pod undocumented
148 0 my ($class, @options) = @_;
149  
150 0 for my $option (@options) {
151 0 ▶ if ($option =~ /^(-v|--version)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^(-v|--version)$/) 0 0 0
152 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 0 print qq!Language::SNUSP v${"VERSION"}!;
154 0 exit 0;
155 }
156 0 ▶ if ($option =~ /^(-\?|-h|--help)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^(-\?|-h|--help)$/) 0 0 0
157 0 die usage();
158 0 exit 0;
159 }
160 0 ▶ if ($option =~ /^(-d|--debug)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^(-d|--debug)$/) 0 0 0
161 0 $debug = 1;
162 0 next;
163 }
164 0 ▶ if ($option =~ /^(-t|--trace)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^(-t|--trace)$/) 0 0 0
165 0 $trace = 1;
166 0 next;
167 }
168 0 ▶ if ($option =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^-/) 0 0 0
169 0 die "Unknown option: '$option'\n\n" . usage();
170 }
171 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 0 0
172 0 push @args, $option;
173 0 next;
174 }
175 0 ▶ if (-f $option) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $option) { } 0 0 0
176 0 $file = $option;
177 }
178 else {
179 0 die "Input file '$option' does not exist.\n";
180 }
181 }
182 0 ▶ die usage() if not $file;
Branch: 1 branch
BranchTrueFalseTotal
if not $file 0 0 0
183 }
184  
185 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
186 0 <<'...';
187 Usage:
188 snusp [options] input_file.snusp
189  
190 Options:
191 -d, --debug # Run program in the visual debugger
192 -t, --trace # Run with trace on
193 -v, --version # Print version and exit
194 -h, --help # Print help and exit
195 ...
196 }
197  
198 2 1;
↑ Summary