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 / ethiopian-multiplication / ethiopian-multiplication.om

75 lines · 1324 bytes Raw Blame History
1note Rosetta Code task "Ethiopian multiplication": https://rosettacode.org/wiki/Ethiopian_multiplication
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text kept " KEPT\n"
7text struck " STRUCK\n"
8text tx " x "
9text teq " = "
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36fn halve n ulong to ulong
37ret shr n 1
38end
39fn double n ulong to ulong
40ret shl n 1
41end
42fn iseven n ulong to bool
43ret eq and n 1 0
44end
45note left column halves to 1, right column doubles; rows with an even left number are struck out and
46note the kept right numbers add up to the product
47fn ethiop a ulong b ulong to ulong
48s = ulong 0
49l = a
50r = b
51while ge l 1 bound 64
52emitu l
53emit sp
54emitu r
55if iseven l
56emit struck
57else
58emit kept
59s = add s r
60end
61l = halve l
62r = double r
63end
64ret s
65end
66fn main to int
67p = ethiop 17 34
68emitu 17
69emit tx
70emitu 34
71emit teq
72emitu p
73emit nl
74ret 0
75end