corpus.sh 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/sh
  2. # Runs the V3 compiler (compiler/M2) over a corpus of standard Modula-2
  3. # sources and reports how many compile cleanly.
  4. #
  5. # Each source is copied into a scratch tree (so its .LST listing never
  6. # touches the corpus) and compiled alone, together with:
  7. # * its sibling DEFINITION (same basename, .def/.DEF) when present, and
  8. # * the DEFINITION of every module it imports, searched in the source
  9. # directory and in the -I-style library dirs listed in $V3_LIBS
  10. # (colon-separated), so cross-module imports resolve.
  11. #
  12. # A file with 0 errors is a pass. Failures are split into CRASH (M2
  13. # died), LIB (an import still unresolved: no .def found) and LANG (a
  14. # real V3 gap).
  15. #
  16. # Usage: ./corpus.sh [corpus-dir ...]
  17. cd "$(dirname "$0")"
  18. ROOT="$(cd ../.. && pwd)"
  19. M2="$ROOT/compiler/M2"
  20. WORK=/tmp/opencode/v3corpus
  21. KNOWN="SYSTEM libc SysIO TextIO Strings Math Conversions RealIO ProgramArgs IOChan Files Storage WholeIO CharClass SysClock"
  22. [ -x "$M2" ] || { echo "compiler/M2 missing -- run compiler/build.sh"; exit 1; }
  23. [ "$#" -gt 0 ] || set -- \
  24. "$HOME/Projets/Projets-Modula2/MyWork/gcc-git/gcc/testsuite/gm2/pim/pass" \
  25. "$HOME/Projets/Projets-Modula2/MyWork/gcc-git/gcc/testsuite/gm2/iso/run/pass"
  26. known() { for k in $KNOWN; do [ "$1" = "$k" ] && return 0; done; return 1; }
  27. # find a DEFINITION file for module $1 in the source dir or $V3_LIBS
  28. finddef() {
  29. for d in "$scrdir" $(echo "$V3_LIBS" | tr ':' ' '); do
  30. [ -d "$d" ] || continue
  31. for cand in "$d/$1.def" "$d/$1.DEF" "$d/$(echo "$1" | tr 'A-Z' 'a-z').def"; do
  32. [ -f "$cand" ] && { echo "$cand"; return; }
  33. done
  34. done
  35. }
  36. # extract imported module names from a source
  37. imports() {
  38. { sed -nE 's/^[[:space:]]*FROM[[:space:]]+([A-Za-z][A-Za-z0-9_]*).*/\1/p' "$1"
  39. sed -nE 's/^[[:space:]]*IMPORT[[:space:]]+(.*)/\1/p' "$1" | sed 's/;.*//' | tr ',' '\n'
  40. } | sed -E 's/[[:space:]]//g' | grep -E '^[A-Za-z][A-Za-z0-9_]*$'
  41. }
  42. rm -rf "$WORK"; mkdir -p "$WORK"; : > "$WORK/results.txt"
  43. n=0
  44. for scrdir in "$@"; do
  45. find "$scrdir" -maxdepth 1 \( -iname '*.mod' -o -iname '*.def' \) 2>/dev/null | sort |
  46. while read -r f; do
  47. n=$((n+1))
  48. w="$WORK/f$n"; mkdir -p "$w"; base=$(basename "$f")
  49. cp "$f" "$w/$base"
  50. files="$w/$base"
  51. # sibling DEFINITION first
  52. case "$base" in
  53. *.mod|*.MOD)
  54. sib="$(echo "$base" | sed -E 's/\.[^.]*$/.def/')"
  55. [ -f "$scrdir/$sib" ] || sib="$(echo "$base" | sed -E 's/\.[^.]*$/.DEF/')"
  56. if [ -f "$scrdir/$sib" ]; then cp "$scrdir/$sib" "$w/$sib"; files="$w/$sib $files"; fi
  57. ;;
  58. esac
  59. # imported DEFINITIONs (for the LIB classification and for resolution)
  60. lib=0
  61. for m in $(imports "$w/$base"); do
  62. known "$m" && continue
  63. df=$(finddef "$m")
  64. if [ -n "$df" ]; then
  65. cp "$df" "$w/$(basename "$df")"; files="$w/$(basename "$df") $files"
  66. else
  67. lib=1
  68. fi
  69. done
  70. "$M2" $files >/dev/null 2>&1
  71. rc=$?
  72. lst="$w/$(echo "$base" | sed 's/\.[^.]*$//').LST"
  73. if [ "$rc" -ge 128 ]; then echo "CRASH||$f" >> "$WORK/results.txt"; continue; fi
  74. if [ ! -f "$lst" ]; then echo "LANG|no listing|$f" >> "$WORK/results.txt"; continue; fi
  75. e=$(grep -oE '[0-9]+ errors?' "$lst" | head -1 | grep -oE '[0-9]+')
  76. [ -z "$e" ] && e=-1
  77. if [ "$e" = 0 ]; then echo "OK||$f" >> "$WORK/results.txt"; continue; fi
  78. m=$(grep -E '^\*\*\*\*\*' "$lst" | head -1 | sed -E 's/^\*\*\*\*\* *\^? *//; s/^\*\*\*\*\*//')
  79. if [ "$lib" = 1 ]; then echo "LIB|$m|$f" >> "$WORK/results.txt"
  80. else echo "LANG|$m|$f" >> "$WORK/results.txt"; fi
  81. done
  82. done
  83. total=$(wc -l < "$WORK/results.txt")
  84. cnt() { grep -c "^$1" "$WORK/results.txt" 2>/dev/null || true; }
  85. echo "total : $total"
  86. echo "compile-OK : $(cnt OK)"
  87. echo "CRASH : $(cnt CRASH)"
  88. echo "LIB (unresolved import): $(cnt LIB)"
  89. echo "LANG (V3 gap) : $(cnt LANG)"
  90. echo "=== LANG error kinds (top) ==="
  91. grep '^LANG' "$WORK/results.txt" | cut -d'|' -f2 | sed -E 's/[0-9]+/N/g' | sort | uniq -c | sort -rn | head -15
  92. echo "=== LANG files (first 40) ==="
  93. grep '^LANG' "$WORK/results.txt" | head -40 | sed -E 's/^LANG\|([^|]*)\|/ [\1] /'