interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / protocol / synapse / board / src / 00-util.om

344 lines · 6837 bytes Raw Blame History
1note synapse-board src/00-util.om -- shared state, output buffers, escaping, input loading, JSON field access.
2note Inputs live in globals (host words may name globals); each JSONL line is copied into a caller-local
3note 64 KiB line buffer before json.om parses it, because a global is never passed to a function.
4const SBCAP ulong 4194304
5const SBOCAP ulong 8388608
6const SBSLOT ulong 4096
7const SBLINES ulong 999999
8text tbmd "BOARD.md"
9text tidx "index.html"
10text emusage "usage: synapse-board render --claims FILE --map FILE [--findings FILE] --out DIR [--now TS] [--head HEX] | serve --dir DIR --bind IP:PORT [--max N]"
11global gin0 uchar 4194304
12global gin1 uchar 4194304
13global gin2 uchar 4194304
14global glen ulong 3
15global gmd uchar 8388608
16global ght uchar 8388608
17global gol ulong 2
18global gerr ulong 1
19global gpath uchar 40960
20global gplen ulong 10
21global gtmp uchar 4096
22global gnow uchar 64
23global gnowl ulong 1
24global gpkt uchar 4096
25text tamp "&"
26text tlt "<"
27text tgt ">"
28text tquot """
29text tapos "'"
30text tnl "\n"
31text tme "synapse-board: "
32note ======== output: w 0 is BOARD.md, w 1 is index.html
33fn sbput w ulong c uchar to void
34n = get gol w
35if ge n SBOCAP
36put gerr 0 1
37ret
38end
39if eq w 0
40put gmd n c
41else
42put ght n c
43end
44put gol w add n 1
45end
46fn sbraw w ulong s view uchar * to void
47for i ulong len s
48sbput w get s i
49end
50end
51fn sbr2 s view uchar * to void
52sbraw 0 s
53sbraw 1 s
54end
55fn sbnum w ulong v ulong to void
56d = new uchar 20 0
57i = ulong 20
58x = v
59for it ulong 20
60i = sub i 1
61put d i uchar add rem x 10 48
62x = div x 10
63if eq x 0
64break
65end
66end
67for k ulong sub 20 i
68sbput w get d add i k
69end
70end
71fn sbn2 v ulong to void
72sbnum 0 v
73sbnum 1 v
74end
75note sbesc1: one byte, escaped for Markdown (w 0) or HTML (w 1); control bytes become a space
76fn sbesc1 w ulong c uchar to void
77if or lt c 32 eq c 127
78sbput w 32
79ret
80end
81if eq c 38
82sbraw w tamp
83ret
84end
85if eq c 60
86sbraw w tlt
87ret
88end
89if eq c 62
90sbraw w tgt
91ret
92end
93if eq w 1
94if eq c 34
95sbraw w tquot
96ret
97end
98if eq c 39
99sbraw w tapos
100ret
101end
102sbput w c
103ret
104end
105if or or eq c 124 eq c 92 or eq c 96 or eq c 91 eq c 93
106sbput w 92
107end
108sbput w c
109end
110fn sbesc w ulong s view uchar * n ulong to void
111for i ulong n
112sbesc1 w get s i
113end
114end
115fn sbe2 s view uchar * n ulong to void
116sbesc 0 s n
117sbesc 1 s n
118end
119note ======== stderr diagnostics
120fn sbsay s view uchar * to void
121r = write 2 tme 0 len tme
122r = write 2 s 0 len s
123r = write 2 tnl 0 len tnl
124end
125fn sbout s view uchar * n ulong to long
126p = ulong 0
127for it ulong 4096
128if ge p n
129ret 0
130end
131w = write 1 s p sub n p
132if le w 0
133ret -5
134end
135p = add p ulong w
136end
137ret -5
138end
139note ======== arguments: slot s of gpath holds the value of one flag
140fn sbtmpeq n ulong t view uchar * to bool
141if ne n len t
142ret false
143end
144for i ulong n
145if ne get gtmp i get t i
146ret false
147end
148end
149ret true
150end
151fn sbslotc s ulong i ulong to uchar
152ret get gpath add mul s SBSLOT i
153end
154fn sbslotl s ulong to ulong
155ret get gplen s
156end
157note sbdecv: slot s as a decimal number, or 4294967296 when not all digits (or empty or too long)
158fn sbdecv s ulong to ulong
159n = sbslotl s
160if or eq n 0 gt n 9
161ret 4294967296
162end
163v = ulong 0
164for i ulong n
165c = sbslotc s i
166if or lt c 48 gt c 57
167ret 4294967296
168end
169v = add mul v 10 ulong sub c 48
170end
171ret v
172end
173note ======== input files: w 0 claims, 1 map, 2 findings
174fn sbinb w ulong i ulong to uchar
175if eq w 0
176ret get gin0 i
177end
178if eq w 1
179ret get gin1 i
180end
181ret get gin2 i
182end
183fn sbrd w ulong fd int p ulong to long
184if eq w 0
185ret read fd gin0 p sub SBCAP p
186end
187if eq w 1
188ret read fd gin1 p sub SBCAP p
189end
190ret read fd gin2 p sub SBCAP p
191end
192note sbload: read the file named by slot s into input w; 0 ok, -65 too large, else minus errno
193fn sbload w ulong s ulong to long
194put glen w 0
195fd = fopen gpath mul s SBSLOT sbslotl s 524288 0
196if lt fd 0
197ret fd
198end
199p = ulong 0
200for it ulong 999999
201if ge p SBCAP
202c = fclose int fd
203ret -65
204end
205r = sbrd w int fd p
206if lt r 0
207if ne r -4
208c = fclose int fd
209ret r
210end
211else
212if eq r 0
213c = fclose int fd
214put glen w p
215ret 0
216end
217p = add p ulong r
218end
219end
220c = fclose int fd
221ret -65
222end
223note sbline: copy the line of input w starting at off (without its newline) into lb; byte count or -1
224fn sbline w ulong off ulong lb edit uchar 65536 to long
225tot = get glen w
226n = ulong 0
227for k ulong 65537
228p = add off n
229if ge p tot
230ret long n
231end
232c = sbinb w p
233if eq c 10
234ret long n
235end
236if ge n 65536
237ret -1
238end
239put lb n c
240n = add n 1
241end
242ret -1
243end
244note ======== JSON fields of one parsed line (root object token 0)
245fn sbkey lb view uchar 65536 tp view uint 16400 key view uchar * to ulong
246k = js_first tp 0
247for it ulong 4096
248if eq k jsnone
249ret jsnone
250end
251if js_streq lb js_off tp k key
252ret add k 1
253end
254k = js_mnext tp k
255end
256ret jsnone
257end
258note sbgets: decoded string value of key into sb; byte count, or -1 when absent or not a string
259fn sbgets lb view uchar 65536 tp view uint 16400 sb edit uchar 65536 key view uchar * to long
260v = sbkey lb tp key
261if eq v jsnone
262ret -1
263end
264if ne js_type tp v jsstr
265ret -1
266end
267ret js_sget lb js_off tp v sb
268end
269fn sbgetu lb view uchar 65536 tp view uint 16400 key view uchar * to ulong
270v = sbkey lb tp key
271if eq v jsnone
272ret 0
273end
274if ne js_type tp v jsnum
275ret 0
276end
277ret js_tou lb js_off tp v
278end
279fn sbgetb lb view uchar 65536 tp view uint 16400 key view uchar * to bool
280v = sbkey lb tp key
281if eq v jsnone
282ret false
283end
284ret eq js_type tp v jstrue
285end
286note sbfld: append the escaped string value of key to output w (nothing when absent)
287fn sbfld w ulong lb view uchar 65536 tp view uint 16400 sb edit uchar 65536 key view uchar * to void
288n = sbgets lb tp sb key
289if gt n 0
290sbesc w sb ulong n
291end
292end
293note Keep the civil-date intermediates within the pinned compiler's register pool.
294fn sbyear doe ulong to ulong
295v = sub doe div doe 1460
296v = add v div doe 36524
297v = sub v div doe 146096
298ret div v 365
299end
300fn sbdate b edit uchar 64 days ulong to void
301z = add days 719468
302era = div z 146097
303z = sub z mul era 146097
304yoe = sbyear z
305y = add yoe mul era 400
306z = sub z sub add mul 365 yoe div yoe 4 div yoe 100
307mp = div add mul 5 z 2 153
308d = add sub z div add mul 153 mp 2 5 1
309m = add mp 3
310if ge mp 10
311m = sub mp 9
312end
313if le m 2
314y = add y 1
315end
316sbput4 b 0 y
317put b 4 45
318sbput2 b 5 m
319put b 7 45
320sbput2 b 8 d
321end
322note sbiso: Unix seconds as YYYY-MM-DDTHH:MM:SSZ into b[0..20) (days-to-civil, proleptic Gregorian)
323fn sbiso b edit uchar 64 secs ulong to ulong
324sbdate b div secs 86400
325sod = rem secs 86400
326put b 10 84
327sbput2 b 11 div sod 3600
328put b 13 58
329sbput2 b 14 rem div sod 60 60
330put b 16 58
331sbput2 b 17 rem sod 60
332put b 19 90
333ret 20
334end
335fn sbput2 b edit uchar 64 p ulong v ulong to void
336put b p uchar add div rem v 100 10 48
337put b add p 1 uchar add rem v 10 48
338end
339fn sbput4 b edit uchar 64 p ulong v ulong to void
340put b p uchar add rem div v 1000 10 48
341put b add p 1 uchar add rem div v 100 10 48
342put b add p 2 uchar add rem div v 10 10 48
343put b add p 3 uchar add rem v 10 48
344end