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.

84 lines
2.0 KiB

#!/usr/bin/env bash
set -euo pipefail
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"
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
echo "[3/4] Building project..."
cmake --build "$BUILD_DIR" -j "$(nproc)"
echo "[4/4] Running parse-tree tests..."
failed=0
if [[ "$SAVE_TREE" == true ]]; then
rm -rf "$TREE_DIR"
mkdir -p "$TREE_DIR"
fi
for case_file in "$ROOT_DIR"/test/test_case/functional/*.sy "$ROOT_DIR"/test/test_case/performance/*.sy; do
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"
failed=1
else
echo "PASS: $case_file -> $out_file"
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
failed=1
else
echo "PASS: $case_file"
fi
fi
done
if [[ "$failed" -ne 0 ]]; then
echo "Batch test finished with failures."
exit 1
fi
echo "Batch test passed."