interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / sort-three-variables.wiki

190 lines · 3008 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text tx "x = "
10text ty "y = "
11text tz "z = "
12text s0 "lions, tigers, and"
13text s1 "bears, oh my!"
14text s2 "(from the \"Wizard of OZ\")"
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 emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
42fn emiti n long to void
43if lt n 0
44emit minus
45emitu add bitcast ulong not n 1
46else
47emitu ulong n
48end
49end
50note sort3 orders three longs with three compare-exchanges and prints them as x, y, z
51fn sort3 x long y long z long to void
52if gt x y
53t = x
54x = y
55y = t
56end
57if gt y z
58t = y
59y = z
60z = t
61end
62if gt x y
63t = x
64x = y
65y = t
66end
67emit tx
68emiti x
69emit nl
70emit ty
71emiti y
72emit nl
73emit tz
74emiti z
75emit nl
76end
77note str writes text number k (0, 1 or 2); a text cannot be chosen by index, so this is an if chain
78fn str k ulong to void
79if eq k 0
80emit s0
81end
82if eq k 1
83emit s1
84end
85if eq k 2
86emit s2
87end
88end
89note cmpb compares two byte strings: negative, zero or positive, like C's strcmp on unsigned bytes
90fn cmpb a view uchar * b view uchar * to long
91for i ulong len a
92if ge i len b
93ret 1
94end
95if ne get a i get b i
96ret sub long get a i long get b i
97end
98end
99if lt len a len b
100ret -1
101end
102ret 0
103end
104note gtk says whether text j sorts after text k; the three pairs are spelled out for the same reason
105fn gtk j ulong k ulong to bool
106c = long 0
107if and eq j 0 eq k 1
108c = cmpb s0 s1
109end
110if and eq j 1 eq k 0
111c = cmpb s1 s0
112end
113if and eq j 0 eq k 2
114c = cmpb s0 s2
115end
116if and eq j 2 eq k 0
117c = cmpb s2 s0
118end
119if and eq j 1 eq k 2
120c = cmpb s1 s2
121end
122if and eq j 2 eq k 1
123c = cmpb s2 s1
124end
125ret gt c 0
126end
127fn showk x ulong y ulong z ulong to void
128emit tx
129str x
130emit nl
131emit ty
132str y
133emit nl
134emit tz
135str z
136emit nl
137end
138note sorts the strings the same way, by swapping which text each variable names
139fn sorts to void
140x = ulong 0
141y = ulong 1
142z = ulong 2
143showk x y z
144if gtk x y
145t = x
146x = y
147y = t
148end
149if gtk y z
150t = y
151y = z
152z = t
153end
154if gtk x y
155t = x
156x = y
157y = t
158end
159showk x y z
160end
161fn main to int
162emit tx
163emiti 77444
164emit nl
165emit ty
166emiti -12
167emit nl
168emit tz
169emiti 0
170emit nl
171sort3 77444 -12 0
172sorts
173ret 0
174end
175</syntaxhighlight>
176{{out}}
177<pre>
178x = 77444
179y = -12
180z = 0
181x = -12
182y = 0
183z = 77444
184x = lions, tigers, and
185y = bears, oh my!
186z = (from the "Wizard of OZ")
187x = (from the "Wizard of OZ")
188y = bears, oh my!
189z = lions, tigers, and
190</pre>