Dreamer/IO.pm

filter: Statementstmt 24.1% 14 / 58 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 3 / 8 Podpod 100% 5 / 5 totaltotal 25.8% 22 / 85 CC 8 SCAR 36.7
CC
8
Cov
19%
get_valid_input_from_user
72.0
add_input_to_inbox
2.0
shift_inbox_to_memory
2.0
CRAP 39.1
« Dreamer/Util.pm ↑ Summary Dreamer/Dump.pm »
1 package VM::Dreamer::IO;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.851';
7  
8 1 ▶use VM::Dreamer::Validate qw{ get_valid_input_regex };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT_OK = qw( get_valid_input_from_user add_input_to_inbox shift_inbox_to_memory add_to_outbox shift_outbox_to_user );
14  
15 0 ▶sub get_valid_input_from_user {
Subroutine get_valid_input_from_user: not called CC=8 SCAR=42.8
Pod documented
16 0 my $machine = shift;
17 0 my $max_tries = 5;
18  
19 0 ▶ unless ($machine) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($machine) 0 0 0
20 0 die "Please pass the machine href to get_input_from_user";
21 }
22  
23 0 my $input;
24 0 my $valid_input = get_valid_input_regex($machine);
25  
26 0 my $greatest_instruction = $machine->{meta}->{greatest}->{instruction};
27 0 my $base = $machine->{meta}->{base};
28 0 my $instruction_width = $machine->{meta}->{width}->{instruction};
29  
30 0 my $tries;
31 0 ▶ for ( $tries = 1; $tries <= $max_tries; $tries++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries <= $max_tries) 0 0 0
32 0 print "Please enter a number from 0 to $greatest_instruction in base $base\n";
33  
34 0 $input = readline(*STDIN);
35 0 chomp $input;
36 0 my $length = length($input);
37  
38 0 ▶ if (!$length) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $length) { } 0 0 0
elsif ($length > $instruction_width) { } 0 0 0
elsif ($length < $instruction_width) { } 0 0 0
39 0 print "It doesn't look like you entered any characters. ";
40 0 next;
41 }
42 elsif ( $length > $instruction_width ) {
43 0 print "It looks like you entered too many characters. ";
44 0 next;
45 }
46 elsif ( $length < $instruction_width ) {
47 0 $input = sprintf "%03s", $input;
48 }
49  
50 0 ▶ if ( $input !~ $valid_input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $input =~ /$valid_input/) { } 0 0 0
51 0 print "Your input doesn't look valid. ";
52 0 next;
53 }
54 else {
55 0 last;
56 }
57 }
58  
59 0 ▶ if ( $tries > $max_tries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries > $max_tries) { } 0 0 0
60 0 die "Did not receive valid input after $max_tries attempts. Please restart your program\n";
61 }
62 else {
63 0 return $input;
64 }
65 }
66  
67 0 ▶sub add_input_to_inbox {
Subroutine add_input_to_inbox: not called CC=1 SCAR=6.9
Pod documented
68 0 my ( $machine, $input ) = @_;
69  
70 0 push @{$machine->{inbox}}, $input;
71  
72 0 return 0;
73  
74 # maybe make this more generic and just call it add_to_inbox
75  
76 # how would I abstract the keyboard / display? what would they
77 # look like? how would I represent them in this code?
78 }
79  
80 0 ▶sub shift_inbox_to_memory {
Subroutine shift_inbox_to_memory: not called CC=1 SCAR=6.9
Pod documented
81 0 my ( $machine, $address ) = @_;
82  
83 0 $machine->{memory}->{$address} = shift @{$machine->{inbox}};
84  
85 0 return 0;
86  
87 # like shift_outbox, maybe make this more generic
88 # at some point and have a target of where to shift
89 # it
90 }
91  
92 0 ▶sub add_to_outbox {
Subroutine add_to_outbox: not called CC=1 SCAR=6.9
Pod documented
93 0 my ( $machine, $operand ) = @_;
94  
95 0 push @{$machine->{outbox}}, $machine->{memory}->{$operand};
96  
97 0 return 0;
98 }
99  
100 0 ▶sub shift_outbox_to_user {
Subroutine shift_outbox_to_user: not called CC=1 SCAR=6.9
Pod documented
101 0 my $machine = shift;
102  
103 0 print shift ( @{$machine->{outbox}} ) . "\n";
104  
105 0 return 0;
106  
107 # maybe, at some point, make this more generic
108 # where it is just called shift_outbox and there
109 # is a target of where to shift it to...
110  
111 # consider stripping leading 0's when outputting
112 # to user
113 }
114  
115 1 1;
116  
117 =pod
118  
119 =head1 NAME
120  
121 VM::Dreamer::IO - IO functionality for Dreamer
122  
123 =head1 SYNOPSIS
124  
125 get_valid_input_from_user($machine)
126 add_input_to_inbox( $machine, $input )
127 shift_inbox_to_memory( $machine, $address )
128  
129 add_to_outbox( $machine, $operand )
130 shift_outbox_to_user($machine)
131  
132 =head1 DESCRIPTION
133  
134 This module handles IO functions for Dreamer. This just means that it can:
135  
136 =over 12
137  
138 =item get input and validate it
139 =item add the input to the Inbox stack
140 =item shift the "oldest" entry in the inbox to an address in memory
141  
142 =item add an item in memory to the Outbox stack
143 =item output the "oldest" entry in the outbox to the user
144  
145 =back
146  
147 =head1 SUBROUTINES
148  
149 =head2 get_valid_input_from_user
150  
151 Prompts the user for input. Valid input is returned otherwise the user is told what they did wrong and asked to try again. Raises an exception if max_tries is exceeded by the user.
152  
153 Note: The user doesn't need to zero-pad their input. They can do so if they like, but the input can't have more digits than the largest number they can enter.
154  
155 I.e. if the largest number they can enter is 999, 15 and 015 are acceptable, but 0015 would be rejected.
156  
157 =head2 add_input_to_inbox
158  
159 Pushes input to the "top" of the Inbox.
160  
161 =head2 shift_inbox_to_memory
162  
163 Shifts the "bottom" of the inbox to an address in memory.
164  
165 =head2 add_to_outbox
166  
167 Adds the information stored at an address in memory to the "top" of the Outbox.
168  
169 =head2 shift_outbox_to_user
170  
171 Putputs the value at the "bottom" of the Outbox to the user. Note all output is zero-padded to have the same number of digits as the "width" of each address in memory.
172  
173 This means that if your memory is 8 digits wide and each digit can be between 0 and 7, the number 715 would be output as 00000715.
174  
175 This may change in a future release.
176  
177 =head1 CONSUMPTION
178  
179 Together, the first three methods are used to implement VM::Dreamer::Instructions::input_to_mb and the last two are used to implemtn VM::Dreamer::Instructions::output_from_mb. These in turn are the INP and OUT operations for Grasshopper.
180  
181 =head1 CAVEATS
182  
183 I've tried to follow the FIFO concept of First In, First Out; however, I don't know how well I understand the underlying concepts. If you know better and see any conceptual issues with the implementation, please let me know.
184  
185 =head1 AUTHOR
186  
187 William Stevenson <william at coders dot coop>
188  
189 =head1 SEE ALSO
190  
191 VM::Dreamer::Instructions
192  
193 =head1 COPYRIGHT AND LICENSE
194  
195 This software is Copyright (c) 2013 by William Stevenson.
196  
197 This is free software, licensed under:
198  
199 The Artistic License 2.0 (GPL Compatible)
200
201 =cut
« Dreamer/Util.pm ↑ Summary Dreamer/Dump.pm »