$sd = "F:\00_MR\MR_Komponents\Internet-SLL\Skeleton\PBX-Editor\TestUsage\PBXA64\Samples\PB"
$compiler = "F:\00_MR\MR_Komponents\Internet-SLL\Skeleton\PBX-Editor\TestUsage\PBXA64\pbxa64.exe"

$pass = 0; $fail = 0; $failList = @{}
Get-ChildItem -Path $sd -Recurse -Include *.pb,*.bas | Select-Object -ExpandProperty FullName | ForEach-Object {
    $rel = $_.Substring($sd.Length + 1)
    $output = & $compiler $_ 2>&1 | Out-String
    if ($LASTEXITCODE -eq 0) { $pass++ }
    else {
        $fail++
        $firstErr = "no pb-error"
        foreach ($ln in ($output -split "`n")) {
            if ($ln -match "error: pb-") { $firstErr = $ln.Trim(); break }
        }
        $failList[$rel] = $firstErr
    }
}

Write-Host ("`n=== FINAL RESULTS ===")
Write-Host ("Pass: " + $pass)
Write-Host ("Fail: " + $fail)
Write-Host ("Total: " + ($pass + $fail))

# Group remaining failures
$groups = @{}
foreach ($k in $failList.Keys) {
    $err = $failList[$k]
    $key = "other"
    if ($err -match "no pb-error") { $key = "no_output" }
    elseif ($err -match "expected identifier") { $key = "expected_identifier" }
    elseif ($err -match "unexpected token") { $key = "unexpected_token" }
    elseif ($err -match "expected expression") { $key = "expected_expr" }
    elseif ($err -match "expected '\)' after array bounds") { $key = "array_bounds" }
    elseif ($err -match "unknown call") { $key = "unknown_call" }
    elseif ($err -match "expected directive") { $key = "first_line" }
    elseif ($err -match "BYREF and must be") { $key = "byref" }
    elseif ($err -match "expected THEN") { $key = "if_then" }
    elseif ($err -match "expected AS after LOCAL") { $key = "local_as" }
    elseif ($err -match "expected assignment") { $key = "assign_call" }
    elseif ($err -match "unsupported string operation") { $key = "unsupported_str" }
    elseif ($err -match "used without declaration") { $key = "undeclared" }
    elseif ($err -match "MID\$ expects") { $key = "mid_fn" }
    elseif ($err -match "expected '\)' after SUB params") { $key = "sub_paren" }
    elseif ($err -match "expected PB type name") { $key = "type_name" }
    elseif ($err -match "TYPE field statement") { $key = "type_stmt" }
    elseif ($err -match "expected AS in TYPE") { $key = "type_as" }
    elseif ($err -match "nested TYPE field") { $key = "nested_type" }
    elseif ($err -match "CODEPTR") { $key = "codeptr" }
    elseif ($err -match "REPEAT\$") { $key = "repeat" }
    elseif ($err -match "ASC expects") { $key = "asc" }
    elseif ($err -match "CHR\$|FORMAT\$|HEX\$") { $key = "chr_str" }
    elseif ($err -match "LEFT\$|RIGHT\$") { $key = "left_right" }
    elseif ($err -match "SIN/COS/SQR") { $key = "math_builtins" }
    elseif ($err -match "unsupported TYPE assignment") { $key = "type_assign" }
    elseif ($err -match "RET inside inline ASM") { $key = "asm_ret" }
    elseif ($err -match "GLOBAL/STATIC TYPE") { $key = "global_type" }
    elseif ($err -match "#PCODE") { $key = "pcode" }
    elseif ($err -match "EXE output requires") { $key = "exe_requires" }
    elseif ($err -match "failed to read") { $key = "include" }
    elseif ($err -match "PAINT") { $key = "paint" }
    elseif ($err -match "IMAGECREATE") { $key = "image" }
    elseif ($err -match "WAITKEY|WAITSTAT") { $key = "waitkey" }
    elseif ($err -match "\.\.\.") { $key = "ellipsis" }

    if (-not $groups.ContainsKey($key)) { $groups[$key] = @() }
    $groups[$key] += "$k => $err"
}

$groups.Keys | Sort-Object | ForEach-Object {
    $k = $_
    Write-Host ("`n--- " + $k + " (" + $groups[$k].Count + ") ---")
    $groups[$k] | ForEach-Object { Write-Host ("  " + $_) }
}
