You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.8 KiB

#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
BUILD_DIR="$ROOT_DIR/build"
ANTLR_DIR="$BUILD_DIR/generated/antlr4"
JAR_PATH="$ROOT_DIR/third_party/antlr-4.13.2-complete.jar"
GRAMMAR_PATH="$ROOT_DIR/src/antlr4/SysY.g4"
COMPILER="$BUILD_DIR/bin/compiler"
SAVE_TREE=false
TREE_DIR="$ROOT_DIR/test_tree"
POSITIVE_CASES=(
"$ROOT_DIR"/test/test_case/functional/*.sy
"$ROOT_DIR"/test/test_case/performance/*.sy
)
NEGATIVE_CASES=(
"$ROOT_DIR"/test/test_case/negative/*.sy
)
positive_total=0
positive_passed=0
positive_failed=0
negative_total=0
negative_passed=0
negative_failed=0
failed_cases=()
print_summary() {
local total passed failed
total=$((positive_total + negative_total))
passed=$((positive_passed + negative_passed))
failed=$((positive_failed + negative_failed))
echo
echo "Summary:"
echo " Positive cases: total=$positive_total, passed=$positive_passed, failed=$positive_failed"
echo " Negative cases: total=$negative_total, passed=$negative_passed, failed=$negative_failed"
echo " Overall: total=$total, passed=$passed, failed=$failed"
if (( ${#failed_cases[@]} > 0 )); then
echo "Failed cases:"
printf ' - %s\n' "${failed_cases[@]}"
fi
}
while [[ $# -gt 0 ]]; do
case "$1" in
--save-tree)
SAVE_TREE=true
;;
*)
echo "Unknown option: $1" >&2
echo "Usage: $0 [--save-tree]" >&2
exit 1
;;
esac
shift
done
echo "[1/4] Generating ANTLR sources..."
mkdir -p "$ANTLR_DIR"
java -jar "$JAR_PATH" \
-Dlanguage=Cpp \
-visitor -no-listener \
-Xexact-output-dir \
-o "$ANTLR_DIR" \
"$GRAMMAR_PATH"
echo "[2/4] Configuring CMake..."
cmake -S "$ROOT_DIR" -B "$BUILD_DIR" -DCMAKE_BUILD_TYPE=Release -DCOMPILER_PARSE_ONLY=ON
echo "[3/4] Building project..."
cmake --build "$BUILD_DIR" -j "$(nproc)"
echo "[4/4] Running parse-tree tests in parse-only mode..."
if [[ "$SAVE_TREE" == true ]]; then
rm -rf "$TREE_DIR"
mkdir -p "$TREE_DIR"
fi
for case_file in "${POSITIVE_CASES[@]}"; do
((positive_total += 1))
if [[ "$SAVE_TREE" == true ]]; then
rel_path="${case_file#"$ROOT_DIR"/test/test_case/}"
rel_dir="$(dirname "$rel_path")"
stem="$(basename "${case_file%.sy}")"
out_dir="$TREE_DIR/$rel_dir"
out_file="$out_dir/$stem.tree"
mkdir -p "$out_dir"
if ! "$COMPILER" --emit-parse-tree "$case_file" >"$out_file" 2>/tmp/lab1_parse.err; then
echo "FAIL: $case_file"
cat /tmp/lab1_parse.err
rm -f "$out_file"
((positive_failed += 1))
failed_cases+=("$case_file")
else
echo "PASS: $case_file -> $out_file"
((positive_passed += 1))
fi
else
if ! "$COMPILER" --emit-parse-tree "$case_file" >/dev/null 2>/tmp/lab1_parse.err; then
echo "FAIL: $case_file"
cat /tmp/lab1_parse.err
((positive_failed += 1))
failed_cases+=("$case_file")
else
echo "PASS: $case_file"
((positive_passed += 1))
fi
fi
done
if (( ${#NEGATIVE_CASES[@]} > 0 )); then
echo
echo "Running negative parse tests..."
for case_file in "${NEGATIVE_CASES[@]}"; do
((negative_total += 1))
if "$COMPILER" --emit-parse-tree "$case_file" >/tmp/lab1_negative.out 2>/tmp/lab1_negative.err; then
echo "FAIL: $case_file (expected parse failure, but parsing succeeded)"
((negative_failed += 1))
failed_cases+=("$case_file")
else
if grep -q '^\[error\] \[parse\]' /tmp/lab1_negative.err; then
echo "PASS: $case_file -> expected parse error"
((negative_passed += 1))
else
echo "FAIL: $case_file (did not report parse error as expected)"
cat /tmp/lab1_negative.err
((negative_failed += 1))
failed_cases+=("$case_file")
fi
fi
done
fi
print_summary
if (( positive_failed + negative_failed > 0 )); then
echo "Batch test finished with failures."
exit 1
fi
echo "Batch test passed."