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 / allergies / .meta / example.om
| 1 | note Exercism-style exercise "allergies": https://exercism.org/tracks/python/exercises/allergies |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note The allergens in score order are eggs 1, peanuts 2, shellfish 4, strawberries 8, tomatoes 16, |
| 4 | note chocolate 32, pollen 64, cats 128; a score is the sum of the allergens (higher bits are ignored). |
| 5 | note allergicto is true when score includes item; list writes the allergens of score, in that order, |
| 6 | note joined by commas, into o and returns the number of bytes written. |
| 7 | note Check it with: sh exercise.sh exercises/allergies |
| 8 | om2 |
| 9 | text al0 "eggs" |
| 10 | text al1 "peanuts" |
| 11 | text al2 "shellfish" |
| 12 | text al3 "strawberries" |
| 13 | text al4 "tomatoes" |
| 14 | text al5 "chocolate" |
| 15 | text al6 "pollen" |
| 16 | text al7 "cats" |
| 17 | fn same a view uchar * b view uchar * to bool |
| 18 | if ne len a len b |
| 19 | ret false |
| 20 | end |
| 21 | for i ulong len a |
| 22 | if ne get a i get b i |
| 23 | ret false |
| 24 | end |
| 25 | end |
| 26 | ret true |
| 27 | end |
| 28 | fn app o edit uchar * p ulong s view uchar * to ulong |
| 29 | q = p |
| 30 | for i ulong len s |
| 31 | put o q get s i |
| 32 | q = add q 1 |
| 33 | end |
| 34 | ret q |
| 35 | end |
| 36 | fn itemidx item view uchar * to ulong |
| 37 | if same item al0 |
| 38 | ret 0 |
| 39 | end |
| 40 | if same item al1 |
| 41 | ret 1 |
| 42 | end |
| 43 | if same item al2 |
| 44 | ret 2 |
| 45 | end |
| 46 | if same item al3 |
| 47 | ret 3 |
| 48 | end |
| 49 | if same item al4 |
| 50 | ret 4 |
| 51 | end |
| 52 | if same item al5 |
| 53 | ret 5 |
| 54 | end |
| 55 | if same item al6 |
| 56 | ret 6 |
| 57 | end |
| 58 | if same item al7 |
| 59 | ret 7 |
| 60 | end |
| 61 | ret 8 |
| 62 | end |
| 63 | fn allergicto item view uchar * score ulong to bool |
| 64 | k = itemidx item |
| 65 | if eq k 8 |
| 66 | ret false |
| 67 | end |
| 68 | ret ne and shr score k 1 0 |
| 69 | end |
| 70 | fn appitem o edit uchar * p ulong k ulong to ulong |
| 71 | if eq k 0 |
| 72 | ret app o p al0 |
| 73 | end |
| 74 | if eq k 1 |
| 75 | ret app o p al1 |
| 76 | end |
| 77 | if eq k 2 |
| 78 | ret app o p al2 |
| 79 | end |
| 80 | if eq k 3 |
| 81 | ret app o p al3 |
| 82 | end |
| 83 | if eq k 4 |
| 84 | ret app o p al4 |
| 85 | end |
| 86 | if eq k 5 |
| 87 | ret app o p al5 |
| 88 | end |
| 89 | if eq k 6 |
| 90 | ret app o p al6 |
| 91 | end |
| 92 | ret app o p al7 |
| 93 | end |
| 94 | fn list score ulong o edit uchar * to ulong |
| 95 | p = ulong 0 |
| 96 | for k ulong 8 |
| 97 | if ne and shr score k 1 0 |
| 98 | if gt p 0 |
| 99 | put o p 44 |
| 100 | p = add p 1 |
| 101 | end |
| 102 | p = appitem o p k |
| 103 | end |
| 104 | end |
| 105 | ret p |
| 106 | end |