omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / reverse-words-in-a-string / reverse-words-in-a-string.om
| 1 | note Rosetta Code task "Reverse words in a string": https://rosettacode.org/wiki/Reverse_words_in_a_string |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text poem "---------- Ice and Fire ------------\n\nfire, in end will world the say Some\nice. in say Some\ndesire of tasted I've what From\nfire. favor who those with hold I\n\n... elided paragraph last ...\n\nFrost Robert -----------------------\n" |
| 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 revline prints the space-separated words of a in reverse order, one space between them |
| 19 | fn revline a view uchar * to void |
| 20 | st = new ulong 64 0 |
| 21 | ln = new ulong 64 0 |
| 22 | k = ulong 0 |
| 23 | i = ulong 0 |
| 24 | while lt i len a bound 4096 |
| 25 | if eq get a i 32 |
| 26 | i = add i 1 |
| 27 | else |
| 28 | j = i |
| 29 | while lt j len a bound 4096 |
| 30 | if eq get a j 32 |
| 31 | break |
| 32 | end |
| 33 | j = add j 1 |
| 34 | end |
| 35 | put st k i |
| 36 | put ln k sub j i |
| 37 | k = add k 1 |
| 38 | i = j |
| 39 | end |
| 40 | end |
| 41 | for w ulong k |
| 42 | if gt w 0 |
| 43 | emit sp |
| 44 | end |
| 45 | x = sub sub k 1 w |
| 46 | emit part a get st x get ln x |
| 47 | end |
| 48 | emit nl |
| 49 | end |
| 50 | fn main to int |
| 51 | s = ulong 0 |
| 52 | for i ulong len poem |
| 53 | if eq get poem i 10 |
| 54 | revline part poem s sub i s |
| 55 | s = add i 1 |
| 56 | end |
| 57 | end |
| 58 | ret 0 |
| 59 | end |