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
omega-mv / seed / omc / gate16.sh
| 1 | #!/bin/sh |
| 2 | # gate16.sh -- named integer constants and loop bounds. `const NAME TYPE INTEGER` is a top-level typed |
| 3 | # value, visible to later functions only (define before use). The first table runs programs and compares |
| 4 | # exit status; the second requires a compile refusal with the exact code (including function mode, where |
| 5 | # a global, a text or a host word stays error 9); the third compiles function-mode units (no `fn main`) |
| 6 | # with constants and requires words byte-identical to the same unit written with literals. The tables |
| 7 | # also pin `while ... bound N` and `for ... bound N`: N 1 .. 2^63-1 (a number or an integer constant), |
| 8 | # more passes trap 4 (exit 104), bound 0 and 2^63 are error 8; a for with a value count and no bound |
| 9 | # keeps the 1000000 default. OMC selects the compiler (default seed/out/omc; a relative path is taken |
| 10 | # from the repository root). |
| 11 | set -eu |
| 12 | R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) |
| 13 | cd "$R" |
| 14 | S=${OMC:-seed/out/omc} |
| 15 | [ -x "$S" ] || { printf 'FAIL: %s missing; run sh seed/omega-from-seed.sh first\n' "$S" >&2; exit 1; } |
| 16 | mkdir -p build/seed/r16 |
| 17 | T=$(mktemp -d build/seed/r16/const.XXXXXX) |
| 18 | trap 'rm -rf "$T"' EXIT HUP INT TERM |
| 19 | bad() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } |
| 20 | say() { printf '%s\n' "$*"; } |
| 21 | |
| 22 | n=0 |
| 23 | while IFS='|' read -r name want src; do |
| 24 | [ -n "$name" ] || continue |
| 25 | n=$((n + 1)) |
| 26 | printf '%b\n' "$src" > "$T/p.om" |
| 27 | set +e |
| 28 | "$S" < "$T/p.om" > "$T/p.bin" 2> "$T/p.err" |
| 29 | rc=$? |
| 30 | set -e |
| 31 | [ "$rc" = 0 ] || bad "$name: compile $rc: $(cat "$T/p.err")" |
| 32 | chmod +x "$T/p.bin" |
| 33 | set +e |
| 34 | "$T/p.bin" > /dev/null 2>&1 |
| 35 | got=$? |
| 36 | set -e |
| 37 | [ "$got" = "$want" ] || bad "$name: exit $got, wanted $want" |
| 38 | done <<'RUN' |
| 39 | basic|5|const N uint 5\nfn main to int\nret int N\nend |
| 40 | long_name_ok|7|const loop_count_limit uint 7\nfn main to int\nret int loop_count_limit\nend |
| 41 | negative|253|const M long -3\nfn main to int\nret int M\nend |
| 42 | expression|6|const N uint 4\nfn main to int\nx = add N uint 2\nret int x\nend |
| 43 | call_argument|8|const K int 7\nfn f a int to int\nret add a K\nend\nfn main to int\nret f int 1\nend |
| 44 | bool_flag|3|const B bool 1\nfn main to int\nif B\nret 3\nend\nret 4\nend |
| 45 | ulong_max|255|const U ulong 18446744073709551615\nfn main to int\nret int rem U ulong 256\nend |
| 46 | long_min|0|const L long -9223372036854775808\nfn main to int\nret int rem L long 256\nend |
| 47 | between_fns|9|fn f to int\nret 1\nend\nconst N int 9\nfn main to int\nret N\nend |
| 48 | length_local|7|const N uint 4\nfn main to int\na = new uint N 0\nput a 3 7\nret int get a 3\nend |
| 49 | length_global|4|const N uint 4\nglobal g uchar N\nfn main to int\nret int len g\nend |
| 50 | length_param|4|const N uint 4\nfn f a view uint N to int\nret int len a\nend\nfn main to int\nx = new uint N 0\nret f x\nend |
| 51 | float_bits|154|const H f64 0.1\nfn main to int\nret int rem bitcast ulong H ulong 256\nend |
| 52 | float32_bits|205|const H f32 0.1\nfn main to int\nret int rem bitcast uint H uint 256\nend |
| 53 | float_arith|0|const H f64 0.1\nfn main to int\nx = add H f64 0.2\nif eq x f64 0.3\nret 1\nend\nret 0\nend |
| 54 | struct_length|32|const N uint 4\nstruct S a uchar N end\nfn main to int\np = new S 0\nret int sizeof S\nend |
| 55 | const_bound|2|const B ulong 3\nfn main to int\nx = ulong 0\nwhile lt x ulong 2 bound B\nx = add x 1\nend\nret int x\nend |
| 56 | trap_const|102|const T uint 2\nfn main to int\ntrap T\nend |
| 57 | for_const|4|const K ulong 4\nfn main to int\nx = ulong 0\nfor i ulong K\nx = add x 1\nend\nret int x\nend |
| 58 | const_order|12|const kk ulong 4\nfn a fs edit long kk to void\nput fs 0 1\nend\nconst k2 ulong 3\nfn b fs edit long kk gs edit long k2 to long\nput fs 3 7\nput gs 2 5\nret add get fs 3 get gs 2\nend\nfn main to int\nx = new long kk 0\ny = new long k2 0\na x\nret int b x y\nend |
| 59 | big_bound|54|const B ulong 3000000\nfn main to int\nx = ulong 0\nwhile lt x 2000000 bound B\nx = add x 1\nend\nret int rem x 97\nend |
| 60 | big_bound_trap|104|fn main to int\nx = ulong 0\nwhile lt x 2000000 bound 1500000\nx = add x 1\nend\nret 0\nend |
| 61 | max_bound|5|fn main to int\nx = ulong 0\nwhile lt x 5 bound 9223372036854775807\nx = add x 1\nend\nret int x\nend |
| 62 | for_bound|54|const B ulong 3000000\nfn main to int\nn = ulong 2000000\nx = ulong 0\nfor i ulong n bound B\nx = add x 1\nend\nret int rem x 97\nend |
| 63 | for_num_bound|89|const B ulong 3000000\nfn main to int\nx = ulong 0\nfor i ulong 1500000 bound B\nx = add x 1\nend\nret int rem x 97\nend |
| 64 | for_bound_trap|104|fn main to int\nn = ulong 2000000\nx = ulong 0\nfor i ulong n bound 1500000\nx = add x 1\nend\nret 0\nend |
| 65 | for_num_bound_trap|104|fn main to int\nx = ulong 0\nfor i ulong 5000 bound 4097\nx = add x 1\nend\nret 0\nend |
| 66 | for_bound_exact|3|fn main to int\nn = uint 3\nx = ulong 0\nfor i uint n bound 3\nx = add x 1\nend\nret int x\nend |
| 67 | for_num_bound_exact|7|fn main to int\nx = ulong 0\nfor i ulong 7 bound 7\nx = add x 1\nend\nret int x\nend |
| 68 | for_max_bound|5|fn main to int\nn = ulong 5\nx = ulong 0\nfor i ulong n bound 9223372036854775807\nx = add x 1\nend\nret int x\nend |
| 69 | for_default_trap|104|fn main to int\nn = ulong 2000000\nx = ulong 0\nfor i ulong n\nx = add x 1\nend\nret 0\nend |
| 70 | RUN |
| 71 | [ "$n" = 31 ] || bad "value table ran $n of 31 cases" |
| 72 | |
| 73 | n=0 |
| 74 | while IFS='|' read -r name want src; do |
| 75 | [ -n "$name" ] || continue |
| 76 | n=$((n + 1)) |
| 77 | printf '%b\n' "$src" > "$T/p.om" |
| 78 | set +e |
| 79 | "$S" < "$T/p.om" > /dev/null 2> "$T/p.err" |
| 80 | rc=$? |
| 81 | set -e |
| 82 | [ "$rc" = "$want" ] || bad "$name: exit $rc, wanted $want ($(cat "$T/p.err"))" |
| 83 | grep -q "error $want\$" "$T/p.err" || bad "$name: stderr lacks error $want" |
| 84 | done <<'REF' |
| 85 | assign|7|const N uint 5\nfn main to int\nN = uint 6\nret 0\nend |
| 86 | duplicate|10|const N uint 5\nconst N uint 6\nfn main to int\nret 0\nend |
| 87 | out_of_range|8|const N uchar 300\nfn main to int\nret 0\nend |
| 88 | ir_float_const|9|ir\nconst H f64 0.1\nfn f to f64\nret H\nend |
| 89 | extra_word|5|const N uint 5 6\nfn main to int\nret 0\nend |
| 90 | long_name|13|const abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl uint 5\nfn main to int\nret 0\nend |
| 91 | reserved|16|const while uint 5\nfn main to int\nret 0\nend |
| 92 | missing_value|4|const N uint\nfn main to int\nret 0\nend |
| 93 | inside_fn|16|fn main to int\nconst N uint 5\nret 0\nend |
| 94 | use_before|3|fn f to int\nret int N\nend\nconst N int 9\nfn main to int\nret f\nend |
| 95 | len_zero|8|const N uint 0\nfn main to int\na = new uint N 0\nret 0\nend |
| 96 | len_big|23|const N uint 16777217\nfn main to int\na = new uint N 0\nret 0\nend |
| 97 | len_varname|16|fn main to int\nn = uint 4\na = new uint n 0\nret 0\nend |
| 98 | len_after|16|fn f to int\na = new uint N 0\nret 0\nend\nconst N uint 4\nfn main to int\nret f\nend |
| 99 | float_as_length|16|const H f64 0.1\nfn main to int\na = new uchar H 0\nret 0\nend |
| 100 | float_int_literal|16|const H f64 5\nfn main to int\nret 0\nend |
| 101 | struct_length_big|14|const N uint 65\nstruct S a uchar N end\nfn main to int\nret 0\nend |
| 102 | float_bound|16|const B f64 0.5\nfn main to int\nwhile eq int 1 int 1 bound B\nend\nret 0\nend |
| 103 | trap_range|8|const T uint 0\nfn main to int\ntrap T\nend |
| 104 | fm_global|9|const N ulong 5\nglobal g ulong 4\nfn f a ulong to ulong\nret add a N\nend |
| 105 | fm_text|9|const N ulong 5\ntext t "hi"\nfn f a ulong to ulong\nret add a N\nend |
| 106 | fm_global_only|9|global g ulong 4\nfn f a ulong to ulong\nret a\nend |
| 107 | fm_text_only|9|text t "hi"\nfn f a ulong to ulong\nret a\nend |
| 108 | fm_host_word|9|const N ulong 5\nfn f a ulong to long\nx = getpid\nret x\nend |
| 109 | fm_trap_range|8|const T uint 15\nfn f a ulong to ulong\ntrap T\nend |
| 110 | fm_float_length|16|const H f64 0.1\nfn f a view uchar H to ulong\nret 0\nend |
| 111 | fm_assign|7|const N ulong 5\nfn f a ulong to ulong\nN = ulong 6\nret a\nend |
| 112 | bound_zero|8|fn main to int\nwhile eq int 1 int 1 bound 0\nend\nret 0\nend |
| 113 | bound_over|8|fn main to int\nwhile eq int 1 int 1 bound 9223372036854775808\nend\nret 0\nend |
| 114 | for_bound_zero|8|fn main to int\nn = ulong 2\nfor i ulong n bound 0\nend\nret 0\nend |
| 115 | for_num_bound_zero|8|fn main to int\nfor i ulong 2 bound 0\nend\nret 0\nend |
| 116 | for_bound_over|8|fn main to int\nn = ulong 2\nfor i ulong n bound 9223372036854775808\nend\nret 0\nend |
| 117 | for_num_bound_over|8|fn main to int\nfor i ulong 2 bound 9223372036854775808\nend\nret 0\nend |
| 118 | for_bound_missing|4|fn main to int\nn = ulong 2\nfor i ulong n bound\nend\nret 0\nend |
| 119 | for_bound_local|16|fn main to int\nn = ulong 2\nfor i ulong n bound n\nend\nret 0\nend |
| 120 | REF |
| 121 | [ "$n" = 35 ] || bad "refusal table ran $n of 35 cases" |
| 122 | |
| 123 | # Function mode (no `fn main`): an integer or float constant folds to its literal, so the bare function |
| 124 | # words must be byte-identical to the same unit written with literals. Each line: name|const unit|literal unit. |
| 125 | n=0 |
| 126 | while IFS='|' read -r name csrc lsrc; do |
| 127 | [ -n "$name" ] || continue |
| 128 | n=$((n + 1)) |
| 129 | printf '%b\n' "$csrc" > "$T/c.om" |
| 130 | printf '%b\n' "$lsrc" > "$T/l.om" |
| 131 | set +e |
| 132 | "$S" < "$T/c.om" > "$T/c.bin" 2> "$T/c.err" |
| 133 | rc=$? |
| 134 | "$S" < "$T/l.om" > "$T/l.bin" 2> "$T/l.err" |
| 135 | lrc=$? |
| 136 | set -e |
| 137 | [ "$rc" = 0 ] || bad "$name: const unit compile $rc: $(cat "$T/c.err")" |
| 138 | [ "$lrc" = 0 ] || bad "$name: literal unit compile $lrc: $(cat "$T/l.err")" |
| 139 | [ -s "$T/c.bin" ] || bad "$name: no function words" |
| 140 | cmp -s "$T/c.bin" "$T/l.bin" || bad "$name: const words differ from literal words" |
| 141 | done <<'FM' |
| 142 | fm_value|const AB ulong 5\nfn top a ulong to ulong\nret add a AB\nend|fn top a ulong to ulong\nret add a 5\nend |
| 143 | fm_negative|const M long -3\nfn top a long to long\nret mul a M\nend|fn top a long to long\nret mul a -3\nend |
| 144 | fm_param_len|const N ulong 4\nfn f a view ulong N to ulong\nret get a sub N 1\nend|fn f a view ulong 4 to ulong\nret get a sub ulong 4 1\nend |
| 145 | fm_new_len|const N uint 4\nfn f k uint to uint\nb = new uint N k\nret get b 3\nend|fn f k uint to uint\nb = new uint 4 k\nret get b 3\nend |
| 146 | fm_bound|const B ulong 3\nfn f a ulong to ulong\nx = ulong 0\nwhile lt x a bound B\nx = add x 1\nend\nret x\nend|fn f a ulong to ulong\nx = ulong 0\nwhile lt x a bound 3\nx = add x 1\nend\nret x\nend |
| 147 | fm_trap|const T uint 5\nfn f a ulong to ulong\nif eq a 1\ntrap T\nend\nret a\nend|fn f a ulong to ulong\nif eq a 1\ntrap 5\nend\nret a\nend |
| 148 | fm_for|const K ulong 4\nfn f a ulong to ulong\nx = a\nfor i ulong K\nx = add x i\nend\nret x\nend|fn f a ulong to ulong\nx = a\nfor i ulong 4\nx = add x i\nend\nret x\nend |
| 149 | fm_two_fns|const N ulong 7\nfn g a ulong to ulong\nret add a N\nend\nfn f a ulong to ulong\nret g mul a N\nend|fn g a ulong to ulong\nret add a 7\nend\nfn f a ulong to ulong\nret g mul a 7\nend |
| 150 | fm_float|const H f64 0.1\nfn f a f64 to f64\nret add a H\nend|fn f a f64 to f64\nret add a f64 0.1\nend |
| 151 | fm_for_bound|const B ulong 3\nfn f a ulong to ulong\nx = ulong 0\nfor i ulong a bound B\nx = add x 1\nend\nret x\nend|fn f a ulong to ulong\nx = ulong 0\nfor i ulong a bound 3\nx = add x 1\nend\nret x\nend |
| 152 | fm_for_default|const B ulong 1000000\nfn f a ulong to ulong\nx = ulong 0\nfor i ulong a bound B\nx = add x 1\nend\nret x\nend|fn f a ulong to ulong\nx = ulong 0\nfor i ulong a\nx = add x 1\nend\nret x\nend |
| 153 | FM |
| 154 | [ "$n" = 11 ] || bad "function-mode table ran $n of 11 cases" |
| 155 | |
| 156 | say "PASS gate16 31 constant programs, 35 refusals and 11 function-mode units" |
| 157 | exit 0 |