File Coverage

blib/lib/VM/Dreamer/Init.pm
Criterion Covered Total %
statement 26 28 92.8
branch 2 4 50.0
condition 2 6 33.3
subroutine 6 6 100.0
pod 4 4 100.0
total 40 48 83.3


line stmt bran cond sub pod time code
1             package VM::Dreamer::Init;
2              
3 2     2   10 use strict;
  2         4  
  2         63  
4 2     2   10 use warnings;
  2         4  
  2         831  
5              
6             our $VERSION = '0.851';
7              
8             require Exporter;
9              
10             our @ISA = qw(Exporter);
11             our @EXPORT_OK = qw( init_counter greatest_digit greatest_number total_width );
12              
13             sub init_counter {
14 13     13 1 21 my $width = shift;
15              
16 13         17 my @array;
17              
18 13         32 for ( my $i = 0; $i < $width; $i++ ) {
19 99         189 push @array, 0;
20             }
21              
22 13         47 return \@array;
23             }
24              
25             sub greatest_digit {
26 24     24 1 26 my $base = shift;
27 24         38 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             sub greatest_number {
36 18     18 1 20 my ( $base, $width ) = @_;
37              
38 18 50 33     69 unless ( $base && $width ) {
39 0         0 die "Please pass the machine's base and this number's width\n";
40             }
41              
42 18         28 my $greatest_digit = greatest_digit($base);
43 18         21 my $greatest_number = '';
44              
45 18         38 for ( my $i = 1; $i <= $width; $i++ ) {
46 108         187 $greatest_number .= $greatest_digit;
47             }
48              
49 18         98 return $greatest_number;
50             }
51              
52             sub total_width {
53 6     6 1 13 my @instruction_part_lengths = @_;
54              
55 6         9 my $total_width = 0;
56              
57 6         11 foreach my $width (@instruction_part_lengths) {
58 12 50 33     82 unless( $width =~ /^0$/ || $width =~ /^[1-9]\d*$/ ) {
59 0         0 die "The widths may only be zero or a positive interger";
60             }
61             else {
62 12         21 $total_width += $width;
63             }
64             }
65              
66 6         18 return $total_width;
67             }
68              
69             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
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