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 / tasks / isbn13-check-digit / isbn13-check-digit.om
| 1 | note Rosetta Code task "ISBN13 check digit": https://rosettacode.org/wiki/ISBN13_check_digit |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text tgood ": good\n" |
| 6 | text tbad ": bad\n" |
| 7 | text i1 "978-0596528126" |
| 8 | text i2 "978-0596528120" |
| 9 | text i3 "978-1788399081" |
| 10 | text i4 "978-1788399083" |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note digits weigh 1, 3, 1, 3, ... skipping hyphens; valid when there are 13 and the sum is 0 mod 10 |
| 23 | fn isbn a view uchar * to bool |
| 24 | s = ulong 0 |
| 25 | k = ulong 0 |
| 26 | for i ulong len a |
| 27 | c = get a i |
| 28 | if and ge c 48 le c 57 |
| 29 | d = ulong sub c 48 |
| 30 | if eq rem k 2 1 |
| 31 | d = mul d 3 |
| 32 | end |
| 33 | s = add s d |
| 34 | k = add k 1 |
| 35 | end |
| 36 | end |
| 37 | ret and eq k 13 eq rem s 10 0 |
| 38 | end |
| 39 | fn show a view uchar * to void |
| 40 | emit a |
| 41 | if isbn a |
| 42 | emit tgood |
| 43 | else |
| 44 | emit tbad |
| 45 | end |
| 46 | end |
| 47 | fn main to int |
| 48 | show i1 |
| 49 | show i2 |
| 50 | show i3 |
| 51 | show i4 |
| 52 | ret 0 |
| 53 | end |