+
+# count the bracket ballance
+sub num_brackets {
+ my $line = shift;
+ my $lastpos;
+ my $cnt = 0;
+
+ $lastpos = -1;
+ while(1) {
+ $lastpos = index($line, "{", $lastpos+1);
+ if ($lastpos < 0) {
+ last;
+ }
+ $cnt++;
+ }
+ $lastpos = -1;
+ while(1) {
+ $lastpos = index($line, "(", $lastpos+1);
+ if ($lastpos < 0) {
+ last;
+ }
+ $cnt++;
+ }
+ $lastpos = -1;
+ while(1) {
+ $lastpos = index($line, "}", $lastpos+1);
+ if ($lastpos < 0) {
+ last;
+ }
+ $cnt--;
+ }
+ $lastpos = -1;
+ while(1) {
+ $lastpos = index($line, ")", $lastpos+1);
+ if ($lastpos < 0) {
+ last;
+ }
+ $cnt--;
+ }
+ return $cnt;
+}