interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / sort-three-variables / sort-three-variables.om

172 lines · 2698 bytes Raw Blame History
1note Rosetta Code task "Sort three variables": https://rosettacode.org/wiki/Sort_three_variables
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tx "x = "
8text ty "y = "
9text tz "z = "
10text s0 "lions, tigers, and"
11text s1 "bears, oh my!"
12text s2 "(from the \"Wizard of OZ\")"
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitu writes n in decimal; 20 digits hold every ulong
25fn emitu n ulong to void
26d = new uchar 20 0
27i = ulong 20
28v = n
29while true bound 20
30i = sub i 1
31put d i uchar add rem v 10 48
32v = div v 10
33if eq v 0
34break
35end
36end
37emit part d i sub 20 i
38end
39note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
40fn emiti n long to void
41if lt n 0
42emit minus
43emitu add bitcast ulong not n 1
44else
45emitu ulong n
46end
47end
48note sort3 orders three longs with three compare-exchanges and prints them as x, y, z
49fn sort3 x long y long z long to void
50if gt x y
51t = x
52x = y
53y = t
54end
55if gt y z
56t = y
57y = z
58z = t
59end
60if gt x y
61t = x
62x = y
63y = t
64end
65emit tx
66emiti x
67emit nl
68emit ty
69emiti y
70emit nl
71emit tz
72emiti z
73emit nl
74end
75note str writes text number k (0, 1 or 2); a text cannot be chosen by index, so this is an if chain
76fn str k ulong to void
77if eq k 0
78emit s0
79end
80if eq k 1
81emit s1
82end
83if eq k 2
84emit s2
85end
86end
87note cmpb compares two byte strings: negative, zero or positive, like C's strcmp on unsigned bytes
88fn cmpb a view uchar * b view uchar * to long
89for i ulong len a
90if ge i len b
91ret 1
92end
93if ne get a i get b i
94ret sub long get a i long get b i
95end
96end
97if lt len a len b
98ret -1
99end
100ret 0
101end
102note gtk says whether text j sorts after text k; the three pairs are spelled out for the same reason
103fn gtk j ulong k ulong to bool
104c = long 0
105if and eq j 0 eq k 1
106c = cmpb s0 s1
107end
108if and eq j 1 eq k 0
109c = cmpb s1 s0
110end
111if and eq j 0 eq k 2
112c = cmpb s0 s2
113end
114if and eq j 2 eq k 0
115c = cmpb s2 s0
116end
117if and eq j 1 eq k 2
118c = cmpb s1 s2
119end
120if and eq j 2 eq k 1
121c = cmpb s2 s1
122end
123ret gt c 0
124end
125fn showk x ulong y ulong z ulong to void
126emit tx
127str x
128emit nl
129emit ty
130str y
131emit nl
132emit tz
133str z
134emit nl
135end
136note sorts the strings the same way, by swapping which text each variable names
137fn sorts to void
138x = ulong 0
139y = ulong 1
140z = ulong 2
141showk x y z
142if gtk x y
143t = x
144x = y
145y = t
146end
147if gtk y z
148t = y
149y = z
150z = t
151end
152if gtk x y
153t = x
154x = y
155y = t
156end
157showk x y z
158end
159fn main to int
160emit tx
161emiti 77444
162emit nl
163emit ty
164emiti -12
165emit nl
166emit tz
167emiti 0
168emit nl
169sort3 77444 -12 0
170sorts
171ret 0
172end