line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package VM::Dreamer::Validate; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
61
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
84
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.851'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1304
|
use VM::Dreamer::Environment qw( get_restrictions ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
123
|
|
9
|
2
|
|
|
2
|
|
1119
|
use VM::Dreamer::Error qw( missing_term invalid_term ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1410
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
require Exporter; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw( validate_definition build_valid_line_regex get_valid_input_regex ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $restrictions = get_restrictions(); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub validate_definition { |
19
|
18
|
|
|
18
|
1
|
23
|
my $machine_definition = shift; |
20
|
|
|
|
|
|
|
|
21
|
18
|
|
|
|
|
34
|
foreach my $term ( qw( base op_code_width operand_width ) ) { |
22
|
42
|
100
|
|
|
|
108
|
unless ( defined $machine_definition->{$term} ) { |
23
|
3
|
|
|
|
|
10
|
die missing_term($term); |
24
|
|
|
|
|
|
|
} |
25
|
39
|
100
|
|
|
|
121
|
unless ( validate_term( $term, $machine_definition->{$term} ) ) { |
26
|
9
|
|
|
|
|
28
|
die invalid_term( $term, $machine_definition->{$term} ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
6
|
|
|
|
|
16
|
return 1; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub validate_term { |
34
|
39
|
|
|
39
|
0
|
54
|
my( $term, $value ) = @_; |
35
|
|
|
|
|
|
|
|
36
|
39
|
100
|
100
|
|
|
294
|
if( $value !~ /^[1-9]\d*$/ ) { |
|
|
100
|
|
|
|
|
|
37
|
5
|
|
|
|
|
14
|
return 0; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
elsif( $value < $restrictions->{$term}->{min} || |
40
|
|
|
|
|
|
|
$value > $restrictions->{$term}->{max} ) { |
41
|
4
|
|
|
|
|
11
|
return 0; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
30
|
|
|
|
|
95
|
return 1; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub build_valid_line_regex { |
49
|
3
|
|
|
3
|
1
|
4
|
my $machine = shift; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
|
|
|
12
|
my( $greatest_digit, $operand_width, $instruction_width ) = ( |
52
|
|
|
|
|
|
|
$machine->{meta}->{greatest}->{digit}, |
53
|
|
|
|
|
|
|
$machine->{meta}->{width}->{operand}, |
54
|
|
|
|
|
|
|
$machine->{meta}->{width}->{instruction} |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# consider replacing above with a slice |
58
|
|
|
|
|
|
|
|
59
|
3
|
50
|
|
|
|
12
|
if ( ! defined $greatest_digit ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
die "Please pass the machine's greatest digit to build_valid_line_regex\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
elsif ( ! defined $operand_width ) { |
63
|
0
|
|
|
|
|
0
|
die "Please pass the machine's op code width to build_valid_line_regex\n"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
elsif( ! defined $instruction_width ) { |
66
|
0
|
|
|
|
|
0
|
die "Please pass the machine's instruction width to build_valid_line_regex\n"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
81
|
return qr/^[0-$greatest_digit]{$operand_width}\t[0-$greatest_digit]{$instruction_width}$/; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub get_valid_input_regex { |
74
|
0
|
|
|
0
|
1
|
|
my $machine = shift; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $greatest_digit = $machine->{meta}->{greatest}->{digit}; |
77
|
0
|
|
|
|
|
|
my $instruction_width = $machine->{meta}->{width}->{instruction}; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
return qr/^[0-$greatest_digit]{$instruction_width}$/; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
# should be consistent on naming of functions - either call them get_valid |
82
|
|
|
|
|
|
|
# or build_valid, but not have one build_valid_line_regex and the other |
83
|
|
|
|
|
|
|
# get_valid_input_regex |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# sub validate_program_line { |
86
|
|
|
|
|
|
|
# my $line = shift; |
87
|
|
|
|
|
|
|
# my $meta = shift; |
88
|
|
|
|
|
|
|
# |
89
|
|
|
|
|
|
|
# my $greatest_digit = $meta->{greatest_digit}; |
90
|
|
|
|
|
|
|
# my $operand_width = $meta->{operand_width}; |
91
|
|
|
|
|
|
|
# my $total_width = $meta->{total_width}; |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
# my $regex = qr/^[0-$greatest_digit]{$operand_width}\t[0-$greatest_digit]{$total_width}$/; |
94
|
|
|
|
|
|
|
# |
95
|
|
|
|
|
|
|
# if ( $line =~ $regex ) { |
96
|
|
|
|
|
|
|
# return; |
97
|
|
|
|
|
|
|
# } |
98
|
|
|
|
|
|
|
# else { |
99
|
|
|
|
|
|
|
# die "Line was not properly formatted: $line\n"; |
100
|
|
|
|
|
|
|
# } |
101
|
|
|
|
|
|
|
# } |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=pod |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 NAME |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
VM::Dreamer::Validate - Quality In / Quality Out |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SYNOPSIS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
validate_definition( $machine_definition ); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 DESCRIPTION |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
These functions help make sure that what comes in is what is expected. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 validate_definition |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Validates the machine's definition. Returns 1 if the definition is value. Otherwise it raises an exception. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 build_valid_line_regex |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Takes the machine's greatest digit, operand_width and instruction_width and returns a regex corresponding to a valid line in an input file to your machine. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $machine = { |
128
|
|
|
|
|
|
|
meta => { |
129
|
|
|
|
|
|
|
greatest => { |
130
|
|
|
|
|
|
|
digit => 9, |
131
|
|
|
|
|
|
|
}, |
132
|
|
|
|
|
|
|
}, |
133
|
|
|
|
|
|
|
width => { |
134
|
|
|
|
|
|
|
operand => 2, |
135
|
|
|
|
|
|
|
instruction => 3, |
136
|
|
|
|
|
|
|
}, |
137
|
|
|
|
|
|
|
}; |
138
|
|
|
|
|
|
|
my $valid_line = build_valid_line_regex($machine); # qr/^[0-9]{2}\t[0-9]{3}$/ |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $machine = { |
141
|
|
|
|
|
|
|
meta => { |
142
|
|
|
|
|
|
|
greatest => { |
143
|
|
|
|
|
|
|
digit => 8, |
144
|
|
|
|
|
|
|
}, |
145
|
|
|
|
|
|
|
}, |
146
|
|
|
|
|
|
|
width => { |
147
|
|
|
|
|
|
|
operand => 6, |
148
|
|
|
|
|
|
|
instruction => 8, |
149
|
|
|
|
|
|
|
}, |
150
|
|
|
|
|
|
|
}; |
151
|
|
|
|
|
|
|
my $valid_line = build_valid_line_regex($machine); # qr/^[0-7]{6}\t[0-7]{8}$/ |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 get_valid_input_regex |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
my $machine = { |
156
|
|
|
|
|
|
|
meta => { |
157
|
|
|
|
|
|
|
greatest => { |
158
|
|
|
|
|
|
|
digit => 1, |
159
|
|
|
|
|
|
|
}, |
160
|
|
|
|
|
|
|
}, |
161
|
|
|
|
|
|
|
width => { |
162
|
|
|
|
|
|
|
instruction => 16, |
163
|
|
|
|
|
|
|
}, |
164
|
|
|
|
|
|
|
}; |
165
|
|
|
|
|
|
|
my $valid_input = get_valid_input_regex($machine); # qr/^[0-1]{16}$/ |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 AUTHOR |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
William Stevenson |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This software is Copyright (c) 2013 by William Stevenson. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
This is free software, licensed under: |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |