Perlito5/DumpToAST.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 193 CC 26 SCAR 57.1
CC
26
Cov
0%
dump_to_ast
420.0
dump_to_ast_deref
56.0
BEGIN
2.0
CRAP 303.3
1 package Perlito5::DumpToAST;
2  
3 use Perlito5::AST;
4 use strict;
5  
6 # $ perl -Isrc5/lib -e ' use strict; use Perlito5::DumpToAST; use Data::Dumper; my $s = [ 1,2.1,{a=>4},undef,\6 ]; $s->[3]=$s->[2]; my $seen = {}; print Dumper Perlito5::DumpToAST::dump_to_ast( $s, $seen, "s" ); '
7  
8 sub dump_to_ast {
9 my ($obj, $seen, $pos) = @_;
10  
11 return Perlito5::AST::Apply->new(code => 'undef', arguments => []) if !defined $obj;
12  
13 my $ref = ref($obj);
14 if (!$ref) {
15 if ( 0+$obj eq $obj ) {
16 return Perlito5::AST::Int->new(int => $obj) if int($obj) == $obj;
17 return Perlito5::AST::Num->new(num => $obj);
18 }
19 return Perlito5::AST::Buf->new(buf => $obj);
20 }
21  
22 my $as_string = "$obj";
23 return $seen->{$as_string} if $seen->{$as_string};
24 $seen->{$as_string} = $pos;
25
26 if ($ref eq 'ARRAY') {
27 my @out;
28 for my $i ( 0 .. $#$obj ) {
29 # TODO - move self-referencing outside the expression
30 my $here = Perlito5::AST::Index::INDEX( $pos, Perlito5::AST::Int->new(int => $i) );
31 push @out, dump_to_ast($obj->[$i], $seen, $here);
32 }
33 return Perlito5::AST::Apply->new(code => 'circumfix:<[ ]>', arguments => \@out);
34 }
35 elsif ($ref eq 'HASH') {
36 my @out;
37 for my $i ( sort keys %$obj ) {
38 # TODO - move self-referencing outside the expression
39 my $here = Perlito5::AST::Lookup::LOOKUP( $pos, Perlito5::AST::Buf->new(buf => $i) );
40 push @out, Perlito5::AST::Apply->new(
41 code => 'infix:<=>>',
42 arguments => [
43 Perlito5::AST::Buf->new(buf => $i),
44 dump_to_ast($obj->{$i}, $seen, $here),
45 ],
46 );
47 }
48 return Perlito5::AST::Apply->new(code => 'circumfix:<{ }>', arguments => \@out);
49 }
50 elsif ($ref eq 'SCALAR' || $ref eq 'REF') {
51 # TODO - move self-referencing outside the expression if needed
52 my $here = Perlito5::AST::Apply->new(code => 'prefix:<$>', arguments => [$pos]);
53 return Perlito5::AST::Apply->new(
54 code => 'prefix:<\\>',
55 arguments => [dump_to_ast($$obj, $seen, $here)]
56 );
57 }
58 elsif ($ref eq 'CODE') {
59 # get the closed variables - see 'Sub' in Perl5 emitter
60 my $closure_flag = bless {}, "Perlito5::dump";
61 my $captures = $obj->($closure_flag) // {};
62  
63 my @vars;
64 my $ast;
65 my $source;
66 my $sub_name;
67 my $package = $captures->{__PKG__};
68 my $current_package = "main";
69 for my $var_id (sort keys %$captures) {
70 next if $var_id eq "__PKG__";
71 next if $Perlito5::BEGIN_SCRATCHPAD{$var_id}; # variable captured at BEGIN
72 if ($var_id eq '__SUB__') {
73 my $sub_id = $captures->{$var_id};
74 $ast = $Perlito5::BEGIN_SUBS{$sub_id};
75  
76 $sub_name = $ast->{namespace} . "::" . $ast->{name}
77 if $ast->{name};
78  
79 # my @data = $ast->emit_perl5();
80 # my $out = [];
81 # Perlito5::Perl5::PrettyPrinter::pretty_print( \@data, 0, $out );
82 $source = $ast;
83 }
84 else {
85 my $var = $Perlito5::BEGIN_LEXICALS{$var_id};
86 $var = Perlito5::AST::Var->new(
87 %$var,
88 sigil => $var->{_real_sigil} || $var->{sigil},
89 );
90 my $decl = $var->{_decl} || 'my'; # our / my
91 if ($decl eq 'our') {
92 my $var_namespace = $var->{_namespace} || $var->{namespace};
93 if ($var_namespace ne $current_package) {
94 push @vars, # "package $package;"
95 Perlito5::AST::Apply->new(
96 'code' => 'package',
97 'namespace' => $var_namespace,
98 'arguments' => [],
99 );
100 $current_package = $var_namespace;
101 }
102 push @vars,
103 Perlito5::AST::Decl->new(
104 'attributes' => [],
105 'decl' => $decl,
106 'type' => '',
107 'var' => $var,
108 );
109 }
110 else {
111 push @vars,
112 # 'my ' . $var . ' = ' . dump_to_ast_deref($captures->{$var_id}, $seen, $pos) . '; ';
113 Perlito5::AST::Apply->new(
114 code => 'infix:<=>',
115 arguments => [
116 Perlito5::AST::Decl->new(
117 'attributes' => [],
118 'decl' => $decl,
119 'type' => '',
120 'var' => $var,
121 ),
122 dump_to_ast_deref($captures->{$var_id}, $seen, $pos), # TODO - $pos should be global
123 ],
124 );
125 }
126 }
127 }
128 if ($package ne $current_package) {
129 push @vars, # "package $package;"
130 Perlito5::AST::Apply->new(
131 'code' => 'package',
132 'namespace' => $package,
133 'arguments' => [],
134 );
135 $current_package = $package;
136 }
137
138 # warn "dump_to_ast: source: ", Perlito5::Dumper::Dumper( $source );
139 return Perlito5::AST::Apply->new(
140 code => 'do',
141 arguments => [
142 Perlito5::AST::Block->new(
143 stmts => [
144 @vars,
145 $source,
146 # ( $sub_name
147 # ? '\\&' . $sub_name # return pointer to subroutine ??? TODO - check this
148 # : ''
149 # ),
150 ],
151 ),
152 ],
153 );
154 }
155 elsif ($ref eq 'Regexp') {
156 my $regex = "$ref"; # (?: ... )
157 return Perlito5::AST::Apply->new(
158 code => 'p5:qr',
159 arguments => [
160 Perlito5::AST::Buf->new( buf => $regex ),
161 Perlito5::AST::Buf->new( buf => "" ),
162 ],
163 );
164 }
165  
166 # TODO find out what kind of reference this is (ARRAY, HASH, ...)
167 local $@ = '';
168
169 my $res = eval {
170 my @out;
171 for my $i ( 0 .. $#$obj ) {
172 my $here = Perlito5::AST::Index::INDEX( $pos, Perlito5::AST::Int->new(int => $i) );
173 push @out,
174 dump_to_ast($obj->[$i], $seen, $here);
175 }
176 return Perlito5::AST::Apply->new(
177 code => 'bless',
178 arguments => [
179 Perlito5::AST::Apply->new(code => 'circumfix:<[ ]>', arguments => \@out),
180 Perlito5::AST::Buf->new(buf => $ref),
181 ],
182 );
183 };
184 return $res if $res;
185  
186 $res = eval {
187 # blessed SCALAR
188 return Perlito5::AST::Apply->new(
189 code => 'bless',
190 arguments => [
191 Perlito5::AST::Apply->new(code => 'prefix:<\\>', arguments => [ dump_to_ast($$obj) ]),
192 ],
193 );
194 };
195 return $res if $res;
196  
197 # assume it's a blessed HASH
198 my @out;
199 for my $i ( sort keys %$obj ) {
200 my $here = Perlito5::AST::Lookup::LOOKUP( $pos, Perlito5::AST::Buf->new(buf => $i) );
201 push @out, Perlito5::AST::Apply->new(
202 code => 'infix:<=>>',
203 arguments => [
204 Perlito5::AST::Buf->new(buf => $i),
205 dump_to_ast($obj->{$i}, $seen, $here),
206 ],
207 );
208 }
209 return Perlito5::AST::Apply->new(
210 code => 'bless',
211 arguments => [
212 Perlito5::AST::Apply->new(code => 'circumfix:<{ }>', arguments => \@out),
213 Perlito5::AST::Buf->new(buf => $ref),
214 ],
215 );
216 }
217  
218 sub dump_to_ast_deref {
219 my ($obj, $seen, $pos) = @_;
220 my $ref = ref($obj);
221 return dump_to_ast(@_) if !$ref;
222 if ($ref eq 'ARRAY') {
223 return '()' unless @$obj;
224 my @out;
225 for my $i ( 0 .. $#$obj ) {
226 my $here = Perlito5::AST::Index::INDEX( $pos, Perlito5::AST::Int->new(int => $i) ); # TODO don't deref
227 push @out, dump_to_ast($obj->[$i], $seen, $here);
228 }
229 return Perlito5::AST::Apply->new(code => 'circumfix:<( )>', arguments => \@out);
230 }
231 elsif ($ref eq 'HASH') {
232 return '()' unless keys %$obj;
233 my @out;
234 for my $i ( sort keys %$obj ) {
235 my $here = Perlito5::AST::Lookup::LOOKUP( $pos, Perlito5::AST::Buf->new(buf => $i) ); # TODO don't deref
236 push @out, Perlito5::AST::Apply->new(
237 code => 'infix:<=>>',
238 arguments => [
239 Perlito5::AST::Buf->new(buf => $i),
240 dump_to_ast($obj->{$i}, $seen, $here),
241 ],
242 );
243 }
244 return Perlito5::AST::Apply->new(code => 'circumfix:<( )>', arguments => \@out);
245 }
246 elsif ($ref eq 'SCALAR' || $ref eq 'REF') {
247 my $here = Perlito5::AST::Apply->new(code => 'prefix:<$>', arguments => [$pos]); # TODO don't deref
248 return dump_to_ast($$obj, $seen, $here);
249 }
250 return dump_to_ast($obj, $seen, $pos);
251 }
252  
253 1;
254