4
use autodie; # die if problem reading or writing a file
6
my $filein = "./agast.txt";
7
my $fileout = "./agast_new.txt";
18
my $myfirstline = $ARGV[0];
19
my $mylastline = $ARGV[1];
20
my $tablename = $ARGV[2];
26
my $success_homogeneous;
28
my $success_structured;
30
open(my $in1, "<", $filein) or die "Can't open $filein: $!";
31
open(my $out, ">", $fileout) or die "Can't open $fileout: $!";
36
while (my $line1 = <$in1>)
40
if (($i1>=$myfirstline)&&($i1<=$mylastline))
42
if($line1=~/if\(ptr\[offset(\d+)/)
44
if($line1=~/if\(ptr\[offset(\d+).*\>.*cb/)
50
if($line1=~/if\(ptr\[offset(\d+).*\<.*c\_b/)
56
die "invalid array index!"
59
$array1[$ifcount1] = $tmp;
60
$array0[$ifcount1] = $i1;
69
$homogeneous=$ifcount1;
70
$success_homogeneous=$ifcount1+1;
71
$structured=$ifcount1+2;
72
$success_structured=$ifcount1+3;
74
close $in1 or die "Can't close $filein: $!";
76
open($in1, "<", $filein) or die "Can't open $filein: $!";
80
while (my $line1 = <$in1>)
83
if (($i1>=$myfirstline)&&($i1<=$mylastline))
85
if ($array0[$ifcount2] == $i1)
89
if ($array0[$ifcount2+1] == ($i1+1))
91
$array2[$ifcount2]=($ifcount2+1);
95
open(my $in2, "<", $filein) or die "Can't open $filein: $!";
97
while (my $line2 = <$in2>)
108
if ($line2=~/goto (\w+)/)
111
if ($tmp eq "homogeneous")
113
$array2[$ifcount2]=$homogeneous;
115
if ($tmp eq "success_homogeneous")
117
$array2[$ifcount2]=$success_homogeneous;
119
if ($tmp eq "structured")
121
$array2[$ifcount2]=$structured;
123
if ($tmp eq "success_structured")
125
$array2[$ifcount2]=$success_structured;
130
die "goto expected: $!";
132
close $in2 or die "Can't close $filein: $!";
134
#find next else and interprete it
135
open(my $in3, "<", $filein) or die "Can't open $filein: $!";
139
while (my $line3 = <$in3>)
148
while (my $line3 = <$in3>)
152
if (($elsecount==0)&&($i3>$i1))
154
if ($line3=~/goto (\w+)/)
157
if ($tmp eq "homogeneous")
159
$array3[$ifcount2]=$homogeneous;
161
if ($tmp eq "success_homogeneous")
163
$array3[$ifcount2]=$success_homogeneous;
165
if ($tmp eq "structured")
167
$array3[$ifcount2]=$structured;
169
if ($tmp eq "success_structured")
171
$array3[$ifcount2]=$success_structured;
176
if ($line3=~/if\(ptr\[offset/)
179
while ($array0[$ifcount4]!=$i3)
182
if ($ifcount4==$ifcount1)
184
die "if else match expected: $!";
186
$array3[$ifcount2]=$ifcount4;
191
die "elseif or elsegoto match expected: $!";
198
if ($line3=~/if\(ptr\[offset/)
212
printf("%3d [%3d][0x%08x]\n", $array0[$ifcount2], $ifcount2, (($array1[$ifcount2]&15)<<28)|($array2[$ifcount2]<<16)|(($array1[$ifcount2]&128)<<5)|($array3[$ifcount2]));
213
close $in3 or die "Can't close $filein: $!";
223
printf(" [%3d][0x%08x]\n", $homogeneous, 252);
224
printf(" [%3d][0x%08x]\n", $success_homogeneous, 253);
225
printf(" [%3d][0x%08x]\n", $structured, 254);
226
printf(" [%3d][0x%08x]\n", $success_structured, 255);
228
close $in1 or die "Can't close $filein: $!";
232
printf $out " static const unsigned long %s[] = {\n ", $tablename;
233
while ($ifcount0 < $ifcount1)
235
printf $out "0x%08x, ", (($array1[$ifcount0]&15)<<28)|($array2[$ifcount0]<<16)|(($array1[$ifcount0]&128)<<5)|($array3[$ifcount0]);
247
printf $out "0x%08x, ", 252;
256
printf $out "0x%08x, ", 253;
265
printf $out "0x%08x, ", 254;
274
printf $out "0x%08x\n", 255;
277
printf $out " };\n\n";
284
close $out or die "Can't close $fileout: $!";