omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / word-wrap.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text rule "----\n" |
| 9 | text para "In olden times when wishing still helped one, there lived a king whose daughters were all beautiful, but the youngest was so beautiful that the sun itself, which has seen so much, was astonished whenever it shone in her face. Close by the king's castle lay a great dark forest, and under an old lime-tree in the forest was a well, and when the day was very warm, the king's child went out into the forest and sat down by the side of the cool fountain, and when she was bored she took a golden ball, and threw it up on high and caught it, and this ball was her favorite plaything." |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note greedy wrap: a word goes on the current line when it still fits in width w, else starts a new one |
| 22 | fn wrap a view uchar * w ulong to void |
| 23 | cur = ulong 0 |
| 24 | i = ulong 0 |
| 25 | while lt i len a bound 4096 |
| 26 | if eq get a i 32 |
| 27 | i = add i 1 |
| 28 | else |
| 29 | j = i |
| 30 | while lt j len a bound 4096 |
| 31 | if eq get a j 32 |
| 32 | break |
| 33 | end |
| 34 | j = add j 1 |
| 35 | end |
| 36 | l = sub j i |
| 37 | if eq cur 0 |
| 38 | cur = l |
| 39 | else |
| 40 | if le add add cur 1 l w |
| 41 | emit sp |
| 42 | cur = add add cur 1 l |
| 43 | else |
| 44 | emit nl |
| 45 | cur = l |
| 46 | end |
| 47 | end |
| 48 | emit part a i l |
| 49 | i = j |
| 50 | end |
| 51 | end |
| 52 | emit nl |
| 53 | end |
| 54 | fn main to int |
| 55 | wrap para 72 |
| 56 | emit rule |
| 57 | wrap para 40 |
| 58 | ret 0 |
| 59 | end |
| 60 | </syntaxhighlight> |
| 61 | {{out}} |
| 62 | <pre> |
| 63 | In olden times when wishing still helped one, there lived a king whose |
| 64 | daughters were all beautiful, but the youngest was so beautiful that the |
| 65 | sun itself, which has seen so much, was astonished whenever it shone in |
| 66 | her face. Close by the king's castle lay a great dark forest, and under |
| 67 | an old lime-tree in the forest was a well, and when the day was very |
| 68 | warm, the king's child went out into the forest and sat down by the side |
| 69 | of the cool fountain, and when she was bored she took a golden ball, and |
| 70 | threw it up on high and caught it, and this ball was her favorite |
| 71 | plaything. |
| 72 | ---- |
| 73 | In olden times when wishing still helped |
| 74 | one, there lived a king whose daughters |
| 75 | were all beautiful, but the youngest was |
| 76 | so beautiful that the sun itself, which |
| 77 | has seen so much, was astonished |
| 78 | ... |
| 79 | forest, and under an old lime-tree in |
| 80 | the forest was a well, and when the day |
| 81 | was very warm, the king's child went out |
| 82 | into the forest and sat down by the side |
| 83 | of the cool fountain, and when she was |
| 84 | bored she took a golden ball, and threw |
| 85 | it up on high and caught it, and this |
| 86 | ball was her favorite plaything. |
| 87 | </pre> |