interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / omega-reference / vendor / examples / echo_args.om

72 lines · 1492 bytes Raw Blame History
1note echo_args: argcnt includes the program name; argread copies one argument without a terminator
2note ---- output helpers: a function works on a global by naming it (a global is never an argument)
3global ob uchar 32
4note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
5note bound: every pass writes at least one byte and n <= 32, so at most 32 passes
6fn outb off ulong n ulong to void
7e = add off n
8p = off
9while lt p e bound 32
10k = write 1 ob p sub e p
11if le k 0
12exit 90
13end
14p = add p ulong k
15end
16end
17note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
18fn putu v ulong to void
19i = ulong 31
20put ob 31 10
21n = v
22while true bound 20
23i = sub i 1
24put ob i uchar add rem n 10 48
25n = div n 10
26if eq n 0
27break
28end
29end
30outb i sub 32 i
31end
32note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
33fn putl v long to void
34if lt v 0
35put ob 0 45
36outb 0 1
37putu add ulong neg add v 1 1
38ret
39end
40putu ulong v
41end
42global ab uchar 32
43text toolong "(longer than 31 bytes)\n"
44fn main to int
45n = argcnt
46putl n
47i = ulong 1
48while lt long i n bound 64
49k = argread i ab 0 31
50if eq k -105
51w = write 1 toolong 0 len toolong
52else
53m = ulong k
54put ab m 10
55outbuf m
56end
57i = add i 1
58end
59ret 0
60end
61note outbuf: write ab[0 .. m] (the argument and its newline); exit 90 on an error
62fn outbuf m ulong to void
63e = add m 1
64p = ulong 0
65while lt p e bound 32
66k = write 1 ab p sub e p
67if le k 0
68exit 90
69end
70p = add p ulong k
71end
72end