| 1 | package VM::Dreamer::Validate; | ||||||||||||||||||||
| 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 | ▶ | use VM::Dreamer::Environment qw( get_restrictions ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use VM::Dreamer::Error qw( missing_term invalid_term ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | require Exporter; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 14 | 2 | our @EXPORT_OK = qw( validate_definition build_valid_line_regex get_valid_input_regex ); | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | our $restrictions = get_restrictions(); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 18 | ▶ | sub validate_definition { | ||||||||||||||||||
|
Subroutine
validate_definition: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 19 | 18 | my $machine_definition = shift; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 18 | foreach my $term ( qw( base op_code_width operand_width ) ) { | |||||||||||||||||||
| 22 | 42 | ▶ | unless ( defined $machine_definition->{$term} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 3 | die missing_term($term); | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | 39 | ▶ | unless ( validate_term( $term, $machine_definition->{$term} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 9 | die invalid_term( $term, $machine_definition->{$term} ); | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 6 | return 1; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 39 | ▶ | sub validate_term { | ||||||||||||||||||
|
Subroutine
validate_term: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 34 | 39 | my( $term, $value ) = @_; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 39 | ▶ | if( $value !~ /^[1-9]\d*$/ ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 37 | 5 | return 0; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | ▶ | elsif( $value < $restrictions->{$term}->{min} || | |||||||||||||||||||
|
Condition100%3/3
$value < $VM::Dreamer::Validate::restrictions->{$term}{"min"} or $value > $VM::Dreamer::Validate::restrictions->{$term}{"max"}
MC/DC100%2/2
$value < $VM::Dreamer::Validate::restrictions->{$term}{"min"} or $value > $VM::Dreamer::Validate::restrictions->{$term}{"max"}
$value < $VM::Dreamer::Validate::restrictions->{$term}{"min"}
$value > $VM::Dreamer::Validate::restrictions->{$term}{"max"}
Truth table100%3/3
A or B
A= $value < $VM::Dreamer::Validate::restrictions->{$term}{"min"}B= $value > $VM::Dreamer::Validate::restrictions->{$term}{"max"}
| |||||||||||||||||||||
| 40 | $value > $restrictions->{$term}->{max} ) { | ||||||||||||||||||||
| 41 | 4 | return 0; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||
| 44 | 30 | return 1; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 3 | ▶ | sub build_valid_line_regex { | ||||||||||||||||||
|
Subroutine
build_valid_line_regex: called CC=4 SCAR=17.2
Pod
documented
| |||||||||||||||||||||
| 49 | 3 | my $machine = shift; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | my( $greatest_digit, $operand_width, $instruction_width ) = ( | ||||||||||||||||||||
| 52 | $machine->{meta}->{greatest}->{digit}, | ||||||||||||||||||||
| 53 | $machine->{meta}->{width}->{operand}, | ||||||||||||||||||||
| 54 | $machine->{meta}->{width}->{instruction} | ||||||||||||||||||||
| 55 | 3 | ); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # consider replacing above with a slice | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 3 | ▶ | if ( ! defined $greatest_digit ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 60 | 0 | die "Please pass the machine's greatest digit to build_valid_line_regex\n"; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | elsif ( ! defined $operand_width ) { | ||||||||||||||||||||
| 63 | 0 | die "Please pass the machine's op code width to build_valid_line_regex\n"; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | elsif( ! defined $instruction_width ) { | ||||||||||||||||||||
| 66 | 0 | die "Please pass the machine's instruction width to build_valid_line_regex\n"; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 3 | return qr/^[0-$greatest_digit]{$operand_width}\t[0-$greatest_digit]{$instruction_width}$/; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | sub get_valid_input_regex { | ||||||||||||||||||
|
Subroutine
get_valid_input_regex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 74 | 0 | 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 | 2 | 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 <william at coders dot coop> | ||||||||||||||||||||
| 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 | ||||||||||||||||||||