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 / map / src / 00-base.om

770 lines · 13396 bytes Raw Blame History
1note synapse-map: repo graph, routing, scaffolding and checks for the agents working on aien-dev.
2note Composed after seed/json/json.om, seed/json/jsonw.om and seed/net/http.om under one om2 line by
3note build.sh. Signatures use unsized arrays (`*`): at this omc pin a const defined after any fn breaks
4note later signatures that name a const as an array length, and every library fn precedes these files.
5note Name prefixes: functions sm_*, globals g*, texts t*. Library names (js_*, h*) are not reused.
6
7note ======== state ========
8note gst slots (ulong): see the const block below.
9global gst ulong 64
10note gpool: every string the program keeps (records, rules, skills, tokens, paths)
11global gpool uchar 8388608
12note gout: stdout and file output; also request bodies (written after the pending output, then cut)
13global gout uchar 4194304
14note gfile: the last file or HTTP response body read
15global gfile uchar 2097152
16note records: 16 string fields (offset, length) and 16 ulong flags per record, at most 1000 records
17global gro ulong 16384
18global grl ulong 16384
19global grf ulong 16384
20note arguments: bytes and (offset, length) pairs, at most 64 arguments
21global gab uchar 65536
22global gao ulong 128
23global gpath uchar 4096
24global gpk uchar 4096
25global gnum uchar 32
26
27const SPOOL ulong 0
28const SNREC ulong 1
29const SOUT ulong 2
30const SNRUL ulong 3
31const SNSKL ulong 4
32const SNTOK ulong 5
33const SXIP ulong 6
34const SXPORT ulong 7
35const SORGO ulong 8
36const SORGL ulong 9
37const SARGC ulong 10
38const SHOMO ulong 11
39const SHOML ulong 12
40const SXST ulong 13
41const SOVF ulong 14
42const SFLEN ulong 15
43const SPLEN ulong 16
44const SNXR ulong 17
45const SCTYP ulong 18
46const SSLEN ulong 19
47const SIDXO ulong 20
48const SIDXL ulong 21
49const SBINO ulong 22
50const SBINL ulong 23
51const SBAD ulong 24
52const SNONE ulong 4294967295
53
54note record string fields
55const FREPO ulong 0
56const FPAR ulong 1
57const FPST ulong 2
58const FPUR ulong 3
59const FLAY ulong 4
60const FDEF ulong 5
61const FPUSH ulong 6
62const FBRF ulong 7
63const FSRC ulong 8
64const FRULE ulong 9
65const FNAME ulong 10
66const FTOP ulong 11
67const FURL ulong 12
68const FCPUR ulong 13
69const FCLAY ulong 14
70const FCPAR ulong 15
71note record flags
72const KARCH ulong 0
73const KFORK ulong 1
74const KBNUL ulong 2
75const KPNUL ulong 3
76const KPIDX ulong 4
77const KVIS ulong 5
78const KRUL ulong 6
79const KCONF ulong 7
80const KSEM ulong 8
81const KSEMS ulong 9
82
83note ======== output buffer ========
84fn sm_ob c uchar to void
85n = get gst SOUT
86if ge n len gout
87put gst SOVF 1
88ret
89end
90put gout n c
91put gst SOUT add n 1
92end
93
94fn sm_ot t view uchar * to void
95for i ulong len t
96sm_ob get t i
97end
98end
99
100fn sm_op o ulong n ulong to void
101for i ulong n
102sm_ob get gpool add o i
103end
104end
105
106fn sm_hexd v ulong to uchar
107if lt v 10
108ret uchar add v 48
109end
110ret uchar add v 87
111end
112
113note sm_oesc: one byte of a JSON string body (RFC 8259 section 7); bytes >= 0x20 other than " and \ pass
114fn sm_oesc c uchar to void
115if eq c 34
116sm_ob 92
117sm_ob 34
118ret
119end
120if eq c 92
121sm_ob 92
122sm_ob 92
123ret
124end
125if lt c 32
126sm_ob 92
127if eq c 10
128sm_ob 110
129ret
130end
131if eq c 9
132sm_ob 116
133ret
134end
135if eq c 13
136sm_ob 114
137ret
138end
139sm_ob 117
140sm_ob 48
141sm_ob 48
142sm_ob sm_hexd ulong shr c 4
143sm_ob sm_hexd ulong and c 15
144ret
145end
146sm_ob c
147end
148
149fn sm_oq o ulong n ulong to void
150sm_ob 34
151for i ulong n
152sm_oesc get gpool add o i
153end
154sm_ob 34
155end
156
157fn sm_otq t view uchar * to void
158sm_ob 34
159for i ulong len t
160sm_oesc get t i
161end
162sm_ob 34
163end
164
165fn sm_on v ulong to void
166if eq v 0
167sm_ob 48
168ret
169end
170k = ulong 0
171x = v
172while gt x 0 bound 21
173put gnum k uchar add rem x 10 48
174x = div x 10
175k = add k 1
176end
177for i ulong k
178sm_ob get gnum sub sub k i 1
179end
180end
181
182note sm_omil: v thousandths as a JSON number, e.g. 667 -> 0.667
183fn sm_omil v ulong to void
184sm_on div v 1000
185sm_ob 46
186f = rem v 1000
187sm_ob uchar add div f 100 48
188sm_ob uchar add rem div f 10 10 48
189sm_ob uchar add rem f 10 48
190end
191
192note sm_ok: `"name":` ; sm_okc: `,"name":`
193fn sm_ok t view uchar * to void
194sm_ob 34
195sm_ot t
196sm_ob 34
197sm_ob 58
198end
199
200fn sm_okc t view uchar * to void
201sm_ob 44
202sm_ok t
203end
204
205fn sm_obool b bool to void
206if b
207sm_ob 116
208sm_ob 114
209sm_ob 117
210sm_ob 101
211ret
212end
213sm_ob 102
214sm_ob 97
215sm_ob 108
216sm_ob 115
217sm_ob 101
218end
219
220fn sm_onull to void
221sm_ob 110
222sm_ob 117
223sm_ob 108
224sm_ob 108
225end
226
227note sm_flush: write gout[0, SOUT) to fd; every pass writes at least one byte or returns
228fn sm_flush fd int to long
229n = get gst SOUT
230p = ulong 0
231while lt p n bound 999999
232k = write fd gout p sub n p
233if le k 0
234put gst SOUT 0
235ret -5
236end
237p = add p ulong k
238end
239put gst SOUT 0
240ret 0
241end
242
243fn sm_err t view uchar * to void
244k = write 2 t 0 len t
245end
246
247fn sm_errp o ulong n ulong to void
248if gt n 0
249k = write 2 gpool o n
250end
251end
252
253fn sm_errnl to void
254k = write 2 gnum 0 0
255put gnum 0 10
256k = write 2 gnum 0 1
257end
258
259text tovf "synapse-map: internal buffer full\n"
260
261note An empty text is an unsized name at omc 2c824805 (nm_len 0 marks `view T *`): its len reads a count
262note register that nothing set, so `text t ""` gives garbage lengths (omega-mv #775, fixed after this pin
263note in e2662fff). The empty view is `part tempty 0 0`; keep it until omega.pin moves past that fix.
264text tempty "."
265
266note sm_die: flush pending stdout, then end the process with code
267fn sm_die code int to void
268if ne get gst SOVF 0
269put gst SOUT 0
270k = write 2 tovf 0 len tovf
271exit 70
272end
273r = sm_flush 1
274exit code
275end
276
277note ======== string pool ========
278fn sm_pb c uchar to void
279o = get gst SPOOL
280if ge o len gpool
281put gst SOVF 2
282ret
283end
284put gpool o c
285put gst SPOOL add o 1
286end
287
288fn sm_pt t view uchar * to ulong
289o = get gst SPOOL
290for i ulong len t
291sm_pb get t i
292end
293ret o
294end
295
296fn sm_pcp o ulong n ulong to ulong
297s = get gst SPOOL
298for i ulong n
299sm_pb get gpool add o i
300end
301ret s
302end
303
304fn sm_pnum v ulong to void
305if eq v 0
306sm_pb 48
307ret
308end
309k = ulong 0
310x = v
311while gt x 0 bound 21
312put gnum k uchar add rem x 10 48
313x = div x 10
314k = add k 1
315end
316for i ulong k
317sm_pb get gnum sub sub k i 1
318end
319end
320
321fn sm_lc c uchar to uchar
322if and ge c 65 le c 90
323ret add c 32
324end
325ret c
326end
327
328fn sm_peq a ulong an ulong b ulong bn ulong to bool
329if ne an bn
330ret false
331end
332for i ulong an
333if ne get gpool add a i get gpool add b i
334ret false
335end
336end
337ret true
338end
339
340note sm_pieq: ASCII case-insensitive equality (GitHub owner/repo names)
341fn sm_pieq a ulong an ulong b ulong bn ulong to bool
342if ne an bn
343ret false
344end
345for i ulong an
346if ne sm_lc get gpool add a i sm_lc get gpool add b i
347ret false
348end
349end
350ret true
351end
352
353fn sm_pteq a ulong an ulong t view uchar * to bool
354if ne an len t
355ret false
356end
357for i ulong an
358if ne get gpool add a i get t i
359ret false
360end
361end
362ret true
363end
364
365note sm_pfind: first index of byte c in pool slice, or n
366fn sm_pfind a ulong n ulong c uchar to ulong
367for i ulong n
368if eq get gpool add a i c
369ret i
370end
371end
372ret n
373end
374
375note ======== records ========
376fn sm_fo r ulong f ulong to ulong
377ret get gro add mul r 16 f
378end
379
380fn sm_fl r ulong f ulong to ulong
381ret get grl add mul r 16 f
382end
383
384fn sm_fset r ulong f ulong o ulong n ulong to void
385put gro add mul r 16 f o
386put grl add mul r 16 f n
387end
388
389fn sm_fsett r ulong f ulong t view uchar * to void
390o = sm_pt t
391sm_fset r f o len t
392end
393
394fn sm_kg r ulong k ulong to ulong
395ret get grf add mul r 16 k
396end
397
398fn sm_ks r ulong k ulong v ulong to void
399put grf add mul r 16 k v
400end
401
402fn sm_fq r ulong f ulong to void
403sm_oq sm_fo r f sm_fl r f
404end
405
406fn sm_feqt r ulong f ulong t view uchar * to bool
407ret sm_pteq sm_fo r f sm_fl r f t
408end
409
410note sm_find: record whose repo equals pool slice (case-insensitive), or SNONE
411fn sm_find o ulong n ulong to ulong
412for r ulong get gst SNREC
413if sm_pieq sm_fo r FREPO sm_fl r FREPO o n
414ret r
415end
416end
417ret SNONE
418end
419
420note sm_parref: length of the owner/repo part of a parent string (bytes before '@')
421fn sm_parref r ulong to ulong
422ret sm_pfind sm_fo r FPAR sm_fl r FPAR 64
423end
424
425note ======== arguments ========
426fn sm_args to long
427n = argcnt
428if gt n 64
429ret -7
430end
431p = ulong 0
432for i ulong ulong n
433k = argread i gab p sub len gab p
434if lt k 0
435ret k
436end
437put gao mul i 2 p
438put gao add mul i 2 1 ulong k
439p = add p ulong k
440end
441put gst SARGC ulong n
442ret 0
443end
444
445fn sm_ao i ulong to ulong
446ret get gao mul i 2
447end
448
449fn sm_al i ulong to ulong
450ret get gao add mul i 2 1
451end
452
453fn sm_aeq i ulong t view uchar * to bool
454if ge i get gst SARGC
455ret false
456end
457o = sm_ao i
458n = sm_al i
459if ne n len t
460ret false
461end
462for j ulong n
463if ne get gab add o j get t j
464ret false
465end
466end
467ret true
468end
469
470note sm_aflag: index of flag t at argument 2 or later, 0 when absent
471fn sm_aflag t view uchar * to ulong
472for i ulong get gst SARGC
473if ge i 2
474if sm_aeq i t
475ret i
476end
477end
478end
479ret 0
480end
481
482text tnoval "synapse-map: missing value for "
483
484note sm_aval: index of the value after flag t, 0 when the flag is absent; a flag without value is usage 64
485fn sm_aval t view uchar * to ulong
486i = sm_aflag t
487if eq i 0
488ret 0
489end
490if ge add i 1 get gst SARGC
491sm_err tnoval
492sm_err t
493sm_errnl
494sm_die 64
495end
496ret add i 1
497end
498
499fn sm_apool i ulong to ulong
500o = get gst SPOOL
501n = sm_al i
502a = sm_ao i
503for j ulong n
504sm_pb get gab add a j
505end
506put gst SSLEN n
507ret o
508end
509
510note sm_wmatch: argument bytes gab[o, o + n) equal w[s, s + n)
511fn sm_wmatch o ulong n ulong w view uchar * s ulong to bool
512for q ulong n
513if ne get gab add o q get w add s q
514ret false
515end
516end
517ret true
518end
519
520note sm_inlist: argument i equals one of the space-separated words of w
521fn sm_inlist i ulong w view uchar * to bool
522o = sm_ao i
523n = sm_al i
524s = ulong 0
525for j ulong add len w 1
526e = eq j len w
527if not e
528e = eq get w j 32
529end
530if e
531if eq sub j s n
532if sm_wmatch o n w s
533ret true
534end
535end
536s = add j 1
537end
538end
539ret false
540end
541
542text tunk "synapse-map: unknown flag "
543
544note sm_argscan: every argument after the subcommand is a value flag (vf) with its value, a switch (sw)
545note or a positional; returns the number of positionals (first index in SSLEN), usage 64 on unknown flags
546fn sm_argscan vf view uchar * sw view uchar * to ulong
547np = ulong 0
548put gst SSLEN 0
549i = ulong 2
550while lt i get gst SARGC bound 70
551o = sm_ao i
552isf = false
553if ge sm_al i 2
554if eq get gab o 45
555if eq get gab add o 1 45
556isf = true
557end
558end
559end
560if isf
561if sm_inlist i vf
562i = add i 2
563else
564if sm_inlist i sw
565i = add i 1
566else
567sm_err tunk
568k = write 2 gab o sm_al i
569sm_errnl
570sm_die 64
571end
572end
573else
574if eq np 0
575put gst SSLEN i
576end
577np = add np 1
578i = add i 1
579end
580end
581ret np
582end
583
584note ======== paths ========
585fn sm_pclr to void
586put gst SPLEN 0
587end
588
589fn sm_pc c uchar to void
590n = get gst SPLEN
591if ge n len gpath
592put gst SOVF 3
593ret
594end
595put gpath n c
596put gst SPLEN add n 1
597end
598
599fn sm_ptx t view uchar * to void
600for i ulong len t
601sm_pc get t i
602end
603end
604
605fn sm_ppool o ulong n ulong to void
606for i ulong n
607sm_pc get gpool add o i
608end
609end
610
611fn sm_parg i ulong to void
612o = sm_ao i
613for j ulong sm_al i
614sm_pc get gab add o j
615end
616end
617
618note sm_phome: gpath = home "/" t
619fn sm_phome t view uchar * to void
620sm_pclr
621sm_ppool get gst SHOMO get gst SHOML
622sm_pc 47
623sm_ptx t
624end
625
626note sm_pbin: gpath = directory of argv[0] "/" t
627fn sm_pbin t view uchar * to void
628sm_pclr
629sm_ppool get gst SBINO get gst SBINL
630sm_pc 47
631sm_ptx t
632end
633
634note sm_envpre: environment entry b starts with t
635fn sm_envpre b view uchar 8192 t view uchar * to bool
636for j ulong len t
637if ne get b j get t j
638ret false
639end
640end
641ret true
642end
643
644note sm_env: value of the environment entry starting with t (e.g. "HOME=") into the pool, or SNONE
645fn sm_env t view uchar * to ulong
646b = new uchar 8192 0
647n = envcnt
648for i ulong ulong n
649k = envread i b 0 8192
650if ge k long len t
651if sm_envpre b t
652o = get gst SPOOL
653for j ulong sub ulong k len t
654sm_pb get b add len t j
655end
656put gst SSLEN sub ulong k len t
657ret o
658end
659end
660end
661ret SNONE
662end
663
664note sm_envkv: the whole environment entry "t<value>" (t ends in '=') into the pool; SNONE when absent
665fn sm_envkv t view uchar * to ulong
666o = sm_pt t
667v = sm_env t
668if eq v SNONE
669ret SNONE
670end
671put gst SSLEN add get gst SSLEN len t
672ret o
673end
674
675note ======== files ========
676note sm_rdfile: gpath into gfile; length, or -errno; -27 when the file is larger than gfile
677fn sm_rdfile to long
678x = new uchar 1 0
679fd = fopen gpath 0 get gst SPLEN 524288 0
680if lt fd 0
681ret fd
682end
683p = ulong 0
684for b ulong 2097153
685if ge p len gfile
686break
687end
688k = read int fd gfile p sub len gfile p
689if lt k 0
690c = fclose int fd
691ret k
692end
693if eq k 0
694c = fclose int fd
695put gst SFLEN p
696ret long p
697end
698p = add p ulong k
699end
700k = read int fd x 0 1
701c = fclose int fd
702if ne k 0
703ret -27
704end
705put gst SFLEN p
706ret long p
707end
708
709note sm_wratom: gout[0, SOUT) into gpath ".tmp", fsync, then rename over gpath (atomic replace)
710fn sm_wratom to long
711n = get gst SPLEN
712if gt add mul n 2 12 len gpk
713ret -36
714end
715put gpk 0 2
716put gpk 1 0
717put gpk 2 uchar and add n 4 255
718put gpk 3 uchar shr add n 4 8
719for i ulong n
720put gpk add 4 i get gpath i
721end
722put gpk add 4 n 46
723put gpk add 5 n 116
724put gpk add 6 n 109
725put gpk add 7 n 112
726q = add n 8
727put gpk q uchar and n 255
728put gpk add q 1 uchar shr n 8
729for i ulong n
730put gpk add add q 2 i get gpath i
731end
732fd = fopen gpk 4 add n 4 524865 420
733if lt fd 0
734put gst SOUT 0
735ret fd
736end
737r = sm_flush int fd
738s = fsync int fd
739c = fclose int fd
740r = hfe r hfe s c
741if lt r 0
742ret r
743end
744ret renames gpk 0 add q add n 2 0
745end
746
747note sm_wrnew: gout[0, SOUT) into gpath, refusing to replace an existing file (O_EXCL)
748fn sm_wrnew to long
749fd = fopen gpath 0 get gst SPLEN 524993 420
750if lt fd 0
751put gst SOUT 0
752ret fd
753end
754r = sm_flush int fd
755c = fclose int fd
756ret hfe r c
757end
758
759note sm_mkdirp: create gpath and its missing parents (errors surface at the next open)
760fn sm_mkdirp to void
761n = get gst SPLEN
762for i ulong n
763if gt i 0
764if eq get gpath i 47
765r = makedir gpath 0 i 493
766end
767end
768end
769r = makedir gpath 0 n 493
770end