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 / substitution-cipher / substitution-cipher.om

56 lines · 1265 bytes Raw Blame History
1note Rosetta Code task "Substitution cipher": https://rosettacode.org/wiki/Substitution_cipher
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5note key: plain letter a..z maps to the letter at the same place here
6text key "phqgiumeaylnofdxjkrcvstzwb"
7text msg "the quick brown fox jumps over the lazy dog, 42 times"
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 enc maps each lower-case letter through key; other bytes are copied
20fn enc a view uchar * r edit uchar * to void
21for i ulong len a
22c = get a i
23if and ge c 97 le c 122
24c = get key sub c 97
25end
26put r i c
27end
28end
29note dec builds the inverse table first, then maps through it
30fn dec a view uchar * r edit uchar * to void
31inv = new uchar 26 0
32for j ulong 26
33put inv sub get key j 97 add uchar j 97
34end
35for i ulong len a
36c = get a i
37if and ge c 97 le c 122
38c = get inv sub c 97
39end
40put r i c
41end
42end
43fn main to int
44n = len msg
45e = new uchar 256 0
46d = new uchar 256 0
47emit msg
48emit nl
49enc msg part e 0 n
50emit part e 0 n
51emit nl
52dec part e 0 n part d 0 n
53emit part d 0 n
54emit nl
55ret 0
56end