Expression/Env.pm

filter: Statementstmt 73.3% 44 / 60 Branchbran 54.7% 23 / 42 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 6 / 10 Podpod 100% 5 / 5 totaltotal 64.7% 79 / 122 CC 25 SCAR 30.6
CC
25
Cov
65%
_validate_ast
31.7
_check_const_type
12.0
new
3.1
CRAP 21.2
↑ Summary Expression.pm »
1 package Data::Typed::Expression::Env;
2  
3 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Data::Typed::Expression::Env - Evalutation environment for typed expressions
11  
12 =head1 VERSION
13  
14 Version 0.005
15  
16 =cut
17  
18 3 our $VERSION = '0.005';
19  
20 =head1 SYNOPSIS
21  
22 See L<Data::Typed::Expression>.
23  
24 =head1 DESCRIPTION
25  
26 An environment is an object describing known types and variables to be used by
27 expressions of type L<Data::Typed::Expression>. Types can be marked as simple
28 ones or defined as compounds (structures) with fields of other types.
29  
30 =head1 METHODS
31  
32 =head2 new
33  
34 Creates a new enviroment in which expressions can be evaluated.
35  
36 Arguments are two hashrefs, the first one containing types declarations, the
37 second one containing variables definitions.
38  
39 Each type declaration is a single hash entry, with type name being the key and
40 type definition being the value.
41  
42 Type definition is either C<undef>, when there is nothing "internal" about the
43 type or a hashref, for compound types. Each entry in compound type definition
44 is a mapping from a compound element name to its type name.
45  
46 For example, if we define the C<car> type as having "color" property, which is
47 represented as RGB triple, "price" as double and "name" as a string, the
48 corresponding type definitions can look like:
49  
50 {
51 car => {
52 color => 'color',
53 price => 'double',
54 name => 'string'
55 },
56 color => {
57 r => 'double',
58 g => 'double',
59 b => 'double'
60 },
61 double => undef,
62 string => undef
63 }
64  
65 Variables definition is a mapping from variable name to its type name.
66  
67 =cut
68  
69 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
70 1 my ($class, $types, $vars) = @_;
71 1 ▶ $vars = { } unless defined $vars;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vars 0 1 1
72 1 ▶ $types = { } unless defined $types;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $types 0 1 1
73 1 my $self = {
74 t => $types,
75 v => $vars
76 };
77
78 1 return bless $self, $class;
79 }
80  
81 =head2 new_with
82  
83 Creates a new environment based on the current one.
84  
85 The created environment contains all the types and variables from the current
86 environment, as well as the new types and variables passed as the arguments,
87 in the same way as to L<new()|/new> method.
88  
89 If types or variables which are defined in the current object and also passed
90 as parameters, definitions given in parameters override the current ones in the
91 created object.
92  
93 =cut
94  
95 0 ▶sub new_with {
Subroutine new_with: not called CC=1 SCAR=6.9
Pod documented
96 0 my ($self, $types, $vars) = @_;
97 my $t = {
98 0 %{$self->{t}},
99 %$types
100 };
101 my $v = {
102 0 %{$self->{v}},
103 %$vars
104 };
105 0 return (ref $self)->new($t, $v);
106 }
107  
108 =head2 get_type_def
109  
110 Returns type definition for a given type name, as passed to L<new()|/new>.
111  
112 =cut
113  
114 0 ▶sub get_type_def {
Subroutine get_type_def: not called CC=1 SCAR=6.9
Pod documented
115 0 return $_[0]->{t}{$_[1]};
116 }
117  
118 =head2 get_var_type
119  
120 Returns variable type name, as passed to L<new()|/new>.
121  
122 =cut
123  
124 0 ▶sub get_var_type {
Subroutine get_var_type: not called CC=1 SCAR=6.9
Pod documented
125 0 return $_[0]->{v}{$_[1]};
126 }
127  
128 =head2 validate
129  
130 Checks if the given expression represents a valid one, in the context of the
131 current environent.
132  
133 For expression to be valid, all used variables, types and components must exist
134 in the environment, and appropriate operators arguments must be of special and
135 coherent types (e.g. C<int> for array indexing or C<int> or C<double> for
136 mathematical operations).
137  
138 Returns name of the type of passed expression.
139  
140 =cut
141  
142 9 ▶sub validate {
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
143 9 return $_[0]->_validate_ast($_[1]->{ast});
144 }
145  
146 0 ▶sub _check_const_type {
Subroutine _check_const_type: not called CC=3 SCAR=24.8
147 0 ▶ if ($_[0] =~ /^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\d+(\.\d+)?$/) 0 0 0
148 0 ▶ return (defined $2) ? 'double' : 'int';
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 0 0 0
149 }
150
151 0 undef;
152 }
153  
154 50 ▶sub _validate_ast {
Subroutine _validate_ast: called CC=21 SCAR=34.6
155 50 my ($self, $ast) = @_;
156 50 ▶ return '' unless defined $ast;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ast 0 50 50
157
158 my ($op, $arg) = (ref $ast) ?
159 50 ▶ ($ast->{op}, $ast->{arg}) :
Branch: 1 branch
BranchTrueFalseTotal
ref $ast ? : 50 0 50
160 ('V', $ast);
161  
162 50 ▶ if ($op eq 'I') {
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq 'I') { } 9 41 50
elsif ($op eq 'D') { } 0 41 41
elsif ($op eq 'V') { } 14 27 41
elsif ($op eq '.') { } 13 14 27
elsif ($op =~ m[[-+*/]]) { } 6 8 14
elsif ($op eq '[]') { } 8 0 8
163 9 return 'int';
164 } elsif ($op eq 'D') {
165 0 return 'double';
166 } elsif ($op eq 'V') {
167 14 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) 0 14 14
168 0 $arg = $arg->[0];
169 }
170 14 ▶ croak "Undefined var: $arg" unless exists $self->{v}{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"v"}{$arg} 1 13 14
171 13 return $self->{v}{$arg};
172 } elsif ($op eq '.') {
173 13 ▶ if (ref $arg->[1] && $arg->[1]{op} ne 'V') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg->[1] and $arg->[1]{"op"} ne "V") 0 13 13
Condition33%1/3
ref $arg->[1] and $arg->[1]{"op"} ne "V"
!ll&&!rl&&r
0130
MC/DC0%0/2
ref $arg->[1] and $arg->[1]{"op"} ne "V"
ref $arg->[1] $arg->[1]{"op"} ne "V"
Truth table33%1/3
A and B
A= ref $arg->[1]B= $arg->[1]{"op"} ne "V"
ABresult
0X0
100
111
174 0 croak "Unexpected element type ($arg->[1]{op}) on right side of '.'";
175 }
176 13 my $subt = $self->_validate_ast($arg->[0]);
177 11 my $e = $arg->[1]{arg};
178 croak "Tried to get elements of simple type ($subt)"
179 11 ▶ unless ref $self->{t}{$subt};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"t"}{$subt} 0 11 11
180 croak "Type ($subt) has no element named $e"
181 11 ▶ unless exists $self->{t}{$subt}{$e};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"t"}{$subt}{$e} 0 11 11
182 11 return $self->{t}{$subt}{$e};
183 } elsif ($op =~ m{[-+*/]}) {
184 6 my $t = 'int';
185 6 for (@$arg) {
186 12 my $tt = $self->_validate_ast($_);
187 12 ▶ if ($tt eq 'int') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tt eq 'int') { } 12 0 12
elsif ($tt eq 'double') { } 0 0 0
188 # fine
189 } elsif ($tt eq 'double') {
190 0 $t = 'double';
191 } else {
192 0 croak "Arithmetic operation ($op) on non-numeric type ($t)";
193 }
194 }
195 6 return $t;
196 } elsif ($op eq '[]') {
197 8 my ($arr, @ind) = @$arg;
198 8 my $subt = $self->_validate_ast($arr);
199 8 for (@ind) {
200 8 my $indt = $self->_validate_ast($_);
201 8 ▶ croak "Can't index ($subt) with non-int ($indt) type"
Branch: 1 branch
BranchTrueFalseTotal
if $indt ne "int" 0 8 8
202 if $indt ne 'int';
203 }
204 8 my $indbr = '[]' x int(@ind);
205 8 ▶ $subt =~ s/\Q$indbr\E$// or
Branch: 1 branch
BranchTrueFalseTotal
$subt =~ s/\Q$indbr\E$// or croak("Tried to index non-array type ($subt) with ($indbr)") 8 0 8
206 croak "Tried to index non-array type ($subt) with ($indbr)";
207 8 return $subt;
208 }
209 }
210  
211 3 1;
212  
↑ Summary Expression.pm »