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 / tests / diagnostics / experiment_issue655.sh

590 lines · 19076 bytes Raw Blame History
1#!/bin/bash
2# tests/diagnostics/experiment_issue655.sh
3# Empirical experiment for issue #655: incremental compiler feedback for .om generation.
4# Tests 10 held-out .om programs with known-good versions and deliberately broken drafts.
5# Verifies:
6# 1. Known-good programs yield zero error records (negative control: output is solely "omega-diag v1", exit code 0).
7# 2. Deliberately broken programs yield exact diagnostics locating the first error span and matching the compiler refusal code.
8set -euo pipefail
9
10ROOT=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
11OMC="${OMC:-$ROOT/seed/out/omc}"
12[ -x "$OMC" ] || { echo "FAIL: $OMC not found or not executable" >&2; exit 1; }
13
14PASS_COUNT=0
15TOTAL=54
16
17echo "=== Issue #655 Milestone A Diagnostic Precision Experiment ==="
18printf "%-14s | %-12s | %-8s | %-9s | %-14s | %s\n" "Task" "Good Result" "Bad Exit" "Span" "Span Text" "Diagnostic"
19echo "---------------------------------------------------------------------------------------------------------"
20
21run_case() {
22 local name="$1"
23 local good="$2"
24 local bad="$3"
25 local exp_text="$4"
26 local exp_code="$5"
27
28 # 1. Good program (negative control)
29 local out_good
30 local rc_good=0
31 out_good=$(printf "check\n%s" "$good" | "$OMC" 2>&1) || rc_good=$?
32 if [ "$rc_good" -ne 0 ] || [ "$out_good" != "omega-diag v1" ]; then
33 echo "FAIL negative control on $name: rc=$rc_good out=$out_good" >&2
34 return 1
35 fi
36
37 # 2. Broken draft
38 local out_bad
39 local rc_bad=0
40 out_bad=$(printf "check\n%s" "$bad" | "$OMC" 2>&1) || rc_bad=$?
41 if [ "$rc_bad" -ne "$exp_code" ]; then
42 echo "FAIL bad exit code on $name: expected $exp_code, got $rc_bad" >&2
43 return 1
44 fi
45
46 # Extract diagnostic record (line 2 of output)
47 local diag_line
48 diag_line=$(echo "$out_bad" | grep -E '^om[12] ' || true)
49 if [ -z "$diag_line" ]; then
50 echo "FAIL missing om record on $name: $out_bad" >&2
51 return 1
52 fi
53
54 # Extract span
55 local span
56 span=$(echo "$diag_line" | grep -o 'span=[0-9]*-[0-9]*' | cut -d= -f2)
57 local a b
58 a=$(echo "$span" | cut -d- -f1)
59 b=$(echo "$span" | cut -d- -f2)
60
61 # Extract span text using python
62 local span_text
63 span_text=$(python3 -c "import sys; src = sys.stdin.read(); a, b = int(sys.argv[1]), int(sys.argv[2]); print(repr(src[a:b]))" "$a" "$b" <<< "$bad")
64
65 local exp_repr
66 exp_repr=$(python3 -c "import sys; print(repr(sys.argv[1]))" "$exp_text")
67
68 if [ "$span_text" != "$exp_repr" ]; then
69 echo "FAIL span text mismatch on $name: expected $exp_repr, got $span_text" >&2
70 return 1
71 fi
72 local category
73 case "$exp_code" in
74 3|10|13) category=NAME ;;
75 4|5|6|9|15|16|22) category=SYNTAX ;;
76 7|11|19) category=TYPE ;;
77 8|21) category=RANGE ;;
78 12|14|23|24) category=CAPACITY ;;
79 17|18) category=FLOW ;;
80 27|28|29|30) category=CONTRACT ;;
81 *) echo "FAIL unmapped diagnostic code $exp_code" >&2; return 1 ;;
82 esac
83 if [[ "$diag_line" != "om1 $category "* ]]; then
84 echo "FAIL category on $name: expected $category, got $diag_line" >&2
85 return 1
86 fi
87 # Finality and repairability are independent; these edited drafts are not capacity failures.
88 if [[ "$diag_line" != *" REPAIRABLE code=$exp_code "* ]] || [[ "$diag_line" == *" "* ]]; then
89 echo "FAIL repairability or spacing on $name: $diag_line" >&2
90 return 1
91 fi
92
93 printf "%-14s | %-12s | %-8s | %-9s | %-14s | %s\n" "$name" "PASS (0 err)" "$rc_bad" "$span" "$span_text" "$diag_line"
94 PASS_COUNT=$((PASS_COUNT + 1))
95}
96run_next_case() {
97 local name="$1"
98 local input="$2"
99 local expected="$3"
100
101 local out
102 local rc=0
103 out=$(printf "next\n%s" "$input" | "$OMC" 2>&1) || rc=$?
104 if [ "$rc" -ne 0 ]; then
105 echo "FAIL next exit code on $name: expected 0, got $rc" >&2
106 echo "$out" >&2
107 return 1
108 fi
109
110 if [ "$out" != "$expected" ]; then
111 echo "FAIL next output mismatch on $name:" >&2
112 diff -u <(echo "$expected") <(echo "$out") >&2 || true
113 return 1
114 fi
115
116 printf "%-14s | %-12s | %-8s | %-9s | %-14s | %s\n" "$name" "PASS (query)" "0" "-" "-" "prefix query matches expected"
117 PASS_COUNT=$((PASS_COUNT + 1))
118}
119
120run_next_neg_control() {
121 local name="$1"
122 local input="$2"
123 local expected="$3"
124 local corrupted="$4"
125
126 local out
127 local rc=0
128 out=$(printf "next\n%s" "$input" | "$OMC" 2>&1) || rc=$?
129 if [ "$rc" -ne 0 ]; then
130 echo "FAIL next exit code on $name: expected 0, got $rc" >&2
131 return 1
132 fi
133
134 if [ "$out" != "$expected" ]; then
135 echo "FAIL next output should match expected on $name" >&2
136 return 1
137 fi
138
139 if [ "$out" = "$corrupted" ]; then
140 echo "FAIL negative control on $name: corrupted expectation was not detected" >&2
141 return 1
142 fi
143
144 printf "%-14s | %-12s | %-8s | %-9s | %-14s | %s\n" "$name" "PASS (negctrl)" "0" "-" "-" "detected altered expected line"
145 PASS_COUNT=$((PASS_COUNT + 1))
146}
147
148# 1. clamp_sub
149run_case "clamp_sub" \
150"fn clamp_sub a uint b uint to uint
151if lt a b
152ret 0
153end
154ret sub a b
155end
156" \
157"fn clamp_sub a uint b uint to uint
158if lt a b
159ret true
160end
161ret sub a b
162end
163" \
164"true" 7
165
166# 2. narrow_sum
167run_case "narrow_sum" \
168"fn narrow_sum a short b short to byte
169ret byte add a b
170end
171" \
172"fn narrow_sum a short b short to byte
173ret byte add a
174end
175" \
176$'\n' 4
177
178# 3. power_mod
179run_case "power_mod" \
180"fn power_mod b uint e uint m uint to uint
181r = uint 1
182base = rem b m
183while gt e 0
184if eq rem e 2 1
185r = rem mul r base m
186end
187base = rem mul base base m
188e = div e 2
189end
190ret r
191end
192" \
193"fn power_mod b uint e uint m uint to uint
194r = 1
195base = rem b m
196while gt e 0
197if eq rem e 2 1
198r = rem mul r base m
199end
200base = rem mul base base m
201e = div e 2
202end
203ret r
204end
205" \
206"1" 11
207
208# 4. tri_kind
209run_case "tri_kind" \
210"fn tri_kind a int b int c int to int
211if eq a b
212ret 1
213end
214ret 0
215end
216" \
217"fn tri_kind a int b int c int to int
218if eq a
219ret 1
220end
221ret 0
222end
223" \
224$'\n' 4
225
226# 5. t_conv
227run_case "t_conv" \
228"fn main to int
229x = ulong 3000000000
230ret int x
231end
232" \
233"fn main to int
234x = ulong 3000000000
235ret int
236end
237" \
238$'\n' 4
239
240# 6. sort
241run_case "sort" \
242"fn sorted a view ulong 10 to ulong
243bad = ulong 0
244for i ulong 9
245if gt get a i get a add i 1
246bad = add bad 1
247end
248end
249ret bad
250end
251" \
252"fn sorted a view ulong 10 to ulong
253bad = ulong 0 1
254for i ulong 9
255if gt get a i get a add i 1
256bad = add bad 1
257end
258end
259ret bad
260end
261" \
262"1" 5
263
264# 7. bytecopy
265run_case "bytecopy" \
266"fn copy_one a uchar to uchar
267ret a
268end
269" \
270"fn copy_one a uchar to uchar
271ret 1000
272end
273" \
274"1000" 8
275
276# 8. echo
277run_case "echo" \
278"fn echo_val v long to long
279ret v
280end
281" \
282"fn echo_val v long to long
283break
284ret v
285end
286" \
287"break" 9
288
289# 9. sum_squares
290run_case "sum_squares" \
291"fn sum_sq a int b int to int
292x = mul a a
293y = mul b b
294ret add x y
295end
296" \
297"fn sum_sq a int b int to int
298x = mul a unknown_var
299y = mul b b
300ret add x y
301end
302" \
303"unknown_var" 13
304
305# 10. t_bounds
306run_case "t_bounds" \
307"fn check_bound x int to int
308if lt x 0
309trap 1
310end
311ret x
312end
313" \
314"fn check_bound x int to int
315if lt x 0
316trap 999
317end
318ret x
319end
320" \
321"999" 8
322# 11. next_while_loop (loop constructs break/continue, local variable, signature)
323run_next_case "next_while" \
324"fn main to int
325a = ulong 3
326while lt a 9
327" \
328"omega-query v1
329scope: function
330constructs: ret if while for trap exit put end note break continue
331names:
332a ulong local
333functions:
334fn main to int"
335
336# 12. next_params (bool param vs int local, non-loop constructs, return void bool)
337run_next_case "next_params" \
338"fn compute c bool to void
339val = int 42
340" \
341"omega-query v1
342scope: function
343constructs: ret if while for trap exit put end note
344names:
345c bool param
346val int local
347functions:
348fn compute to void bool"
349
350# 13. next_sig_multi (multiple typed params and locals, multi-arg signature)
351run_next_case "next_sig_multi" \
352"fn add_scaled x uint y uint to uint
353s = uint 2
354" \
355"omega-query v1
356scope: function
357constructs: ret if while for trap exit put end note
358names:
359x uint param
360y uint param
361s uint local
362functions:
363fn add_scaled to uint uint uint"
364
365# 14. next_toplevel (toplevel scope, global and const declarations)
366run_next_case "next_toplevel" \
367"global g_buf uchar 16
368const MAX_LEN ulong 1024
369" \
370"omega-query v1
371scope: toplevel
372constructs: fn const global text struct note
373names:
374g_buf uchar global
375MAX_LEN ulong const
376functions:"
377
378# 15. next_for_loop (for loop allows break and continue, loop index is local)
379run_next_case "next_for_loop" \
380"fn loop_test to int
381sum = int 0
382for i ulong 10
383" \
384"omega-query v1
385scope: function
386constructs: ret if while for trap exit put end note break continue
387names:
388sum int local
389i ulong local
390functions:
391fn loop_test to int"
392
393# 16. next_om2_func (om2 profile, completed preceding function and active function)
394run_next_case "next_om2_func" \
395"om2
396fn helper x ulong to ulong
397ret x
398end
399fn caller a ulong to ulong
400b = ulong 1
401while lt b 10
402" \
403"omega-query v1
404scope: function
405constructs: ret if while for trap exit put end note break continue
406names:
407a ulong param
408b ulong local
409functions:
410fn helper to ulong ulong
411fn caller to ulong ulong"
412
413# 17. next_om2_view (om2 view parameter kind)
414run_next_case "next_om2_view" \
415"om2
416fn process buf view uchar 32 to void
417n = ulong 0
418" \
419"omega-query v1
420scope: function
421constructs: ret if while for trap exit put end note
422names:
423buf uchar view
424n ulong local
425functions:
426fn process to void uchar"
427
428# 18. next_neg_control (negative control: fails if expected output has wrong kind)
429run_next_neg_control "next_neg_ctrl" \
430"fn main to int
431a = ulong 3
432while lt a 9
433" \
434"omega-query v1
435scope: function
436constructs: ret if while for trap exit put end note break continue
437names:
438a ulong local
439functions:
440fn main to int" \
441"omega-query v1
442scope: function
443constructs: ret if while for trap exit put end note break continue
444names:
445a ulong param
446functions:
447fn main to int"
448
449# qty must be preserved in names and function signatures, not reported as view.
450run_next_case "next_qty" \
451$'fn grow n qty to qty\nv = qty 1\n' \
452$'omega-query v1\nscope: function\nconstructs: ret if while for trap exit put end note\nnames:\nn qty param\nv qty local\nfunctions:\nfn grow to qty qty'
453
454run_check_record() {
455 local name="$1" input="$2" code="$3" expected="$4" mode="${5:-check}" out rc=0
456 out=$(printf "%s\n%s" "$mode" "$input" | "$OMC" 2>/dev/null) || rc=$?
457 if [ "$rc" -ne "$code" ] || [ "$out" != "$expected" ]; then
458 echo "FAIL check record on $name: rc=$rc out=$out" >&2
459 return 1
460 fi
461 printf '%s | PASS (exact check record)\n' "$name"
462 PASS_COUNT=$((PASS_COUNT + 1))
463}
464
465run_check_record "check_final" $'fn f to int\nret true\nend\n' 7 \
466$'omega-diag v1\nom1 TYPE span=16-20 FINAL REPAIRABLE code=7 line=2 expect=value found=true'
467run_check_record "check_unknown" $'fn f to int\nret missing\nend\n' 3 \
468$'omega-diag v1\nom1 NAME span=16-23 UNKNOWN REPAIRABLE code=3 line=2 expect=name found=missing'
469run_check_record "check_incomplete" $'fn f to int\n' 15 \
470$'omega-diag v1\nom1 SYNTAX span=12-12 INCOMPLETE REPAIRABLE code=15 line=2 expect=end found=eof'
471
472# Register exhaustion is still UNREPAIRABLE, regardless of finality.
473capacity=$'fn f to int\n'
474for i in {1..17}; do capacity+="v$i = int 0"$'\n'; done
475capacity+=$'ret 0\nend\n'
476run_check_record "check_capacity" "$capacity" 12 \
477$'omega-diag v1\nom1 CAPACITY span=201-204 FINAL UNREPAIRABLE code=12 line=18 expect=word found=int'
478
479# DIAG-1: exact records from the category audit, with corrected wire categories.
480run_check_record "check_range" $'fn f to uchar\nret 1000\nend\n' 8 \
481$'omega-diag v1\nom1 RANGE span=18-22 FINAL REPAIRABLE code=8 line=2 expect=number found=1000'
482run_check_record "check_flow" $'fn f to int\nend\n' 18 \
483$'omega-diag v1\nom1 FLOW span=15-16 FINAL REPAIRABLE code=18 line=3 expect=newline found=0a'
484run_check_record "check_size" $'global g uchar 16777217\nfn main to int\nret 0\nend\n' 23 \
485$'omega-diag v1\nom1 CAPACITY span=15-23 FINAL UNREPAIRABLE code=23 line=1 expect=type found=16777217'
486run_check_record "check_dead" $'fn f to int\nret 0\nret 1\nend\n' 17 \
487$'omega-diag v1\nom1 FLOW span=18-21 FINAL REPAIRABLE code=17 line=3 expect=word found=ret'
488
489# Cover the remaining readily constructed category mappings, independently of spans.
490run_category_case() {
491 local name="$1" code="$2" category="$3" input="$4" out rc=0
492 out=$(printf 'check\n%s' "$input" | "$OMC" 2>/dev/null) || rc=$?
493 if [ "$rc" -ne "$code" ] ||
494 [[ "$out" != $'omega-diag v1\nom1 '"$category "* ]] ||
495 [[ "$out" != *" code=$code "* ]] ||
496 [ "$(printf '%s\n' "$out" | wc -l)" -ne 2 ]; then
497 echo "FAIL category on $name: rc=$rc out=$out" >&2
498 return 1
499 fi
500 PASS_COUNT=$((PASS_COUNT + 1))
501}
502run_category_case "category_number" 6 SYNTAX $'fn f to int\nx = 12a\nret 0\nend\n'
503run_category_case "category_duplicate" 10 NAME $'fn f x int x int to int\nret 0\nend\n'
504run_category_case "category_syntax" 16 SYNTAX $'wat\n'
505run_category_case "category_alias" 19 TYPE $'global g uchar 4\nfn f a edit uchar 4 to void\nend\nfn main to int\nf g\nret 0\nend\n'
506run_category_case "category_index" 21 RANGE $'fn main to int\na = new uint 4 0\nput a 4 1\nret 0\nend\n'
507run_category_case "category_text" 22 SYNTAX $'text t "a\\q"\nfn main to int\nret 0\nend\n'
508table_capacity=''
509for ((i=1; i<=8193; i++)); do table_capacity+="global g$i uchar 1"$'\n'; done
510table_capacity+=$'fn main to int\nret 0\nend\n'
511run_category_case "category_table" 14 CAPACITY "$table_capacity"
512
513# DIAG-2: compare every byte and record, including the final newline. The small
514# control uses the same legal function names without crossing the old msg pool.
515scratch=$(mktemp -d)
516trap 'rm -rf "$scratch"' EXIT
517for count in 10 1200; do
518 printf 'next\n' > "$scratch/query.om"
519 printf 'omega-query v1\nscope: toplevel\nconstructs: fn const global text struct note\nnames:\nfunctions:\n' > "$scratch/query.want"
520 for ((i=0; i<count; i++)); do
521 name="function_${i}_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
522 printf 'fn %s to int\nret 0\nend\n' "$name" >> "$scratch/query.om"
523 printf 'fn %s to int\n' "$name" >> "$scratch/query.want"
524 done
525 rc=0
526 "$OMC" < "$scratch/query.om" > "$scratch/query.out" 2> "$scratch/query.err" || rc=$?
527 if [ "$rc" -ne 0 ] || [ -s "$scratch/query.err" ] ||
528 ! cmp -s "$scratch/query.want" "$scratch/query.out" ||
529 [ "$(grep -c '^fn ' "$scratch/query.out")" -ne "$count" ]; then
530 echo "FAIL next complete records count=$count rc=$rc" >&2
531 diff -u "$scratch/query.want" "$scratch/query.out" >&2 || true
532 exit 1
533 fi
534 PASS_COUNT=$((PASS_COUNT + 1))
535done
536
537# DIAG-3: each failed-output probe has an identical successful-output control.
538# Ordinary compilation is the independent pre-existing error-2 control.
539[ -c /dev/full ] || { echo "FAIL: /dev/full unavailable" >&2; exit 1; }
540for mode in check next normal; do
541 {
542 [ "$mode" = normal ] || printf '%s\n' "$mode"
543 printf 'fn main to int\nret 0\nend\n'
544 } > "$scratch/io.om"
545 rc=0
546 "$OMC" < "$scratch/io.om" > "$scratch/io.out" 2> "$scratch/io.err" || rc=$?
547 if [ "$rc" -ne 0 ] || [ ! -s "$scratch/io.out" ] || [ -s "$scratch/io.err" ]; then
548 echo "FAIL $mode successful-output control: rc=$rc" >&2
549 exit 1
550 fi
551 if [ "$mode" = check ]; then
552 printf 'omega-diag v1\n' > "$scratch/io.want"
553 cmp -s "$scratch/io.want" "$scratch/io.out" || { echo "FAIL check output control" >&2; exit 1; }
554 elif [ "$mode" = next ]; then
555 printf 'omega-query v1\nscope: toplevel\nconstructs: fn const global text struct note\nnames:\nfunctions:\nfn main to int\n' > "$scratch/io.want"
556 cmp -s "$scratch/io.want" "$scratch/io.out" || { echo "FAIL next output control" >&2; exit 1; }
557 fi
558 rc=0
559 "$OMC" < "$scratch/io.om" > /dev/full 2> "$scratch/io.err" || rc=$?
560 if [ "$rc" -ne 2 ]; then
561 echo "FAIL $mode /dev/full: expected compiler I/O error 2, got $rc" >&2
562 exit 1
563 fi
564 PASS_COUNT=$((PASS_COUNT + 1))
565done
566
567# Remaining audit prefixes preserve profile routing and finality boundaries.
568run_next_case "audit_next_after_ret" $'fn f to int\nret 0\n' $'omega-query v1\nscope: function\nconstructs: ret if while for trap exit put end note\nnames:\nfunctions:\nfn f to int'
569run_next_case "audit_next_after_break" $'fn f to int\nwhile true\nbreak\n' $'omega-query v1\nscope: function\nconstructs: ret if while for trap exit put end note break continue\nnames:\nfunctions:\nfn f to int'
570run_next_case "audit_next_after_global" $'global g uchar 1\n' $'omega-query v1\nscope: toplevel\nconstructs: fn const global text struct note\nnames:\ng uchar global\nfunctions:'
571run_next_case "audit_next_function_complete" $'fn f to int\nret 0\nend\n' $'omega-query v1\nscope: toplevel\nconstructs: fn const global text struct note\nnames:\nfunctions:\nfn f to int'
572run_check_record "audit_next_in_struct" $'struct S a ulong\n' 4 $'omega-diag v1\nom1 SYNTAX span=16-17 FINAL REPAIRABLE code=4 line=2 expect=type found=0a' next
573run_check_record "audit_check_prefix_note" $'note comment\nfn f to int\nret true\nend\n' 7 $'omega-diag v1\nom1 TYPE span=29-33 FINAL REPAIRABLE code=7 line=3 expect=value found=true'
574run_check_record "audit_check_ir_bad" $'ir\nfn f to int\nret true\nend\n' 16 $'omega-diag v1\nom1 SYNTAX span=0-2 FINAL REPAIRABLE code=16 line=1 expect=word found=ir'
575run_check_record "audit_check_ir" $'ir\nfn f to int\nret 0\nend\n' 16 $'omega-diag v1\nom1 SYNTAX span=0-2 FINAL REPAIRABLE code=16 line=1 expect=word found=ir'
576run_check_record "audit_check_om2" $'om2\nfn f to int\nret true\nend\n' 7 $'omega-diag v1\nom2 TYPE span=20-24 FINAL REPAIRABLE code=7 line=3 expect=value found=true'
577run_check_record "audit_check_om2fmt" $'om2 fmt\nfn f to int\nret true\nend\n' 16 $'omega-diag v1\nom2 SYNTAX span=0-0 FINAL REPAIRABLE code=16 line=1 expect=word found=-'
578run_check_record "audit_check_om2diag" $'om2 diag\nfn f to int\nret true\nend\n' 7 $'omega-diag v1\nom2 TYPE span=25-29 FINAL REPAIRABLE code=7 line=3 expect=value found=true'
579run_check_record "audit_finality_unknown" $'fn f to int\nret nosuch\nend\n' 3 $'omega-diag v1\nom1 NAME span=16-22 UNKNOWN REPAIRABLE code=3 line=2 expect=name found=nosuch'
580run_check_record "audit_finality_bad_line" $'fn f to int\nret add 1\n' 4 $'omega-diag v1\nom1 SYNTAX span=21-22 FINAL REPAIRABLE code=4 line=3 expect=value found=0a'
581run_check_record "audit_finality_end_type" $'fn f to int\nret true' 7 $'omega-diag v1\nom1 TYPE span=16-20 FINAL REPAIRABLE code=7 line=2 expect=value found=true'
582run_check_record "audit_finality_badnum" $'fn f to int\nret 1z' 6 $'omega-diag v1\nom1 SYNTAX span=16-18 FINAL REPAIRABLE code=6 line=2 expect=number found=1z'
583
584[ "$PASS_COUNT" -eq "$TOTAL" ] || {
585 echo "FAIL case count: ran $PASS_COUNT, expected $TOTAL" >&2
586 exit 1
587}
588
589echo "---------------------------------------------------------------------------------------------------------"
590echo "ALL $PASS_COUNT/$TOTAL EXPERIMENT CHECKS PASSED (negative control + exact first error span matching)"