lib/Language/RAM.pm

filter: Statementstmt 67.2% 150 / 223 Branchbran 68.9% 131 / 190 Conditioncond 50.0% 19 / 38 MC/DCmcdc 36.3% 8 / 22 Subroutinesub 74.2% 26 / 35 Podpod 100% 32 / 32 totaltotal 67.7% 366 / 540 CC 115 SCAR 32.1
CC
115
Cov
67%
run
182.0
asl
30.0
get_first_memory_snapshot
30.0
CRAP 24.7
↑ Summary script/ram.pl »
1 package Language::RAM;
2  
3 21 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Language::RAM - A "Random Access Machine" Emulator
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 21 our $VERSION = '0.011';
18  
19  
20 =head1 SYNOPSIS
21  
22 This module provides a library and an interpreter to emulate a basic
23 "Random Access Machine". This computer model uses an assembler-like syntax
24 and can be used to test simple algorithms for their complexity.
25  
26 You can use C<ram.pl> to run Random Access Machines and get
27 extensive information on memory usage, memory changes and complexity.
28 See C<ram.pl --help> for details.
29  
30 use Language::RAM;
31  
32 my $input = "INPUT 0\nOUTPUT 1\na <-- s[0]\na <-- a * s[0]\ns[1] <-- a\nHALT";
33  
34 my %machine = Language::RAM::asl($input);
35 die "$machine{'error'}" if ($machine{'error'} ne '');
36  
37 my $ret = Language::RAM::run(\%machine, [8]); # Returns 8^2
38 if($ret) {
39 print STDERR "Error from machine: $machine{'error'}\n";
40 }
41  
42 my %output = Language::RAM::get_output(\%machine);
43 print "OUTPUT FROM MACHINE:\n";
44 foreach (sort { $a <=> $b } keys %output) {
45 printf "%4d=%d\n", $_, $output{$_} // 0;
46 }
47  
48 =head1 EXPORT
49  
50 To use Language::RAM, you only need C<asl>, C<run>, C<get_output>, C<get_code_stats>,
51 C<get_mem_stats>, C<get_line>, C<get_first_memory_snapshot>, C<get_snapshots>
52 and C<replay_snapshot>.
53  
54 =head1 GENERAL
55  
56 =head2 asl(input)
57  
58 Return random access machine from input string (one command per line).
59  
60 The returned hash has the following keys/values:
61  
62 =over 4
63  
64 =item B<input_layout>=I<list ref>
65  
66 Range of input slots defined by INPUT command.
67  
68 =item B<output_layout>=I<list ref>
69  
70 Range of output slots defined by OUTPUT command.
71  
72 =item B<error>=I<string>
73  
74 Empty string. Not empty on errors.
75  
76 =item B<code>=I<hash ref>
77  
78 Hash of address => AST-Token.
79  
80 =item B<memory>=I<hash ref>
81  
82 Hash of slot name => value.
83  
84 =item B<ip>=I<current address>
85  
86 =item B<stats>=I<hash ref>
87  
88 =over 4
89  
90 =item B<memory_usage>=I<hash ref>
91  
92 Hash of slot name => [(reads writes)].
93  
94 =item B<command_usage>=I<hash ref>
95  
96 Hash of address => counter.
97  
98 =back
99  
100 =item B<snaps>=I<hash ref>
101  
102 Memory snapshots of each assignment.
103 Hash of step => [(ip, address, new_value)].
104  
105 =item B<steps>=I<step counter>
106  
107 =back
108  
109 =cut
110  
111 sub asl {
112 0 ▶ my %machine = (
Subroutine asl: not called CC=5 SCAR=34.0
Pod documented
113 code => {},
114 lines => {},
115 error => ''
116 );
117 0 my $ip = -1;
118 0 foreach (split /\n/, $_[0]) {
119 0 $_ =~ s/\A\s+|\s+\Z//g;
120 0 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 0 0
121 0 my ($n_ip, $value) = &ast($_, \%machine, $ip);
122 0 ▶ return %machine unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
123 0 ▶ next if $n_ip == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $n_ip == -1 0 0 0
124 0 $ip = $n_ip;
125 0 $machine{'code'}{$ip} = $value;
126 }
127 0 return %machine;
128 }
129  
130 =head2 run(machine, input, limit, snapshots)
131  
132 Run machine until it halts or stepcounter reaches limit.
133  
134 =over 4
135  
136 =item B<machine>=I<machine ref>
137  
138 =item B<input>=I<list ref>
139  
140 Values will be loaded into memory before execution according to paramters given
141 by INPUT.
142  
143 =item B<limit>=I<number>
144  
145 =item B<snapshots>=I<boolean>
146  
147 Set to true to generate detailed memory snapshots of each command.
148  
149 =back
150  
151 Returns empty string on success, error string on error.
152  
153 =cut
154  
155 sub run {
156 0 ▶ my $machine = $_[0];
Subroutine run: not called CC=13 SCAR=52.0
Pod documented
157 0 my $limit = -1;
158 0 ▶ $limit = $_[2] if(@_ >= 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 3 0 0 0
159  
160 0 $machine->{'input'} = $_[1];
161 0 $machine->{'error'} = '';
162 0 $machine->{'ip'} = 0;
163 0 $machine->{'steps'} = 0;
164 0 $machine->{'memory'} = {};
165 0 ▶ $machine->{'snaps'} = {} if $_[3];
Branch: 1 branch
BranchTrueFalseTotal
if $_[3] 0 0 0
166  
167 0 ▶ while (my ($index, $id) = each @{$machine->{'input_layout'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @{$$machine{"input_layout"};}) 0 0 0
168 0 ▶ $machine->{'memory'}{$id} = $machine->{'input'}[$index] // 0;
Condition0%0/2
$machine->{"input"}[$index] // 0
l!l
00
MC/DC0%0/1
$machine->{"input"}[$index] // 0
$machine->{"input"}[$index]
Truth table0%0/2
A
A= $machine->{"input"}[$index]
Aresult
11
00
169 }
170  
171 0 ▶ while($limit == -1 || $machine->{'steps'} < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit == -1 or $machine->{"steps"} < $limit) 0 0 0
Condition0%0/3
$limit == -1 or $machine->{"steps"} < $limit
l!l&&r!l&&!r
000
MC/DC0%0/2
$limit == -1 or $machine->{"steps"} < $limit
$limit == -1 $machine->{"steps"} < $limit
Truth table0%0/3
A or B
A= $limit == -1B= $machine->{"steps"} < $limit
ABresult
1X1
011
000
172 0 my $current = $machine->{'code'}{$machine->{'ip'}};
173  
174 0 ▶ unless($current) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current) 0 0 0
175 0 return $machine->{'error'} = "Reached nocode at $machine->{'ip'}";
176 }
177  
178 0 ▶ unless(exists $machine->{'stats'}{'command_usage'}{$machine->{'ip'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $machine->{"stats"}{"command_usage"}{$machine->{"ip"}}) 0 0 0
179 0 $machine->{'stats'}{'command_usage'}{$machine->{'ip'}} = 0;
180 }
181 0 ++$machine->{'stats'}{'command_usage'}{$machine->{'ip'}};
182  
183 0 ▶ $machine->{'snaps'}{$machine->{'steps'}} = [($machine->{'ip'})] if $_[3];
Branch: 1 branch
BranchTrueFalseTotal
if $_[3] 0 0 0
184  
185 0 ▶ &eval($current, $machine, 0, $_[3]) or return $machine->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
&eval($current, $machine, 0, $_[3]) or return $machine->{"error"} 0 0 0
186  
187 0 ++$machine->{'ip'};
188 0 ++$machine->{'steps'};
189 }
190  
191 0 ▶ if($limit > 0 && $machine->{'steps'} == $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > 0 and $machine->{"steps"} == $limit) 0 0 0
Condition0%0/3
$limit > 0 and $machine->{"steps"} == $limit
!ll&&!rl&&r
000
MC/DC0%0/2
$limit > 0 and $machine->{"steps"} == $limit
$limit > 0 $machine->{"steps"} == $limit
Truth table0%0/3
A and B
A= $limit > 0B= $machine->{"steps"} == $limit
ABresult
0X0
100
111
192 0 return $machine->{'error'} = "Readed op limit at $machine->{'ip'}(aborted after $limit ops)";
193 }
194 0 return '';
195 }
196  
197 =head2 get_output(machine)
198  
199 Return output from machine.
200  
201 =over 4
202  
203 =item B<machine>=I<machine ref>
204  
205 =back
206  
207 Returns a hash of slot => value.
208  
209 =cut
210  
211 sub get_output {
212 0 ▶ my %ret;
Subroutine get_output: not called CC=2 SCAR=17.9
Pod documented
213 0 foreach (@{$_[0]->{'output_layout'}}) {
214 0 $ret{$_} = $_[0]->{'memory'}{$_};
215 }
216 0 return %ret;
217 }
218  
219 =head2 get_code_stats(machine)
220  
221 Return code statistics from machine.
222  
223 =over 4
224  
225 =item B<machine>=I<machine ref>
226  
227 =back
228  
229 Returns a hash of address => counter.
230  
231 =cut
232  
233 sub get_code_stats {
234 0 ▶ return %{$_[0]->{'stats'}{'command_usage'}};
Subroutine get_code_stats: not called CC=1 SCAR=6.9
Pod documented
235 }
236  
237 =head2 get_mem_stats(machine)
238  
239 Return memory statistics from machine.
240  
241 =over 4
242  
243 =item B<machine>=I<machine ref>
244  
245 =back
246  
247 Returns a hash of slot => counter.
248  
249 =cut
250  
251 sub get_mem_stats {
252 0 ▶ return %{$_[0]->{'stats'}{'memory_usage'}};
Subroutine get_mem_stats: not called CC=1 SCAR=6.9
Pod documented
253 }
254  
255 =head2 get_line(machine, id)
256  
257 Return line at id.
258  
259 =over 4
260  
261 =item B<machine>=I<machine ref>
262  
263 =item B<id>=I<number>
264  
265 =back
266  
267 Returns line as string.
268  
269 =cut
270  
271 sub get_line {
272 0 ▶ return $_[0]->{'lines'}{$_[1]};
Subroutine get_line: not called CC=1 SCAR=6.9
Pod documented
273 }
274  
275 =head2 get_first_memory_snapshot(machine)
276  
277 Returns a memory snapshot (a hash) of index => value of the memory at step -1
278 (before the machine starts).
279  
280 =over 4
281  
282 =item B<machine>=I<machine ref>
283  
284 =back
285  
286 =cut
287  
288 sub get_first_memory_snapshot {
289 0 ▶ my %snapshot = ();
Subroutine get_first_memory_snapshot: not called CC=5 SCAR=34.0
Pod documented
290 0 my $machine = $_[0];
291 0 my $snapshots = &get_snapshots($machine);
292  
293 0 foreach (keys %$snapshots) {
294 0 ▶ next unless exists $$snapshots{$_}->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $snapshots->{$_}[1] 0 0 0
295 0 $snapshot{$$snapshots{$_}->[1]} = 0;
296 }
297  
298 0 ▶ while (my ($index, $id) = each @{$$machine{'input_layout'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @{$$machine{"input_layout"};}) 0 0 0
299 0 ▶ $snapshot{$id} = $$machine{'input'}->[$index] // 0;
Condition0%0/2
$machine->{"input"}[$index] // 0
l!l
00
MC/DC0%0/1
$machine->{"input"}[$index] // 0
$machine->{"input"}[$index]
Truth table0%0/2
A
A= $machine->{"input"}[$index]
Aresult
11
00
300 }
301  
302 0 return %snapshot;
303 }
304  
305 =head2 get_snapshots(machine)
306  
307 Returns a hash ref of step => [(ip, addr, value)].
308  
309 =over 4
310  
311 =item B<machine>=I<machine ref>
312  
313 =back
314  
315 =cut
316  
317 sub get_snapshots {
318 0 ▶ return $_[0]->{'snaps'};
Subroutine get_snapshots: not called CC=1 SCAR=6.9
Pod documented
319 }
320  
321 =head2 replay_snapshot(machine, memory, from, to)
322  
323 Replay steps from to to of machine in memory.
324  
325 =over 4
326  
327 =item B<machine>=I<machine ref>
328  
329 =item B<memory>=I<ref to memory snapshot>
330  
331 =item B<from>=I<step number to start at>
332  
333 =item B<to>=I<step number to stop at>
334  
335 =back
336  
337 =cut
338  
339 sub replay_snapshot {
340 0 ▶ foreach ($_[2]..$_[3]) {
Subroutine replay_snapshot: not called CC=3 SCAR=24.8
Pod documented
341 0 my $step = $_[0]->{'snaps'}{$_};
342 0 ▶ next unless exists $$step[1];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $step->[1] 0 0 0
343 0 $_[1]->{$$step[1]} = $$step[2];
344 }
345 }
346  
347 =head1 ABSTRACT SYNTAX TREE
348  
349 =head2 ast(line, machine, ip)
350  
351 Return AST of line.
352  
353 =over 4
354  
355 =item B<line>=I<string>
356  
357 =item B<machine>=I<machine ref>
358  
359 =item B<ip>=I<current address>
360  
361 =back
362  
363 Returns (ip, ast).
364  
365 =over 4
366  
367 =item B<ip>=<address>
368  
369 Address of line (either generated or read from line, see README).
370  
371 -1 if line is INPUT/OUTPUT statement.
372  
373 =item B<ast>=<ast reference>
374  
375 undef on error.
376  
377 =back
378  
379 =cut
380  
381 sub ast {
382 6 ▶ my $l = $_[0];
Subroutine ast: called CC=6 SCAR=17.9
Pod documented
383 6 $l =~ s(//.+\Z)();
384 6 $l =~ s/\A\s+|\s+\Z//g;
385  
386 6 my $ip = $_[2] + 1;
387 6 ▶ if($l =~ /\A(\d+):\s*(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\A(\d+):\s*(.+)/) 2 4 6
388 2 $ip = $1;
389 2 $l = $2;
390 }
391  
392 6 ▶ while(exists $_[1]->{'code'}{$ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[1]{"code"}{$ip}) 2 6 8
393 2 ++$ip;
394 }
395  
396 6 ▶ if($l =~ /\A(INPUT|OUTPUT)(.*)\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\A(INPUT|OUTPUT)(.*)\Z/) 3 3 6
397 3 ▶ if($2 eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "") 1 2 3
398 1 $_[1]->{'error'} = "$ip> $1 expects an argument";
399 1 return (-1, undef);
400 }
401 2 my $ret = &ast_eval_io($2, $_[1]);
402 2 ▶ if($1 eq 'INPUT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'INPUT') { } 1 1 2
403 1 $_[1]->{'input_layout'} = $ret;
404 } else {
405 1 $_[1]->{'output_layout'} = $ret;
406 }
407 2 return (-1, $ret);
408 }
409  
410 3 $_[1]->{'lines'}{$ip} = $l;
411  
412 3 return ($ip, &get_ast($l, $_[1], $ip));
413 }
414  
415 =head2 ast_eval_io(line, machine)
416  
417 Return INPUT/OUTPUT layout.
418  
419 =over 4
420  
421 =item B<line>=I<string>
422  
423 =item B<machine>=I<machine ref>
424  
425 =back
426  
427 Returns a reference to a list of indices or undef on error.
428  
429 =cut
430  
431 sub ast_eval_io {
432 10 ▶ if(index($_[0], ' ') != -1) {
Subroutine ast_eval_io: called CC=9 SCAR=22.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (index($_[0], ' ') != -1) { } 4 6 10
433 4 my @parms = split /\s+/, $_[0];
434 4 my $ret = [()];
435  
436 4 foreach (@parms) {
437 6 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 2 4 6
438 4 my $r = &ast_eval_io($_, $_[1]);
439 4 ▶ return undef unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 4 4
440 4 push @{$ret}, @{$r};
441 }
442  
443 4 ▶ if(@{$ret} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ret;} == 0) 1 3 4
444 1 $_[1]->{'error'} = 'Command expects argument';
445 }
446 4 return $ret;
447 } else {
448 6 ▶ if($_[0] =~ /\A(\d+)..(\d+)\Z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /\A(\d+)..(\d+)\Z/) { } 1 5 6
elsif ($_[0] =~ /\A(\d+)\Z/) { } 4 1 5
449 1 my @ret = ();
450 1 ▶ for(my $i = $1; $i <= $2; $i++) {push @ret, $i;}
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $2) 3 1 4
451 1 return \@ret;
452 } elsif($_[0] =~ /\A(\d+)\Z/) {
453 4 return [$1];
454 } else {
455 1 $_[1]->{'error'} = "Argument not numeric: $_[0]";
456 1 return undef;
457 }
458 }
459 }
460  
461 =head2 get_ast(input, machine, ip)
462  
463 Return AST-Token from line.
464  
465 =over 4
466  
467 =item B<input>=I<string>
468  
469 =item B<machine>=I<machine ref>
470  
471 =item B<ip>=I<current address>
472  
473 =back
474  
475 Return AST-Token or undef.
476  
477 =cut
478  
479 sub get_ast {
480 124 ▶ if($_[0] =~ /\A(\-?\d+(?:\.\d+)?)\Z/) {
Subroutine get_ast: called CC=11 SCAR=26.2
Pod documented
Branch: 9 branches
BranchTrueFalseTotal
if ($_[0] =~ /\A(\-?\d+(?:\.\d+)?)\Z/) { } 30 94 124
elsif ($_[0] =~ /\A(a|i|i1|i2|i3)\Z/) { } 62 32 94
elsif ($_[0] =~ /\As\[\s*(.+?)\s*\]\Z/) { } 13 19 32
elsif ($_[0] =~ /\Ajump\s+(.+)\s*\Z/) { } 3 16 19
elsif ($_[0] =~ /\A(.+?)\s*<--?\s*(.+?)\s*\Z/) { } 3 13 16
elsif ($_[0] =~ /\A(.+?)\s*(<|<=|!?=|>=|>)\s*0\Z/) { } 2 11 13
elsif ($_[0] =~ /\Aif\s+(.+?)\s+then\s+(.+)\Z/) { } 0 11 11
elsif ($_[0] =~ /\A(.+?)\s*(\+|\-|\*|div|mod)\s*(.+)\Z/) { } 11 0 11
elsif ($_[0] eq 'HALT') { } 0 0 0
481 30 &ast_imm($1);
482 } elsif ($_[0] =~ /\A(a|i|i1|i2|i3)\Z/) {
483 62 ▶ &ast_reg($1 eq 'i' ? 'i1' : $1);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'i' ? : 0 62 62
484 } elsif ($_[0] =~ /\As\[\s*(.+?)\s*\]\Z/) {
485 13 &ast_mem($1, $_[1], $_[2]);
486 } elsif ($_[0] =~ /\Ajump\s+(.+)\s*\Z/) {
487 3 &ast_jump($1, $_[1], $_[2]);
488 } elsif ($_[0] =~ /\A(.+?)\s*<--?\s*(.+?)\s*\Z/) {
489 3 &ast_assign($1, $2, $_[1], $_[2]);
490 } elsif ($_[0] =~ /\A(.+?)\s*(<|<=|!?=|>=|>)\s*0\Z/) {
491 2 &ast_cond($1, $2, $_[1], $_[2]);
492 } elsif ($_[0] =~ /\Aif\s+(.+?)\s+then\s+(.+)\Z/) {
493 0 &ast_cond_jump($1, $2, $_[1], $_[2]);
494 } elsif ($_[0] =~ /\A(.+?)\s*(\+|\-|\*|div|mod)\s*(.+)\Z/) {
495 11 &ast_algo($1, $2, $3, $_[1], $_[2]);
496 } elsif ($_[0] eq 'HALT') {
497 0 return [('halt')];
498 } else {
499 0 $_[1]->{'error'} = "Unknown input: $_[0]";
500 0 return undef;
501 }
502 }
503  
504 =head2 ast_imm(value)
505  
506 Returns AST-Token for immutable values.
507  
508 =over 4
509  
510 =item B<value>=I<number>
511  
512 =back
513  
514 Returns C<[('imm', value)]>.
515  
516 =cut
517  
518 sub ast_imm {
519 31 ▶ return [('imm', $_[0])];
Subroutine ast_imm: called CC=1 SCAR=0.0
Pod documented
520 }
521  
522 =head2 ast_reg(register)
523  
524 Returns AST-Token for a register (a, i1..i3).
525  
526 =over 4
527  
528 =item B<register>=I<register>
529  
530 =back
531  
532 Returns C<[('reg', register)]>.
533  
534 =cut
535  
536 sub ast_reg {
537 63 ▶ return [('reg', $_[0])];
Subroutine ast_reg: called CC=1 SCAR=0.0
Pod documented
538 }
539  
540 =head2 ast_algo(left, op, right, machine, ip)
541  
542 Returns AST-Token for an arithmetic expression.
543  
544 =over 4
545  
546 =item B<left>=I<left side of expression>
547  
548 =item B<op>=I<operation>
549  
550 =item B<right>=I<right side of expression>
551  
552 =item B<machine>=I<machine ref>
553  
554 =item B<ip>=I<current address>
555  
556 =back
557  
558 Returns C<[('algo', type, left, op, right)]> or undef on error.
559  
560 =over 4
561  
562 =item B<type>=I<boolean>
563  
564 True if left side of expression is register a, otherwise false.
565  
566 =item B<left>=I<ast-token>
567  
568 =item B<op>=I<string>
569  
570 =item B<right>=I<ast-token>
571  
572 =back
573  
574 =cut
575  
576 21 my %algo_right = qw(imm 0 mem 0 mmem 0);
577 sub ast_algo {
578 18 ▶ (my $left = &get_ast($_[0], $_[3], $_[4])) or return undef;
Subroutine ast_algo: called CC=9 SCAR=23.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $left = &get_ast($_[0], $_[3], $_[4]) or return undef 0 18 18
579 18 ▶ (my $right = &get_ast($_[2], $_[3], $_[4])) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $right = &get_ast($_[2], $_[3], $_[4]) or return undef 0 18 18
580  
581 18 ▶ $left->[0] eq 'reg'
Branch: 1 branch
BranchTrueFalseTotal
$left->[0] eq "reg" or return &report($_[3], "$_[4]> Expected reg, got: $left->[0]($_[0])") 1 17 18
582 or return &report($_[3], "$_[4]> Expected reg, got: $left->[0]($_[0])");
583  
584 17 ▶ (exists $algo_right{$right->[0]})
Branch: 1 branch
BranchTrueFalseTotal
exists $algo_right{$right->[0]} or return &report($_[3], "$_[4]> Expected imm, mem or mmem, got: $right->[0]($_[2])") 1 16 17
585 or return &report($_[3], "$_[4]> Expected imm, mem or mmem, got: $right->[0]($_[2])");
586  
587 16 my $type = $left->[1] eq 'a';
588 16 ▶ ($type || ($right->[0] eq 'imm' && ($_[1] eq '+' || $_[1] eq '-')))
Branch: 1 branch
BranchTrueFalseTotal
$type or $right->[0] eq "imm" and $_[1] eq "+" || $_[1] eq "-" or return &report($_[3], "$_[4]> Index register only allows addition or subtraction with imm ($_[0]$_[1]$_[2])") 1 15 16
Condition55%5/9
$type or $right->[0] eq "imm" and $_[1] eq "+" || $_[1] eq "-"
l!l&&r!l&&!r
5011
$right->[0] eq "imm" and $_[1] eq "+" || $_[1] eq "-"
!ll&&!rl&&r
0110
$_[1] eq "+" or $_[1] eq "-"
l!l&&r!l&&!r
1001
MC/DC25%1/4
$type or $right->[0] eq "imm" and $_[1] eq "+" || $_[1] eq "-"
$type $right->[0] eq "imm" $_[1] eq "+" $_[1] eq "-"
Truth table40%2/5
A or B and C or D
A= $typeB= $right->[0] eq "imm"C= $_[1] eq "+"D= $_[1] eq "-"
ABCDresult
1XXX1
011X1
01011
00XX0
01000
589 or return &report($_[3], "$_[4]> Index register only allows addition or subtraction with imm ($_[0]$_[1]$_[2])");
590  
591 15 return [('algo', $type, $left, $_[1], $right)];
592 }
593  
594 =head2 ast_mem(inner, machine, ip)
595  
596 Returns AST-Token for memory slot.
597  
598 =over 4
599  
600 =item B<inner>=I<string>
601  
602 =item B<machine>=I<machine ref>
603  
604 =item B<ip>=I<current address>
605  
606 =back
607  
608 Returns C<[('mem', imm)]>, C<[('mmem', ast)]> or undef on error.
609  
610 =cut
611  
612 sub ast_mem {
613 18 ▶ (my $inner = &get_ast(@_)) or return undef;
Subroutine ast_mem: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $inner = &get_ast(@_) or return undef 0 18 18
614  
615 18 ▶ ($inner->[0] eq 'imm') and return [('mem', $inner->[1])];
Branch: 1 branch
BranchTrueFalseTotal
$inner->[0] eq "imm" and return ["mem", $inner->[1]] 6 12 18
616  
617 12 ▶ if($inner->[0] eq 'algo') {
Branch: 2 branches
BranchTrueFalseTotal
if ($inner->[0] eq 'algo') { } 10 2 12
elsif ($inner->[0] eq 'reg' and $inner->[1] ne 'a') { } 1 1 2
618 10 ▶ ($inner->[1] == 0)
Branch: 1 branch
BranchTrueFalseTotal
$inner->[1] == 0 or return &report($_[1], "$_[2]> Cannot use register a in mmem ($_[0])") 1 9 10
619 or return &report($_[1], "$_[2]> Cannot use register a in mmem ($_[0])");
620  
621 9 return [('mmem', $inner)];
622 ▶ } elsif ($inner->[0] eq 'reg' && $inner->[1] ne 'a') {
Condition66%2/3
$inner->[0] eq "reg" and $inner->[1] ne "a"
!ll&&!rl&&r
101
MC/DC50%1/2
$inner->[0] eq "reg" and $inner->[1] ne "a"
$inner->[0] eq "reg" $inner->[1] ne "a"
Truth table66%2/3
A and B
A= $inner->[0] eq "reg"B= $inner->[1] ne "a"
ABresult
0X0
100
111
623 1 return [('mmem', $inner)];
624 1 } else {return &report($_[1], "$_[2]> Expected imm, algo or index register, got: $inner->[0]($_[0])");}
625 }
626  
627 =head2 ast_cond(cond, op, machine, ip)
628  
629 Returns AST-Token for conditional.
630  
631 =over 4
632  
633 =item B<cond>=I<string>
634  
635 =item B<op>=I<string>
636  
637 =item B<machine>=I<machine ref>
638  
639 =item B<ip>=I<current address>
640  
641 =back
642  
643 Returns C<[('cond', reg, op)]> or undef on error.
644  
645 =over 4
646  
647 =item B<reg>=I<ast-token>
648  
649 =item B<op>=I<string>
650  
651 =back
652  
653 =cut
654  
655 sub ast_cond {
656 27 ▶ (my $reg = &get_ast($_[0], $_[2], $_[3])) or return undef;
Subroutine ast_cond: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $reg = &get_ast($_[0], $_[2], $_[3]) or return undef 0 27 27
657  
658 27 ▶ ($reg->[0] eq 'reg')
Branch: 1 branch
BranchTrueFalseTotal
$reg->[0] eq "reg" or return &report($_[2], "$_[3]> Expected reg, got: $reg->[0]($_[0])") 1 26 27
659 or return &report($_[2], "$_[3]> Expected reg, got: $reg->[0]($_[0])");
660  
661 26 return [('cond', $reg, $_[1])];
662 }
663  
664 =head2 ast_jump(imm, machine, ip)
665  
666 Returns AST-Token for jump instruction.
667  
668 =over 4
669  
670 =item B<imm>=I<number>
671  
672 =item B<machine>=I<machine ref>
673  
674 =item B<ip>=I<current address>
675  
676 =back
677  
678 Returns C<[('jump', imm)]> or undef on error.
679  
680 =cut
681  
682 sub ast_jump {
683 5 ▶ (my $dest = &get_ast(@_)) or return undef;
Subroutine ast_jump: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $dest = &get_ast(@_) or return undef 0 5 5
684  
685 5 ▶ ($dest->[0] eq 'imm')
Branch: 1 branch
BranchTrueFalseTotal
$dest->[0] eq "imm" or return &report($_[1], "$_[2]> Expected imm, got: $dest->[0]($_[0])") 1 4 5
686 or return &report($_[1], "$_[2]> Expected imm, got: $dest->[0]($_[0])");
687  
688 4 return [('jump', $dest)];
689 }
690  
691 =head2 ast_cond_jump(cond, jump, machine, ip)
692  
693 Returns AST-Token for if cond then jump k.
694  
695 =over 4
696  
697 =item B<cond>=I<string>
698  
699 =item B<jump>=I<string>
700  
701 =item B<machine>=I<machine ref>
702  
703 =item B<ip>=I<current address>
704  
705 =back
706  
707 Returns C<[('if', cond, jump)]> or undef on error.
708  
709 =over 4
710  
711 =item B<cond>=I<ast-token>
712  
713 =item B<jump>=I<ast-token>
714  
715 =back
716  
717 =cut
718  
719 sub ast_cond_jump {
720 3 ▶ (my $cond = &get_ast($_[0], $_[2], $_[3])) or return undef;
Subroutine ast_cond_jump: called CC=5 SCAR=16.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $cond = &get_ast($_[0], $_[2], $_[3]) or return undef 0 3 3
721  
722 3 ▶ ($cond->[0] eq 'cond')
Branch: 1 branch
BranchTrueFalseTotal
$cond->[0] eq "cond" or return &report($_[2], "$_[3]> Expected cond, got: $cond->[0]($_[0])") 1 2 3
723 or return &report($_[2], "$_[3]> Expected cond, got: $cond->[0]($_[0])");
724  
725 2 ▶ (my $jump = &get_ast($_[1], $_[2], $_[3])) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $jump = &get_ast($_[1], $_[2], $_[3]) or return undef 0 2 2
726 2 ▶ ($jump->[0] eq 'jump')
Branch: 1 branch
BranchTrueFalseTotal
$jump->[0] eq "jump" or return &report($_[2], "$_[3]> Expected jump, got: $jump->[0]($_[1])") 1 1 2
727 or return &report($_[2], "$_[3]> Expected jump, got: $jump->[0]($_[1])");
728  
729 1 return [('if', $cond, $jump)];
730 }
731  
732 =head2 ast_assign(left, right, machine, ip)
733  
734 Returns AST-Token for assignment.
735  
736 =over 4
737  
738 =item B<left>=I<left side of expression>
739  
740 =item B<right>=I<right side of expression>
741  
742 =item B<machine>=I<machine ref>
743  
744 =item B<ip>=I<current address>
745  
746 =back
747  
748 Returns C<[('assign', left, right)]> or undef on error.
749  
750 =over 4
751  
752 =item B<left>=I<ast-token>
753  
754 =item B<right>=I<ast-token>
755  
756 =back
757  
758 =cut
759  
760 21 my %assign_right = qw(imm 0 mem 0 reg 0);
761 21 my %assign_a_right = qw(mmem 0 algo 0);
762 sub ast_assign {
763 15 ▶ (my $left = &get_ast($_[0], $_[2], $_[3])) or return undef;
Subroutine ast_assign: called CC=16 SCAR=28.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $left = &get_ast($_[0], $_[2], $_[3]) or return undef 0 15 15
764 15 ▶ (my $right = &get_ast($_[1], $_[2], $_[3])) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $right = &get_ast($_[1], $_[2], $_[3]) or return undef 0 15 15
765  
766 15 ▶ if($left->[0] eq 'reg') {
Branch: 3 branches
BranchTrueFalseTotal
if ($left->[0] eq 'reg') { } 10 5 15
elsif ($left->[0] eq 'mem') { } 2 3 5
elsif ($left->[0] eq 'mmem') { } 2 1 3
767 10 my $rcheck = exists $assign_right{$right->[0]};
768  
769 10 ▶ if($left->[1] eq 'a') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->[1] eq 'a') { } 8 2 10
770 8 ▶ ($rcheck || (exists $assign_a_right{$right->[0]}))
Branch: 1 branch
BranchTrueFalseTotal
$rcheck or exists $assign_a_right{$right->[0]} or return &report($_[2], "$_[3]> Expected imm, reg, mem, mmem or algo, got: $right->[0]($_[1])") 1 7 8
Condition100%3/3
$rcheck or exists $assign_a_right{$right->[0]}
l!l&&r!l&&!r
611
MC/DC100%2/2
$rcheck or exists $assign_a_right{$right->[0]}
$rcheck exists $assign_a_right{$right->[0]}
Truth table100%3/3
A or B
A= $rcheckB= exists $assign_a_right{$right->[0]}
ABresult
1X1
011
000
771 or return &report($_[2], "$_[3]> Expected imm, reg, mem, mmem or algo, got: $right->[0]($_[1])");
772 } else {
773 2 ▶ ($rcheck || $right->[0] eq 'algo')
Branch: 1 branch
BranchTrueFalseTotal
$rcheck or $right->[0] eq "algo" or return &report($_[2], "$_[3]> Expected imm, reg, mem or algo, got: $right->[0]($_[1])") 1 1 2
Condition66%2/3
$rcheck or $right->[0] eq "algo"
l!l&&r!l&&!r
011
MC/DC50%1/2
$rcheck or $right->[0] eq "algo"
$rcheck $right->[0] eq "algo"
Truth table66%2/3
A or B
A= $rcheckB= $right->[0] eq "algo"
ABresult
1X1
011
000
774 or return &report($_[2], "$_[3]> Expected imm, reg, mem or algo, got: $right->[0]($_[1])");
775  
776 1 ▶ (!$right->[1] || $right->[0] ne 'algo') or return &report($_[2], "$_[3]> register a not allowed in i(1|2|3) assignment ($_[1])");
Branch: 1 branch
BranchTrueFalseTotal
not $right->[1] or $right->[0] ne "algo" or return &report($_[2], "$_[3]> register a not allowed in i(1|2|3) assignment ($_[1])") 1 0 1
Condition33%1/3
not $right->[1] or $right->[0] ne "algo"
l!l&&r!l&&!r
001
MC/DC0%0/2
not $right->[1] or $right->[0] ne "algo"
not $right->[1] $right->[0] ne "algo"
Truth table33%1/3
not(A) or B
A= not $right->[1]B= $right->[0] ne "algo"
ABresult
1X1
011
000
777 }
778 } elsif ($left->[0] eq 'mem') {
779 2 ▶ ($right->[0] eq 'reg')
Branch: 1 branch
BranchTrueFalseTotal
$right->[0] eq "reg" or return &report($_[2], "$_[3]> Expected reg, got: $right->[0]($_[1])") 1 1 2
780 or return &report($_[2], "$_[3]> Expected reg, got: $right->[0]($_[1])");
781 } elsif ($left->[0] eq 'mmem') {
782 2 ▶ ($right->[0] eq 'reg' && $right->[1] eq 'a')
Branch: 1 branch
BranchTrueFalseTotal
$right->[0] eq "reg" and $right->[1] eq "a" or return &report($_[2], "$_[3]> Expected register a, got: $right->[0]($_[1])") 1 1 2
Condition66%2/3
$right->[0] eq "reg" and $right->[1] eq "a"
!ll&&!rl&&r
101
MC/DC50%1/2
$right->[0] eq "reg" and $right->[1] eq "a"
$right->[0] eq "reg" $right->[1] eq "a"
Truth table66%2/3
A and B
A= $right->[0] eq "reg"B= $right->[1] eq "a"
ABresult
0X0
100
111
783 or return &report($_[2], "$_[3]> Expected register a, got: $right->[0]($_[1])");
784 1 } else {return &report($_[2], "$_[3]> Expected reg, mem or mmem, got: $left->[0]($_[0])");}
785 9 return [('assign', $left, $right)];
786 }
787  
788 =head1 EVALUATION
789  
790 =head2 eval(ast, machine)
791  
792 Evaluate ast.
793  
794 =over 4
795  
796 =item B<ast>=I<ast reference>
797  
798 =item B<machine>=I<machine reference>
799  
800 =back
801  
802 Returns undef on error.
803  
804 =cut
805  
806 21 my %eval_funcs = (
807 imm => \&eval_imm,
808 reg => \&eval_mem,
809 mem => \&eval_mem,
810 mmem => \&eval_mmem,
811 algo => \&eval_algo,
812 cond => \&eval_cond,
813 if => \&eval_if,
814 jump => \&eval_jump,
815 assign => \&eval_assign
816 );
817  
818 sub eval {
819 113 ▶ if ($_[0]->[0] eq 'halt') {
Subroutine eval: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][0] eq "halt") 0 113 113
820 0 return undef;
821 }
822 113 ▶ if (exists $eval_funcs{$_[0]->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $eval_funcs{$_[0][0]}) { } 113 0 113
823 113 return $eval_funcs{$_[0]->[0]}->(@_);
824 } else {
825 0 $_[1]->{'error'} = "AST Element $_[0][0] not supported";
826 0 return undef;
827 }
828 }
829  
830 =head2 eval_imm(ast)
831  
832 my $ast = [qw(imm 2)];
833  
834 Returns immutable value of ast.
835  
836 =over 4
837  
838 =item B<ast>=I<ast reference>
839  
840 =back
841  
842 =cut
843  
844 sub eval_imm {
845 22 ▶ $_[0]->[1];
Subroutine eval_imm: called CC=1 SCAR=0.0
Pod documented
846 }
847  
848 =head2 eval_mem(ast, machine, type)
849  
850 my $ast = [qw(mem 2)];
851  
852 Returns value of/reference to/address of memory block.
853  
854 =over 4
855  
856 =item B<ast>=I<ast reference>
857  
858 =item B<machine>=I<machine reference>
859  
860 =item B<type>=I<number>
861  
862 Returns value of memory block if B<type> is 0.
863  
864 Returns reference to memory block if B<type> is 1.
865  
866 Returns address of memory block if B<type> is 2.
867  
868 =back
869  
870 =cut
871  
872 sub eval_mem {
873 87 ▶ unless(exists $_[1]->{'memory'}{$_[0]->[1]}) {
Subroutine eval_mem: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[1]{"memory"}{$_[0][1]}) 6 81 87
874 6 $_[1]->{'memory'}{$_[0]->[1]} = 0;
875 }
876  
877 87 &inc_mem_stat($_[1], $_[0]->[1], $_[2]);
878  
879 87 ▶ return $_[1]->{'memory'}{$_[0]->[1]} unless ($_[2] // 0) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[2] // 0) > 0 75 12 87
Condition100%2/2
$_[2] // 0
l!l
1275
MC/DC100%1/1
$_[2] // 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
880 12 ▶ return \$_[1]->{'memory'}{$_[0]->[1]} if $_[2] == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] == 1 10 2 12
881 2 return $_[0]->[1];
882 }
883  
884 =head2 eval_mmem(ast, machine, type)
885  
886 my $ast = [('mmem', "algo here, see eval_algo")];
887  
888 Same as C<eval_mem>, but evaluate inner expression.
889  
890 Returns undef if inner expression could not be evaluated.
891  
892 =cut
893  
894 sub eval_mmem {
895 13 ▶ return undef unless (defined(my $val = &eval($_[0]->[1], $_[1])));
Subroutine eval_mmem: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $val = &eval($_[0][1], $_[1])) 0 13 13
896 13 return &eval_mem([('mem', $val)], $_[1], $_[2]);
897 }
898  
899 =head2 eval_algo(ast, machine)
900  
901 my $ast = [('algo', 1, [qw(reg a)], '+', [qw(mem 2)])];
902  
903 Return result of arithmetic expression.
904  
905 =over 4
906  
907 =item B<ast>=I<ast reference>
908  
909 =item B<machine>=I<machine reference>
910  
911 =back
912  
913 Returns undef if left side, right side or operation failed to evaluate.
914  
915 =cut
916  
917 sub eval_algo {
918 32 ▶ return undef unless (defined(my $left = &eval($_[0]->[2], $_[1])));
Subroutine eval_algo: called CC=8 SCAR=21.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $left = &eval($_[0][2], $_[1])) 0 32 32
919 32 ▶ return undef unless (defined(my $right = &eval($_[0]->[4], $_[1])));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $right = &eval($_[0][4], $_[1])) 0 32 32
920  
921 32 ▶ if($_[0]->[3] eq '+') {
Branch: 5 branches
BranchTrueFalseTotal
if ($_[0][3] eq '+') { } 17 15 32
elsif ($_[0][3] eq '-') { } 6 9 15
elsif ($_[0][3] eq '*') { } 3 6 9
elsif ($_[0][3] eq 'div') { } 3 3 6
elsif ($_[0][3] eq 'mod') { } 3 0 3
922 17 return $left + $right;
923 } elsif($_[0]->[3] eq '-') {
924 6 return $left - $right;
925 } elsif($_[0]->[3] eq '*') {
926 3 return $left * $right;
927 } elsif($_[0]->[3] eq 'div') {
928 3 return int($left / $right);
929 } elsif($_[0]->[3] eq 'mod') {
930 3 return $left % $right;
931 } else {
932 0 $_[1]->{'error'} = "Operator not supported: $_[0][3]";
933 0 return undef;
934 }
935 }
936  
937 =head2 eval_cond(ast, machine)
938  
939 my $ast = [('cond', [qw(reg a)], '<=')];
940  
941 Return result of conditional (always compares against 0).
942  
943 =over 4
944  
945 =item B<ast>=I<ast reference>
946  
947 =item B<machine>=I<machine reference>
948  
949 =back
950  
951 Returns undef if left side or operation failed to evaluate.
952  
953 =cut
954  
955 sub eval_cond {
956 20 ▶ return undef unless (defined(my $val = &eval($_[0]->[1], $_[1])));
Subroutine eval_cond: called CC=8 SCAR=21.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $val = &eval($_[0][1], $_[1])) 0 20 20
957  
958 20 ▶ if($_[0]->[2] eq '<') {
Branch: 6 branches
BranchTrueFalseTotal
if ($_[0][2] eq '<') { } 4 16 20
elsif ($_[0][2] eq '<=') { } 3 13 16
elsif ($_[0][2] eq '=') { } 3 10 13
elsif ($_[0][2] eq '!=') { } 3 7 10
elsif ($_[0][2] eq '>=') { } 3 4 7
elsif ($_[0][2] eq '>') { } 4 0 4
959 4 return $val < 0;
960 } elsif($_[0]->[2] eq '<=') {
961 3 return $val <= 0;
962 } elsif($_[0]->[2] eq '=') {
963 3 return $val == 0;
964 } elsif($_[0]->[2] eq '!=') {
965 3 return $val != 0;
966 } elsif($_[0]->[2] eq '>=') {
967 3 return $val >= 0;
968 } elsif($_[0]->[2] eq '>') {
969 4 return $val > 0;
970 } else {
971 0 $_[1]->{'error'} = "Operator not supported: $_[0][2]";
972 0 return undef;
973 }
974 }
975  
976 =head2 eval_if(ast, machine)
977  
978 my $ast = [('if', "cond here, see eval_cond", "jump here, see eval_jump")];
979  
980 Jump if conditional evaluates to true.
981  
982 =over 4
983  
984 =item B<ast>=I<ast reference>
985  
986 =item B<machine>=I<machine reference>
987  
988 =back
989  
990 Returns undef if conditional returned an error.
991  
992 =cut
993  
994 sub eval_if {
995 2 ▶ return undef unless (defined(my $cond = &eval($_[0]->[1], $_[1])));
Subroutine eval_if: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $cond = &eval($_[0][1], $_[1])) 0 2 2
996 2 ▶ &eval_jump($_[0]->[2], $_[1]) if $cond;
Branch: 1 branch
BranchTrueFalseTotal
if $cond 1 1 2
997 2 return 1;
998 }
999  
1000 =head2 eval_jump(ast, machine)
1001  
1002 my $ast = [('jump', [qw(imm 2)])];
1003  
1004 Jump to address.
1005  
1006 =over 4
1007  
1008 =item B<ast>=I<ast reference>
1009  
1010 =item B<machine>=I<machine reference>
1011  
1012 =back
1013  
1014 Returns undef if address could not be evaluated.
1015  
1016 =cut
1017  
1018 sub eval_jump {
1019 2 ▶ return undef unless (defined(my $val = &eval($_[0]->[1], $_[1])));
Subroutine eval_jump: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $val = &eval($_[0][1], $_[1])) 0 2 2
1020 2 $_[1]->{'ip'} = $val - 1;
1021 2 return 1;
1022 }
1023  
1024 =head2 eval_assign(ast, machine)
1025  
1026 my $ast = [('assign', "left side", "right side")];
1027  
1028 Evaluate assignment.
1029  
1030 =over 4
1031  
1032 =item B<ast>=I<ast reference>
1033  
1034 =item B<machine>=I<machine reference>
1035  
1036 =back
1037  
1038 Returns undef if left or right side could not be evaluated.
1039  
1040 =cut
1041  
1042 sub eval_assign {
1043 6 ▶ return undef unless (my $left = &eval($_[0]->[1], $_[1], 1));
Subroutine eval_assign: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless my $left = &eval($_[0][1], $_[1], 1) 0 6 6
1044 6 ▶ return undef unless (defined(my $right = &eval($_[0]->[2], $_[1])));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $right = &eval($_[0][2], $_[1])) 0 6 6
1045 6 $$left = $right;
1046 6 ▶ &add_snapshot($_[1], &eval($_[0]->[1], $_[1], 2), $right) if $_[1]->{'snaps'};
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{"snaps"} 0 6 6
1047 6 return 1;
1048 }
1049  
1050 =head1 STATISTICS
1051  
1052 =head2 inc_mem_stat(machine, mem, action)
1053  
1054 Increases access counter of one memory slot. These stats can later be retrieved
1055 with C<get_mem_stats>.
1056  
1057 =over 4
1058  
1059 =item B<machine>=I<machine ref>
1060  
1061 =item B<mem>=I<memory address>
1062  
1063 =item B<action>=I<boolean>
1064  
1065 Add write action to memory slot if B<action> is true.
1066  
1067 Add read action to memory slot if B<action> is false.
1068  
1069 =back
1070  
1071 =cut
1072  
1073 sub inc_mem_stat {
1074 92 ▶ return if ($_[2] // 0) == 2; #Bail if used in stat collection.
Subroutine inc_mem_stat: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] // 0) == 2 2 90 92
Condition100%2/2
$_[2] // 0
l!l
1775
MC/DC100%1/1
$_[2] // 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
1075  
1076 90 ▶ unless(exists $_[0]->{'stats'}{'memory_usage'}{$_[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[0]{"stats"}{"memory_usage"}{$_[1]}) 17 73 90
1077 17 $_[0]->{'stats'}{'memory_usage'}{$_[1]} = [qw(0 0)];
1078 }
1079  
1080 90 ▶ ++$_[0]->{'stats'}{'memory_usage'}{$_[1]}[$_[2] ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 12 78 90
1081 }
1082  
1083 =head2 add_snapshot(machine, addr, value)
1084  
1085 Add replayable snapshot where memory slot addr changes to value.
1086  
1087 =over 4
1088  
1089 =item B<machine>=I<machine ref>
1090  
1091 =item B<addr>=I<number>
1092  
1093 =item B<value>=I<number>
1094  
1095 =back
1096  
1097 =cut
1098  
1099 sub add_snapshot {
1100 1 ▶ push @{$_[0]->{'snaps'}{$_[0]->{'steps'}}}, ($_[1], $_[2]);
Subroutine add_snapshot: called CC=1 SCAR=0.0
Pod documented
1101 }
1102  
1103 =head2 report(machine, message)
1104  
1105 Set error string of machine to message.
1106  
1107 =over 4
1108  
1109 =item B<machine>=I<machine ref>
1110  
1111 =item B<message>=I<string>
1112  
1113 =back
1114  
1115 Returns undef.
1116  
1117 =cut
1118  
1119 sub report {
1120 15 ▶ $_[0]->{'error'} = $_[1];
Subroutine report: called CC=1 SCAR=0.0
Pod documented
1121 15 return undef;
1122 }
1123  
1124 =head1 AUTHOR
1125  
1126 Fabian Stiewitz, C<< <deprint at cpan.org> >>
1127  
1128 =head1 BUGS
1129  
1130 Please report any bugs or feature requests to C<bug-language-ram at rt.cpan.org>, or through
1131 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Language-RAM>. I will be notified, and then you'll
1132 automatically be notified of progress on your bug as I make changes.
1133  
1134  
1135  
1136  
1137 =head1 SUPPORT
1138  
1139 You can find documentation for this module with the perldoc command.
1140  
1141 perldoc Language::RAM
1142  
1143  
1144 You can also look for information at:
1145  
1146 =over 4
1147  
1148 =item * RT: CPAN's request tracker (report bugs here)
1149  
1150 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Language-RAM>
1151  
1152 =item * AnnoCPAN: Annotated CPAN documentation
1153  
1154 L<http://annocpan.org/dist/Language-RAM>
1155  
1156 =item * CPAN Ratings
1157  
1158 L<http://cpanratings.perl.org/d/Language-RAM>
1159  
1160 =item * Search CPAN
1161  
1162 L<http://search.cpan.org/dist/Language-RAM/>
1163  
1164 =back
1165  
1166  
1167 =head1 ACKNOWLEDGEMENTS
1168  
1169  
1170 =head1 LICENSE AND COPYRIGHT
1171  
1172 Copyright 2015 Fabian Stiewitz.
1173  
1174 This program is free software; you can redistribute it and/or modify it
1175 under the terms of the the Artistic License (2.0). You may obtain a
1176 copy of the full license at:
1177  
1178 L<http://www.perlfoundation.org/artistic_license_2_0>
1179  
1180 Any use, modification, and distribution of the Standard or Modified
1181 Versions is governed by this Artistic License. By using, modifying or
1182 distributing the Package, you accept this license. Do not use, modify,
1183 or distribute the Package, if you do not accept this license.
1184  
1185 If your Modified Version has been derived from a Modified Version made
1186 by someone other than you, you are nevertheless required to ensure that
1187 your Modified Version complies with the requirements of this license.
1188  
1189 This license does not grant you the right to use any trademark, service
1190 mark, tradename, or logo of the Copyright Holder.
1191  
1192 This license includes the non-exclusive, worldwide, free-of-charge
1193 patent license to make, have made, use, offer to sell, sell, import and
1194 otherwise transfer the Package with respect to any patent claims
1195 licensable by the Copyright Holder that are necessarily infringed by the
1196 Package. If you institute patent litigation (including a cross-claim or
1197 counterclaim) against any party alleging that the Package constitutes
1198 direct or contributory patent infringement, then this Artistic License
1199 to you shall terminate on the date that such litigation is filed.
1200  
1201 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1202 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1203 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1204 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1205 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1206 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1207 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1208 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1209  
1210  
1211 =cut
1212  
1213 21 1; # End of Language::RAM
↑ Summary script/ram.pl »