omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / examples / functions.om
| 1 | note functions: at most 6 parameters; a call may name any function in the unit; recursion in program mode |
| 2 | note ---- output helpers: a function works on a global by naming it (a global is never an argument) |
| 3 | global ob uchar 32 |
| 4 | note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90 |
| 5 | note bound: every pass writes at least one byte and n <= 32, so at most 32 passes |
| 6 | fn outb off ulong n ulong to void |
| 7 | e = add off n |
| 8 | p = off |
| 9 | while lt p e bound 32 |
| 10 | k = write 1 ob p sub e p |
| 11 | if le k 0 |
| 12 | exit 90 |
| 13 | end |
| 14 | p = add p ulong k |
| 15 | end |
| 16 | end |
| 17 | note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes |
| 18 | fn putu v ulong to void |
| 19 | i = ulong 31 |
| 20 | put ob 31 10 |
| 21 | n = v |
| 22 | while true bound 20 |
| 23 | i = sub i 1 |
| 24 | put ob i uchar add rem n 10 48 |
| 25 | n = div n 10 |
| 26 | if eq n 0 |
| 27 | break |
| 28 | end |
| 29 | end |
| 30 | outb i sub 32 i |
| 31 | end |
| 32 | note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1 |
| 33 | fn putl v long to void |
| 34 | if lt v 0 |
| 35 | put ob 0 45 |
| 36 | outb 0 1 |
| 37 | putu add ulong neg add v 1 1 |
| 38 | ret |
| 39 | end |
| 40 | putu ulong v |
| 41 | end |
| 42 | fn main to int |
| 43 | putu fact 20 |
| 44 | putu gcd 1071 462 |
| 45 | putu ulong pick 1 2 3 4 5 6 |
| 46 | ret 0 |
| 47 | end |
| 48 | fn fact n ulong to ulong |
| 49 | if le n 1 |
| 50 | ret 1 |
| 51 | end |
| 52 | ret mul n fact sub n 1 |
| 53 | end |
| 54 | fn gcd a ulong b ulong to ulong |
| 55 | x = a |
| 56 | y = b |
| 57 | while ne y 0 bound 200 |
| 58 | t = rem x y |
| 59 | x = y |
| 60 | y = t |
| 61 | end |
| 62 | ret x |
| 63 | end |
| 64 | fn pick a int b int c int d int e int f int to int |
| 65 | ret add add a f mul b sub e d |
| 66 | end |