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 / si / rosetta / tasks / insertion-sort / insertion-sort.om
| 1 | note Rosetta Code task "Sorting algorithms/Insertion sort": https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text minus "-" |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note emitu writes n in decimal; 20 digits hold every ulong |
| 19 | fn emitu n ulong to void |
| 20 | d = new uchar 20 0 |
| 21 | i = ulong 20 |
| 22 | v = n |
| 23 | while true bound 20 |
| 24 | i = sub i 1 |
| 25 | put d i uchar add rem v 10 48 |
| 26 | v = div v 10 |
| 27 | if eq v 0 |
| 28 | break |
| 29 | end |
| 30 | end |
| 31 | emit part d i sub 20 i |
| 32 | end |
| 33 | note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow |
| 34 | fn emiti n long to void |
| 35 | if lt n 0 |
| 36 | emit minus |
| 37 | emitu add bitcast ulong not n 1 |
| 38 | else |
| 39 | emitu ulong n |
| 40 | end |
| 41 | end |
| 42 | fn isort a edit long 10 to void |
| 43 | for i ulong 9 |
| 44 | k = get a add i 1 |
| 45 | j = add i 1 |
| 46 | note and evaluates both sides, so the bound test and the load are separate lines |
| 47 | while gt j 0 bound 10 |
| 48 | if le get a sub j 1 k |
| 49 | break |
| 50 | end |
| 51 | put a j get a sub j 1 |
| 52 | j = sub j 1 |
| 53 | end |
| 54 | put a j k |
| 55 | end |
| 56 | end |
| 57 | fn show a view long 10 to void |
| 58 | for i ulong 10 |
| 59 | if gt i 0 |
| 60 | emit sp |
| 61 | end |
| 62 | emiti get a i |
| 63 | end |
| 64 | emit nl |
| 65 | end |
| 66 | fn main to int |
| 67 | a = new long 10 0 |
| 68 | put a 0 31 |
| 69 | put a 1 41 |
| 70 | put a 2 59 |
| 71 | put a 3 26 |
| 72 | put a 4 -41 |
| 73 | put a 5 58 |
| 74 | put a 6 0 |
| 75 | put a 7 -9223372036854775808 |
| 76 | put a 8 9223372036854775807 |
| 77 | put a 9 26 |
| 78 | show a |
| 79 | isort a |
| 80 | show a |
| 81 | ret 0 |
| 82 | end |