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 / ackermann-function / ackermann-function.om
| 1 | note Rosetta Code task "Ackermann function": https://rosettacode.org/wiki/Ackermann_function |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text pre "A(" |
| 6 | text cm ", " |
| 7 | text post ") = " |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note emitu writes n in decimal; 20 digits hold every ulong |
| 20 | fn emitu n ulong to void |
| 21 | d = new uchar 20 0 |
| 22 | i = ulong 20 |
| 23 | v = n |
| 24 | while true bound 20 |
| 25 | i = sub i 1 |
| 26 | put d i uchar add rem v 10 48 |
| 27 | v = div v 10 |
| 28 | if eq v 0 |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | emit part d i sub 20 i |
| 33 | end |
| 34 | note recursive, as the task asks; program mode allows recursion, the 8 MiB stack bounds its depth |
| 35 | fn ack m ulong n ulong to ulong |
| 36 | if eq m 0 |
| 37 | ret add n 1 |
| 38 | end |
| 39 | if eq n 0 |
| 40 | ret ack sub m 1 1 |
| 41 | end |
| 42 | ret ack sub m 1 ack m sub n 1 |
| 43 | end |
| 44 | fn main to int |
| 45 | for m ulong 4 |
| 46 | for n ulong 5 |
| 47 | emit pre |
| 48 | emitu m |
| 49 | emit cm |
| 50 | emitu n |
| 51 | emit post |
| 52 | emitu ack m n |
| 53 | emit nl |
| 54 | end |
| 55 | end |
| 56 | ret 0 |
| 57 | end |