#!/usr/bin/awk -f

function clearvars () {
  real       = "";
  cpu        = "";
  codesize   = "";
  crashed    = 0;
  failed     = 0;
  runtime    = 0;
  cycles     = "";
  instrs     = "";
  branches   = "";
  branchmisses = "";
}


function output () {
  if (runtime == 1) {

    if (real == "") {
       crashed = 1;
    }

    failure = "";

    if (crashed != 0) {
       failure = "CRASH";
    }
    else if (failed != 0) {
      failure = "FAIL";
    }

    printf "(%-9s %9.6f %9.6f %9s %9s %9s %9s %6s)\n", test, real, cpu, cycles, instrs, branches, branchmisses, failure;
  }

  clearvars();
}

BEGIN {
  test       = "";
  clearvars();
}

/^Testing/ {
  output();
  test=$2;
}

/^Compiling.../ {
  runtime = 0;
}

/^Running.../ {
  if (runtime == 1) {
    output();
  }
  runtime = 1;
}

runtime == 1 && $0 == "*** wrong result ***" {
  failed = 1;
}

runtime == 1 && /^Command .* with non-zero/ {
  crashed = 1;
}

runtime == 1 && /^Command terminated by signal/ {
  crashed = 1;
}

runtime == 1 && /Abort trap/ {
  crashed = 1;
}

runtime == 1 && / === context ===/ {
  crashed = 1;
}

runtime == 1 && /ERROR/ && $0 !~ /^FATAL-ERROR$/ && $0 !~ /^SCHEME-ERROR$/ && $0 !~ /^SLATEX-ERROR$/ {
  crashed = 1;
}

runtime == 1 && /^[ ]*[0-9.]+ secs cpu time \([0-9.]+ user, [0-9.]+ system\)$/ {
  cpu = $1;
}

runtime == 1 && /^[ ]*[0-9]+ ms cpu time \([0-9]+ user, [0-9]+ system\)$/ {
  cpu = $1/1000;
}

runtime == 1 && /^[ ]*[0-9.]+ secs real time$/ {
  real = $1;
}

runtime == 1 && /^[ ]*[0-9]+ ms real time$/ {
  real = $1/1000;
}

runtime == 1 && /^cpu time: ([0-9]+) real time: ([0-9]+)/ {
  cpu = $3;
  real = $6;
}

runtime == 1 && /^[ ]*([0-9.]+) real [ ]*([0-9.]+) user [ ]*([0-9.]+) sys$/ {
  cpu = ($3 + $5) * 1000;
  real = $1 * 1000;
}

runtime == 1 && /^code size = [0-9]+$/ {
  codesize = $4;
}

runtime == 1 && /^[ ]*[0-9,]+[ ]+cycles .*$/ {
  cycles = $1;
  gsub(",","",cycles);
}

runtime == 1 && /^[ ]*[0-9,]+[ ]+instructions .*$/ {
  instrs = $1;
  gsub(",","",instrs);
}

runtime == 1 && /^[ ]*[0-9,]+[ ]+branches .*$/ {
  branches = $1;
  gsub(",","",branches);
}

runtime == 1 && /^[ ]*[0-9,]+[ ]+branch-misses .*$/ {
  branchmisses = $1;
  gsub(",","",branchmisses);
}

END { output() }
