interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / phrase-reversals / phrase-reversals.om

72 lines · 1348 bytes Raw Blame History
1note Rosetta Code task "Phrase reversals": https://rosettacode.org/wiki/Phrase_reversals
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text msg "rosetta code phrase reversal"
7note emit writes every byte of a to stdout; a short write is retried, an error exits 1
8fn emit a view uchar * to void
9o = ulong 0
10while lt o len a bound 4096
11k = write 1 a o sub len a o
12if le k 0
13exit 1
14end
15o = add o ulong k
16end
17end
18note emitc writes one byte
19fn emitc c uchar to void
20b = new uchar 1 0
21put b 0 c
22emit b
23end
24note words splits a into (start, length) pairs at single spaces and returns how many
25fn words a view uchar * st edit ulong 16 ln edit ulong 16 to ulong
26k = ulong 0
27s = ulong 0
28for i ulong add len a 1
29e = eq i len a
30if not e
31e = eq get a i 32
32end
33if e
34put st k s
35put ln k sub i s
36k = add k 1
37s = add i 1
38end
39end
40ret k
41end
42fn revb a view uchar * to void
43for i ulong len a
44emitc get a sub sub len a 1 i
45end
46end
47fn main to int
48st = new ulong 16 0
49ln = new ulong 16 0
50k = words msg st ln
51note 1: the whole string reversed
52revb msg
53emit nl
54note 2: each word reversed, order kept
55for w ulong k
56if gt w 0
57emit sp
58end
59revb part msg get st w get ln w
60end
61emit nl
62note 3: the word order reversed, words kept
63for w ulong k
64if gt w 0
65emit sp
66end
67x = sub sub k 1 w
68emit part msg get st x get ln x
69end
70emit nl
71ret 0
72end