interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / si / rosetta / tasks / word-wrap / word-wrap.om

57 lines · 1539 bytes Raw Blame History
1note Rosetta Code task "Word wrap": https://rosettacode.org/wiki/Word_wrap
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text rule "----\n"
7text 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."
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note greedy wrap: a word goes on the current line when it still fits in width w, else starts a new one
20fn wrap a view uchar * w ulong to void
21cur = ulong 0
22i = ulong 0
23while lt i len a bound 4096
24if eq get a i 32
25i = add i 1
26else
27j = i
28while lt j len a bound 4096
29if eq get a j 32
30break
31end
32j = add j 1
33end
34l = sub j i
35if eq cur 0
36cur = l
37else
38if le add add cur 1 l w
39emit sp
40cur = add add cur 1 l
41else
42emit nl
43cur = l
44end
45end
46emit part a i l
47i = j
48end
49end
50emit nl
51end
52fn main to int
53wrap para 72
54emit rule
55wrap para 40
56ret 0
57end