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