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