#!/usr/bin/env bash
# temporal.sh — report temporal type violations from emitted relations.
#
# The emitter still has to emit SOMETHING for an illegal expression, so the
# TIME_TYPE_ERROR placeholder stays in the generated C. What moved out is the
# judgment and the wording: codegen records "temporal:instant_plus_instant" and
# this decides what that means.
set -uo pipefail
REL="${1:?usage: temporal.sh <relations-file> [rules]}"
RULES="${2:-$(dirname "${BASH_SOURCE[0]}")/temporal.rel}"
[ -f "$REL" ] || exit 0
V=0
while read -r caller _ rest; do
    [ "${rest#temporal:}" = "$rest" ] && continue
    kind="${rest#temporal:}"
    msg=$(awk -v k="$kind" '$1==k && $2=="means" {sub(/^[^ ]+[ ]+means[ ]+/,""); print; exit}' "$RULES")
    [ -n "$msg" ] || msg="$kind"
    printf 'temporal type error in %s: %s\n' "$caller" "$msg"
    V=$((V+1))
done < <(sort -u "$REL")
[ "$V" -eq 0 ] && echo "temporal: clean"
exit "$V"
