read_file_score32.pl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use autodie; # die if problem reading or writing a file
  5. my $filein = "./agast_score.txt";
  6. my $fileout = "./agast_new.txt";
  7. my $i1=1;
  8. my $i2=1;
  9. my $i3=1;
  10. my $tmp;
  11. my $ifcount0=0;
  12. my $ifcount1=0;
  13. my $ifcount2=0;
  14. my $ifcount3=0;
  15. my $ifcount4=0;
  16. my $elsecount;
  17. my $myfirstline = $ARGV[0];
  18. my $mylastline = $ARGV[1];
  19. my $tablename = $ARGV[2];
  20. my @array0 = ();
  21. my @array1 = ();
  22. my @array2 = ();
  23. my @array3 = ();
  24. my $is_not_a_corner;
  25. my $is_a_corner;
  26. open(my $in1, "<", $filein) or die "Can't open $filein: $!";
  27. open(my $out, ">", $fileout) or die "Can't open $fileout: $!";
  28. $array0[0] = 0;
  29. $i1=1;
  30. while (my $line1 = <$in1>)
  31. {
  32. chomp $line1;
  33. $array0[$i1] = 0;
  34. if (($i1>=$myfirstline)&&($i1<=$mylastline))
  35. {
  36. if($line1=~/if\(ptr\[offset(\d+)/)
  37. {
  38. if($line1=~/if\(ptr\[offset(\d+).*\>.*cb/)
  39. {
  40. $tmp=$1;
  41. }
  42. else
  43. {
  44. if($line1=~/if\(ptr\[offset(\d+).*\<.*c\_b/)
  45. {
  46. $tmp=$1+128;
  47. }
  48. else
  49. {
  50. die "invalid array index!"
  51. }
  52. }
  53. $array1[$ifcount1] = $tmp;
  54. $array0[$ifcount1] = $i1;
  55. $ifcount1++;
  56. }
  57. else
  58. {
  59. }
  60. }
  61. $i1++;
  62. }
  63. $is_not_a_corner=$ifcount1;
  64. $is_a_corner=$ifcount1+1;
  65. close $in1 or die "Can't close $filein: $!";
  66. open($in1, "<", $filein) or die "Can't open $filein: $!";
  67. $i1=1;
  68. while (my $line1 = <$in1>)
  69. {
  70. chomp $line1;
  71. if (($i1>=$myfirstline)&&($i1<=$mylastline))
  72. {
  73. if ($array0[$ifcount2] == $i1)
  74. {
  75. $array2[$ifcount2]=0;
  76. $array3[$ifcount2]=0;
  77. if ($array0[$ifcount2+1] == ($i1+1))
  78. {
  79. $array2[$ifcount2]=($ifcount2+1);
  80. }
  81. else
  82. {
  83. open(my $in2, "<", $filein) or die "Can't open $filein: $!";
  84. $i2=1;
  85. while (my $line2 = <$in2>)
  86. {
  87. chomp $line2;
  88. if ($i2 == $i1)
  89. {
  90. last;
  91. }
  92. $i2++;
  93. }
  94. my $line2 = <$in2>;
  95. chomp $line2;
  96. if ($line2=~/goto (\w+)/)
  97. {
  98. $tmp=$1;
  99. if ($tmp eq "is_not_a_corner")
  100. {
  101. $array2[$ifcount2]=$is_not_a_corner;
  102. }
  103. if ($tmp eq "is_a_corner")
  104. {
  105. $array2[$ifcount2]=$is_a_corner;
  106. }
  107. }
  108. else
  109. {
  110. die "goto expected: $!";
  111. }
  112. close $in2 or die "Can't close $filein: $!";
  113. }
  114. #find next else and interpret it
  115. open(my $in3, "<", $filein) or die "Can't open $filein: $!";
  116. $i3=1;
  117. $ifcount3=0;
  118. $elsecount=0;
  119. while (my $line3 = <$in3>)
  120. {
  121. chomp $line3;
  122. $i3++;
  123. if ($i3 == $i1)
  124. {
  125. last;
  126. }
  127. }
  128. while (my $line3 = <$in3>)
  129. {
  130. chomp $line3;
  131. $ifcount3++;
  132. if (($elsecount==0)&&($i3>$i1))
  133. {
  134. if ($line3=~/goto (\w+)/)
  135. {
  136. $tmp=$1;
  137. if ($tmp eq "is_not_a_corner")
  138. {
  139. $array3[$ifcount2]=$is_not_a_corner;
  140. }
  141. if ($tmp eq "is_a_corner")
  142. {
  143. $array3[$ifcount2]=$is_a_corner;
  144. }
  145. }
  146. else
  147. {
  148. if ($line3=~/if\(ptr\[offset/)
  149. {
  150. $ifcount4=0;
  151. while ($array0[$ifcount4]!=$i3)
  152. {
  153. $ifcount4++;
  154. if ($ifcount4==$ifcount1)
  155. {
  156. die "if else match expected: $!";
  157. }
  158. $array3[$ifcount2]=$ifcount4;
  159. }
  160. }
  161. else
  162. {
  163. die "elseif or elsegoto match expected: $!";
  164. }
  165. }
  166. last;
  167. }
  168. else
  169. {
  170. if ($line3=~/if\(ptr\[offset/)
  171. {
  172. $elsecount++;
  173. }
  174. else
  175. {
  176. if ($line3=~/else/)
  177. {
  178. $elsecount--;
  179. }
  180. }
  181. }
  182. $i3++;
  183. }
  184. printf("%3d [%3d][0x%08x]\n", $array0[$ifcount2], $ifcount2, (($array1[$ifcount2]&15)<<28)|($array2[$ifcount2]<<16)|(($array1[$ifcount2]&128)<<5)|($array3[$ifcount2]));
  185. close $in3 or die "Can't close $filein: $!";
  186. $ifcount2++;
  187. }
  188. else
  189. {
  190. }
  191. }
  192. $i1++;
  193. }
  194. printf(" [%3d][0x%08x]\n", $is_not_a_corner, 254);
  195. printf(" [%3d][0x%08x]\n", $is_a_corner, 255);
  196. close $in1 or die "Can't close $filein: $!";
  197. $ifcount0=0;
  198. $ifcount2=0;
  199. printf $out " static const unsigned long %s[] = {\n ", $tablename;
  200. while ($ifcount0 < $ifcount1)
  201. {
  202. printf $out "0x%08x, ", (($array1[$ifcount0]&15)<<28)|($array2[$ifcount0]<<16)|(($array1[$ifcount0]&128)<<5)|($array3[$ifcount0]);
  203. $ifcount0++;
  204. $ifcount2++;
  205. if ($ifcount2==8)
  206. {
  207. $ifcount2=0;
  208. printf $out "\n";
  209. printf $out " ";
  210. }
  211. }
  212. printf $out "0x%08x, ", 254;
  213. $ifcount0++;
  214. $ifcount2++;
  215. if ($ifcount2==8)
  216. {
  217. $ifcount2=0;
  218. printf $out "\n";
  219. printf $out " ";
  220. }
  221. printf $out "0x%08x\n", 255;
  222. $ifcount0++;
  223. $ifcount2++;
  224. printf $out " };\n\n";
  225. $#array0 = -1;
  226. $#array1 = -1;
  227. $#array2 = -1;
  228. $#array3 = -1;
  229. close $out or die "Can't close $fileout: $!";