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
omega-mv / seed / text / tests / txdrv.om
| 1 | note txdrv: test driver for seed/text/text.om. Composed as: om2, text.om, this file (gate.sh does it). |
| 2 | note stdin: records, each an op byte and its fields; a byte string is a u32 little-endian length and the |
| 3 | note bytes, a number a u64 little-endian. One output line per record (see gen.om for the layout). |
| 4 | note All of stdin is read into one 3 MiB local array, which every case slices with part or (o, n). |
| 5 | global obuf uchar 1048576 |
| 6 | global oc ulong 1 |
| 7 | global gp ulong 1 |
| 8 | |
| 9 | fn oflush to void |
| 10 | off = ulong 0 |
| 11 | n = get oc 0 |
| 12 | while lt off n bound 1048576 |
| 13 | w = write 1 obuf off sub n off |
| 14 | if le w 0 |
| 15 | exit int 90 |
| 16 | end |
| 17 | off = add off ulong w |
| 18 | end |
| 19 | put oc 0 0 |
| 20 | end |
| 21 | |
| 22 | fn oputb b uchar to void |
| 23 | m = get oc 0 |
| 24 | put obuf m b |
| 25 | put oc 0 add m 1 |
| 26 | end |
| 27 | |
| 28 | fn oputn v ulong to void |
| 29 | d = new uchar 20 0 |
| 30 | i = ulong 0 |
| 31 | x = v |
| 32 | put d 0 uchar add rem x 10 48 |
| 33 | x = div x 10 |
| 34 | i = add i 1 |
| 35 | while gt x 0 bound 20 |
| 36 | put d i uchar add rem x 10 48 |
| 37 | x = div x 10 |
| 38 | i = add i 1 |
| 39 | end |
| 40 | while gt i 0 bound 20 |
| 41 | i = sub i 1 |
| 42 | oputb get d i |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | fn oputl v long to void |
| 47 | if lt v 0 |
| 48 | oputb 45 |
| 49 | oputn add ulong neg add v 1 1 |
| 50 | ret |
| 51 | end |
| 52 | oputn ulong v |
| 53 | end |
| 54 | |
| 55 | fn oend to void |
| 56 | oputb 10 |
| 57 | if gt get oc 0 1040000 |
| 58 | oflush |
| 59 | end |
| 60 | end |
| 61 | |
| 62 | fn rd8 b view uchar * to ulong |
| 63 | p = get gp 0 |
| 64 | put gp 0 add p 1 |
| 65 | ret ulong get b p |
| 66 | end |
| 67 | |
| 68 | fn rdn b view uchar * k ulong to ulong |
| 69 | p = get gp 0 |
| 70 | x = ulong 0 |
| 71 | for i ulong k |
| 72 | x = or x shl ulong get b add p i mul i 8 |
| 73 | end |
| 74 | put gp 0 add p k |
| 75 | ret x |
| 76 | end |
| 77 | |
| 78 | note rds: a byte string; leaves its offset in gp and returns its length after skipping it. |
| 79 | fn rds b view uchar * to ulong |
| 80 | n = rdn b 4 |
| 81 | p = get gp 0 |
| 82 | put gp 0 add p n |
| 83 | ret n |
| 84 | end |
| 85 | |
| 86 | note copyto: b[p .. p+n) into a (the second string of a case: one array may not be passed twice in a call). |
| 87 | fn copyto b view uchar * p ulong n ulong a edit uchar * to void |
| 88 | for i ulong n |
| 89 | put a i get b add p i |
| 90 | end |
| 91 | end |
| 92 | |
| 93 | fn cmpcase b view uchar * to void |
| 94 | sb = new uchar 262144 0 |
| 95 | na = rds b |
| 96 | pa = sub get gp 0 na |
| 97 | nb = rds b |
| 98 | pb = sub get gp 0 nb |
| 99 | copyto b pb nb sb |
| 100 | oputb 69 |
| 101 | oputb 32 |
| 102 | e = tx_eq part b pa na part sb 0 nb |
| 103 | if e |
| 104 | oputb 49 |
| 105 | else |
| 106 | oputb 48 |
| 107 | end |
| 108 | oputb 32 |
| 109 | c = tx_cmp part b pa na part sb 0 nb |
| 110 | oputl long c |
| 111 | oend |
| 112 | end |
| 113 | |
| 114 | fn findcase b view uchar * to void |
| 115 | sb = new uchar 262144 0 |
| 116 | nh = rds b |
| 117 | ph = sub get gp 0 nh |
| 118 | nn = rds b |
| 119 | pn = sub get gp 0 nn |
| 120 | from = rdn b 8 |
| 121 | copyto b pn nn sb |
| 122 | r = tx_find part b ph nh part sb 0 nn from |
| 123 | oputb 70 |
| 124 | oputb 32 |
| 125 | if eq r txnone |
| 126 | oputb 110 |
| 127 | else |
| 128 | oputn r |
| 129 | end |
| 130 | oend |
| 131 | end |
| 132 | |
| 133 | fn splitcase b view uchar * to void |
| 134 | ns = rds b |
| 135 | ps = sub get gp 0 ns |
| 136 | sep = uchar rd8 b |
| 137 | cnt = ulong 0 |
| 138 | p = ulong 0 |
| 139 | lens = new ulong 4096 0 |
| 140 | while true bound 4096 |
| 141 | e = tx_field part b ps ns sep p |
| 142 | put lens cnt sub e p |
| 143 | cnt = add cnt 1 |
| 144 | if eq e ns |
| 145 | break |
| 146 | end |
| 147 | p = add e 1 |
| 148 | end |
| 149 | oputb 83 |
| 150 | oputb 32 |
| 151 | oputn cnt |
| 152 | for i ulong cnt |
| 153 | oputb 32 |
| 154 | oputn get lens i |
| 155 | end |
| 156 | oend |
| 157 | end |
| 158 | |
| 159 | fn parsecase b view uchar * op ulong to void |
| 160 | n = rds b |
| 161 | o = sub get gp 0 n |
| 162 | v = new ulong 1 0 |
| 163 | s = new long 1 0 |
| 164 | r = long 0 |
| 165 | if eq op 68 |
| 166 | r = tx_pdec b o n v |
| 167 | end |
| 168 | if eq op 72 |
| 169 | r = tx_phex b o n v |
| 170 | end |
| 171 | if eq op 73 |
| 172 | r = tx_pdeci b o n s |
| 173 | end |
| 174 | oputb uchar op |
| 175 | if lt r 0 |
| 176 | oputt3 101 114 114 |
| 177 | oputn ulong neg r |
| 178 | oend |
| 179 | ret |
| 180 | end |
| 181 | oputb 32 |
| 182 | oputb 111 |
| 183 | oputb 107 |
| 184 | oputb 32 |
| 185 | if eq op 73 |
| 186 | oputl get s 0 |
| 187 | else |
| 188 | oputn get v 0 |
| 189 | end |
| 190 | oend |
| 191 | end |
| 192 | |
| 193 | note oputt3: a space, three bytes and a space. |
| 194 | fn oputt3 a uchar b uchar c uchar to void |
| 195 | oputb 32 |
| 196 | oputb a |
| 197 | oputb b |
| 198 | oputb c |
| 199 | oputb 32 |
| 200 | end |
| 201 | |
| 202 | fn fmtcase b view uchar * op ulong to void |
| 203 | v = rdn b 8 |
| 204 | w = rdn b 8 |
| 205 | cap = rdn b 8 |
| 206 | fb = new uchar 64 0 |
| 207 | o = sub 64 cap |
| 208 | r = long 0 |
| 209 | if eq op 102 |
| 210 | r = tx_fdec v fb o |
| 211 | end |
| 212 | if eq op 103 |
| 213 | r = tx_fdeci bitcast long v fb o |
| 214 | end |
| 215 | if eq op 120 |
| 216 | r = tx_fhex v w fb o |
| 217 | end |
| 218 | oputb uchar op |
| 219 | oputb 32 |
| 220 | if lt r 0 |
| 221 | oputb 69 |
| 222 | oend |
| 223 | ret |
| 224 | end |
| 225 | for i ulong sub ulong r o |
| 226 | oputb get fb add o i |
| 227 | end |
| 228 | oend |
| 229 | end |
| 230 | |
| 231 | fn u8case b view uchar * to void |
| 232 | n = rds b |
| 233 | o = sub get gp 0 n |
| 234 | r = tx_u8ok b o n |
| 235 | oputb 85 |
| 236 | oputb 32 |
| 237 | oputn sub r o |
| 238 | oend |
| 239 | end |
| 240 | |
| 241 | note dec: "P cp len" or "P n"; enc: "Q" and the bytes in hex, "Q E" (no room) or "Q R" (range). |
| 242 | fn deccase b view uchar * to void |
| 243 | n = rds b |
| 244 | o = sub get gp 0 n |
| 245 | r = tx_u8dec b o n |
| 246 | oputb 80 |
| 247 | oputb 32 |
| 248 | if eq r txnone |
| 249 | oputb 110 |
| 250 | oend |
| 251 | ret |
| 252 | end |
| 253 | oputn shr r 3 |
| 254 | oputb 32 |
| 255 | oputn and r 7 |
| 256 | oend |
| 257 | end |
| 258 | |
| 259 | fn enccase b view uchar * to void |
| 260 | cp = rdn b 8 |
| 261 | cap = rdn b 8 |
| 262 | fb = new uchar 8 0 |
| 263 | o = sub 8 cap |
| 264 | r = tx_u8enc cp fb o |
| 265 | oputb 81 |
| 266 | oputb 32 |
| 267 | if eq r txespace |
| 268 | oputb 69 |
| 269 | oend |
| 270 | ret |
| 271 | end |
| 272 | if eq r txerange |
| 273 | oputb 82 |
| 274 | oend |
| 275 | ret |
| 276 | end |
| 277 | for i ulong sub ulong r o |
| 278 | v = get fb add o i |
| 279 | oputb uchar hexc shr v 4 |
| 280 | oputb uchar hexc and v 15 |
| 281 | end |
| 282 | oend |
| 283 | end |
| 284 | |
| 285 | fn hexc d uchar to uchar |
| 286 | if lt d 10 |
| 287 | ret add d 48 |
| 288 | end |
| 289 | ret add d 87 |
| 290 | end |
| 291 | |
| 292 | fn main to int |
| 293 | inb = new uchar 3145728 0 |
| 294 | off = ulong 0 |
| 295 | k = long 1 |
| 296 | while and gt k 0 lt off len inb bound 999999 |
| 297 | k = read 0 inb off sub len inb off |
| 298 | if gt k 0 |
| 299 | off = add off ulong k |
| 300 | end |
| 301 | end |
| 302 | if eq off len inb |
| 303 | ret 91 |
| 304 | end |
| 305 | while lt get gp 0 off bound 999999 |
| 306 | op = rd8 inb |
| 307 | if eq op 69 |
| 308 | cmpcase inb |
| 309 | else |
| 310 | if eq op 70 |
| 311 | findcase inb |
| 312 | else |
| 313 | if eq op 83 |
| 314 | splitcase inb |
| 315 | else |
| 316 | if eq op 85 |
| 317 | u8case inb |
| 318 | else |
| 319 | if eq op 80 |
| 320 | deccase inb |
| 321 | else |
| 322 | if eq op 81 |
| 323 | enccase inb |
| 324 | else |
| 325 | if or eq op 68 or eq op 72 eq op 73 |
| 326 | parsecase inb op |
| 327 | else |
| 328 | if or eq op 102 or eq op 103 eq op 120 |
| 329 | fmtcase inb op |
| 330 | else |
| 331 | ret 92 |
| 332 | end |
| 333 | end |
| 334 | end |
| 335 | end |
| 336 | end |
| 337 | end |
| 338 | end |
| 339 | end |
| 340 | end |
| 341 | oflush |
| 342 | ret 0 |
| 343 | end |