omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / character-codes / character-codes.om
| 1 | note Rosetta Code task "Character codes": https://rosettacode.org/wiki/Character_codes |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text ch "a" |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note emitu writes n in decimal; 20 digits hold every ulong |
| 19 | fn emitu n ulong to void |
| 20 | d = new uchar 20 0 |
| 21 | i = ulong 20 |
| 22 | v = n |
| 23 | while true bound 20 |
| 24 | i = sub i 1 |
| 25 | put d i uchar add rem v 10 48 |
| 26 | v = div v 10 |
| 27 | if eq v 0 |
| 28 | break |
| 29 | end |
| 30 | end |
| 31 | emit part d i sub 20 i |
| 32 | end |
| 33 | note emitc writes one byte |
| 34 | fn emitc c uchar to void |
| 35 | b = new uchar 1 0 |
| 36 | put b 0 c |
| 37 | emit b |
| 38 | end |
| 39 | fn main to int |
| 40 | note a byte is a number: the code of a is get ch 0, and the number 97 printed as a byte is a |
| 41 | emit ch |
| 42 | emit sp |
| 43 | emitu ulong get ch 0 |
| 44 | emit nl |
| 45 | emitu 97 |
| 46 | emit sp |
| 47 | emitc 97 |
| 48 | emit nl |
| 49 | ret 0 |
| 50 | end |