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 / explain / gate.sh

116 lines · 6601 bytes Raw Blame History
1#!/bin/sh
2# gate.sh -- the explain tool (seed/explain/explain.om): plain-language refusals without changing omc.
3# sh seed/explain/gate.sh build explain with seed/out/omc and run every check below
4# sh seed/explain/gate.sh table print the MEANS line generated from seed/omc/error-codes.reg
5# Checks: (1) the MEANS line in explain.om equals the registry; a changed meaning is caught (negative
6# control). (2) Every gate15 refusal case, read from gate15.sh itself, keeps omc's exit code and prints
7# exactly "p.om:L: error C: MEANING" and " L | <line L of p.om>". (3) A valid program compiles to the
8# same bytes as omc alone and runs. (4) Stand-in compilers: a wrong-form message, a code that differs
9# from the message, a signal, a missing compiler, 100 KB of stderr; and usage. (5) Two runs are identical.
10set -eu
11R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
12cd "$R"
13S=seed/out/omc
14E=seed/explain/explain.om
15REG=$R/seed/omc/error-codes.reg
16table() { awk -F'|' 'BEGIN{printf "text means \""} $1=="omc"{printf "%s\\n", $5} END{printf "\"\n"}' "$REG"; }
17if [ "${1:-}" = table ]; then table; exit 0; fi
18[ -x "$S" ] || { printf 'FAIL: %s missing; run sh seed/omega-from-seed.sh first\n' "$S" >&2; exit 1; }
19mkdir -p build/seed/explain
20T=$(mktemp -d build/seed/explain/run.XXXXXX)
21trap 'rm -rf "$T"' EXIT HUP INT TERM
22bad() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
23say() { printf 'PASS %s\n' "$*"; }
24meaning() { awk -F'|' -v c="$1" '$1=="omc" && $2==c {print $5}' "$REG"; }
25
26# (1) table drift, with its negative control
27drift() { [ "$(grep '^text means "' "$1")" = "$(table)" ]; }
28drift "$E" || bad "explain.om MEANS differs from $REG; regenerate with: sh seed/explain/gate.sh table"
29sed 's/\\ntype mismatch\\n/\\ntype mismatches\\n/' "$E" > "$T/mut.om"
30cmp -s "$E" "$T/mut.om" && bad "negative control: the mutant table equals the original"
31drift "$T/mut.om" && bad "negative control: a changed meaning was not caught"
32say "table equals the registry (30 codes); a changed meaning is caught"
33
34"$S" < "$E" > "$T/explain" 2> "$T/build.err" || bad "omc refused explain.om: $(cat "$T/build.err")"
35chmod +x "$T/explain"
36X=$R/$T/explain
37O=$R/$S
38say "explain builds with $S ($(wc -c < "$T/explain") bytes)"
39
40# (2) every gate15 refusal case
41awk "/<<'CASES'/{f=1;next} /^CASES\$/{f=0} f" seed/omc/gate15.sh > "$T/cases"
42want_n=$(grep -c '|' "$T/cases")
43[ "$want_n" -ge 20 ] || bad "read only $want_n cases from gate15.sh"
44n=0
45cd "$T"
46while IFS='|' read -r name want line src; do
47 [ -n "$name" ] || continue
48 printf '%b\n' "$src" > p.om
49 set +e; "$X" "$O" p.om p.bin > p.out 2> p.err; rc=$?; set -e
50 [ "$rc" = "$want" ] || bad "$name: exit $rc, wanted $want ($(cat p.err))"
51 m=$(meaning "$want")
52 [ -n "$m" ] || bad "$name: no registry meaning for $want"
53 [ "$(sed -n 1p p.err)" = "p.om:$line: error $want: $m" ] || bad "$name: line 1 '$(sed -n 1p p.err)'"
54 src_line=$(sed -n "${line}p" p.om)
55 [ "$(grep -c '' p.om)" -ge "$line" ] || src_line='(end of file)'
56 [ "$(sed -n 2p p.err)" = " $line | $src_line" ] || bad "$name: line 2 '$(sed -n 2p p.err)', wanted ' $line | $src_line'"
57 [ "$(grep -c '' p.err)" = 2 ] || bad "$name: $(grep -c '' p.err) stderr lines, wanted 2"
58 [ -s p.out ] && bad "$name: wrote to stdout"
59 n=$((n + 1))
60done < cases
61[ "$n" = "$want_n" ] || bad "ran $n of $want_n gate15 cases"
62say "all $n gate15 refusal cases: omc's exit code kept, meaning and source line shown"
63
64# (3) valid control: same bytes as omc alone, and the program runs
65printf 'fn main to int\nret 7\nend\n' > g.om
66"$O" < g.om > g.direct 2> g.derr || bad "valid control refused by omc"
67set +e; "$X" "$O" g.om g.bin > g.out 2> g.err; rc=$?; set -e
68[ "$rc" = 0 ] || bad "valid control: exit $rc ($(cat g.err))"
69[ -s g.err ] && bad "valid control: stderr '$(cat g.err)'"
70[ "$(cat g.out)" = "g.om: compiled, program written to g.bin" ] || bad "valid control: stdout '$(cat g.out)'"
71cmp -s g.direct g.bin || bad "valid control: output differs from omc alone"
72chmod +x g.bin
73set +e; ./g.bin; rc=$?; set -e
74[ "$rc" = 7 ] || bad "valid control: program exit $rc, wanted 7"
75say "valid program: same bytes as omc alone, runs with exit 7"
76
77# om2 diag: the machine record passes through after the explanation
78printf 'om2 diag\nfn main to int\nx = add int 1 uint 2\nret 0\nend\n' > d.om
79set +e; "$X" "$O" d.om d.bin 2> d.err; rc=$?; "$O" < d.om > /dev/null 2> d.direct; set -e
80[ "$rc" = 7 ] || bad "om2 diag: exit $rc"
81[ "$(sed -n 1p d.err)" = "d.om:3: error 7: $(meaning 7)" ] || bad "om2 diag: line 1 '$(sed -n 1p d.err)'"
82[ "$(sed -n 3p d.err)" = "$(sed -n 2p d.direct)" ] || bad "om2 diag: record not passed through"
83say "om2 diag record passed through unchanged after the explanation"
84
85# (4) stand-in compilers and usage
86odd="explain: the compiler's message is not in the form omc: line L: error C"
87stand() { printf '#!/bin/sh\n%s\n' "$2" > "$1"; chmod +x "$1"; }
88stand c_form 'echo boom >&2; exit 7'
89stand c_code 'echo "omc: line 2: error 7" >&2; exit 9'
90stand c_sig 'kill -9 $$'
91stand c_big 'i=0; while [ $i -lt 1600 ]; do printf "%%064d\\n" 0 >&2; i=$((i+1)); done; exit 3'
92stand c_huge 'echo "omc: line 99999999999999999999999: error 7" >&2; exit 7'
93chk() { # chk NAME COMPILER WANT_RC WANT_LAST_LINE
94 set +e; "$X" "$R/$T/$2" g.om s.bin > s.out 2> s.err; rc=$?; set -e
95 [ "$rc" = "$3" ] || bad "$1: exit $rc, wanted $3 ($(tail -1 s.err))"
96 [ "$(tail -1 s.err)" = "$4" ] || bad "$1: last line '$(tail -1 s.err)', wanted '$4'"
97}
98chk "wrong-form message" c_form 7 "$odd"
99chk "code differs from message" c_code 9 "$odd"
100chk "compiler killed" c_sig 66 "explain: the compiler was stopped by signal 9"
101chk "100 KB of stderr" c_big 3 "$odd"
102chk "line number past 20 digits" c_huge 7 "$odd"
103set +e; "$X" /nonexistent/omc g.om s.bin 2> s.err; rc=$?; set -e
104[ "$rc" = 65 ] && [ "$(cat s.err)" = "explain: the compiler could not be started" ] || bad "missing compiler: exit $rc '$(cat s.err)'"
105set +e; "$X" "$O" no-such.om s.bin 2> s.err; rc=$?; set -e
106[ "$rc" = 65 ] && [ "$(cat s.err)" = "explain: cannot open no-such.om" ] || bad "missing source: exit $rc '$(cat s.err)'"
107set +e; "$X" "$O" g.om 2> s.err; rc=$?; set -e
108[ "$rc" = 64 ] && [ "$(cat s.err)" = "usage: explain OMC SRC OUT" ] || bad "usage: exit $rc '$(cat s.err)'"
109say "stand-ins: odd message, code mismatch, signal, missing compiler, missing source, 100 KB stderr, usage"
110
111# (5) determinism
112printf 'fn f to int\nret q\nend\n' > r.om
113set +e; "$X" "$O" r.om r.bin 2> r1.err; "$X" "$O" r.om r.bin 2> r2.err; set -e
114cmp -s r1.err r2.err || bad "two runs differ"
115say "two runs identical"
116printf 'RC=0\n'