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
omega-mv / si / rosetta / exercises / run-length-encoding / .meta / example.om
| 1 | note Exercism-style exercise "run-length-encoding": https://exercism.org/tracks/python/exercises/run-length-encoding |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note encode writes the run-length encoding of s into o: each run of 2 or more equal bytes becomes its |
| 4 | note decimal count followed by the byte, a single byte stays as it is. decode reverses it. Both |
| 5 | note return the number of bytes written. |
| 6 | note Check it with: sh exercise.sh exercises/run-length-encoding |
| 7 | om2 |
| 8 | fn app o edit uchar * p ulong s view uchar * to ulong |
| 9 | q = p |
| 10 | for i ulong len s |
| 11 | put o q get s i |
| 12 | q = add q 1 |
| 13 | end |
| 14 | ret q |
| 15 | end |
| 16 | fn appnum o edit uchar * p ulong n ulong to ulong |
| 17 | d = new uchar 20 0 |
| 18 | i = ulong 20 |
| 19 | v = n |
| 20 | while true bound 20 |
| 21 | i = sub i 1 |
| 22 | put d i uchar add rem v 10 48 |
| 23 | v = div v 10 |
| 24 | if eq v 0 |
| 25 | break |
| 26 | end |
| 27 | end |
| 28 | ret app o p part d i sub 20 i |
| 29 | end |
| 30 | fn encode s view uchar * o edit uchar * to ulong |
| 31 | p = ulong 0 |
| 32 | i = ulong 0 |
| 33 | n = len s |
| 34 | while lt i n bound 999999 |
| 35 | c = get s i |
| 36 | r = ulong 1 |
| 37 | while lt add i r n bound 999999 |
| 38 | if ne get s add i r c |
| 39 | break |
| 40 | end |
| 41 | r = add r 1 |
| 42 | end |
| 43 | if gt r 1 |
| 44 | p = appnum o p r |
| 45 | end |
| 46 | put o p c |
| 47 | p = add p 1 |
| 48 | i = add i r |
| 49 | end |
| 50 | ret p |
| 51 | end |
| 52 | fn decode s view uchar * o edit uchar * to ulong |
| 53 | p = ulong 0 |
| 54 | r = ulong 0 |
| 55 | for i ulong len s |
| 56 | c = get s i |
| 57 | if and ge c 48 le c 57 |
| 58 | r = add mul r 10 ulong sub c 48 |
| 59 | else |
| 60 | if eq r 0 |
| 61 | r = ulong 1 |
| 62 | end |
| 63 | for j ulong r |
| 64 | put o p c |
| 65 | p = add p 1 |
| 66 | end |
| 67 | r = ulong 0 |
| 68 | end |
| 69 | end |
| 70 | ret p |
| 71 | end |