blib/lib/Zuzu/Env.pm

filter: Statementstmt 93.5% 72 / 77 Branchbran 80.5% 29 / 36 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 20 / 20 Podpod 70.0% 7 / 10 totaltotal 89.9% 134 / 149 CC 21 SCAR 10.6
CC
21
Cov
90%
declare
5.0
set_weak_slot
4.2
alias_to_ref
4.0
CRAP 2.9
« blib/lib/Zuzu/Util/Number.pm ↑ Summary blib/lib/Zuzu/Value/Regexp.pm »
1 package Zuzu::Env;
2  
3 70 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 70 our $VERSION = '0.007002';
6  
7 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 70 has 'parent' => ( is => 'rw' );
10 48 ▶has 'slots' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
11 48 ▶has 'const' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 48 ▶has 'types' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
13 48 ▶has 'weak' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
14 48 ▶has 'special_props' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 163235 ▶sub _new_fast {
Subroutine _new_fast: called CC=1 SCAR=0.0
17 163235 my ( $class, $parent ) = @_;
18  
19 163235 return bless {
20 parent => $parent,
21 slots => {},
22 const => {},
23 types => {},
24 weak => {},
25 special_props => {},
26 }, $class;
27 }
28  
29 159247 ▶sub declare {
Subroutine declare: called CC=5 SCAR=16.1
Pod documented
30 my (
31 159247 $self, $name, $value, $is_const,
32 $declared_type, $is_weak_storage,
33 ) = @_;
34  
35 159247 ▶ die "Internal redeclare $name" if exists $self->{slots}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"slots"}{$name} 0 159247 159247
36 159247 $self->{slots}{$name} = \$value; # aliasing by reference
37 159247 ▶ $self->{const}{$name} = $is_const ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_const ? : 100750 58497 159247
38 159247 ▶ $self->{types}{$name} = $declared_type // 'Any';
Condition100%2/2
$declared_type // "Any"
l!l
51692107555
MC/DC100%1/1
$declared_type // "Any"
$declared_type
Truth table100%2/2
A
A= $declared_type
Aresult
11
00
39 159247 ▶ $self->{weak}{$name} = $is_weak_storage ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_weak_storage ? : 5 159242 159247
40  
41 159247 return $self->{slots}{$name};
42 }
43  
44 717674 ▶sub alias_to_ref {
Subroutine alias_to_ref: called CC=4 SCAR=13.9
Pod documented
45 my (
46 717674 $self, $name, $ref, $is_const,
47 $declared_type, $is_weak_storage,
48 ) = @_;
49  
50 717674 $self->{slots}{$name} = $ref;
51 717674 ▶ $self->{const}{$name} = $is_const ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_const ? : 693719 23955 717674
52 717674 ▶ $self->{types}{$name} = $declared_type // 'Any';
Condition100%2/2
$declared_type // "Any"
l!l
68290934765
MC/DC100%1/1
$declared_type // "Any"
$declared_type
Truth table100%2/2
A
A= $declared_type
Aresult
11
00
53 717674 ▶ $self->{weak}{$name} = $is_weak_storage ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_weak_storage ? : 1 717673 717674
54  
55 717674 return $ref;
56 }
57  
58 27374 ▶sub find_ref {
Subroutine find_ref: called CC=3 SCAR=11.0
Pod documented
59 27374 my ($self, $name) = @_;
60  
61 27374 my $env = $self;
62 27374 ▶ while ($env) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 28794 8741 37535
63 28794 ▶ return $env->{slots}{$name} if exists $env->{slots}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $env->{"slots"}{$name} 18633 10161 28794
64 10161 $env = $env->{parent};
65 }
66  
67 8741 return undef;
68 }
69  
70 8 ▶sub is_const_here {
Subroutine is_const_here: called CC=2 SCAR=6.9
Pod documented
71 8 my ($self, $name) = @_;
72  
73 70 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 8 ▶ return $self->{const}{$name} if exists $self->{const}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"const"}{$name} 3 5 8
75  
76 5 return undef;
77 }
78  
79 2 ▶sub is_weak_here {
Subroutine is_weak_here: called CC=2 SCAR=7.2
Pod documented
80 2 my ($self, $name) = @_;
81  
82 70 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶ return $self->{weak}{$name} if exists $self->{weak}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"weak"}{$name} 2 0 2
84  
85 0 return undef;
86 }
87  
88 4 ▶sub is_weak_slot {
Subroutine is_weak_slot: called CC=3 SCAR=11.2
Pod documented
89 4 my ($self, $name) = @_;
90  
91 4 my $env = $self;
92 4 ▶ while ($env) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 5 0 5
93 5 ▶ return $env->{weak}{$name} if exists $env->{weak}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $env->{"weak"}{$name} 4 1 5
94 1 $env = $env->{parent};
95 }
96  
97 0 return 0;
98 }
99  
100 2 ▶sub set_weak_slot {
Subroutine set_weak_slot: called CC=4 SCAR=14.5
Pod documented
101 2 my ( $self, $name, $is_weak_storage ) = @_;
102  
103 2 ▶ if ( exists $self->{slots}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"slots"}{$name}) 1 1 2
104 1 ▶ $self->{weak}{$name} = $is_weak_storage ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_weak_storage ? : 0 1 1
105 1 return $self->{weak}{$name};
106 }
107  
108 return $self->{parent}->set_weak_slot( $name, $is_weak_storage )
109 1 ▶ if $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"} 1 0 1
110  
111 0 return undef;
112 }
113  
114 3 ▶sub declared_type_for {
Subroutine declared_type_for: called CC=3 SCAR=12.7
Pod undocumented
115 3 my ( $self, $name ) = @_;
116  
117 3 my $env = $self;
118 3 ▶ while ($env) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 3 0 3
119 3 ▶ return $env->{types}{$name} if exists $env->{types}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $env->{"types"}{$name} 3 0 3
120 0 $env = $env->{parent};
121 }
122  
123 0 return 'Any';
124 }
125  
126 39 ▶sub get_special_prop {
Subroutine get_special_prop: called CC=3 SCAR=11.0
Pod undocumented
127 39 my ( $self, $key ) = @_;
128  
129 return $self->{special_props}{$key}
130 39 ▶ if exists $self->{special_props}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"special_props"}{$key} 4 35 39
131 return $self->{parent}->get_special_prop( $key )
132 35 ▶ if $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"} 6 29 35
133  
134 29 return undef;
135 }
136  
137 3 ▶sub set_special_prop {
Subroutine set_special_prop: called CC=1 SCAR=0.0
Pod undocumented
138 3 my ( $self, $key, $value ) = @_;
139  
140 3 $self->{special_props}{$key} = $value;
141 3 return $value;
142 }
143  
144 =pod
145  
146 =head1 NAME
147  
148 Zuzu::Env - lexical environment used for variable bindings
149  
150 =head1 DESCRIPTION
151  
152 Stores lexical bindings as references, tracks const-ness, and resolves
153 names through parent scopes.
154  
155 =head1 INHERITANCE
156  
157 Inherits from C<Moo::Object>.
158  
159 =head1 ROLES
160  
161 None.
162  
163 =head1 ATTRIBUTES
164  
165 =head2 parent
166  
167 Type: B<Maybe[InstanceOf["Zuzu::Env"]]>.
168  
169 Enclosing lexical scope, or undef for the global scope.
170  
171 =head2 slots
172  
173 Type: B<HashRef[ScalarRef]>.
174  
175 Variable storage map from names to scalar references.
176  
177 =head2 const
178  
179 Type: B<HashRef[Bool]>.
180  
181 Const-ness map; true entries cannot be reassigned.
182  
183 =head2 weak
184  
185 Type: B<HashRef[Bool]>.
186  
187 Weak-storage map; true entries store weakable values as weak references.
188  
189 =head1 METHODS
190  
191 =head2 new
192  
193 Constructs and returns a new instance of this class.
194  
195 =head2 declare
196  
197 Declares a new name in the current environment.
198  
199 =head2 alias_to_ref
200  
201 Binds a name to an existing scalar reference.
202  
203 =head2 find_ref
204  
205 Finds a variable reference in current or parent scopes.
206  
207 =head2 is_const_here
208  
209 Reports whether a locally-declared name is const.
210  
211 =head2 is_weak_here
212  
213 Reports whether a locally-declared name uses weak storage.
214  
215 =head2 is_weak_slot
216  
217 Reports whether a name uses weak storage, resolving through parent
218 scopes.
219  
220 =head2 set_weak_slot
221  
222 Updates the weak-storage flag for an existing name.
223  
224 =head1 SEE ALSO
225  
226 Subclasses: none in this distribution.
227  
228 =head1 COPYRIGHT AND LICENCE
229  
230 B<< Zuzu::Env >> is copyright Toby Inkster.
231  
232 It is free software; you may redistribute it and/or modify it under
233 the terms of either the Artistic License 1.0 or the GNU General Public
234 License version 2.
235  
236 =cut
237  
238 70 1;
« blib/lib/Zuzu/Util/Number.pm ↑ Summary blib/lib/Zuzu/Value/Regexp.pm »