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