interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / fts / tlib.om

126 lines · 2107 bytes Raw Blame History
1note ===== tests/fts/tlib.om : harness helpers (test only, not product) =====
2note buffered stdout, decimal/hex printing, whole-file reading in chunks
3global tob uchar 1048576
4global tobn ulong 1
5global tib uchar 16777216
6global tibn ulong 1
7global tibp ulong 1
8global targ uchar 4096
9fn t_flush to void
10n = get tobn 0
11o = ulong 0
12while lt o n bound 999999
13w = write 1 tob o sub n o
14if le w 0
15exit 90
16end
17o = add o ulong w
18end
19put tobn 0 0
20end
21fn t_ch c ulong to void
22if ge get tobn 0 1048576
23t_flush
24end
25n = get tobn 0
26put tob n uchar c
27put tobn 0 add n 1
28end
29fn t_dec v ulong to void
30d = new uchar 24 0
31k = ulong 0
32x = v
33while true bound 30
34put d k uchar add 48 rem x 10
35k = add k 1
36x = div x 10
37if eq x 0
38break
39end
40end
41while gt k 0 bound 30
42k = sub k 1
43t_ch ulong get d k
44end
45end
46fn t_sdec v long to void
47if lt v 0
48t_ch 45
49t_dec ulong neg v
50ret
51end
52t_dec ulong v
53end
54fn t_hex v ulong w ulong to void
55i = w
56while gt i 0 bound 20
57i = sub i 1
58h = and shr v mul i 4 15
59if lt h 10
60t_ch add h 48
61else
62t_ch add h 87
63end
64end
65end
66note open argv[k] as a file for reading; returns fd (exits 91 on failure)
67fn t_open k ulong to int
68n = argread k targ 0 4096
69if lt n 0
70exit 91
71end
72fd = fopen targ 0 ulong n 0 0
73if lt fd 0
74exit 92
75end
76ret int fd
77end
78note refill tib from fd keeping unread bytes; returns bytes available
79fn t_fill fd int to ulong
80p = get tibp 0
81n = get tibn 0
82r = sub n p
83for i ulong r
84put tib i get tib add p i
85end
86put tibp 0 0
87put tibn 0 r
88while lt get tibn 0 16777216 bound 999999
89m = get tibn 0
90k = read fd tib m sub 16777216 m
91if lt k 0
92exit 93
93end
94if eq k 0
95break
96end
97put tibn 0 add m ulong k
98end
99ret get tibn 0
100end
101note ensure at least need bytes are buffered (false at end of file)
102fn t_need fd int need ulong to bool
103if ge sub get tibn 0 get tibp 0 need
104ret true
105end
106z = t_fill fd
107ret ge sub get tibn 0 get tibp 0 need
108end
109fn t_u64 to ulong
110p = get tibp 0
111v = ulong 0
112for i ulong 8
113v = or v shl ulong get tib add p i mul i 8
114end
115put tibp 0 add p 8
116ret v
117end
118fn t_u32 to ulong
119p = get tibp 0
120v = ulong 0
121for i ulong 4
122v = or v shl ulong get tib add p i mul i 8
123end
124put tibp 0 add p 4
125ret v
126end