omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / 99-bottles-of-beer / 99-bottles-of-beer.om
| 1 | note Rosetta Code task "99 bottles of beer": https://rosettacode.org/wiki/99_bottles_of_beer |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text nomore "No more" |
| 6 | text b1 " bottle" |
| 7 | text bn " bottles" |
| 8 | text wall " of beer on the wall\n" |
| 9 | text beer " of beer\n" |
| 10 | text take "Take one down, pass it around\n" |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | fn bottles n ulong to void |
| 38 | if eq n 0 |
| 39 | emit nomore |
| 40 | else |
| 41 | emitu n |
| 42 | end |
| 43 | if eq n 1 |
| 44 | emit b1 |
| 45 | else |
| 46 | emit bn |
| 47 | end |
| 48 | end |
| 49 | fn main to int |
| 50 | for i ulong 99 |
| 51 | n = sub 99 i |
| 52 | bottles n |
| 53 | emit wall |
| 54 | bottles n |
| 55 | emit beer |
| 56 | emit take |
| 57 | bottles sub n 1 |
| 58 | emit wall |
| 59 | if lt i 98 |
| 60 | emit nl |
| 61 | end |
| 62 | end |
| 63 | ret 0 |
| 64 | end |