Dreamer/Init.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran 75.0% 6 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 4 / 4 totaltotal 80.3% 49 / 61 CC 8 SCAR 11.9
CC
8
Cov
83%
total_width
4.6
greatest_number
4.3
init_counter
2.0
CRAP 3.3
« Dreamer/Validate.pm ↑ Summary Dreamer/Operation.pm »
1 package VM::Dreamer::Init;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.851';
7  
8 2 require Exporter;
9  
10 2 our @ISA = qw(Exporter);
11 2 our @EXPORT_OK = qw( init_counter greatest_digit greatest_number total_width );
12  
13 13 ▶sub init_counter {
Subroutine init_counter: called CC=2 SCAR=6.9
Pod documented
14 13 my $width = shift;
15  
16 13 my @array;
17  
18 13 ▶ for ( my $i = 0; $i < $width; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $width) 99 13 112
19 99 push @array, 0;
20 }
21  
22 13 return \@array;
23 }
24  
25 24 ▶sub greatest_digit {
Subroutine greatest_digit: called CC=1 SCAR=0.0
Pod documented
26 24 my $base = shift;
27 24 return $base - 1;
28 }
29  
30 # should just be able to pass 1 to greatest_number
31  
32 # also, probably don't need to initialize
33 # $greatest_number to an empty string
34  
35 18 ▶sub greatest_number {
Subroutine greatest_number: called CC=4 SCAR=14.6
Pod documented
36 18 my ( $base, $width ) = @_;
37  
38 18 ▶ unless ( $base && $width ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base and $width) 0 18 18
Condition33%1/3
$base and $width
!ll&&!rl&&r
0018
MC/DC0%0/2
$base and $width
$base $width
Truth table33%1/3
A and B
A= $baseB= $width
ABresult
0X0
100
111
39 0 die "Please pass the machine's base and this number's width\n";
40 }
41  
42 18 my $greatest_digit = greatest_digit($base);
43 18 my $greatest_number = '';
44  
45 18 ▶ for ( my $i = 1; $i <= $width; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $width) 108 18 126
46 108 $greatest_number .= $greatest_digit;
47 }
48  
49 18 return $greatest_number;
50 }
51  
52 6 ▶sub total_width {
Subroutine total_width: called CC=4 SCAR=15.2
Pod documented
53 6 my @instruction_part_lengths = @_;
54  
55 6 my $total_width = 0;
56  
57 6 foreach my $width (@instruction_part_lengths) {
58 12 ▶ unless( $width =~ /^0$/ || $width =~ /^[1-9]\d*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width =~ /^0$/ or $width =~ /^[1-9]\d*$/) { } 12 0 12
Condition33%1/3
$width =~ /^0$/ or $width =~ /^[1-9]\d*$/
l!l&&r!l&&!r
0120
MC/DC0%0/2
$width =~ /^0$/ or $width =~ /^[1-9]\d*$/
$width =~ /^0$/ $width =~ /^[1-9]\d*$/
Truth table33%1/3
A or B
A= $width =~ /^0$/B= $width =~ /^[1-9]\d*$/
ABresult
1X1
011
000
59 0 die "The widths may only be zero or a positive interger";
60 }
61 else {
62 12 $total_width += $width;
63 }
64 }
65  
66 6 return $total_width;
67 }
68  
69 2 1;
70  
71 # init_counter takes an integer n and return a reference to an array which has n elements, each of which are 0
72  
73 # greatest digit takes a number n (the base) and returns n - 1
74  
75 # greatest_number takes two numbers n and r and returns a number p which has r digits, each of which are n
76  
77 # total width taks two numbers n and r and returns n + r
78  
79 =pod
80  
81 =head1 NAME
82  
83 VM::Dreamer::Init - Functions to help with Initialization
84  
85 =head1 SYNOPSIS
86  
87 my $counter = init_counter(8); # [ 0, 0, 0, 0, 0, 0, 0, 0 ]
88 my $greatest_digit = greatest_digit(8); # 7
89 my $greatest_number = greatest_number( 10, 4); # '9999'
90 my $total_width = total_width( 1, 2 ); # 3
91  
92 =head1 DESCRIPTION
93  
94 =head2 init_counter
95  
96 Takes a positive integer n and returns a reference to an an array of n elements, each of which is 0.
97  
98 In Dreamer, counters are machine parts like the counter and the accumulator.
99  
100 =head2 greatest_digit
101  
102 Takes a positive integer n and returns n - 1. Really only meant to be used for n from 2 to 10 inclusive, though no validation is performed here. The idea is that if the base is 8, the greatest digit would be 7.
103  
104 =head2 greatest_number
105  
106 Given a base and a width, returns a string of n digits, each of which is one less then the base.
107  
108 For example, if the base is 2 and the width is 9, the greatest_number would be 111111111.
109  
110 =head2 total_width
111  
112 Just adds up the elements in an array, but also performes validation checking to make sure that each element is zero or a positive integer.
113  
114 For example, if the op_code_width is 4 and the operand width is 12, the total_width would be 16. This is useful for figuring out how long an instruction is given the widths of its parts.
115  
116 As Dreamer only operates on one operand machines right now, this will really only be passed 2 elements in the array - the op_code_width and the operand_width. But, I decided to generalize it for use later.
117  
118 =head1 AUTHOR
119  
120 William Stevenson <william at coders dot coop>
121  
122 =head1 COPYRIGHT AND LICENSE
123  
124 This software is Copyright (c) 2013 by William Stevenson.
125  
126 This is free software, licensed under:
127  
128 The Artistic License 2.0 (GPL Compatible)
129
130 =cut
« Dreamer/Validate.pm ↑ Summary Dreamer/Operation.pm »