lib/Language/RAM.pm

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