Perlito5/CompileTime/Dumper.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 240 CC 24 SCAR 50.9
CC
24
Cov
0%
_dump_AST_from_scope
240.0
emit_globals_after_BEGIN
110.0
BEGIN
2.0
CRAP 162.3
1 package Perlito5::CompileTime::Dumper;
2  
3 use Perlito5::DumpToAST;
4 use strict;
5  
6 # TODO
7 # sub generate_eval_string {
8 # my ($source, $strict) = @_;
9 # my $source_new = "";
10 # $@ = "";
11 # eval {
12 # # print STDERR "[[[ $source ]]]\n";
13 # my $m = Perlito5::Grammar::exp_stmts($source, 0);
14 # my $block = Perlito5::AST::Block->new( stmts => Perlito5::Match::flat($m) );
15 #
16 # ## TODO: enable emit_compile_time()
17 # ##
18 # ## $ node perlito5.js -I src5/lib t5/op/pow.t
19 # ## [[[ { package main;
20 # ## {
21 # ## chdir 't' if -d 't';
22 # ## @INC = '../lib';
23 # ## require './test.pl';
24 # ## } }; 1 ]]]
25 # ## Error in BEGIN block: TypeError: Cannot call method 'p5aget' of undefined
26 # ##
27 # # my @data = $block->emit_compile_time;
28 #
29 # my @data = $block->emit_perl5;
30 # my $out = [];
31 # Perlito5::Perl5::PrettyPrinter::pretty_print( \@data, 0, $out );
32 # $source_new = join( '', @$out ), ";1\n";
33 # # print STDERR "[[[ $source_new ]]]\n";
34 # };
35 # return $source_new;
36 # }
37  
38 sub _dump_AST_from_scope {
39 my ($name, $item, $vars, $dumper_seen,) = @_;
40 no strict 'refs';
41  
42 my $sigil = substr($name, 0, 1);
43 if (ref($item) eq 'Perlito5::AST::Sub' && $item->{name}) {
44 # TODO
45 # _dump_global($item, $seen, $dumper_seen, $vars, $tab);
46 # warn "# don't know how to initialize subroutine $name in BEGIN";
47 return;
48 }
49  
50 if (substr($name, 7, 1) lt 'A') {
51 # encode special variable names like $main::" to ${'main::"'}
52 $name = $sigil . '{' . Perlito5::Dumper::escape_string(substr($name,1)) . '}'
53 }
54 my $ast = $item->{ast};
55 if (ref($ast) eq 'Perlito5::AST::Var' && $ast->{_decl} eq "our") {
56 # "our" variables are lexical aliases; we want the original global variable name
57 $ast = Perlito5::AST::Var->new(
58 %$ast,
59 sigil => $ast->{'_real_sigil'} || $ast->{'sigil'},
60 namespace => $ast->{'namespace'} || $ast->{'_namespace'},
61 decl => 'global',
62 );
63 $name = $ast->{sigil} . $ast->{namespace} . "::" . $ast->{name};
64 # return if $Perlito5::GLOBAL->{$name}; # skip if we've seen this before
65 }
66 my $bareword = substr($name, 1);
67 if (ref($ast) eq 'Perlito5::AST::Var' && $sigil eq '$') {
68 my $value = ${$bareword};
69 return if !defined($value);
70 push @$vars, # "$name = ", $dump;
71 Perlito5::AST::Apply->new(
72 code => 'infix:<=>',
73 arguments => [
74 $ast,
75 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #'$name ),
76 ],
77 );
78 }
79 elsif (ref($ast) eq 'Perlito5::AST::Var' && $sigil eq '@') {
80  
81 # @{'X::ISA'} ???
82 $bareword = substr($bareword, 2, -2) if substr($bareword,0,2) eq "{'";
83  
84 my $value = \@{$bareword};
85 push @$vars, # "*$bareword = ", $dump;
86 Perlito5::AST::Apply->new(
87 code => 'infix:<=>',
88 arguments => [
89 Perlito5::AST::Var->new( %$ast, sigil => '*' ),
90 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #''\\' . $name ),
91 ],
92 );
93 }
94 elsif (ref($ast) eq 'Perlito5::AST::Var' && $sigil eq '%') {
95 my $value = \%{$bareword};
96 push @$vars, # "*$bareword = ", $dump;
97 Perlito5::AST::Apply->new(
98 code => 'infix:<=>',
99 arguments => [
100 Perlito5::AST::Var->new( %$ast, sigil => '*' ),
101 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #''\\' . $name ),
102 ],
103 );
104 }
105 elsif (ref($ast) eq 'Perlito5::AST::Var' && $sigil eq '*') {
106 # *mysub = sub {...}
107  
108 # *{'strict::import'} ???
109 $bareword = substr($bareword, 2, -2) if substr($bareword,0,2) eq "{'";
110  
111 if (exists &{$bareword}) {
112 my $value = \&{$bareword};
113 push @$vars, # "*$bareword = ", $dump;
114 Perlito5::AST::Apply->new(
115 code => 'infix:<=>',
116 arguments => [
117 Perlito5::AST::Var->new( %$ast, sigil => '*' ),
118 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #''\\&' . $bareword ),
119 ],
120 );
121 }
122 if (defined ${$bareword}) {
123 my $value = \${$bareword};
124 push @$vars, # "*$bareword = ", $dump;
125 Perlito5::AST::Apply->new(
126 code => 'infix:<=>',
127 arguments => [
128 Perlito5::AST::Var->new( %$ast, sigil => '*' ),
129 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #''\\$' . $bareword ),
130 ],
131 );
132 }
133 if (@{$bareword}) {
134 my $value = \@{$bareword};
135 push @$vars, # "*$bareword = ", $dump;
136 Perlito5::AST::Apply->new(
137 code => 'infix:<=>',
138 arguments => [
139 Perlito5::AST::Var->new( %$ast, sigil => '*' ),
140 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #''\\@' . $bareword ),
141 ],
142 );
143 }
144 if (keys %{$bareword}) {
145 my $value = \%{$bareword};
146 push @$vars, # "*$bareword = ", $dump;
147 Perlito5::AST::Apply->new(
148 code => 'infix:<=>',
149 arguments => [
150 Perlito5::AST::Var->new( %$ast, sigil => '*' ),
151 Perlito5::DumpToAST::dump_to_ast( $value, $dumper_seen, $ast ), #'\\%' . $bareword ),
152 ],
153 );
154 }
155  
156 }
157 else {
158 # warn "# don't know how to initialize variable $name in BEGIN";
159 }
160  
161 }
162  
163 # TODO
164 # - move global variables from SCOPE to GLOBAL
165 # - analyze the list of captures and resolve shared lexicals
166 # - when 2 closures share code, we need to decide if the captured
167 # variables are shared or not.
168 # Closures can have un-shared variables if the closure is created
169 # in a loop inside BEGIN
170 # - shared lexicals can be obtained from the data in @Perlito5::BASE_SCOPE
171  
172 # - this Perl warning should probably be fatal in Perlito5:
173 # Variable "$z" will not stay shared
174 # the "perl" behaviour is hard to replicate - see misc/compile-time.
175 # Example:
176 #
177 # use strict; use warnings;
178 # sub z0 {
179 # my $z = shift;
180 # sub z1 { $z }
181 # }
182 #
183 # alternately:
184 # sub z0 {
185 # my $z = shift;
186 # BEGIN { *z1 = sub { $z } }
187 # }
188 #
189 # sub z0 {
190 # my $z = shift;
191 # BEGIN { $z }
192 # }
193 #
194 # the variable '$z' will be shared only on the 'first' execution of 'z0';
195 # subsequent executions of 'z0' will create a new pad.
196 #
197 # BEGIN blocks inside loops have a similar problem, but don't
198 # generate a warning in "perl".
199  
200  
201 # problems to look for:
202 # - closures created in loops in BEGIN blocks share variable names,
203 # but the variables belong to different "pads" / activation records"
204 # - closures created after variable redefinition don't share variables
205 # - lexical variables can be shared across closures
206 # - our variables
207 # - in order to conserve memory at compile-time,
208 # create subroutine stubs that expand into instrumented code when called
209 # - bootstrapping rewrites Perlito5::* subroutines and globals, probably breaking the compiler.
210 # special-casing the Perlito5 namespace at compile-time should work around the problem.
211  
212 # Note
213 # - variables with 'undef' value don't need to be processed,
214 # because the runtime will re-create them
215  
216  
217 sub emit_globals_after_BEGIN {
218 # return a structure with the global variable declarations
219 # this is used to initialize the ahead-of-time program
220  
221 my $scope = shift() // $Perlito5::GLOBAL;
222 my $vars = [];
223 my $seen = {};
224 my $dumper_seen = {};
225 my $tab = "";
226  
227 # exclude %ENV, $] - these should use whatever is set at runtime
228 delete $scope->{'%main::ENV'};
229 delete $scope->{'$main::]'};
230 delete $scope->{'$main::ARGV'};
231 delete $scope->{'@main::_'};
232 local $_; # not sure about keeping $_
233  
234 my @dump_these = (
235 '$main::0',
236 '$main::a',
237 '$main::b',
238 '$main::_',
239 '%main::INC',
240 '@Perlito5::END_BLOCK', # __END__ blocks
241 '@Perlito5::INIT_BLOCK', # __INIT__ blocks
242 '%Perlito5::DATA_SECTION', # __DATA__ contents
243 '%Perlito5::BEGIN_SCRATCHPAD', # BEGIN captures
244 );
245 for my $pkg (keys %{$Perlito5::PACKAGES}) {;
246 push @dump_these, '@' . $pkg . "::ISA"; # dump @ISA
247 }
248 DUMP:
249 for my $v ( @dump_these ) {
250 # inject global variable in the scope, if it is not there already
251 no strict 'refs';
252 my ($sigil, $namespace, $name) = $v =~ /^([\$\@%])(\w+)::(.*)$/;
253  
254 if ($sigil eq '@' && @{ $namespace . "::" . $name } == 0) {
255 next DUMP;
256 }
257 if ($sigil eq '%' && keys %{ $namespace . "::" . $name } == 0) {
258 next DUMP;
259 }
260  
261 $scope->{$v} //= {
262 'ast' => Perlito5::AST::Var->new(
263 'name' => $name,
264 'sigil' => $sigil,
265 '_decl' => 'global',
266 'namespace' => $namespace,
267 ),
268 value => (
269 $sigil eq '$' ? \${ $namespace . "::" . $name }
270 : $sigil eq '@' ? \@{ $namespace . "::" . $name }
271 : $sigil eq '%' ? \%{ $namespace . "::" . $name }
272 : undef
273 ),
274 };
275 }
276  
277 # # TODO - look for imported subroutines
278 # for my $glob (%{ $pkg . "::" }) {
279 # my $name = *{$glob}{NAME};
280 # if (defined &{ $pkg . '::' . $name }) {
281 # warn "sub $pkg :: " . $name . "\n";
282 # }
283 # }
284  
285  
286 for my $id (keys %Perlito5::BEGIN_SCRATCHPAD) {
287 # BEGIN side-effects
288 my $ast = $Perlito5::BEGIN_SCRATCHPAD{$id};
289 my $sigil = $ast->{_real_sigil} || $ast->{sigil};
290 if (!$ast->{namespace}) {
291 $ast->{namespace} = "Perlito5::BEGIN";
292 $ast->{name} = "_" . $id . "_" . $ast->{name};
293 }
294 my $fullname = "$ast->{namespace}::$ast->{name}";
295  
296 # print STDERR "BEGIN SIDE EFECT: $sigil $fullname\n";
297  
298 if ($sigil eq '$') {
299 $scope->{$sigil . $fullname} //= {
300 ast => $ast,
301 value => \${$fullname},
302 };
303 }
304 elsif ($sigil eq '@') {
305 $scope->{$sigil . $fullname} //= {
306 ast => $ast,
307 value => \@{$fullname},
308 };
309 }
310 elsif ($sigil eq '%') {
311 $scope->{$sigil . $fullname} //= {
312 ast => $ast,
313 value => \%{$fullname},
314 };
315 }
316 }
317  
318 # return a structure with the global variable declarations
319 my $vars = [];
320 my $dumper_seen = {};
321  
322 # dump subroutine stubs (prototypes)
323 for my $fullname (sort keys %$Perlito5::PROTO) {
324 my $proto = $Perlito5::PROTO->{$fullname};
325 my @parts = split "::", $fullname;
326 my $name = pop @parts;
327 push @$vars,
328 Perlito5::AST::Sub->new(
329 'namespace' => join( "::", @parts ),
330 'sig' => $proto,
331 'name' => $name,
332 'block' => undef,
333 'attributes' => []
334 );
335 }
336  
337 for my $name (sort keys %$scope) {
338 my $item = $scope->{$name};
339 _dump_AST_from_scope($name, $item, $vars, $dumper_seen);
340 }
341 return $vars;
342 }
343  
344 1;
345