blib/lib/B/DeparseTree/Printer.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 166 SCAR 54.8
CC
15
Cov
0%
format_info
72.0
format_info_short
30.0
short_str
6.0
CRAP 240.0
1 # Copyright (c) 2015 Rocky Bernstein
2 use strict; use warnings;
3 use rlib '../..';
4  
5 package B::DeparseTree::Printer;
6  
7 our($VERSION, @EXPORT, @ISA);
8 $VERSION = '3.2.0';
9 @ISA = qw(Exporter);
10 @EXPORT = qw(format_info format_info_walk);
11  
12  
13 use constant sep_string => ('=' x 40) . "\n";
14  
15 # Elide string with ... if it is too long, and
16 # show control characters in string.
17 sub short_str($;$) {
18 my ($str, $maxwidth) = @_;
19 $maxwidth ||= 20;
20  
21 if (length($str) > $maxwidth) {
22 my $chop = $maxwidth - 3;
23 $str = substr($str, 0, $chop) . '...' . substr($str, -$chop);
24 }
25 $str =~ s/\cK/\\cK/g;
26 $str =~ s/\f/\\f/g;
27 $str =~ s/\n/\\n/g;
28 $str =~ s/\t/\\t/g;
29 return $str
30 }
31  
32 sub format_info_short($$)
33 {
34 my ($info, $show_body) = @_;
35  
36 my %i = %{$info};
37 my $text;
38 my $op = $i{op};
39 if ($op) {
40 $text = sprintf(
41 "0x%x %s/%s: \"%s\"",
42 $$op,
43 $i{type},
44 $op->name,
45 short_str($i{text}));
46 } else {
47 $text = sprintf(
48 "? %s: \"%s\"%s",
49 $i{type},
50 short_str($i{text}));
51 }
52 if (exists $i{maybe_parens}) {
53 my %mp = %{$i{maybe_parens}};
54 if (B::DeparseTree::Node::parens_test($info, $mp{cx}, $mp{prec})) {
55 $text .= ' - parens';
56 }
57 }
58 if (exists $i{body} and $show_body) {
59 $text .= ("\n\t" .
60 join(",\n\t",
61 map(sprintf("0x%x %s '%s'", ${$_->{op}},
62 $_->{type}, short_str($_->{text})),
63 @{$i{body}})));
64 }
65 # FIXME: other ops
66 return $text;
67 }
68  
69 sub format_info($)
70 {
71 my $info = shift;
72 my %i = %{$info};
73 my $fmt = <<EOF;
74 type :%s
75 op :%s
76 cop line: %s
77 parent : %s
78 text:
79 %s
80 ---------
81 sep: '%s'
82 EOF
83 my $text = sprintf($fmt,
84 $i{type},
85 $i{op} ? $i{op}->name : '???',
86 $i{cop} ? $i{cop}->line : 'none',
87 exists $i{parent} ? sprintf("0x%x", $i{parent}) : 'no parent',
88 $i{text},
89 $i{sep});
90 my @texts = @{${i}{texts}};
91 for (my $j=0; $j < scalar @texts; $j++) {
92 my $line = short_str($texts[$j]);
93 $text .= "text[$j]: \"$line\"\n";
94  
95 }
96 if ($i{body}) {
97 $text .= sep_string;
98 my @body = @{$i{body}};
99 for (my $j=0; $j < scalar @body; $j++) {
100 $text .= sprintf("body[$j]: %s\n", format_info_short($body[$j], 1));
101 }
102 }
103 if (exists $i{other_ops}) {
104 $text .= sep_string;
105 my @other_ops = @{$i{other_ops}};
106 for (my $j=0; $j < scalar @other_ops; $j++) {
107 my $op = $other_ops[$j];
108 $text .= sprintf("other_ops[$j]: 0x%x %s\n", $$op, $op->name);
109 }
110 }
111 if (exists $i{maybe_parens}) {
112 $text .= sep_string;
113 my %maybe_parens = %{$i{maybe_parens}};
114 foreach my $key (sort keys %maybe_parens) {
115 $text .= sprintf "%s: %g\n", $key, $maybe_parens{$key};
116 }
117 $text .= sprintf("need parens: %s\n",
118 B::DeparseTree::Node::parens_test($info, $maybe_parens{context},
119 $maybe_parens{precedence}) ?
120 'yes' : 'no');
121 }
122 return $text;
123 }
124  
125 sub format_info_walk($$);
126 sub format_info_walk($$)
127 {
128 my ($info, $indent_level) = @_;
129 my $text = '';
130 $text = format_info_short($info, 0);
131 $indent_level += 2;
132 return $text unless exists $info->{body};
133 my @body = @{$info->{body}};
134 for (my $i=0; $i < scalar @body; $i++) {
135 my $info = $body[$i];
136 my $lead = "\n" . (' ' x $indent_level) . "[$i] ";
137 $text .= ($lead . format_info_walk($info, $indent_level));
138 }
139 return $text;
140 }
141  
142 unless(caller) {
143 require B::DeparseTree;
144 eval {
145 sub fib {
146 my $x = shift;
147 return 1 if $x <= 1;
148 return(fib($x-1) + fib($x-2))
149 }
150 };
151 my $deparse = B::DeparseTree->new("-p", "-l", "-c", "-sC");
152 my $info = $deparse->coderef2info(\&fib);
153 print format_info($info), "\n";
154 print format_info_short($info, 1), "\n";
155 print '*' x 30 . "\n";
156 print format_info_walk($info, 0), "\n";
157 }
158  
159 1;