sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / harness / examples / structs.om
| 1 | note structs: 8-byte cells in order, no padding; a struct is never a wire layout |
| 2 | note a struct line comes before the first fn |
| 3 | struct Pair a uint b long end |
| 4 | note ---- output helpers: a function works on a global by naming it (a global is never an argument) |
| 5 | global ob uchar 32 |
| 6 | note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90 |
| 7 | note bound: every pass writes at least one byte and n <= 32, so at most 32 passes |
| 8 | fn outb off ulong n ulong to void |
| 9 | e = add off n |
| 10 | p = off |
| 11 | while lt p e bound 32 |
| 12 | k = write 1 ob p sub e p |
| 13 | if le k 0 |
| 14 | exit 90 |
| 15 | end |
| 16 | p = add p ulong k |
| 17 | end |
| 18 | end |
| 19 | note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes |
| 20 | fn putu v ulong to void |
| 21 | i = ulong 31 |
| 22 | put ob 31 10 |
| 23 | n = v |
| 24 | while true bound 20 |
| 25 | i = sub i 1 |
| 26 | put ob i uchar add rem n 10 48 |
| 27 | n = div n 10 |
| 28 | if eq n 0 |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | outb i sub 32 i |
| 33 | end |
| 34 | note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1 |
| 35 | fn putl v long to void |
| 36 | if lt v 0 |
| 37 | put ob 0 45 |
| 38 | outb 0 1 |
| 39 | putu add ulong neg add v 1 1 |
| 40 | ret |
| 41 | end |
| 42 | putu ulong v |
| 43 | end |
| 44 | note swapsum: an edit parameter may be written; a view parameter may not (error 20) |
| 45 | fn swapsum p edit Pair to long |
| 46 | t = long get p a |
| 47 | put p a uint 7 |
| 48 | ret add t get p b |
| 49 | end |
| 50 | fn main to int |
| 51 | p = new Pair 5 -12 |
| 52 | v = swapsum p |
| 53 | putl v |
| 54 | putu ulong get p a |
| 55 | putu sizeof Pair |
| 56 | putu offsetof Pair b |
| 57 | putu sizeof uint |
| 58 | ret 0 |
| 59 | end |