interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / harness / examples / stdin_count.om

71 lines · 1508 bytes Raw Blame History
1note stdin_count: read stdin to its end in 7-byte reads, counting bytes and lines
2note read may return fewer bytes than asked, 0 at the end, or minus the errno: handle all three
3note ---- output helpers: a function works on a global by naming it (a global is never an argument)
4global ob uchar 32
5note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
6note bound: every pass writes at least one byte and n <= 32, so at most 32 passes
7fn outb off ulong n ulong to void
8e = add off n
9p = off
10while lt p e bound 32
11k = write 1 ob p sub e p
12if le k 0
13exit 90
14end
15p = add p ulong k
16end
17end
18note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
19fn putu v ulong to void
20i = ulong 31
21put ob 31 10
22n = v
23while true bound 20
24i = sub i 1
25put ob i uchar add rem n 10 48
26n = div n 10
27if eq n 0
28break
29end
30end
31outb i sub 32 i
32end
33note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
34fn putl v long to void
35if lt v 0
36put ob 0 45
37outb 0 1
38putu add ulong neg add v 1 1
39ret
40end
41putu ulong v
42end
43global buf uchar 7
44fn main to int
45bytes = ulong 0
46lines = ulong 0
47sum = ulong 0
48note bound: every pass but the last reads at least one byte; this program accepts at most 65536 bytes
49while true bound 65537
50k = read 0 buf 0 7
51if lt k 0
52exit 91
53end
54if eq k 0
55break
56end
57n = ulong k
58for i ulong n
59c = get buf i
60sum = add sum ulong c
61if eq c 10
62lines = add lines 1
63end
64end
65bytes = add bytes n
66end
67putu bytes
68putu lines
69putu sum
70ret 0
71end