Dreamer/Instructions.pm

filter: Statementstmt 26.1% 17 / 65 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 26.6% 4 / 15 Podpod 100% 11 / 11 totaltotal 30.7% 32 / 104 CC 6 SCAR 15.0
CC
6
Cov
22%
branch_if_positive
12.0
load
6.0
branch_if_zero
6.0
CRAP 4.5
« Dreamer/Dump.pm ↑ Summary Dreamer.pm »
1 package VM::Dreamer::Instructions;
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::IO qw( get_valid_input_from_user add_input_to_inbox shift_inbox_to_memory add_to_outbox shift_outbox_to_user );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use VM::Dreamer::Util qw( arrayify_string stringify_array add_two_arrays subtract_two_arrays );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12  
13 1 our @ISA = qw(Exporter);
14 1 our @EXPORT_OK = qw( input_to_mb output_from_mb store load add subtract branch_always branch_if_zero branch_if_positive halt );
15  
16 0 ▶sub input_to_mb {
Subroutine input_to_mb: not called CC=1 SCAR=6.9
Pod documented
17 0 my ( $machine, $operand ) = @_;
18  
19 0 my $input = get_valid_input_from_user($machine);
20  
21 0 add_input_to_inbox( $machine, $input );
22  
23 0 shift_inbox_to_memory( $machine, $operand );
24  
25 0 return 0;
26 }
27  
28 0 ▶sub output_from_mb {
Subroutine output_from_mb: not called CC=1 SCAR=6.9
Pod documented
29 0 my ( $machine, $operand ) = @_;
30  
31 0 add_to_outbox( $machine, $operand );
32  
33 0 shift_outbox_to_user($machine);
34  
35 0 return 0;
36 }
37  
38 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
Pod documented
39 0 my ( $machine, $operand ) = @_;
40  
41 0 $machine->{memory}->{$operand} = stringify_array( $machine->{accumulator} );
42  
43 0 return 0;
44  
45 # Assuming that operand is a valid mailbox value
46 }
47  
48 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
Pod documented
49 0 my ( $machine, $operand ) = @_;
50  
51 0 ▶ if ( $machine->{memory}->{$operand} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($machine->{'memory'}{$operand}) { } 0 0 0
52 0 $machine->{accumulator} = arrayify_string( $machine->{memory}->{$operand} );
53 0 $machine->{n_flag} = 0;
54 }
55 else {
56 0 die "No value stored at address $operand to load onto the accumulator\n";
57 }
58  
59 0 return 0;
60 }
61  
62 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
63 0 my ( $machine, $operand ) = @_;
64  
65 0 my $augend = $machine->{accumulator};
66 0 my $addend = arrayify_string( $machine->{memory}->{$operand} );
67  
68 0 $machine->{accumulator} = add_two_arrays( $augend, $addend, $machine->{meta}->{greatest}->{digit} );
69  
70 0 return 0;
71 }
72  
73 0 ▶sub subtract {
Subroutine subtract: not called CC=1 SCAR=6.9
Pod documented
74 0 my ( $machine, $operand ) = @_;
75  
76 0 my $minuend = $machine->{accumulator};
77 0 my $subtrahend = arrayify_string( $machine->{memory}->{$operand} );
78  
79 0 ( $machine->{accumulator}, $machine->{n_flag} ) = subtract_two_arrays( $minuend, $subtrahend, $machine->{meta}->{greatest}->{digit} );
80  
81 0 return 0;
82 }
83  
84 0 ▶sub branch_always {
Subroutine branch_always: not called CC=1 SCAR=6.9
Pod documented
85 0 my ( $machine, $operand ) = @_;
86  
87 0 $machine->{counter} = arrayify_string($operand);
88  
89 0 return 0;
90 }
91  
92 0 ▶sub branch_if_zero {
Subroutine branch_if_zero: not called CC=2 SCAR=17.9
Pod documented
93 0 my ( $machine, $operand ) = @_;
94  
95 0 my $accumulator = stringify_array( $machine->{accumulator} );
96  
97 0 ▶ if ( $accumulator == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accumulator == 0) 0 0 0
98 0 $machine->{counter} = arrayify_string($operand);
99 }
100  
101 # in Perl, a string of zeros is treated as the number 0 when used
102 # in numeric context, e.g. '000' == 0 would be true
103  
104 0 return 0;
105 }
106  
107 0 ▶sub branch_if_negative {
Subroutine branch_if_negative: not called CC=2 SCAR=17.9
Pod documented
108 0 my ( $machine, $operand ) = @_;
109  
110 0 ▶ if ( $machine->{n_flag} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($machine->{"n_flag"} == 1) 0 0 0
111 0 $machine->{counter} = arrayify_string($operand);
112 }
113  
114 0 return 0;
115 }
116  
117 0 ▶sub branch_if_positive {
Subroutine branch_if_positive: not called CC=3 SCAR=24.8
Pod documented
118 0 my ( $machine, $operand ) = @_;
119  
120 0 my $accumulator = stringify_array( $machine->{counter} );
121  
122 0 ▶ if ( $accumulator > 0 && $machine->{n_flag} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accumulator > 0 and $machine->{"n_flag"} == 0) 0 0 0
Condition0%0/3
$accumulator > 0 and $machine->{"n_flag"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$accumulator > 0 and $machine->{"n_flag"} == 0
$accumulator > 0 $machine->{"n_flag"} == 0
Truth table0%0/3
A and B
A= $accumulator > 0B= $machine->{"n_flag"} == 0
ABresult
0X0
100
111
123 0 $machine->{counter} = arrayify_string($operand);
124 }
125  
126 # just like above, in Perl, a sting of numbers is treated like
127 # the number itself and padded zeros are ignored when used
128 # in numeric context
129  
130 # e.g. '0050' > 0 would be true, so there is no need, in this
131 # language, to strip them
132  
133 0 return 0;
134 }
135  
136 0 ▶sub halt {
Subroutine halt: not called CC=1 SCAR=6.9
Pod documented
137 0 my $machine = shift;
138  
139 0 $machine->{halt} = 1;
140  
141 0 return 0;
142 }
143  
144 1 1;
145  
146 =pod
147  
148 =head1 NAME
149  
150 VM::Dreamer::Instructions - The heavy lifting
151  
152 =head1 SYNOPSIS
153  
154 input_to_mb( $machine, '72' );
155 output_from_my( $machine, $operand );
156 store( $machine, $operand );
157 load( $machine, $operand );
158 add( $machine, $operand );
159 subtract( $machine, $operand );
160 branch_always( $machine, $operand );
161 branch_if_zero( $machine, $operand );
162 branch_if_negative( $machine, $operand );
163 branch_if_positive( $machine, $operand );
164 halt($machine);
165  
166 =head1 DESCRIPTION
167  
168 These functions will be used in a dispatch table in VM::Dreamer::Languages::YourMachine where you map your operation codes to these functions.
169  
170 A base 10 machine with an op code width of 1 might map op code 7 to branch_always. A base 2 machine with an opcode width of 4 might map 0101 to subtract.
171  
172 For an example, see VM::Dreamer::Languages::Grasshopper
173  
174 If you'd like to add more instructions, you can do so here in VM::Dreamer::Instructions and send me a pull request. You can also use VM::Dreamer::Local for your own code.
175  
176 =head2 input_to_mb
177  
178 input_to_mb( $machine, '72' );
179  
180 Prompts the user for input and, if valid, stores their input in memory at address '72'.
181  
182 =head2 output_from_mb
183  
184 output_from_mb( $machine, '72' );
185  
186 Outputs the value stored in memory at address '72' to the user.
187  
188 =head2 store
189  
190 store( $machine, '1011101' );
191  
192 Stores the value on the accumulator at address '1011101' in memory.
193  
194 =head2 load
195  
196 load( $machine, '271104523210' );
197  
198 Loads the value stored in memory at address '271104523210' onto the accumulator.
199  
200 =head2 add
201  
202 add( $machine, '10011100101001' );
203  
204 Adds the value stored in memory at address '10011100101001' to the current value on the accumulator.
205  
206 =head2 subtract( $machine, '9744321229' );
207  
208 Subtracts the value stored in memory at address '9744321229' from the current value on the accumulator.
209  
210 If the resulting value is negative, the negative flag is set and the remaining value on the accumulator is undefined.
211  
212 =head2 branch_always
213  
214 branch_always( $machine, '432' );
215  
216 Sets the value of the counter to 432, always.
217  
218 This means that the next instruction executed will be fetched from address 432 in memory.
219  
220 =head2 branch_if_zero
221  
222 branch_if_zero( $machine, '34221023' );
223  
224 Sets the value of the counter to 34221023 if the value on the accumulator is zero.
225  
226 In other words, if the accumulator is zero, fetch the next instruction from address 34221023 in memory.
227  
228 =head2 branch_if_negative
229  
230 branch_if_negative( $machine, '1521532' );
231  
232 Sets the value of the counter to 1521532 if the negative flag is set.
233  
234 In other words, if the negative flag is set, fetch the next instruction from address 1521532 in memory.
235  
236 =head2 branch_if_positive
237  
238 branch_if_positive( $machine, '92345' );
239  
240 Sets the value of the counter to 92345 if the value on the accumulator is greater than zero and the negative flag is not set.
241  
242 In other words, if the negative flag is unset and the accumulator is greater than 0, fetch the next instruction from address 92345 in memory.
243  
244 =head2 halt
245  
246 halt($machine);
247  
248 Sets the halt flag to 1. This means that the machine should cease operation.
249  
250 =head1 SEE ALSO
251  
252 VM::Dreamer::execute_next_instruction
253 VM::Dreamer::Languages::Grasshopper
254  
255 =head1 AUTHOR
256  
257 William Stevenson <william at coders dot coop>
258  
259 =head1 COPYRIGHT AND LICENSE
260  
261 This software is Copyright (c) 2013 by William Stevenson.
262  
263 This is free software, licensed under:
264  
265 The Artistic License 2.0 (GPL Compatible)
266
267 =cut
« Dreamer/Dump.pm ↑ Summary Dreamer.pm »