Perlito5/Grammar/Scope.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 306 CC 35 SCAR 45.8
CC
35
Cov
0%
check_variable_declarations
210.0
lookup_variable
72.0
lookup_variable_inner
72.0
CRAP 97.6
1 package Perlito5::Grammar::Scope;
2  
3 use Perlito5::AST;
4 use strict;
5  
6 sub new {
7 return { block => [] };
8 }
9  
10 sub new_base_scope {
11 return { block => [] }
12 }
13  
14 sub create_new_compile_time_scope {
15 # start new compile-time lexical scope
16 push @Perlito5::BASE_SCOPE, {
17 block => [],
18 hint_scalar => $^H,
19 hint_hash => {%^H},
20 };
21 # warn "ENTER $Perlito5::PKG_NAME \n";
22 # print STDERR "create_new_compile_time_scope [ $^H ]\n";
23 }
24  
25 sub end_compile_time_scope {
26 # warn "EXIT\n";
27 my $scope = pop @Perlito5::BASE_SCOPE;
28 $^H = $scope->{hint_scalar};
29 %^H = %{ $scope->{hint_hash} || {} };
30 # print STDERR "end_compile_time_scope [ $^H ]\n";
31 }
32  
33 sub compile_time_glob_set {
34 # set a GLOB at compile-time
35 my ($glob, $value, $namespace) = @_;
36 if ( !ref($glob) ) {
37 if ( $glob !~ /::/ ) {
38 $glob = $namespace . '::' . $glob;
39 }
40 # mark the variable as "seen"
41 my @parts = split "::", $glob;
42 my $name = pop @parts;
43 Perlito5::AST::Var->new( name => $name, namespace => join("::", @parts), sigil => "*", _decl => "global" );
44  
45 if (ref($value) eq 'SCALAR') {
46 $Perlito5::VARS{'$' . $glob} = 1;
47 }
48 if (ref($value) eq 'HASH') {
49 $Perlito5::VARS{'%' . $glob} = 1;
50 }
51 if (ref($value) eq 'ARRAY') {
52 $Perlito5::VARS{'@' . $glob} = 1;
53 }
54 }
55 *{$glob} = $value;
56 }
57  
58 sub lookup_variable {
59 # search for a variable declaration in the compile-time scope
60 my $var = shift;
61 my $scope = shift() // 0;
62  
63 return $var if $var->{namespace}; # global variable
64 return $var if $var->{_decl}; # predeclared variable
65  
66 my $look = lookup_variable_inner($var, $scope);
67 return $look if $look;
68  
69 return if ref($var) ne 'Perlito5::AST::Var';
70  
71 if ( $var->is_special_var() ) {
72 # special variable
73 $var->{_decl} = 'global';
74 $var->{_namespace} = 'main';
75 return $var;
76 }
77  
78 if ( $var->{sigil} eq '$' && ( $var->{name} eq 'a' || $var->{name} eq 'b' ) ) {
79 if ( !$var->{_real_sigil} ) {
80 # special variables $a and $b
81 $var->{_decl} = 'global';
82 $var->{_namespace} = $Perlito5::PKG_NAME;
83 return $var;
84 }
85 }
86 return;
87 }
88  
89  
90 sub lookup_variable_inner {
91 # search for a variable declaration in the compile-time scope
92 my ($var, $scope) = @_;
93  
94 # warn "depth $scope ", $#Perlito5::BASE_SCOPE, "\n";
95 return if $scope > $#Perlito5::BASE_SCOPE;
96  
97 if ( $scope < $#Perlito5::BASE_SCOPE ) {
98 # lookup in the inner scope first
99 my $look = lookup_variable_inner($var, $scope + 1);
100 return $look if $look;
101 }
102 my $block = $Perlito5::BASE_SCOPE[$scope]->{block};
103  
104 if ( ($Perlito5::BASE_SCOPE[$scope]->{compacted} + 100) < @$block ) {
105 # TODO - simplify this
106 # garbage-collect the scope
107 my %seen;
108 my @out;
109 my $start = $#$block - 500;
110 $start = 1 if $start < 1;
111 for my $i ($start .. $#{$block}) {
112 my $item = $block->[$i];
113 my $s = join(':', map {;
114 $_ . '=' . $item->{$_}
115 } sort {;
116 $a cmp $b
117 } keys(%{$item}));
118 $seen{$s}++ || push(@out, $item)
119 }
120 # print STDERR:: 'block ', scalar(@{$block}), ' ', scalar(@out), ' ', $Perlito5::BASE_SCOPE[$scope]->{compacted}, "\n";
121 $Perlito5::BASE_SCOPE[$scope]->{'block'} = [ @{$block}[ 0 .. $start - 1 ], @out ];
122 $Perlito5::BASE_SCOPE[$scope]->{compacted} += 100;
123 }
124  
125 for my $item (reverse @$block) {
126 if (ref($item) eq 'Perlito5::AST::Var' && $item->{_decl}
127 && $item->{_decl} ne 'global'
128 && $item->{name} eq $var->{name}) {
129 my $sigil = $var->{_real_sigil} || $var->{sigil};
130 my $item_sigil = $item->{_real_sigil} || $item->{sigil};
131 # TODO - namespace
132 # TODO - $a[10] $#a ${"a"}
133 # TODO - check "strict"
134 if ($sigil eq $item_sigil) {
135 # print "found name: $item->{sigil} $item->{name} decl: $item->{_decl}\n";
136 return $item;
137 }
138 }
139 }
140 return;
141 }
142  
143 sub check_variable_declarations {
144 # examine the variables that were used in the last statement
145 # - check if the variables were declared
146 # - insert the variables in the current compile-time scope
147  
148 # print "env: ", Perlito5::Dumper::Dumper( \@Perlito5::BASE_SCOPE );
149 for my $item ( @Perlito5::SCOPE_STMT ) {
150 if (ref($item) eq 'Perlito5::AST::Var') {
151 my $var = $item;
152 my $look = lookup_variable($var);
153 if ($look) {
154 $var->{_id} = $look->{_id} if $look->{_id};
155 $var->{_decl} = $look->{_decl} if $look->{_decl};
156 $var->{_namespace} = $look->{_namespace} if $look->{_namespace};
157 }
158 else {
159 # unknown variable
160 if ( $^H & $Perlito5::STRICT_VARS ) {
161 # warn "look: ", Perlito5::Dumper::Dumper(\@Perlito5::SCOPE_STMT);
162 my $sigil = $var->{_real_sigil} || $var->{sigil};
163 if ($sigil ne '*' && $sigil ne '&') {
164 if (length($var->{name}) >= 2 && substr($var->{name}, -2) eq '::') {
165 # looks like a symbol table reference
166 }
167 elsif ($Perlito5::VARS{ $sigil . $Perlito5::PKG_NAME . '::' . $var->{name} }) {
168 # "use vars"
169 }
170 else {
171 Perlito5::Compiler::error( 'Global symbol "' . $sigil . $var->{name} . '"'
172 . ' requires explicit package name' );
173 }
174 }
175 }
176 $var->{_decl} = 'global';
177 $var->{_namespace} = $Perlito5::PKG_NAME;
178 }
179 if ($var->{name} && ($var->{namespace} || $var->{_namespace})) {
180 my $compiletime_name;
181 if ($var->{'name'} lt 'A' || $var->{'name'} eq '\\') {
182 $compiletime_name =
183 ($var->{_real_sigil} || $var->{sigil})
184 . $var->{name};
185 }
186 else {
187 $compiletime_name =
188 ($var->{_real_sigil} || $var->{sigil})
189 . ($var->{namespace} || $var->{_namespace})
190 . "::" . $var->{name}
191 . ($var->{_decl} eq "global" ? "" : $var->{_id} ? "_" . $var->{_id} : "");
192 }
193 $Perlito5::GLOBAL->{$compiletime_name} = { value => undef, ast => $var };
194 }
195 }
196 }
197 push @{ $Perlito5::BASE_SCOPE[-1]->{block} }, @Perlito5::SCOPE_STMT;
198 @Perlito5::SCOPE_STMT = ();
199 }
200  
201 sub get_snapshot {
202 # return a structure with the variable declarations in the current compile-time scope
203 # this is used by eval-string at runtime
204 my @result;
205 my $scope = shift() // 0;
206  
207 if ( $scope < $#Perlito5::BASE_SCOPE ) {
208 # lookup in the inner scope first
209 my $look = get_snapshot($scope + 1);
210 unshift @result, @{ $look->{block} };
211 }
212 my $block = $Perlito5::BASE_SCOPE[$scope]->{block};
213 for my $item (@$block) {
214 if ( ref($item) eq 'Perlito5::AST::Var'
215 && $item->{_decl}
216 && $item->{_decl} ne "global"
217 && $item->{_decl} ne "local" )
218 {
219 unshift @result, $item;
220 }
221 }
222 return { block => \@result };
223 }
224  
225 1;
226