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 / tools / omegese / test_omegese.sh

1283 lines · 46980 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# tools/omegese/test_omegese.sh <omegese> <omega_emit> <checker2> -- Omegese checks (docs/one_layer/OMEGESE.md).
18# 0. with OMEGESE_SANITIZED=1 (an ASan and UBSan build): canary negative controls, then every sanitizer
19# exit fails the run;
20# 1. every public dev program (tests/one_layer/omegese/dev) -> omegese emit -> omega_emit emit -> checker2
21# ACCEPT; 1a nested and trial-2 programs; 1b coverage programs; 1c omegese next on fixed prefixes;
22# 2. refusals: each bad program is refused by omegese with its expected reason, and a control (the
23# same program with the defect fixed) is accepted;
24# 3. loop guard negative control: the dev04 op list with the hidden loop counter increment removed
25# still passes omega_emit, and checker2 must REJECT it as C2-LOOP-UNBOUNDED;
26# 4. tokenizer gate (only when OMEGESE_TOKCOUNT and OMEGESE_TOKENIZER are set): every fixed word of
27# the language, with its leading space, is exactly one token;
28# 5. next oracle: every word of every accepted program is legal where it is written;
29# 6. completeness: single-word mutants (OMEGESE_MUTANTS per program): emit accepts iff every word legal;
30# 7. soundness fuzz (OMEGESE_WALKS walks of OMEGESE_STEPS items) closed by omegese complete; then the
31# checker2 residual (OMEGESE_RESIDUAL_WALKS walks from each of two starts, closed by complete, through
32# omega_emit and checker2; at most OMEGESE_RESIDUAL_MAX, default 0, rejected there); then the
33# truncation fuzz: every byte prefix through emit, next and next --machine answers, never crashes,
34# allows the byte that follows, and is finished by complete.
35set -euo pipefail
36HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
37ROOT="$(cd "$HERE/../.." && pwd)"
38[ $# -eq 3 ] || { echo "usage: test_omegese.sh <omegese> <omega_emit> <checker2>" >&2; exit 2; }
39G="$1" OME="$2" C2="$3"
40T="$(mktemp -d "${TMPDIR:-/tmp}/omegese-test.XXXXXXXX")"; trap 'rm -rf "$T"' EXIT
41fails=0
42bad() { echo "FAIL $*"; fails=$((fails + 1)); }
43
44# 0. sanitized run (OMEGESE_SANITIZED=1: <omegese> is built with ASan and UBSan, which exit 86 on a
45# finding; omegese itself only exits 0, 1 or 2). <omegese> is wrapped so that every exit 86 is logged
46# with its report and fails the run at the end, wherever the test reads the exit code. Negative control
47# first: each binary in OMEGESE_CANARIES (built with -DOMEGESE_CANARY=1, a read after free, or =2, a
48# signed overflow) must be caught, so a sanitizer that is silently off cannot pass.
49if [ "${OMEGESE_SANITIZED:-0}" = 1 ]; then
50 export ASAN_OPTIONS="exitcode=86" UBSAN_OPTIONS="halt_on_error=1:print_stacktrace=1:exitcode=86"
51 nc=0
52 for c in ${OMEGESE_CANARIES:-}; do
53 rc=0; "$c" emit "$ROOT/tests/one_layer/omegese/dev/dev01.omegese" "$T/canary.json" > /dev/null 2> "$T/canary.err" || rc=$?
54 if [ "$rc" -eq 86 ] && grep -qE 'heap-use-after-free|signed integer overflow' "$T/canary.err"; then nc=$((nc + 1))
55 else bad "sanitizer negative control $(basename "$c"): not caught (exit $rc)"; fi
56 done
57 [ "$nc" -eq 2 ] || bad "sanitizer negative control: $nc canaries caught, want 2 (set OMEGESE_CANARIES)"
58 echo "sanitizers: $nc/2 canaries caught (read after free, signed overflow)"
59 printf '%s\n' '#!/usr/bin/env bash' "e=\"\$(mktemp)\"; rc=0; '$G' \"\$@\" 2> \"\$e\" || rc=\$?" \
60 "[ \$rc -ne 86 ] || { echo \"== \$*\"; cat \"\$e\"; } >> '$T/san.log'" 'cat "$e" >&2; rm -f "$e"; exit $rc' > "$T/gsan"
61 chmod +x "$T/gsan"; G="$T/gsan"
62fi
63
64# 1. dev programs
65n=0
66for f in "$ROOT"/tests/one_layer/omegese/dev/dev*.omegese; do
67 id="$(basename "$f" .omegese)"
68 if ! "$G" emit "$f" "$T/$id.json" > "$T/$id.out"; then bad "$id omegese: $(cat "$T/$id.out")"; continue; fi
69 if ! "$OME" emit "$T/$id.json" "$T/$id.oscir" > "$T/$id.out"; then bad "$id omega_emit: $(cat "$T/$id.out")"; continue; fi
70 v="$("$C2" check "$T/$id.oscir" 2>&1 | head -1 || true)"
71 case "$v" in checker2=ACCEPT*) n=$((n + 1)) ;; *) bad "$id checker2: $v" ;; esac
72done
73[ "$n" -eq 10 ] || bad "dev programs accepted $n/10"
74echo "dev: $n/10 omegese -> omega_emit -> checker2 ACCEPT"
75
76# 1a. nested values: tests/one_layer/omegese/nested (dev tasks rewritten with nested values and
77# brackets) and tests/one_layer/omegese/trial2 (the model's own answers from phase-2 trial 2,
78# verbatim, that v0 refused for nesting or brackets): omegese -> omega_emit -> checker2 ACCEPT.
79# trial2/dev07 has a real extra value and must stay refused, at its line, for that reason.
80accept() { # <file> <id>: omegese -> omega_emit -> checker2 ACCEPT
81 if ! "$G" emit "$1" "$T/$2.json" > "$T/$2.out"; then bad "$2 omegese: $(cat "$T/$2.out")"; return 1; fi
82 if ! "$OME" emit "$T/$2.json" "$T/$2.oscir" > "$T/$2.out"; then bad "$2 omega_emit: $(cat "$T/$2.out")"; return 1; fi
83 local v; v="$("$C2" check "$T/$2.oscir" 2>&1 | head -1 || true)"
84 case "$v" in checker2=ACCEPT*) return 0 ;; *) bad "$2 checker2: $v"; return 1 ;; esac
85}
86nn=0 nt=0
87for f in "$ROOT"/tests/one_layer/omegese/nested/*.omegese; do accept "$f" "nested-$(basename "$f" .omegese)" && nn=$((nn + 1)); done
88for f in "$ROOT"/tests/one_layer/omegese/trial2/*.omegese; do
89 id="$(basename "$f" .omegese)"
90 if [ "$id" = dev07 ]; then
91 out="$("$G" emit "$f" "$T/x.json" || true)"
92 [ "$out" = "REFUSED omegese: line 3: le has 3 values after it; it takes 2" ] || bad "trial2 dev07 (extra value) must be refused: got '$out'"
93 else accept "$f" "trial2-$id" && nt=$((nt + 1)); fi
94done
95[ "$nn" -eq 6 ] && [ "$nt" -eq 5 ] || bad "nested $nn/6, trial2 $nt/5"
96echo "nested: $nn/6 rewritten dev programs and $nt/5 trial-2 model answers ACCEPT; trial-2 dev07 refused for its extra value"
97
98# 1a'. review fixes: a conversion of a typed number of another type must convert (not reuse the
99# number's register under the wrong type); brackets around a for count change nothing
100printf 'fn f to long\nret long int 5\nend\n' > "$T/s1.omegese"
101accept "$T/s1.omegese" stacked-ret || true
102printf 'fn f x ulong to ulong\nret shl x uchar int 300\nend\n' > "$T/s2.omegese"
103accept "$T/s2.omegese" stacked-shift || true
104grep -q '"op": "cast"' "$T/stacked-shift.json" 2>/dev/null || bad "uchar int 300 must emit a cast (trap 5), not reuse the int const"
105printf 'fn f to uint\ns = uint 0\nfor i ulong %s\ns = add s 1\nend\nret s\nend\n' 8 > "$T/f1.omegese"
106printf 'fn f to uint\ns = uint 0\nfor i ulong %s\ns = add s 1\nend\nret s\nend\n' '( ( 8 ) )' > "$T/f2.omegese"
107"$G" emit "$T/f1.omegese" "$T/f1.json" > /dev/null && "$G" emit "$T/f2.omegese" "$T/f2.json" > /dev/null \
108 && cmp -s "$T/f1.json" "$T/f2.json" || bad "for i ulong ( ( 8 ) ) must lower exactly as for i ulong 8"
109printf 'fn f to uint\ns = uint 0\nfor i ulong ( 2000000 )\ns = add s 1\nend\nret s\nend\n' > "$T/f3.omegese"
110out="$("$G" emit "$T/f3.omegese" "$T/f3.json" || true)"
111[ "$out" = "REFUSED omegese: line 3: count '2000000' must be 0..1000000" ] || bad "bracketed count over the limit: got '$out'"
112echo "review fixes: stacked conversions convert; brackets around a for count change nothing"
113
114# 1b. coverage programs (break, nested loops, else-if, a for with a name as the count, arrays and
115# calls across functions): omegese -> omega_emit -> checker2 ACCEPT
116cov=0
117awk -v d="$T" '/^== / { k++; f = d "/c" k ".omegese"; next } { print > f }' <<'EOF'
118== break and nested loops
119fn f n uint to uint
120s = uint 0
121for i uint n
122for j uint 8
123if gt j i
124break
125end
126s = add s j
127end
128end
129ret s
130end
131== else-if chain
132fn sign a int to int
133if lt a 0
134ret -1
135else
136if gt a 0
137ret 1
138end
139end
140ret 0
141end
142== arrays and calls
143fn sum4 v view uint 4 to uint
144s = uint 0
145for i uint 4
146x = get v i
147s = add s x
148end
149ret s
150end
151fn fill e edit uint 4 to void
152for i uint 4
153put e i i
154end
155end
156fn f to uint
157a = new uint 4 0
158m = edit a
159fill m
160s = sum4 a
161free a
162ret s
163end
164== IR type names as written in the task signature
165fn clamp_add a u8 b u8 to u8
166s = add a b
167if lt s a
168ret u8 255
169end
170ret s
171end
172== calls without values, a bool function as a condition
173fn seven to uint
174ret 7
175end
176fn odd a uint to bool
177b = and a 1
178ret ne b 0
179end
180fn f a uint to uint
181x = seven
182if odd a
183ret x
184end
185ret a
186end
187== nested values in every place a value goes: call args, get index, put index and value, for count, ret, conversions, brackets
188fn sq a uint to uint
189ret mul a a
190end
191fn f v view uint 4 n uint to uint
192s = uint 0
193for i uint sub n 1
194s = add s sq get v rem i 4
195end
196w = new uint 4 0
197put w rem add s 2 4 mul s 2
198for j uint (4)
199s = add s get w j
200end
201free w
202ret uint ulong add (s) 0
203end
204== a number first takes the type of the other value; shifts; neg and not nested; a call with no values nested
205fn seven to int
206ret 7
207end
208fn g a int b ulong to bool
209x = sub 100 neg a
210y = shl 1 and b 7
211z = add seven x
212if and not eq z 0 gt y 3
213ret true
214end
215ret ne (rem 10 z) 0
216end
217== a while condition with nested values; trap ends a path; borrows as call arguments
218fn total v view uint 4 to uint
219ret add add get v 0 get v 1 add get v 2 get v 3
220end
221fn h a uint to uint
222while lt mul a a 1000 bound 64
223a = add a 1
224end
225if gt a 100
226trap 1
227else
228if eq a 7
229trap 2
230end
231end
232b = new uint 4 a
233c = total view b
234free b
235ret c
236end
237EOF
238for f in "$T"/c*.omegese; do
239 if "$G" emit "$f" "$T/c.json" > "$T/c.out" && "$OME" emit "$T/c.json" "$T/c.oscir" > "$T/c.out"; then
240 v="$("$C2" check "$T/c.oscir" 2>&1 | head -1 || true)"
241 case "$v" in checker2=ACCEPT*) cov=$((cov + 1)) ;; *) bad "coverage $(basename "$f") checker2: $v" ;; esac
242 else bad "coverage $(basename "$f"): $(cat "$T/c.out")"; fi
243done
244echo "coverage: $cov/$(ls "$T"/c*.omegese | wc -l) accepted"
245
246# 1c. omegese next: what may come next after a prefix, exactly (docs/one_layer/OMEGESE.md section 6).
247# At a line start: "words:" (statement words and functions to call), "assign:" (names that may
248# be assigned), "new-name:"; elsewhere: "names:", "number: lo..hi" (the numbers that may stand
249# there), "new-name:" and "words:" (fixed words, functions, brackets, "(space)" inside a word,
250# "(newline)", "(end of input)"). Only what can still end in a program emit accepts is offered.
251nx() { printf '%b' "$1" > "$T/p"; "$G" next "$T/p" || true; }
252nxk=0
253nxeq() { # <prefix> <expected output>
254 nxk=$((nxk + 1))
255 local got; got="$(nx "$1")"
256 [ "$got" = "$(printf '%b' "$2")" ] || bad "next after '$1': got '$got'"
257}
258nxcase() { # <prefix> <glob the output must match> <what>
259 nxk=$((nxk + 1))
260 local got; got="$(nx "$1")"
261 # shellcheck disable=SC2254
262 case "$got" in $2) ;; *) bad "next $3: got '$got'" ;; esac
263}
264nxeq 'fn f a uint to uint\nret a\n' 'words: end'
265nxeq 'fn f a uint b ulong to uint\nx = add a ' 'names: a\nnumber: 0..4294967295\nwords: add sub mul div rem and or xor shl shr not uint u32 ('
266nxeq 'fn f a uint to uint\nfor i uint 4\n' 'assign: a\nnew-name: any name not in use\nwords: if while for ret trap break end'
267nxcase 'fn f a uint to uint\nret a\nret ' 'REFUSED omegese: *path already ended*' 'after a dead line: not refused'
268nxcase 'x = add ' 'REFUSED omegese: *outside a function*' 'before fn: not refused'
269nxeq 'fn odd a uint to bool\nret true\nend\nfn f a uint fl view bool 4 to uint\nif ' 'words: true false and or xor eq ne lt le gt ge not get bool odd ('
270# nesting: a stack of open operand slots, each with the type it needs
271nxeq 'fn f r uint q ulong c bool to uint\nx = add mul r ' 'names: r\nnumber: 0..4294967295\nwords: add sub mul div rem and or xor shl shr not uint u32 ('
272nxeq 'fn f r uint q ulong c bool to uint\nx = add mul r r ' 'names: r\nnumber: 0..4294967295\nwords: add sub mul div rem and or xor shl shr not uint u32 ('
273nxeq 'fn f r uint q ulong c bool to uint\nret sub ' 'names: r\nnumber: 0..4294967295\nwords: add sub mul div rem and or xor shl shr not uint u32 ('
274nxeq 'fn f p short to short\nif lt p short ' 'names: p\nnumber: -32768..32767\nwords: add sub mul div rem and or xor shl shr neg not uchar ushort uint ulong byte short int long u8 u16 u32 u64 i8 i16 i32 i64 ('
275# the type is still open: every whole-number name (bool and arrays excluded for add) and any number
276# some whole-number type holds; after a bare number, no second bare number, and only names it fits
277nxeq 'fn f r uint q ulong c bool to uint\nx = add ' 'names: r q\nnumber: -9223372036854775808..18446744073709551615\nwords: add sub mul div rem and or xor shl shr neg not uchar ushort uint ulong byte short int long u8 u16 u32 u64 i8 i16 i32 i64 ('
278nxeq 'fn f r uint q ulong c bool to uint\nx = add 1 ' 'names: r q\nwords: add sub mul div rem and or xor shl shr neg not uchar ushort uint ulong byte short int long u8 u16 u32 u64 i8 i16 i32 i64 ('
279nxeq 'fn f a uchar b uint to uint\nx = add 300 ' 'names: b\nwords: add sub mul div rem and or xor shl shr neg not ushort uint ulong short int long u16 u32 u64 i16 i32 i64 ('
280# brackets, the end of a value, arrays, trap
281nxeq 'fn f r uint q ulong c bool to uint\nx = (add r r ' 'words: )'
282nxeq 'fn f r uint x uint to uint\nwhile le add mul r r 4 r ' 'words: bound (newline)'
283nxeq 'fn f r uint x uint to uint\nret add r (r) ' 'words: (newline)'
284nxeq 'fn f v view uint 4 e edit uint 4 to uint\nput ' 'names: e'
285nxeq 'fn g v view uint 4 to uint\nret 1\nend\nfn f a own uint 4 to uint\nx = g ' 'names: a\nwords: own edit view ('
286nxeq 'fn f p short c bool to short\nif c\ntrap 1\n' 'words: else end'
287nxcase 'fn f r uint to uint\nx = add r r r ' 'REFUSED omegese: line 2: add has 3 values after it; it takes 2' 'after an extra value: not refused'
288nxcase 'fn f r uint to uint\nx = add 1 2 ' 'REFUSED omegese: *two numbers*' 'after two numbers: not refused'
289nxcase 'fn f r uint to uint\nx = add r ) ' 'REFUSED omegese: *no matching*' 'after a stray ): not refused'
290# never offer a word that leads to a dead end: no end before ret, no trap while b is unread, no put
291# or free without an array
292nxeq 'fn f a uint to uint\nb = add a 1\n' 'assign: a b\nnew-name: any name not in use\nwords: if while for ret'
293nxcase 'fn f a uint to uint\nb = add a 1\nend\n' 'REFUSED omegese: *without ret*' 'end before ret: not refused'
294nxcase 'fn f a uint to uint\nelse\n' 'REFUSED omegese: line 2: else without an open if' 'stray else: not refused'
295nxcase 'fn f a uint to uint\nbreak\n' 'REFUSED omegese: line 2: break outside a loop' 'stray break: not refused'
296nxcase 'fn f a uint to uint\nb = add a 1\ntrap 1\n' "REFUSED omegese: line 4: *'b' is written but never read*" 'a trap that leaves b unread: not refused'
297# ret must read every name the ended path leaves unread: b only (no bare number, no a alone); a bool
298# with no function to turn it into a number cannot be read by a number; inside an if, only the
299# names of the if
300nxeq 'fn f a uint to uint\nb = add a 1\nret ' 'names: b\nwords: add sub mul div rem and or xor shl shr not uint u32 ('
301nxcase 'fn f a uint to uint\nb = gt a 1\nret ' "REFUSED omegese: *'b' is written but never read*" 'an unreadable bool before ret: not refused'
302nxeq 'fn h c bool to ulong\nret ulong 0\nend\nfn f a uint to uint\nb = gt a 1\nret add a ' 'words: add sub mul div rem and or xor shl shr not uint u32 ('
303nxeq 'fn f a uint v view bool 4 to bool\nx = view v\nret eq ' 'words: true false and or xor eq ne lt le gt ge not get bool ('
304nxeq 'fn f a uint v view uint 4 to uint\nx = view v\nif gt a 2\ny = add a 1\nret ' 'names: y\nwords: add sub mul div rem and or xor shl shr not get uint u32 ('
305# inside a word: the words that extend it, a name not in use, and what may end it
306nxeq 'fn f a uint to uint\nb = add a 1\nen' 'new-name: any name not in use\nwords: ( (space)'
307nxeq 'fn f a uint to uint\nb = add a 1\nre' 'new-name: any name not in use\nwords: ret ( (space)'
308nxeq 'fn f a uint to uint\nx = new uint 4 0\nfree x\nx = new uint ' 'number: 4..4'
309nxeq 'fn f a uint to uint\nx = new uint 4 0\nfree x\nx = new uint 4' 'number: 4..4\nwords: ( (space)'
310nxcase 'fn f a uint to uint\nx = new uint 4 0\nfree x\nx = new uint 1' 'REFUSED omegese: line 4: *own uint 1 but the name holds own uint 4*' 'a length no number extends: not refused'
311# ownership (checker2's C2-OWN rules, OMEGESE.md 6.4): no ret while an own array holds its array on
312# some path, no second free; at a line start ret is left out until the array is freed; after a loop
313# that frees and refills it the array still holds one on some path, so complete frees it
314nxcase 'fn f a own uint 4 to uint\nret ' "REFUSED omegese: line 2: no text after this prefix*'a' still holds its array*" 'ret while an own array holds its array: not refused'
315nxcase 'fn f a own uint 4 to uint\nfree a\nfree ' "REFUSED omegese: line 3: *freed or moved on every path*" 'a second free: not refused'
316nxk=$((nxk + 1)); got="$(printf 'fn f a own uint 4 to uint\n' > "$T/p"; "$G" next --machine "$T/p")"
317[ "$got" = "$(printf 'word if\nword while\nword for\nword put\nword free\nword trap\nword a\nnewname a f\nspace\nnewline')" ] || bad "next --machine with an own array that must be freed: got '$got'"
318nxk=$((nxk + 1)); got="$(printf 'fn f a own uint 4 n uint to uint\nwhile gt n 0\nfree a\na = new uint 4 n\nn = sub n 1\nend\n' > "$T/p"; "$G" complete "$T/p")"
319[ "$got" = "$(printf 'free a\nret 0\nend')" ] || bad "complete after a loop that refills an own array: got '$got'"
320# a loop's state at its head is known at its end line: a use of an array freed before the loop is
321# refused there unless the body gives it a new one, so end is left out and complete writes those lines
322# (free first: the array may hold the one an earlier pass gave it)
323nxeq 'fn f a own uint 4 n uint to uint\nfree a\nwhile gt get a 0 n\nn = add n 1\n' 'assign: a n\nnew-name: any name not in use\nwords: if while for put free'
324nxcase 'fn f a own uint 4 n uint to uint\nfree a\nwhile gt get a 0 n\nn = add n 1\nend\n' "REFUSED omegese: line 5: 'a' holds no array at line 3 on every path, once this loop is closed*" 'end of a loop whose head uses a freed array: not refused'
325nxk=$((nxk + 1)); got="$(printf 'fn f a own uint 4 n uint to uint\nfree a\nwhile gt get a 0 n\nn = add n 1\n' > "$T/p"; "$G" complete "$T/p")"
326[ "$got" = "$(printf 'free a\na = new uint 4 0\nend\nfree a\nret 0\nend')" ] || bad "complete in a loop whose head uses a freed array: got '$got'"
327# a loop that gives an array a second one while it still holds the first leaks on its first pass: no
328# line after it repairs that, so no completion exists
329nxcase 'fn f n uint to void\nx = new uint 4 n\nwhile gt n 0\nx = new uint 4 n\n' "REFUSED omegese: line 5: no text after this prefix*'x' still holds an array at line 4 on some path*" 'a loop that gives an array a second one: not refused'
330# a loop opened at the nesting limit is refused before its head state is set up (no shift past bit 63)
331{ echo 'fn f n uint to uint'; for ((i = 0; i < 63; i++)); do echo "if gt n $i"; done; echo 'while gt n 0'; } > "$T/deep.omegese"
332nxk=$((nxk + 1)); got="$("$G" emit "$T/deep.omegese" "$T/x.json" 2>&1 || true)"
333case "$got" in "REFUSED omegese: line 65: more than 64 nested blocks"*) ;; *) bad "a loop at the nesting limit: got '$got'" ;; esac
334# 300 uses of one array inside a loop wait as one check (the same state): accepted, as checker2 does
335{ echo 'fn f a own uint 4 n uint to uint'; echo 'for i uint 3'; for ((i = 0; i < 300; i++)); do echo 'n = get a 0'; done; echo 'end'; echo 'free a'; echo 'ret n'; echo 'end'; } > "$T/many.omegese"
336nxk=$((nxk + 1)); accept "$T/many.omegese" many-uses-in-a-loop || true
337nxeq 'fn f a short to short\nret add a -' 'number: -32768..32767'
338nxeq 'fn f a short to short\nret add a -0' 'REFUSED omegese: line 2: no text after this prefix makes a program emit accepts ('"'"'-0'"'"' is not a number (digits, an optional minus sign, no leading zeros, no -0))'
339# the number ranges each slot allows: every scalar type, a minus sign where none fits, and the fixed
340# limits (trap codes, array lengths, loop bounds and counts)
341nxnum() { # <prefix> <lo..hi> <what>: the number line is exactly that interval
342 nxk=$((nxk + 1))
343 local got; got="$(nx "$1")"$'\n'
344 [[ $got == *"number: $2"$'\n'* ]] || bad "next $3: got '$got'"
345}
346for tr in uchar:0..255 ushort:0..65535 uint:0..4294967295 ulong:0..18446744073709551615 byte:-128..127 \
347 short:-32768..32767 int:-2147483648..2147483647 long:-9223372036854775808..9223372036854775807; do
348 nxnum "fn f a ${tr%%:*} to ${tr%%:*}\\nret add a " "${tr#*:}" "range of ${tr%%:*}"
349done
350nxcase 'fn f a uint to uint\nret add a -' 'REFUSED omegese: line 2: no text after this prefix*' 'a minus sign in an unsigned slot: not refused'
351nxnum 'fn f a uint to uint\ntrap ' '1..14' 'trap codes'
352nxnum 'fn f a uint to uint\nx = new uint ' '1..64' 'array lengths'
353nxnum 'fn f a uint to uint\nwhile lt a 3 bound ' '1..1000000' 'loop bounds'
354nxnum 'fn f a uint to uint\nfor i uint ' '0..1000000' 'loop counts'
355# the machine form: one item per line
356nxm() { printf '%b' "$1" > "$T/p"; "$G" next --machine "$T/p" || true; }
357nxk=$((nxk + 1)); got="$(nxm 'fn f a uint to uint\nb = add a 1\nre')"
358[ "$got" = "$(printf 'word ret\nnewname\n(\nspace')" ] || bad "next --machine after 're': got '$got'"
359nxk=$((nxk + 1)); got="$(nxm 'fn f a uint b ulong to uint\nret a\nend\n')"
360[ "$got" = "$(printf 'word fn\nspace\nnewline\neof')" ] || bad "next --machine after a function: got '$got'"
361nxk=$((nxk + 1)); got="$(nxm 'fn f a uint b ulong to uint\nx = add a')"
362[ "$got" = "$(printf 'word add\nword and\nword a\n(\nspace')" ] || bad "next --machine inside a name: got '$got'"
363nxk=$((nxk + 1)); got="$(nxm 'fn f a uint to uint\n')"
364[ "$got" = "$(printf 'word if\nword while\nword for\nword ret\nword trap\nword a\nnewname a f\nspace\nnewline')" ] || bad "next --machine at a line start (OMEGESE.md 6.2): got '$got'"
365nxk=$((nxk + 1)); got="$(nxm 'fn f ab uint b uint to uint\nfor abc uint 3\nx = add abc a')"
366[ "$got" = "$(printf 'word add\nword and\nword ab\nword abc')" ] || bad "next --machine, names that extend the word: got '$got'"
367nxk=$((nxk + 1)); got="$(nxm 'fn f ab uint b uint to uint\nfor abc uint 3\na')"
368[ "$got" = "$(printf 'word ab\nnewname ab abc\n(\nspace')" ] || bad "next --machine, a new name that extends names in use: got '$got'"
369# complete: the witness text, which emit accepts after the prefix
370nxk=$((nxk + 1)); got="$(printf 'fn f a uint v view uint 4 to uint\nx = view v\nif gt a 2\ny = add a 1\nret ' > "$T/p"; "$G" complete "$T/p")"
371[ "$got" = "$(printf ' y\nend\nx = x\nret 0\nend')" ] || bad "complete inside an if: got '$got'"
372echo "next: $nxk prefixes give the expected words"
373
374# 2. refusals: "== <expected reason substring>", the bad program, "-- control", the fixed program
375r=0
376awk -v d="$T" '
377 /^== / { k++; sub(/^== /, ""); print > (d "/r" k ".want"); f = d "/r" k ".bad"; next }
378 /^-- control$/ { f = d "/r" k ".ok"; next }
379 { print > f }' <<'EOF'
380== needs two values of one type
381fn f a uint b ulong to uint
382x = add a b
383ret a
384end
385-- control
386fn f a uint b uint to uint
387x = add a b
388ret x
389end
390== is not a name in scope here
391fn f a uint to uint
392if gt a 1
393y = add a 1
394a = y
395end
396ret y
397end
398-- control
399fn f a uint to uint
400y = a
401if gt a 1
402y = add a 1
403end
404ret y
405end
406== can reach its end without ret
407fn f a uint to uint
408if gt a 1
409ret a
410end
411end
412-- control
413fn f a uint to uint
414if gt a 1
415ret a
416end
417ret 0
418end
419== can never run
420fn f a uint to uint
421ret a
422b = add a 1
423end
424-- control
425fn f a uint to uint
426b = add a 1
427ret b
428end
429== does not fit uchar
430fn f to uchar
431x = uchar 300
432ret x
433end
434-- control
435fn f to uchar
436x = uchar 255
437ret x
438end
439== two numbers
440fn f to uint
441x = add 1 2
442ret x
443end
444-- control
445fn f to uint
446x = uint 1
447y = add x 2
448ret y
449end
450== the type of this number is unknown
451fn f to uint
452x = 5
453ret x
454end
455-- control
456fn f to uint
457x = uint 5
458ret x
459end
460== a condition must be a bool
461fn f a uint to uint
462if a
463ret 1
464end
465ret 0
466end
467-- control
468fn f a uint to uint
469if ne a 0
470ret 1
471end
472ret 0
473end
474== must be 1..1000000
475fn f a uint to uint
476while ne a 0 bound 2000000
477a = shr a 1
478end
479ret a
480end
481-- control
482fn f a uint to uint
483while ne a 0 bound 64
484a = shr a 1
485end
486ret a
487end
488== else without an open if
489fn f a uint to uint
490else
491ret a
492end
493-- control
494fn f a uint to uint
495ret a
496end
497== is the loop counter
498fn f to uint
499s = uint 0
500for i uint 4
501i = add i 1
502end
503ret s
504end
505-- control
506fn f to uint
507s = uint 0
508for i uint 4
509s = add s i
510end
511ret s
512end
513== is not part of Omegese
514fn f a uint b uint to uint
515x = add a, b
516ret x
517end
518-- control
519fn f a uint b uint to uint
520x = add a b
521ret x
522end
523== put needs an own or edit array
524fn f to uint
525a = new uint 4 0
526v = view a
527put v 0 1
528free a
529ret 0
530end
531-- control
532fn f to uint
533a = new uint 4 0
534put a 0 1
535x = get a 0
536free a
537ret x
538end
539== nor an op, a type or a function
540fn f a uint to uint
541x = g a
542ret x
543end
544-- control
545fn g a uint to uint
546ret a
547end
548fn f a uint to uint
549x = g a
550ret x
551end
552== neg needs a signed number
553fn f a uint to uint
554ret neg a
555end
556-- control
557fn f a int to int
558ret neg a
559end
560== g takes uint there
561fn g a uint to uint
562ret a
563end
564fn f b ulong to uint
565x = g b
566ret x
567end
568-- control
569fn g a uint to uint
570ret a
571end
572fn f b ulong to uint
573c = uint b
574x = g c
575ret x
576end
577== cannot call itself
578fn f a uint to uint
579x = f a
580ret x
581end
582-- control
583fn f a uint to uint
584ret a
585end
586== is named twice
587fn f a uint a ulong to ulong
588ret a
589end
590-- control
591fn f a uint b ulong to ulong
592ret b
593end
594== is written but never read
595fn f a uint to uint
596x = add a 1
597rte x
598ret x
599end
600-- control
601fn f a uint to uint
602x = add a 1
603ret x
604end
605== is already a name here
606fn f to uint
607i = uint 0
608for i uint 10
609end
610ret i
611end
612-- control
613fn f to uint
614i = uint 0
615for k uint 10
616i = add i k
617end
618ret i
619end
620== is a word of the language, not a name
621fn f bound uint to uint
622ret bound
623end
624-- control
625fn f n uint to uint
626ret n
627end
628== ( ... ) must hold exactly one value; 'b' is extra
629fn f a uint b uint to uint
630x = add (a b) a
631ret x
632end
633-- control
634fn f a uint b uint to uint
635x = add (a) b
636ret x
637end
638== ')' has no matching '('
639fn f a uint b uint to uint
640x = add a b)
641ret x
642end
643-- control
644fn f a uint b uint to uint
645x = (add a b)
646ret x
647end
648== '(' has no matching ')'
649fn f a uint b uint to uint
650x = (add a b
651ret x
652end
653-- control
654fn f a uint b uint to uint
655x = (add a (b))
656ret x
657end
658== ( ) holds no value
659fn f a uint b uint to uint
660x = add () a
661ret x
662end
663-- control
664fn f a uint b uint to uint
665x = add (b) a
666ret x
667end
668== line 2: add has 3 values after it; it takes 2
669fn f a uint b uint to uint
670x = add a b a
671ret x
672end
673-- control
674fn f a uint b uint to uint
675x = add add a b a
676ret x
677end
678== line 2: sub has 1 value after it; it takes 2
679fn f a uint b uint to uint
680x = sub mul a b
681ret x
682end
683-- control
684fn f a uint b uint to uint
685x = sub mul a b a
686ret x
687end
688== line 2: le has 3 values after it; it takes 2
689fn f a uint b uint to uint
690while le add a 1 b a bound 8
691a = add a 1
692end
693ret a
694end
695-- control
696fn f a uint b uint to uint
697while le add a 1 b bound 8
698a = add a 1
699end
700ret a
701end
702== line 2: extra words at the end of the line: 'b'
703fn f a uint b uint to uint
704ret a b
705end
706-- control
707fn f a uint b uint to uint
708ret a
709end
710== mul 2 3: two numbers
711fn f a uint to uint
712x = add a mul 2 3
713ret x
714end
715-- control
716fn f a uint to uint
717x = add a mul a 3
718ret x
719end
720== add needs two values of one type: 'a' is uint, '(ulong b)' is ulong
721fn f a uint b uint to uint
722x = add a (ulong b)
723ret x
724end
725-- control
726fn f a uint b uint to uint
727x = add a (uint b)
728ret x
729end
730== g has 1 value after it; it takes 2
731fn g a uint b uint to uint
732ret add a b
733end
734fn f a uint to uint
735ret g a
736end
737-- control
738fn g a uint b uint to uint
739ret add a b
740end
741fn f a uint to uint
742ret g a mul a 2
743end
744== the path already ended (ret, trap or break)
745fn f a uint to uint
746trap 1
747x = add a 1
748ret x
749end
750-- control
751fn f a uint to uint
752if gt a 9
753trap 1
754end
755x = add a 1
756ret x
757end
758== new makes an array for a name
759fn total v view uint 4 to uint
760ret get v 0
761end
762fn f a uint to uint
763x = total new uint 4 a
764ret x
765end
766-- control
767fn total v view uint 4 to uint
768ret get v 0
769end
770fn f a uint to uint
771w = new uint 4 a
772x = total w
773free w
774ret x
775end
776== still holds its array at this return on some path
777fn f a own uint 4 to uint
778ret 1
779end
780-- control
781fn f a own uint 4 to uint
782free a
783ret 1
784end
785== still holds its array at this return on some path
786fn f n uint to void
787x = new uint 4 n
788put x 0 n
789end
790-- control
791fn f n uint to void
792x = new uint 4 n
793put x 0 n
794free x
795end
796== was already freed or moved on every path
797fn f a own uint 4 to void
798free a
799free a
800end
801-- control
802fn f a own uint 4 n uint to void
803while gt n 0
804free a
805a = new uint 4 n
806n = sub n 1
807end
808free a
809end
810== was freed or moved on every path to this line
811fn g b own uint 4 to void
812free b
813end
814fn f a own uint 4 to uint
815g own a
816x = get a 0
817ret x
818end
819-- control
820fn g b own uint 4 to void
821free b
822end
823fn f a own uint 4 to uint
824x = get a 0
825g own a
826ret x
827end
828== still holds an array on some path to this line
829fn f n uint to uint
830x = new uint 4 n
831x = new uint 4 0
832y = get x 0
833free x
834ret y
835end
836-- control
837fn f n uint to uint
838x = new uint 4 n
839free x
840x = new uint 4 0
841y = get x 0
842free x
843ret y
844end
845== still holds its array at this return on some path
846fn f n uint to uint
847x = new uint 4 n
848if gt n 3
849ret 0
850end
851free x
852ret 1
853end
854-- control
855fn f n uint to uint
856x = new uint 4 n
857if gt n 3
858free x
859end
860free x
861ret 1
862end
863== out of scope here still holds its data on some path to this return
864fn f n uint to uint
865if gt n 3
866y = new uint 4 n
867put y 0 n
868else
869trap 1
870end
871ret 0
872end
873-- control
874fn f n uint to uint
875if gt n 3
876y = new uint 4 n
877put y 0 n
878free y
879end
880ret 0
881end
882== 'a' holds no array at line 3 on every path, once this loop is closed
883fn f a own uint 4 n uint to uint
884free a
885while gt get a 0 n
886n = add n 1
887end
888ret n
889end
890-- control
891fn f a own uint 4 n uint to uint
892free a
893while gt get a 0 n
894free a
895a = new uint 4 n
896n = add n 1
897end
898free a
899ret n
900end
901== 'a' holds no array at line 4 on every path, once this loop is closed
902fn f a own uint 4 n uint to void
903free a
904while gt n 0
905free a
906n = sub n 1
907end
908end
909-- control
910fn f a own uint 4 n uint to void
911free a
912while gt n 0
913free a
914a = new uint 4 n
915n = sub n 1
916end
917free a
918end
919== 'x' still holds an array at line 4 on some path, once this loop is closed
920fn f n uint to void
921x = new uint 4 n
922while gt n 0
923x = new uint 4 n
924n = sub n 1
925end
926free x
927end
928-- control
929fn f n uint to void
930x = new uint 4 n
931while gt n 0
932free x
933x = new uint 4 n
934n = sub n 1
935end
936free x
937end
938== 'x' still holds its array at the return on line 4 on some path, once this loop is closed
939fn f n uint to uint
940x = new uint 4 n
941while gt n 0
942ret n
943end
944free x
945ret 0
946end
947-- control
948fn f n uint to uint
949x = new uint 4 n
950while gt n 0
951free x
952ret n
953end
954free x
955ret 0
956end
957== 'a' holds no array at line 5 on every path, once this loop is closed
958fn f a own uint 4 n uint to void
959free a
960while gt n 0
961while gt n 1
962x = get a 0
963n = sub n x
964end
965n = sub n 1
966end
967end
968-- control
969fn f a own uint 4 n uint to void
970free a
971while gt n 0
972while gt n 1
973x = get a 0
974n = sub n x
975end
976free a
977a = new uint 4 n
978n = sub n 1
979end
980free a
981end
982EOF
983for want in "$T"/r*.want; do
984 k="${want%.want}"
985 w="$(cat "$want")"
986 out="$("$G" emit "$k.bad" "$T/x.json" || true)"
987 case "$out" in "REFUSED omegese: "*"$w"*) r=$((r + 1)) ;; *) bad "refusal '$w': got '$out'" ;; esac
988 if ! "$G" emit "$k.ok" "$T/x.json" > "$T/x.out" || ! "$OME" emit "$T/x.json" "$T/x.oscir" > "$T/x.out"; then
989 bad "control for '$w': $(cat "$T/x.out")"
990 else
991 v="$("$C2" check "$T/x.oscir" 2>&1 | head -1 || true)"
992 case "$v" in checker2=ACCEPT*) ;; *) bad "control for '$w' checker2: $v" ;; esac
993 fi
994done
995echo "refusals: $r/$(ls "$T"/r*.want | wc -l) refused with the expected reason, every control accepted"
996
997# 3. loop guard negative control
998ctr="$(grep -o '"op": "ge", "dst": [0-9]*, "a": [0-9]*' "$T/dev04.json" | head -1 | sed 's/.*"a": //')"
999sed -E "s/\{\"op\": \"add\", \"dst\": $ctr, \"a\": $ctr, \"b\": [0-9]+\}, //" "$T/dev04.json" > "$T/m.json"
1000if cmp -s "$T/dev04.json" "$T/m.json"; then bad "loop mutant: the counter increment was not found"; fi
1001if "$OME" emit "$T/m.json" "$T/m.oscir" > "$T/m.out"; then
1002 v="$("$C2" check "$T/m.oscir" 2>&1 | head -1 || true)"
1003 case "$v" in *"rule=C2-LOOP-UNBOUNDED"*) echo "loop guard: mutant without the counter increment REJECTED (C2-LOOP-UNBOUNDED)" ;;
1004 *) bad "loop mutant not rejected: $v" ;; esac
1005else bad "loop mutant refused earlier than checker2: $(cat "$T/m.out")"; fi
1006
1007# 4. tokenizer gate
1008if [ -n "${OMEGESE_TOKCOUNT:-}" ] && [ -n "${OMEGESE_TOKENIZER:-}" ]; then
1009 multi="$("$OMEGESE_TOKCOUNT" "$OMEGESE_TOKENIZER" words $("$G" words) bound | awk '$1 != 1 {print $3}')"
1010 if [ -n "$multi" ]; then bad "words that are not one token: $multi"; else echo "tokenizer gate: every fixed word is one token"; fi
1011else
1012 echo "tokenizer gate: SKIPPED (set OMEGESE_TOKCOUNT and OMEGESE_TOKENIZER)"
1013fi
1014
1015
1016# 5. next is exact (docs/one_layer/OMEGESE.md section 6). `next --machine` prints the legal items, one
1017# per line: word <w>, newname <excluded>..., number <lo> <hi>, (, ), space, newline, eof.
1018# Oracle: at every word boundary of every accepted program (dev, nested, trial-2, coverage, refusal
1019# controls) the word that follows is legal, and eof is legal at the end. Truncation fuzz: every
1020# byte prefix of the nested, trial-2 and coverage programs goes through emit and next and ends with
1021# exit 0 or 1 (no crash; the suite also runs on an ASan/UBSan build); for accepted programs next
1022# must also allow the byte that follows, and complete must finish the prefix into an accepted program.
1023FIXEDW=" $("$G" words | tr '\n' ' ')u8 u16 u32 u64 i8 i16 i32 i64 "
1024dec_le() { # <a> <b>: a <= b, for canonical decimal integers of any size
1025 local a="$1" b="$2" x y
1026 if [[ $a == -* ]]; then
1027 [[ $b == -* ]] || return 0
1028 x="${a#-}" y="${b#-}"
1029 [ ${#x} -ne ${#y} ] && { [ ${#x} -gt ${#y} ]; return; }
1030 [[ ! $x < $y ]]; return
1031 fi
1032 [[ $b == -* ]] && return 1
1033 [ ${#a} -ne ${#b} ] && { [ ${#a} -lt ${#b} ]; return; }
1034 [[ ! $a > $b ]]
1035}
1036mallowed() { # <items> <word>: do the items allow the whole word (NL a newline, EOF the end of input)
1037 local items="$1" w="$2" k lo hi rest
1038 local it=$'\n'"$items"$'\n'
1039 case "$items" in REFUSED*|"") return 1 ;; esac
1040 case "$w" in
1041 NL) [[ $it == *$'\nnewline\n'* ]]; return ;;
1042 EOF) [[ $it == *$'\neof\n'* ]]; return ;;
1043 "("|")") [[ $it == *$'\n'"$w"$'\n'* ]]; return ;;
1044 esac
1045 [[ $it == *$'\n'"word $w"$'\n'* ]] && return 0
1046 if [[ $w =~ ^(0|-?[1-9][0-9]*)$ ]]; then
1047 while read -r k lo hi; do [ "$k" = number ] && dec_le "$lo" "$w" && dec_le "$w" "$hi" && return 0; done <<< "$items"
1048 return 1
1049 fi
1050 [[ $w =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] && [ ${#w} -le 63 ] && [[ $FIXEDW != *" $w "* ]] || return 1
1051 while read -r k rest; do
1052 [ "$k" = newname ] || continue
1053 [[ " $rest " == *" $w "* ]] && return 1
1054 return 0
1055 done <<< "$items"
1056 return 1
1057}
1058toks() { # <text>: TO[] the byte offset and TW[] the text of every word (NL for a newline)
1059 local s="$1" i=0 n=${#1} j c
1060 TO=() TW=()
1061 while ((i < n)); do
1062 c="${s:i:1}"
1063 case "$c" in
1064 " "|$'\t'|$'\r') i=$((i + 1)) ;;
1065 $'\n') TO+=("$i"); TW+=(NL); i=$((i + 1)) ;;
1066 "("|")") TO+=("$i"); TW+=("$c"); i=$((i + 1)) ;;
1067 *) j=$i
1068 while ((j < n)); do case "${s:j:1}" in " "|$'\t'|$'\r'|$'\n'|"("|")") break ;; esac; j=$((j + 1)); done
1069 TO+=("$i"); TW+=("${s:i:j-i}"); i=$j ;;
1070 esac
1071 done
1072}
1073mnext() { printf '%s' "$1" > "$T/p"; "$G" next --machine "$T/p" 2>&1 || true; }
1074NB=0
1075walk_in() { # <text>: every word is legal at its position and eof at the end; else WHERE says where not
1076 local s="$1" k items
1077 toks "$s"
1078 for ((k = 0; k < ${#TW[@]}; k++)); do
1079 items="$(mnext "${s:0:TO[k]}")"
1080 NB=$((NB + 1))
1081 mallowed "$items" "${TW[k]}" || { WHERE="'${TW[k]}' at byte ${TO[k]}: $(head -3 <<< "$items" | tr '\n' '|')"; return 1; }
1082 done
1083 items="$(mnext "$s")"
1084 mallowed "$items" EOF || { WHERE="the end of input: $(head -3 <<< "$items" | tr '\n' '|')"; return 1; }
1085}
1086progs=("$ROOT"/tests/one_layer/omegese/dev/*.omegese "$ROOT"/tests/one_layer/omegese/nested/*.omegese "$T"/c*.omegese "$T"/r*.ok)
1087for f in "$ROOT"/tests/one_layer/omegese/trial2/*.omegese; do [ "$(basename "$f")" = dev07.omegese ] || progs+=("$f"); done
1088for f in "${progs[@]}"; do
1089 s="$(cat "$f")"$'\n'
1090 walk_in "$s" || bad "next blocks $WHERE of $(basename "$f")"
1091done
1092echo "next oracle: every word and the end of input legal at all $NB word boundaries of ${#progs[@]} accepted programs"
1093
1094# 6. completeness and soundness against emit: single-word mutants of every accepted program (a word
1095# deleted, doubled or replaced by a fixed word, a name of the program, a number or a fresh name).
1096# emit accepts a mutant iff every word of it was legal at its position and eof at its end.
1097RANDOM=445
1098NUMS=(0 1 -1 7 14 15 64 65 127 128 255 256 -128 -129 1000000 1000001 4294967295 4294967296 -0 007)
1099read -ra FIXA <<< "$FIXEDW"
1100nmut=0 nacc=0 NB=0
1101for f in "${progs[@]}"; do
1102 s="$(cat "$f")"$'\n'
1103 toks "$s"
1104 ow=("${TW[@]}")
1105 ids=()
1106 for w in "${ow[@]}"; do [[ $w =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] && [[ $FIXEDW != *" $w "* ]] && ids+=("$w"); done
1107 for ((m = 0; m < ${OMEGESE_MUTANTS:-2}; m++)); do
1108 n=${#ow[@]} k=$((RANDOM % ${#ow[@]})) how=$((RANDOM % 6))
1109 case $how in
1110 0) rep=() ;;
1111 1) rep=("${ow[k]}" "${ow[k]}") ;;
1112 2) rep=("${NUMS[RANDOM % ${#NUMS[@]}]}") ;;
1113 3) rep=("${ids[RANDOM % ${#ids[@]}]}") ;;
1114 4) rep=("zq$m") ;;
1115 *) rep=("${FIXA[RANDOM % ${#FIXA[@]}]}") ;;
1116 esac
1117 out="" prev=NL
1118 for ((q = 0; q < n; q++)); do
1119 if ((q == k)); then set -- "${rep[@]}"; else set -- "${ow[q]}"; fi
1120 for w in "$@"; do
1121 if [ "$w" = NL ]; then out+=$'\n'; else [ "$prev" = NL ] || out+=" "; out+="$w"; fi
1122 prev="$w"
1123 done
1124 done
1125 printf '%s' "$out" > "$T/mut.omegese"
1126 acc=0; "$G" emit "$T/mut.omegese" "$T/mut.json" > /dev/null 2>&1 && acc=1
1127 in=0; walk_in "$out" && in=1
1128 nmut=$((nmut + 1)) nacc=$((nacc + acc))
1129 [ "$acc" = "$in" ] || bad "mutant of $(basename "$f") (word $k -> '${rep[*]}'): emit accepts=$acc, every word legal=$in (${WHERE:-})"
1130 done
1131done
1132echo "completeness: $nmut single-word mutants ($nacc accepted by emit): emit accepts iff every word was legal ($NB word boundaries)"
1133
1134# 7. soundness fuzz: from random accepted prefixes, pick random legal items and extend; the legal set is
1135# never empty, a word picked is still legal after any of its first characters, and `omegese
1136# complete` (close everything) finishes the walk into a program emit accepts, in a bounded number of
1137# lines (at most the words of the prefix plus 3).
1138RANDOM=4452
1139nwalk=0 nstep=0
1140# walk_items <steps>: extend P by up to <steps> random legal items (stops at eof); every set must be
1141# nonempty and a word picked must stay legal after each of its first characters. Returns 1 (and counts a
1142# failure) when not.
1143walk_items() {
1144 local st items it x lo hi v q d j c
1145 for ((st = 0; st < $1; st++)); do
1146 items="$(mnext "$P")"
1147 if [ -z "$items" ] || [[ $items == REFUSED* ]]; then bad "soundness: no legal item after $(printf '%s' "$P" | tail -c 70 | tr '\n' '|') ($items)"; return 1; fi
1148 mapfile -t IT <<< "$items"
1149 it="${IT[RANDOM % ${#IT[@]}]}" x=""
1150 nstep=$((nstep + 1))
1151 case "$it" in
1152 eof) return 0 ;;
1153 newline) P+=$'\n' ;;
1154 space) P+=" " ;;
1155 "("|")") P+="$it" ;;
1156 "word "*) x="${it#word }" ;;
1157 newname*) for ((q = 0; q < 9; q++)); do x="n$RANDOM"; mallowed "$items" "$x" && break; done ;;
1158 number*) read -r _ lo hi <<< "$it"; c=()
1159 for v in "$lo" "$hi" 0 1 -1 9 100; do dec_le "$lo" "$v" && dec_le "$v" "$hi" && c+=("$v"); done
1160 # and a random value inside: by arithmetic when the interval fits, else random digits
1161 if [ ${#lo} -le 17 ] && [ ${#hi} -le 17 ]; then
1162 c+=("$((lo + (RANDOM << 30 | RANDOM << 15 | RANDOM) % (hi - lo + 1)))")
1163 else
1164 for ((q = 0; q < 9; q++)); do
1165 v=$((1 + RANDOM % 9)); for ((d = RANDOM % 20; d > 0; d--)); do v+=$((RANDOM % 10)); done
1166 [[ $lo == -* ]] && ((RANDOM % 2)) && v="-$v"
1167 dec_le "$lo" "$v" && dec_le "$v" "$hi" && { c+=("$v"); break; }
1168 done
1169 fi
1170 x="${c[RANDOM % ${#c[@]}]}" ;;
1171 *) bad "soundness: unknown item '$it'"; return 1 ;;
1172 esac
1173 [ -n "$x" ] || continue
1174 if [ ${#x} -ge 2 ]; then
1175 j=$((1 + RANDOM % (${#x} - 1)))
1176 mallowed "$(mnext "$P${x:0:j}")" "$x" || { bad "soundness: '$x' legal, but not after its first $j characters: $(printf '%s' "$P" | tail -c 50 | tr '\n' '|')"; return 1; }
1177 fi
1178 P+="$x "
1179 done
1180}
1181# close_walk <out file>: `omegese complete` finishes P; the whole text goes to <out file>, which emit must
1182# accept (returns 1 and counts a failure when complete refuses or emit refuses the result)
1183close_walk() {
1184 printf '%s' "$P" > "$T/w.omegese"
1185 if ! "$G" complete "$T/w.omegese" > "$T/w.tail" 2> "$T/w.err"; then bad "soundness: complete refused after a legal walk: $(cat "$T/w.tail" "$T/w.err" | head -2)"; return 1; fi
1186 cat "$T/w.omegese" "$T/w.tail" > "$1"
1187 "$G" emit "$1" "$T/w2.json" > "$T/w2.out" || { bad "soundness: walk + completion refused: $(cat "$T/w2.out")"; return 1; }
1188}
1189for ((wk = 0; wk < ${OMEGESE_WALKS:-40}; wk++)); do
1190 f="${progs[RANDOM % ${#progs[@]}]}"
1191 s="$(cat "$f")"$'\n'
1192 toks "$s"
1193 P="${s:0:TO[RANDOM % ${#TO[@]}]}"
1194 case "${P: -1}" in ""|" "|$'\n'|"("|")") ;; *) P+=" " ;; esac # start at a word boundary
1195 walk_items "${OMEGESE_STEPS:-25}" || continue
1196 close_walk "$T/w2.omegese" || continue
1197 toks "$P"
1198 lines=$(grep -c '' "$T/w.tail" || true)
1199 [ "$lines" -le $((${#TW[@]} + 3)) ] || bad "soundness: completion of $lines lines for a prefix of ${#TW[@]} words"
1200 nwalk=$((nwalk + 1))
1201done
1202echo "soundness fuzz: $nwalk/${OMEGESE_WALKS:-40} random walks ($nstep legal items picked) never met an empty set and closed into accepted programs"
1203# 8. checker2 residual (OMEGESE.md 6.4): `omegese next` is exact relative to emit, not to omega_emit and
1204# checker2. Random walks from the empty prefix (whole programs) and from cut points of the suite's
1205# programs, each closed by `omegese complete` (so emit accepts it, checked), then omega_emit and
1206# checker2: every rejection after emit is counted by stage and reason. The suite's own programs are
1207# the zero line (all accepted, sections 1 and 1a). OMEGESE_RESIDUAL_WALKS sets the number of walks
1208# of each kind; OMEGESE_RESIDUAL_MAX the rejections allowed (default 0: none is known after the
1209# rules folded into the oracle; a measured rate is printed either way).
1210declare -A RES=()
1211chain_after_emit() { # <program> -> "" when omega_emit and checker2 accept, else "<stage> <reason>"
1212 "$G" emit "$1" "$T/r.json" > "$T/r.out" 2>&1 || { echo "emit $(head -c 80 "$T/r.out")"; return; }
1213 if ! "$OME" emit "$T/r.json" "$T/r.oscir" > "$T/r.out" 2>&1; then
1214 echo "omega_emit $(head -n 1 "$T/r.out" | sed -E "s/'[^']*'/X/g; s/[0-9]+/N/g" | cut -c1-70)"; return
1215 fi
1216 local v; v="$("$C2" check "$T/r.oscir" 2>&1 | head -n 1 || true)"
1217 case "$v" in checker2=ACCEPT*) ;; *) echo "checker2 $(grep -oE 'C2-[A-Z0-9-]+' <<< "$v" | head -n 1)" ;; esac
1218}
1219nres=0 nrej=0
1220RANDOM=4453
1221for ((wk = 0; wk < ${OMEGESE_RESIDUAL_WALKS:-40}; wk++)); do
1222 for start in empty cut; do
1223 if [ "$start" = empty ]; then P=""
1224 else
1225 f="${progs[RANDOM % ${#progs[@]}]}"; s="$(cat "$f")"$'\n'; toks "$s"
1226 P="${s:0:TO[RANDOM % ${#TO[@]}]}"
1227 case "${P: -1}" in ""|" "|$'\n'|"("|")") ;; *) P+=" " ;; esac
1228 fi
1229 walk_items "${OMEGESE_STEPS:-25}" || continue
1230 close_walk "$T/res.omegese" || continue
1231 nres=$((nres + 1))
1232 why="$(chain_after_emit "$T/res.omegese")"
1233 [ -n "$why" ] || continue
1234 nrej=$((nrej + 1)); RES[$why]=$((${RES[$why]:-0} + 1))
1235 [ -n "${OMEGESE_RESIDUAL_KEEP:-}" ] && cp "$T/res.omegese" "$OMEGESE_RESIDUAL_KEEP/rej-$nrej.omegese"
1236 done
1237done
1238for k in "${!RES[@]}"; do echo " residual: ${RES[$k]} x $k"; done | LC_ALL=C sort
1239echo "checker2 residual: $nrej of $nres completed walks that emit accepts are rejected later (omega_emit or checker2)"
1240[ "$nres" -gt 0 ] || bad "residual: no completed walk"
1241[ "$nrej" -le "${OMEGESE_RESIDUAL_MAX:-0}" ] || bad "residual: $nrej walks pass emit and are rejected later (allowed ${OMEGESE_RESIDUAL_MAX:-0})"
1242nf=0 nfs=0
1243WC=$'[^ \t\r\n()]'
1244for f in "$ROOT"/tests/one_layer/omegese/nested/*.omegese "$ROOT"/tests/one_layer/omegese/trial2/*.omegese "$T"/c*.omegese; do
1245 s="$(cat "$f")"$'\n'
1246 # an accepted program: every byte prefix must also be open (next exit 0), allow the byte that
1247 # follows (the whole word it belongs to, a space, a newline, a bracket or the end), and be finished
1248 # by `omegese complete` into a program emit accepts. A refused one (trial2/dev07): no crash only.
1249 full=0; printf '%s' "$s" > "$T/p"; "$G" emit "$T/p" "$T/p.json" > /dev/null 2>&1 && full=1
1250 for ((i = 0; i <= ${#s}; i++)); do
1251 printf '%s' "${s:0:i}" > "$T/p"
1252 rc=0; "$G" emit "$T/p" "$T/p.json" > /dev/null 2>&1 || rc=$?
1253 [ "$rc" -le 1 ] || { bad "emit exit $rc on byte prefix $i of $(basename "$f")"; break; }
1254 rc=0; "$G" next "$T/p" > /dev/null 2>&1 || rc=$?
1255 [ "$rc" -le 1 ] || { bad "next exit $rc on byte prefix $i of $(basename "$f")"; break; }
1256 rc=0; items="$("$G" next --machine "$T/p" 2>&1)" || rc=$?
1257 [ "$rc" -le 1 ] || { bad "next --machine exit $rc on byte prefix $i of $(basename "$f")"; break; }
1258 nf=$((nf + 1))
1259 [ "$full" = 1 ] || continue
1260 [ "$rc" -eq 0 ] || { bad "byte prefix $i of $(basename "$f") refused: $items"; break; }
1261 c="${s:i:1}"
1262 case "$c" in
1263 "") w=EOF ;;
1264 " "|$'\t') w=space ;;
1265 $'\n') w=NL ;;
1266 "("|")") w="$c" ;;
1267 *) [[ ${s:0:i} =~ ($WC*)$ ]]; w="${BASH_REMATCH[1]}"; [[ ${s:i} =~ ^($WC*) ]]; w+="${BASH_REMATCH[1]}" ;;
1268 esac
1269 if [ "$w" = space ]; then [[ $'\n'"$items"$'\n' == *$'\nspace\n'* ]]
1270 else mallowed "$items" "$w"; fi || { bad "byte prefix $i of $(basename "$f"): the next byte ('$w') is not allowed"; break; }
1271 rc=0; "$G" complete "$T/p" > "$T/p.c" 2>&1 || rc=$?
1272 { [ "$rc" -eq 0 ] && cat "$T/p" "$T/p.c" > "$T/pc.omegese" && "$G" emit "$T/pc.omegese" "$T/p.json" > /dev/null 2>&1; } \
1273 || { bad "byte prefix $i of $(basename "$f"): complete does not finish it into an accepted program"; break; }
1274 nfs=$((nfs + 1))
1275 done
1276done
1277echo "truncation fuzz: $nf byte prefixes through emit, next and next --machine, exit 0 or 1; at the $nfs prefixes of accepted programs, next allows the byte that follows and complete finishes into a program emit accepts"
1278if [ "${OMEGESE_SANITIZED:-0}" = 1 ]; then
1279 if [ -s "$T/san.log" ]; then bad "sanitizer reports: $(grep -m3 -E '^==|ERROR|runtime error' "$T/san.log")"
1280 else echo "sanitizers: no ASan or UBSan report in the whole run"; fi
1281fi
1282[ "$fails" -eq 0 ] || { echo "test-omegese: $fails failure(s)"; exit 1; }
1283echo "test-omegese: PASS"