chiark / gitweb /
remove a thing that ought to have been deleted
[the-pointy-bits.git] / total-checker
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 $|=1;
6
7 die unless @ARGV == 1;
8 my ($file) = @ARGV;
9 die if $file =~ m{^-};
10
11 open STDIN, "<", $file or die "$file $!";
12 open L, "expand -t4 |" or die $!;
13 our (@l) = <L>;
14 $?=0; $!=0; close L or die "$? $!";
15
16 unshift @l, '';
17
18 my $block_start = 0;
19 my $heading;
20
21 foreach my $i (0..$#l) {
22   $_ = $l[$i];
23   if (m{^\`\`\`}) {
24     if ($block_start) {
25       $block_start = 0;
26     } else {
27       $block_start = $i + 1;
28     }
29   }
30
31   if (!$block_start && m{^\#+ (\S.*)\n}) {
32     $heading = $1;
33   }
34
35   if (defined($block_start) && m{^(total +)(\d+)\s}) {
36     my ($lhs, $exp) = ($1, $2);
37     my $got = 0;
38     foreach my $j ($block_start .. ($i-1)) {
39       $_ = $l[$j];
40       my $earlier = length($lhs) - 1;
41       next if m{^\#};
42       next unless length > $earlier+1;
43       die "$file:$j: ($i): $_ ?" unless m{^.{$earlier} (\S+)\s};
44       my $here = $1;
45       next unless $here =~ m{^[-+]?\d+$};
46       $got += $here;
47     }
48     my $delta = $exp - $got;
49     print "$file: checking total: $heading\n";
50     if ($delta) {
51       print STDERR "$file:$i: expected $exp but got $got, spend $delta\n";
52     }
53     next;
54   }
55 }