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 / si / rosetta / tasks / insertion-sort / insertion-sort.om

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