interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / omc / gate-recursion.sh

99 lines · 4473 bytes Raw Blame History
1#!/bin/sh
2# gate-recursion.sh -- bounded recursion in seeded Omegese (issue #645). The compiler under test is
3# seed/out/omc (OMC overrides it). seed/omc/tests/recursion/nest.om is a mutually recursive descent
4# evaluator (primary -> expr -> term -> primary) with an explicit depth bound (DMAX 16) and a work
5# bound (FUEL 64). Every row of cases.tsv must give its exact exit status and stdout: depth 16 is
6# accepted and depth 17 is refused with status 40 before any frame for it exists, 64 primaries are
7# accepted and the 65th is refused with status 42. Then each source mutant (one line, whose site
8# occurs exactly once) must compile and be detected by a wrong status or stdout on some row whose
9# status is below 100. A mutant that does not compile, or is noticed only by a trap or a signal, is
10# INVALID and fails the gate: a crash is never a killed mutant. RC=0 only when everything passed.
11set -eu
12R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
13cd "$R"
14B=build/seed/recursion
15T=seed/omc/tests/recursion
16S=${OMC:-seed/out/omc}
17mkdir -p "$B"
18[ -x "$S" ] || { printf 'FAIL: %s missing; run sh seed/omega-from-seed.sh first\n' "$S" >&2; exit 1; }
19bad() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
20say() { printf '%s\n' "$*"; }
21
22# one input file and one expected-stdout file per row
23awk -F '\t' -v d="$B" '
24/^#/ || NF == 0 { next }
25NF != 4 { print "bad row " NR > "/dev/stderr"; exit 1 }
26{
27 x = $2; s = x
28 if (x ~ /^nest:/ || x ~ /^open:/) {
29 split(x, f, ":"); s = ""
30 for (i = 0; i < f[2]; i++) s = s "("
31 s = s f[3]
32 if (f[1] == "nest") for (i = 0; i < f[2]; i++) s = s ")"
33 } else if (x ~ /^sum:/) {
34 split(x, f, ":"); s = "1"
35 for (i = 1; i < f[2]; i++) s = s "+1"
36 }
37 printf "%s\n", s > (d "/" $1 ".in"); close(d "/" $1 ".in")
38 printf "%s\n", $4 > (d "/" $1 ".want"); close(d "/" $1 ".want")
39 print $1, $3 > (d "/rows")
40}' "$T/cases.tsv" || bad "cases.tsv is malformed"
41
42# build SRC BIN: rc 0 and empty stderr, else returns 1
43build() {
44 set +e
45 "$S" < "$1" > "$2" 2> "$2.err"
46 crc=$?
47 set -e
48 [ "$crc" = 0 ] && [ ! -s "$2.err" ] || return 1
49 chmod +x "$2"
50}
51
52# check BIN: prints one line per row, "ok", "wrong" (status < 100) or "trap" (status >= 100)
53check() {
54 while read -r name want; do
55 set +e
56 "$1" < "$B/$name.in" > "$B/$name.out"
57 rc=$?
58 set -e
59 if [ "$rc" = "$want" ] && cmp -s "$B/$name.out" "$B/$name.want"; then st=ok
60 elif [ "$rc" -ge 100 ]; then st=trap
61 else st=wrong; fi
62 printf '%s %s %s %s\n' "$name" "$st" "$rc" "$want"
63 done < "$B/rows"
64}
65
66say "omc sha256 $(sha256sum "$S" | cut -d' ' -f1)"
67say "source sha256 $(sha256sum "$T/nest.om" | cut -d' ' -f1)"
68build "$T/nest.om" "$B/nest" || bad "nest.om: compiler exit $crc ($(cat "$B/nest.err"))"
69say "binary sha256 $(sha256sum "$B/nest" | cut -d' ' -f1)"
70check "$B/nest" > "$B/base.res"
71n=$(wc -l < "$B/rows")
72while read -r name st rc want; do
73 [ "$st" = ok ] || bad "$name: exit $rc (want $want), stdout $(cat "$B/$name.out")"
74 say "PASS $name exit $rc: $(cat "$B/$name.out")"
75done < "$B/base.res"
76
77# mutant NAME OLD-LINE NEW-LINE
78m=0
79mutant() {
80 m=$((m + 1))
81 c=$(grep -cxF -- "$2" "$T/nest.om" || true)
82 [ "$c" = 1 ] || bad "mutant $1: site found $c times: $2"
83 awk -v o="$2" -v n="$3" '$0 == o { $0 = n } { print }' "$T/nest.om" > "$B/mut-$1.om"
84 cmp -s "$T/nest.om" "$B/mut-$1.om" && bad "mutant $1: source unchanged"
85 build "$B/mut-$1.om" "$B/mut-$1" || bad "mutant $1: INVALID, compiler exit $crc ($(cat "$B/mut-$1.err"))"
86 check "$B/mut-$1" > "$B/mut-$1.res"
87 k=$(awk '$2 == "wrong" { print $1 " exit " $3 " want " $4; exit }' "$B/mut-$1.res")
88 [ -n "$k" ] || bad "mutant $1: not detected ($(awk '$2 != "ok"' "$B/mut-$1.res" | tr '\n' ' '))"
89 say "PASS mutant $1 detected: $k"
90}
91mutant depth-limit-17 'const DMAX ulong 16' 'const DMAX ulong 17'
92mutant depth-off-by-one 'if gt nd DMAX' 'if ge nd DMAX'
93mutant depth-no-count 'nd = add depth 1' 'nd = depth'
94mutant fuel-limit-65 'const FUEL ulong 64' 'const FUEL ulong 65'
95mutant fuel-no-decrement 'put St 3 sub get St 3 1' 'put St 3 get St 3'
96mutant eval-mul-to-add 'v = mul v r' 'v = add v r'
97mutant digit-order 'v = add mul v 10 sub c 48' 'v = add mul 10 sub c 48 v'
98mutant return-frame 'ret inner' 'ret nd'
99say "RECURSION PASS cases=$n mutants=$m/$m"