3 ### Convert dumps to `read_tree' input format
5 ### (c) 2024 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Xyla, a library of binary trees.
12 ### Xyla is free software: you can redistribute it and/or modify it under
13 ### the terms of the GNU Lesser General Public License as published by the
14 ### Free Software Foundation; either version 3 of the License, or (at your
15 ### option) any later version.
17 ### Xyla is distributed in the hope that it will be useful, but WITHOUT
18 ### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 ### FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20 ### License for more details.
22 ### You should have received a copy of the GNU Lesser General Public
23 ### License along with Xyla. If not, see <https://www.gnu.org/licenses/>.
35 return { key => $k, left => undef, right => undef, flags => "" };
41 if ($s =~ /^ \s* _ \s* (.*) $/x) { return undef, $1; }
42 $s =~ /^ \s* \( \s* (.*) $/x or die "bad string";
43 (my $left, $s) = read_tree $1;
44 $s =~ /^ \s* ([*]*) \s* (\d+) \s* (.*) $/x or die "bad string";
45 (my $flags, my $key) = ($1, $2);
46 (my $right, $s) = read_tree $3;
47 $s =~ /^ \s* \) \s* (.*) $/x or die "bad string";
49 $node->{left} = $left; $node->{right} = $right; $node->{flags} = $flags;
62 if ($firstp && /^\s*[(_]/) {
63 ($tree, my $tail) = read_tree $_;
64 $tail =~ /^ \s* $/x or die "bad string";
69 m{^ \s+ \#0x[0-9a-f]{8} \s+ \(n \s* = \s* \d+\)
70 (\s+) \(([ *+=-])\) (?: \s+ (0x[0-9a-f]{8} \s* \$)) \s+ (\d+) $}x
71 or die "bad line `$_'";
78 if ($opt_r && $sigil eq "*") { $node->{flags} .= "*"; }
79 if (defined $weight) { $node->{flags} .= $weight . " "; }
81 while ($SP && $LV[$SP - 1] > $lv)
82 { $SP--; my $n = $N[$SP]; $n->{right} = $left; $left = $n; }
83 $node->{left} = $left; $N[$SP] = $node; $LV[$SP] = $lv; $SP++;
85 while ($SP) { $SP--; my $n = $N[$SP]; $n->{right} = $tree; $tree = $n; }
88 sub show_tree ($$$$) {
89 my ($node, $dp, $lp, $nl) = @_;
90 if ($node->{flags} eq "*") { $dp = ($dp&-2) + 1; }
92 show_tree $node->{left}, $dp + 1, $lp + 1, $nl;
93 print "\n" . "\t" x $dp . $node->{flags} . $node->{key};
96 print "\t" x $dp . "(" x $lp . "_ " . $node->{flags} . $node->{key};
98 if ($node->{right}) { show_tree $node->{right}, $dp + 1, 1, 1; }
102 show_tree $tree, 1, 1, 0;
105 ###----- That's all, folks --------------------------------------------------