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 / topological-sort / topological-sort.om

161 lines · 3371 bytes Raw Blame History
1note Rosetta Code task "Topological sort": https://rosettacode.org/wiki/Topological_sort
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text data "des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee;dw01 ieee dw01 dware gtech;dw02 ieee dw02 dware;dw03 std synopsys dware dw03 dw02 dw01 ieee gtech;dw04 dw04 ieee dw01 dware gtech;dw05 dw05 ieee dware;dw06 dw06 ieee dware;dw07 ieee dware;dware ieee dware;gtech ieee gtech;ramlib std ieee;std_cell_lib ieee std_cell_lib;synopsys;"
8text tcyc "un-orderable:"
9global nst ulong 32
10global nln ulong 32
11global dep uchar 1024
12global indeg ulong 32
13global sent uchar 32
14global nn ulong 1
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitu writes n in decimal; 20 digits hold every ulong
27fn emitu n ulong to void
28d = new uchar 20 0
29i = ulong 20
30v = n
31while true bound 20
32i = sub i 1
33put d i uchar add rem v 10 48
34v = div v 10
35if eq v 0
36break
37end
38end
39emit part d i sub 20 i
40end
41note data lists one library per ;-terminated group: the library, then what it depends on; a library
42note that names itself is ignored, as the task asks. Names are kept as (start, length) in data.
43note same says whether the names at (a, la) and (b, lb) in data are the same bytes
44fn same a ulong la ulong b ulong lb ulong to bool
45if ne la lb
46ret false
47end
48for i ulong la
49if ne get data add a i get data add b i
50ret false
51end
52end
53ret true
54end
55note before says whether name i sorts before name j, comparing bytes as C's strcmp does
56fn before i ulong j ulong to bool
57a = get nst i
58b = get nst j
59la = get nln i
60lb = get nln j
61for k ulong la
62if ge k lb
63ret false
64end
65if ne get data add a k get data add b k
66ret lt get data add a k get data add b k
67end
68end
69ret lt la lb
70end
71note intern returns the number of the name at (s, l), adding it to the table if it is new
72fn intern s ulong l ulong to ulong
73n = get nn 0
74for i ulong n
75if same get nst i get nln i s l
76ret i
77end
78end
79put nst n s
80put nln n l
81put nn 0 add n 1
82ret n
83end
84note load reads every group: the first word is the library u, each later word v adds the edge v -> u
85fn load to void
86first = true
87u = ulong 0
88st = ulong 0
89for i ulong len data
90c = get data i
91if or eq c 32 eq c 59
92if gt i st
93v = intern st sub i st
94if first
95u = v
96first = false
97else
98addedge v u
99end
100end
101st = add i 1
102if eq c 59
103first = true
104end
105end
106end
107end
108fn addedge v ulong u ulong to void
109if ne v u
110if eq get dep add mul v 32 u 0
111put dep add mul v 32 u 1
112put indeg u add get indeg u 1
113end
114end
115end
116fn name i ulong to void
117emit part data get nst i get nln i
118end
119note Kahn's algorithm: output the smallest ready name (in-degree 0), then release what depends on it;
120note each round outputs one name, so nn rounds suffice; names never output are part of a cycle
121fn main to int
122load
123n = get nn 0
124for r ulong n
125b = n
126for i ulong n
127if and eq get sent i 0 eq get indeg i 0
128if eq b n
129b = i
130else
131if before i b
132b = i
133end
134end
135end
136end
137if eq b n
138break
139end
140put sent b 1
141if gt r 0
142emit sp
143end
144name b
145for j ulong n
146if eq get dep add mul b 32 j 1
147put indeg j sub get indeg j 1
148end
149end
150end
151emit nl
152emit tcyc
153for i ulong n
154if eq get sent i 0
155emit sp
156name i
157end
158end
159emit nl
160ret 0
161end