blib/lib/Language/Nouse.pm

filter: Statementstmt 85.1% 172 / 202 Branchbran 69.2% 54 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 20 / 25 Podpod 76.9% 10 / 13 totaltotal 80.5% 256 / 318 CC 43 SCAR 22.2
CC
43
Cov
81%
step
20.4
load_assembly
12.0
get_assembly
11.4
CRAP 9.2
↑ Summary
1 package Language::Nouse;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.04';
7  
8 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
9 3 my $class = shift;
10 3 my $self = bless {}, $class;
11  
12 3 my $options = shift;
13 3 $self->clear();
14  
15 3 $self->{sub_get} = \&NOUSE_DEFAULT_get;
16 3 $self->{sub_put} = \&NOUSE_DEFAULT_put;
17  
18 3 return $self;
19 }
20  
21 8 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
22 8 my ($self) = @_;
23 8 $self->{ring} = [];
24 8 $self->{stack} = [];
25 8 $self->{ring_pointer} = 0;
26 }
27  
28 0 ▶sub set_get {
Subroutine set_get: not called CC=1 SCAR=6.9
Pod documented
29 0 my ($self, $new) = @_;
30 0 $self->{sub_get} = $new;
31 }
32  
33 1 ▶sub set_put {
Subroutine set_put: called CC=1 SCAR=0.0
Pod documented
34 1 my ($self, $new) = @_;
35 1 $self->{sub_put} = $new;
36 }
37  
38 0 ▶sub NOUSE_DEFAULT_get {
Subroutine NOUSE_DEFAULT_get: not called CC=1 SCAR=6.9
Pod undocumented
39 0 return getc;
40 }
41  
42 0 ▶sub NOUSE_DEFAULT_put {
Subroutine NOUSE_DEFAULT_put: not called CC=1 SCAR=6.9
Pod undocumented
43 0 print $_[0];
44 }
45  
46 4 ▶sub load_linenoise {
Subroutine load_linenoise: called CC=4 SCAR=13.9
Pod documented
47 4 my ($self, $input) = @_;
48  
49 4 $self->_reset_ring();
50  
51 4 my $op = qr/[#:<>+?^]/;
52 4 my $mul = qr/[0-9a-z_]/;
53  
54 4 my $not_op = qr/[^#:<>+?^]/;
55 4 my $not_mul = qr/[^0-9a-z_]/;
56  
57 4 ▶ while ($input =~ m/$not_op*($op)$not_mul*($mul)/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /$not_op*($op)$not_mul*($mul)/g) 43 4 47
58  
59 43 my $this_op = $1;
60 43 my $this_mul = $2;
61  
62 43 $this_op =~ y/#:<>+?^/0123456/;
63  
64 43 ▶ $this_mul = 10 + (ord($this_mul) - ord('a')) if ($this_mul =~ m/[a-z]/);
Branch: 1 branch
BranchTrueFalseTotal
if $this_mul =~ /[a-z]/ 24 19 43
65 43 ▶ $this_mul = 36 if ($this_mul eq '_');
Branch: 1 branch
BranchTrueFalseTotal
if $this_mul eq "_" 0 43 43
66  
67 43 my $op_code = ($this_mul * 7) + $this_op;
68  
69 43 push @{$self->{ring}}, $op_code;
70 }
71  
72 }
73  
74 4 ▶sub load_assembly {
Subroutine load_assembly: called CC=11 SCAR=24.9
Pod documented
75 4 my ($self, $input) = @_;
76  
77 4 $self->_reset_ring();
78  
79 4 $input =~ s/#(.*?)(\r|\n|$)/$2/g;
80  
81 4 my @tokens = split /[,\n\r]/, $input;
82  
83 4 for my $token(@tokens){
84 16 $token =~ s/\s*(.*?)\s*/$1/;
85  
86 16 ▶ if ($token =~ m/(cut|paste|read|write|add|test|swap) (\d+)/i){
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /(cut|paste|read|write|add|test|swap) (\d+)/i) { } 15 1 16
elsif ($token =~ /(\d+)/) { } 0 1 1
87  
88 15 my $op = $1;
89 15 my $mul = $2;
90  
91 0 ▶ if ($op eq 'cut'){$op = 0;}
Branch: 7 branches
BranchTrueFalseTotal
if ($op eq 'cut') { } 0 15 15
elsif ($op eq 'paste') { } 0 15 15
elsif ($op eq 'read') { } 3 12 15
elsif ($op eq 'write') { } 3 9 12
elsif ($op eq 'add') { } 3 6 9
elsif ($op eq 'test') { } 3 3 6
elsif ($op eq 'swap') { } 3 0 3
92 0 elsif ($op eq 'paste'){$op = 1;}
93 3 elsif ($op eq 'read'){$op = 2;}
94 3 elsif ($op eq 'write'){$op = 3;}
95 3 elsif ($op eq 'add'){$op = 4;}
96 3 elsif ($op eq 'test'){$op = 5;}
97 3 elsif ($op eq 'swap'){$op = 6;}
98  
99 15 push @{$self->{ring}}, ($mul * 7) + $op;
100  
101 }elsif ($token =~ m/(\d+)/){
102  
103 0 push @{$self->{ring}}, $1+0;
104  
105 }
106 }
107 }
108  
109 36 ▶sub get_linenoise {
Subroutine get_linenoise: called CC=4 SCAR=13.9
Pod documented
110 36 my ($self) = @_;
111  
112 36 $self->_reset_ring();
113  
114 36 my $buffer = '';
115 36 for my $raw(@{$self->{ring}}){
116 943 my $op = $raw % 7;
117 943 my $mul = int($raw / 7);
118  
119 943 $op =~ y/0123456/#:<>+?^/;
120  
121 943 ▶ if ($mul == 36){
Branch: 2 branches
BranchTrueFalseTotal
if ($mul == 36) { } 0 943 943
elsif ($mul > 9) { } 656 287 943
122 0 $mul = '_';
123 }elsif ($mul > 9){
124 656 $mul = chr(ord('a') + ($mul - 10));
125 }
126  
127 943 $buffer .= $op.$mul;
128 }
129 36 return $buffer;
130 }
131  
132 5 ▶sub get_assembly {
Subroutine get_assembly: called CC=11 SCAR=24.4
Pod documented
133 5 my ($self, $per_line) = @_;
134  
135 5 $self->_reset_ring();
136  
137 5 ▶ $per_line = 4 unless defined $per_line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $per_line 5 0 5
138  
139 5 my @ops;
140  
141 5 for my $raw(@{$self->{ring}}){
142 25 my $op = $raw % 7;
143 25 my $mul = int($raw / 7);
144  
145 25 ▶ if ($op == 0){
Branch: 7 branches
BranchTrueFalseTotal
if ($op == 0) { } 0 25 25
elsif ($op == 1) { } 0 25 25
elsif ($op == 2) { } 5 20 25
elsif ($op == 3) { } 5 15 20
elsif ($op == 4) { } 5 10 15
elsif ($op == 5) { } 5 5 10
elsif ($op == 6) { } 5 0 5
146 0 $op = 'cut';
147 }elsif ($op == 1){
148 0 $op = 'paste';
149 }elsif ($op == 2){
150 5 $op = 'read';
151 }elsif ($op == 3){
152 5 $op = 'write';
153 }elsif ($op == 4){
154 5 $op = 'add';
155 }elsif ($op == 5){
156 5 $op = 'test';
157 }elsif ($op == 6){
158 5 $op = 'swap';
159 }
160  
161 25 push @ops, "$op $mul";
162 }
163  
164 5 my $buffer = '';
165 5 ▶ while (@ops){
Branch: 1 branch
BranchTrueFalseTotal
if (@ops) 10 5 15
166 10 $buffer .= join(', ', splice @ops, 0, $per_line)."\n";
167 }
168  
169 5 return $buffer;
170 }
171  
172 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
173 0 my ($self) = @_;
174  
175 0 ▶ while(scalar(@{$self->{ring}})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"ring"};}) 0 0 0
176 0 $self->step();
177 }
178 }
179  
180 30 ▶sub step {
Subroutine step: called CC=14 SCAR=30.1
Pod documented
181 30 my ($self) = @_;
182  
183 30 my ($op, $mul, $raw) = $self->_get_op();
184  
185 30 my $skip = scalar(@{$self->{stack}}) * $mul;
186 30 $skip++;
187  
188 30 ▶ if ($op == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 0) 3 27 30
189 # cut
190 3 $self->_skip($skip);
191 3 $self->_push($self->_get_oprand());
192 3 $self->_remove_op();
193 3 $skip--;
194 }
195  
196 30 ▶ if ($op == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 1) 1 29 30
197 # paste
198 1 $self->_skip($skip);
199 1 ▶ if (scalar(@{$self->{stack}})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'stack'};}) { } 1 0 1
200 1 $self->_insert_op($self->_pop());
201 }else{
202 0 $self->_insert_op($self->_get_oprand());
203 }
204 }
205  
206 30 ▶ if ($op == 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 2) 0 30 30
207 # read
208 0 my $in = &{$self->{sub_get}}();
209 0 ▶ if (defined $in){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in) 0 0 0
210 0 $self->_push(ord($in) % 256);
211 }
212 }
213  
214 30 ▶ if ($op == 3){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 3) 14 16 30
215 # write
216 14 ▶ if (scalar(@{$self->{stack}})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"stack"};}) 14 0 14
217 14 &{$self->{sub_put}}(chr($self->_peek()));
218 }
219 }
220  
221 30 ▶ if ($op == 4){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 4) 11 19 30
222 # add
223 11 ▶ if (scalar(@{$self->{stack}})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"stack"};}) 11 0 11
224 11 $self->_skip($skip);
225 11 my $oprand = $self->_get_oprand();
226 11 $oprand += $self->_pop();
227 11 $self->_push($oprand % 256);
228 }
229 }
230  
231 30 ▶ if ($op == 5){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 5) 0 30 30
232 # test
233 0 ▶ if (scalar(@{$self->{stack}})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"stack"};}) 0 0 0
234 0 $self->_skip($skip);
235 0 my $oprand = $self->_get_oprand();
236 0 ▶ if ($oprand == $self->_peek()){
Branch: 1 branch
BranchTrueFalseTotal
if ($oprand == $self->_peek) 0 0 0
237 0 $self->_pop();
238 }
239 }
240 }
241  
242 30 ▶ if ($op == 6){
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 6) 1 29 30
243 # swap
244 1 $self->_swap();
245 }
246  
247 30 $self->_skip($skip);
248 }
249  
250 45 ▶sub _skip {
Subroutine _skip: called CC=2 SCAR=6.9
251 45 my ($self, $by) = @_;
252 # skip the ring pointer along by $by places
253 45 $self->{ring_pointer} += $by;
254  
255 45 my $s = scalar(@{$self->{ring}});
256 45 ▶ if ($s == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 0) 1 44 45
257 1 $self->{ring_pointer} = 0;
258 1 return;
259 }
260  
261 44 $self->{ring_pointer} = $self->{ring_pointer} % $s;
262 }
263  
264 30 ▶sub _get_op {
Subroutine _get_op: called CC=1 SCAR=0.0
265 30 my ($self) = @_;
266  
267 30 my $raw = $self->_get_oprand();
268 30 my $op = $raw % 7;
269 30 my $mul = int($raw / 7);
270  
271 30 return ($op, $mul, $raw);
272 }
273  
274 44 ▶sub _get_oprand {
Subroutine _get_oprand: called CC=2 SCAR=7.0
275 44 my ($self) = @_;
276 44 ▶ die "ARGH: The ring is empty and you're asking for an oprand!" if !scalar(@{$self->{ring}});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"ring"};} 0 44 44
277 44 return $self->{ring}->[$self->{ring_pointer}];
278 }
279  
280 28 ▶sub _push {
Subroutine _push: called CC=1 SCAR=0.0
281 28 my ($self, $value) = @_;
282 28 push @{$self->{stack}}, $value;
283 }
284  
285 26 ▶sub _pop {
Subroutine _pop: called CC=1 SCAR=0.0
286 26 my ($self) = @_;
287 26 return pop @{$self->{stack}};
288 }
289  
290 14 ▶sub _peek {
Subroutine _peek: called CC=1 SCAR=0.0
291 14 my ($self) = @_;
292 14 my $data = $self->_pop();
293 14 $self->_push($data);
294 14 return $data;
295 }
296  
297 3 ▶sub _remove_op {
Subroutine _remove_op: called CC=1 SCAR=0.0
298 3 my ($self) = @_;
299 3 splice @{$self->{ring}}, $self->{ring_pointer}, 1;
300 }
301  
302 1 ▶sub _insert_op {
Subroutine _insert_op: called CC=1 SCAR=0.0
303 1 my ($self, $value) = @_;
304 1 splice @{$self->{ring}}, $self->{ring_pointer}, 0, ($value);
305 }
306  
307 49 ▶sub _reset_ring {
Subroutine _reset_ring: called CC=1 SCAR=0.0
308 49 my ($self) = @_;
309  
310 49 my $p = $self->{ring_pointer};
311 49 my $l = scalar(@{$self->{ring}});
312  
313 49 my @new_ring = splice(@{$self->{ring}}, $p, $l-$p);
314 49 push @new_ring, splice(@{$self->{ring}}, 0, $p);
315  
316 49 $self->{ring} = \@new_ring;
317 49 $self->{ring_pointer} = 0;
318 }
319  
320 1 ▶sub _swap {
Subroutine _swap: called CC=1 SCAR=0.0
321 1 my ($self) = @_;
322  
323 1 my @new_ring = @{$self->{stack}};
324  
325 1 my $p = $self->{ring_pointer};
326 1 my $l = scalar(@{$self->{ring}});
327  
328 1 my @new_stack = splice(@{$self->{ring}}, $p, $l-$p);
329 1 push @new_stack, splice(@{$self->{ring}}, 0, $p);
330  
331 1 $self->{stack} = \@new_stack;
332 1 $self->{ring} = \@new_ring;
333 1 $self->{ring_pointer} = 0;
334 }
335  
336  
337 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
338 0 my ($self) = @_;
339  
340 0 print "RING: ".join(',',@{$self->{ring}})." STACK:".join(',',@{$self->{stack}})."\n";
341 }
342  
343 3 1;
344  
345 __END__
↑ Summary