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 / core / hive / main.om

645 lines · 11677 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/hive/main.om: Sovereign Agent Hive CLI.
3note Reads hex hive commands line-by-line on stdin and emits state, geometry,
4note routing, and task-lease results on stdout. Exits nonzero on any refused
5note or malformed command.
6note Compose with: si/directives/om2.om core/hive/lib.om core/hive/main.om
7
8text h_cmd_init "INIT"
9text h_cmd_place "PLACE"
10text h_cmd_auto "AUTO"
11text h_cmd_block "BLOCK"
12text h_cmd_unblk "UNBLOCK"
13text h_cmd_dist "DIST"
14text h_cmd_nbrs "NEIGHBORS"
15text h_cmd_ring "RING"
16text h_cmd_route "ROUTE"
17text h_cmd_task "TASK"
18text h_cmd_claim "CLAIM"
19text h_cmd_asgn "ASSIGN"
20text h_cmd_tick "TICK"
21text h_cmd_stat "STAT"
22
23text h_ok_init "OK INIT\n"
24text h_ok_place "OK PLACE aid="
25text h_ok_auto "OK AUTO aid="
26text h_ok_blk "OK BLOCK "
27text h_ok_unblk "OK UNBLOCK "
28text h_ok_dist "OK DIST d="
29text h_ok_nbrs "OK NEIGHBORS"
30text h_ok_ring "OK RING k="
31text h_ok_route "OK ROUTE hops="
32text h_ok_task "OK TASK tid="
33text h_ok_claim "OK CLAIM tid="
34text h_ok_asgn "OK ASSIGN tid="
35text h_ok_tick "OK TICK reclaimed="
36text h_ok_stat "OK STAT agents="
37text h_err_pfx "ERR code="
38text h_sp " "
39text h_lp "("
40text h_cm ","
41text h_rp ")"
42text h_nl "\n"
43text h_k_aid " aid="
44text h_k_q " q="
45text h_k_r " r="
46text h_k_cnt " cells="
47text h_k_blk " blocked="
48text h_k_tk " tasks="
49text h_k_md " max_dist="
50
51fn h_eq_slice a view uchar * b view uchar * to bool
52if ne len a len b
53ret false
54end
55n = len a
56i = ulong 0
57while lt i n bound 65536
58if ne get a i get b i
59ret false
60end
61i = add i 1
62end
63ret true
64end
65
66fn h_is_space c uchar to bool
67if eq c 32
68ret true
69end
70if eq c 9
71ret true
72end
73ret eq c 13
74end
75
76fn h_next_tok line view uchar * pos edit ulong 1 dst edit ulong 2 to bool
77note dst[0]=start, dst[1]=len
78n = len line
79p = get pos 0
80while lt p n bound 65536
81if not h_is_space get line p
82break
83end
84p = add p 1
85end
86if ge p n
87put pos 0 p
88ret false
89end
90st = p
91while lt p n bound 65536
92if h_is_space get line p
93break
94end
95p = add p 1
96end
97put dst 0 st
98put dst 1 sub p st
99put pos 0 p
100ret true
101end
102
103fn h_parse_i32 tok view uchar * dst edit int 1 to bool
104n = len tok
105if eq n 0
106ret false
107end
108if gt n 11
109ret false
110end
111idx = ulong 0
112is_neg = false
113if eq get tok 0 45
114is_neg = true
115idx = ulong 1
116if eq n 1
117ret false
118end
119end
120acc = long 0
121while lt idx n bound 12
122c = get tok idx
123if or lt c 48 gt c 57
124ret false
125end
126acc = add mul acc 10 long sub c 48
127if gt acc 2147483648
128ret false
129end
130idx = add idx 1
131end
132if is_neg
133acc = sub 0 acc
134else
135if gt acc 2147483647
136ret false
137end
138end
139put dst 0 int acc
140ret true
141end
142
143fn h_read_ints line view uchar * pos edit ulong 1 vals edit int 8 want uint to bool
144tok = new ulong 2 0
145v = new int 1 0
146i = uint 0
147while lt i want bound 8
148if not h_next_tok line pos tok
149ret false
150end
151if not h_parse_i32 part line get tok 0 get tok 1 v
152ret false
153end
154put vals ulong i get v 0
155i = add i 1
156end
157ret true
158end
159
160fn h_emit_err code uint to void
161w1 = hive_write_all 1 h_err_pfx
162w2 = hive_write_u64 1 ulong code
163w3 = hive_write_all 1 h_nl
164if and w1 and w2 w3
165ret
166end
167exit 2
168end
169
170fn h_emit_pair q int r int to void
171w1 = hive_write_all 1 h_lp
172w2 = hive_write_i32 1 q
173w3 = hive_write_all 1 h_cm
174w4 = hive_write_i32 1 r
175w5 = hive_write_all 1 h_rp
176if and w1 and w2 and w3 and w4 w5
177ret
178end
179exit 2
180end
181
182fn h_cmd_do_route args view int 8 to bool
183src = new HexCoord get args 0 get args 1
184dst = new HexCoord get args 2 get args 3
185pq = new int 289 0
186pr = new int 289 0
187hops = hive_route src dst pq pr
188if lt hops 0
189h_emit_err uint neg hops
190ret false
191end
192w1 = hive_write_all 1 h_ok_route
193w2 = hive_write_i32 1 hops
194if not and w1 w2
195exit 2
196end
197n = add ulong hops 1
198i = ulong 0
199while lt i n bound 289
200w = hive_write_all 1 h_sp
201if not w
202exit 2
203end
204h_emit_pair get pq i get pr i
205i = add i 1
206end
207wn = hive_write_all 1 h_nl
208if not wn
209exit 2
210end
211ret true
212end
213
214fn h_cmd_do_ring args view int 8 to bool
215ctr = new HexCoord get args 0 get args 1
216rqs = new int 192 0
217rrs = new int 192 0
218if lt get args 2 0
219h_emit_err HIVE_ERR_BAD_ARG
220ret false
221end
222k = uint get args 2
223cnt = hive_ring ctr k rqs rrs
224if eq cnt 0
225h_emit_err HIVE_ERR_BAD_ARG
226ret false
227end
228w1 = hive_write_all 1 h_ok_ring
229w2 = hive_write_u64 1 ulong k
230w3 = hive_write_all 1 h_k_cnt
231w4 = hive_write_u64 1 ulong cnt
232if not and w1 and w2 and w3 w4
233exit 2
234end
235i = ulong 0
236while lt i ulong cnt bound 192
237w = hive_write_all 1 h_sp
238if not w
239exit 2
240end
241h_emit_pair get rqs i get rrs i
242i = add i 1
243end
244wn = hive_write_all 1 h_nl
245if not wn
246exit 2
247end
248ret true
249end
250
251fn h_cmd_do_nbrs args view int 8 to bool
252qs = new int 6 0
253rs = new int 6 0
254hive_neighbors get args 0 get args 1 qs rs
255w0 = hive_write_all 1 h_ok_nbrs
256if not w0
257exit 2
258end
259for i ulong 6
260ws = hive_write_all 1 h_sp
261if not ws
262exit 2
263end
264h_emit_pair get qs i get rs i
265end
266wn = hive_write_all 1 h_nl
267if not wn
268exit 2
269end
270ret true
271end
272
273fn h_must ok bool to void
274if not ok
275exit 2
276end
277end
278
279fn h_cmd_do_stat to bool
280bd = new HiveBounds 0 0 0 0 0 0
281hive_compute_bounds bd
282h_must hive_write_all 1 h_ok_stat
283h_must hive_write_u64 1 ulong get bd cnt
284h_must hive_write_all 1 h_k_blk
285h_must hive_write_i32 1 get g_meta 5
286h_must hive_write_all 1 h_k_tk
287h_must hive_write_i32 1 get g_meta 4
288h_must hive_write_all 1 h_k_md
289h_must hive_write_i32 1 get bd max_dist
290h_must hive_write_all 1 h_nl
291ret true
292end
293
294fn h_exec_geom cmd view uchar * line view uchar * pos edit ulong 1 to int
295note returns 0=not handled, 1=ok, -1=err
296args = new int 8 0
297if h_eq_slice cmd h_cmd_dist
298if not h_read_ints line pos args 4
299h_emit_err HIVE_ERR_BAD_ARG
300ret -1
301end
302d = hive_hex_dist get args 0 get args 1 get args 2 get args 3
303w1 = hive_write_all 1 h_ok_dist
304w2 = hive_write_i32 1 d
305w3 = hive_write_all 1 h_nl
306if and w1 and w2 w3
307ret 1
308end
309exit 2
310end
311if h_eq_slice cmd h_cmd_nbrs
312if not h_read_ints line pos args 2
313h_emit_err HIVE_ERR_BAD_ARG
314ret -1
315end
316if h_cmd_do_nbrs args
317ret 1
318end
319ret -1
320end
321if h_eq_slice cmd h_cmd_ring
322if not h_read_ints line pos args 3
323h_emit_err HIVE_ERR_BAD_ARG
324ret -1
325end
326if h_cmd_do_ring args
327ret 1
328end
329ret -1
330end
331if h_eq_slice cmd h_cmd_route
332if not h_read_ints line pos args 4
333h_emit_err HIVE_ERR_BAD_ARG
334ret -1
335end
336if h_cmd_do_route args
337ret 1
338end
339ret -1
340end
341ret 0
342end
343
344fn h_exec_grid cmd view uchar * line view uchar * pos edit ulong 1 to int
345args = new int 8 0
346pt = new HexCoord 0 0
347if h_eq_slice cmd h_cmd_init
348if not h_read_ints line pos args 3
349h_emit_err HIVE_ERR_BAD_ARG
350ret -1
351end
352if or lt get args 1 1 lt get args 2 1
353h_emit_err HIVE_ERR_BAD_ARG
354ret -1
355end
356rc = hive_init get args 0 uint get args 1 uint get args 2
357if ne rc HIVE_OK
358h_emit_err rc
359ret -1
360end
361if hive_write_all 1 h_ok_init
362ret 1
363end
364exit 2
365end
366if h_eq_slice cmd h_cmd_place
367if not h_read_ints line pos args 5
368h_emit_err HIVE_ERR_BAD_ARG
369ret -1
370end
371if lt get args 0 1
372h_emit_err HIVE_ERR_BAD_ARG
373ret -1
374end
375if or lt get args 3 0 gt get args 3 255
376h_emit_err HIVE_ERR_BAD_ARG
377ret -1
378end
379if lt get args 4 1
380h_emit_err HIVE_ERR_BAD_ARG
381ret -1
382end
383rc = hive_place_agent uint get args 0 get args 1 get args 2 uchar get args 3 uint get args 4
384if ne rc HIVE_OK
385h_emit_err rc
386ret -1
387end
388h_must hive_write_all 1 h_ok_place
389h_must hive_write_i32 1 get args 0
390h_must hive_write_all 1 h_k_q
391h_must hive_write_i32 1 get args 1
392h_must hive_write_all 1 h_k_r
393h_must hive_write_i32 1 get args 2
394h_must hive_write_all 1 h_nl
395ret 1
396end
397if h_eq_slice cmd h_cmd_auto
398if not h_read_ints line pos args 4
399h_emit_err HIVE_ERR_BAD_ARG
400ret -1
401end
402if or lt get args 0 1 lt get args 1 0
403h_emit_err HIVE_ERR_BAD_ARG
404ret -1
405end
406if or lt get args 2 0 gt get args 2 255
407h_emit_err HIVE_ERR_BAD_ARG
408ret -1
409end
410if lt get args 3 1
411h_emit_err HIVE_ERR_BAD_ARG
412ret -1
413end
414rc = hive_place_auto uint get args 0 uint get args 1 uchar get args 2 uint get args 3 pt
415if ne rc HIVE_OK
416h_emit_err rc
417ret -1
418end
419h_must hive_write_all 1 h_ok_auto
420h_must hive_write_i32 1 get args 0
421h_must hive_write_all 1 h_k_q
422h_must hive_write_i32 1 get pt q
423h_must hive_write_all 1 h_k_r
424h_must hive_write_i32 1 get pt r
425h_must hive_write_all 1 h_nl
426ret 1
427end
428if h_eq_slice cmd h_cmd_block
429if not h_read_ints line pos args 2
430h_emit_err HIVE_ERR_BAD_ARG
431ret -1
432end
433rc = hive_block_cell get args 0 get args 1
434if ne rc HIVE_OK
435h_emit_err rc
436ret -1
437end
438w1 = hive_write_all 1 h_ok_blk
439if not w1
440exit 2
441end
442h_emit_pair get args 0 get args 1
443if hive_write_all 1 h_nl
444ret 1
445end
446exit 2
447end
448if h_eq_slice cmd h_cmd_unblk
449if not h_read_ints line pos args 2
450h_emit_err HIVE_ERR_BAD_ARG
451ret -1
452end
453rc = hive_unblock_cell get args 0 get args 1
454if ne rc HIVE_OK
455h_emit_err rc
456ret -1
457end
458w1 = hive_write_all 1 h_ok_unblk
459if not w1
460exit 2
461end
462h_emit_pair get args 0 get args 1
463if hive_write_all 1 h_nl
464ret 1
465end
466exit 2
467end
468ret 0
469end
470
471fn h_exec_task cmd view uchar * line view uchar * pos edit ulong 1 to int
472args = new int 8 0
473asg = new uint 1 0
474if h_eq_slice cmd h_cmd_task
475if not h_read_ints line pos args 4
476h_emit_err HIVE_ERR_BAD_ARG
477ret -1
478end
479if or lt get args 0 1 lt get args 3 0
480h_emit_err HIVE_ERR_BAD_ARG
481ret -1
482end
483rc = hive_task_create uint get args 0 get args 1 get args 2 uint get args 3
484if ne rc HIVE_OK
485h_emit_err rc
486ret -1
487end
488w1 = hive_write_all 1 h_ok_task
489w2 = hive_write_i32 1 get args 0
490w3 = hive_write_all 1 h_nl
491if and w1 and w2 w3
492ret 1
493end
494exit 2
495end
496if h_eq_slice cmd h_cmd_claim
497if not h_read_ints line pos args 3
498h_emit_err HIVE_ERR_BAD_ARG
499ret -1
500end
501if or lt get args 0 1 lt get args 1 1
502h_emit_err HIVE_ERR_BAD_ARG
503ret -1
504end
505if lt get args 2 1
506h_emit_err HIVE_ERR_BAD_ARG
507ret -1
508end
509rc = hive_task_claim uint get args 0 uint get args 1 ulong get args 2
510if ne rc HIVE_OK
511h_emit_err rc
512ret -1
513end
514h_must hive_write_all 1 h_ok_claim
515h_must hive_write_i32 1 get args 0
516h_must hive_write_all 1 h_k_aid
517h_must hive_write_i32 1 get args 1
518h_must hive_write_all 1 h_nl
519ret 1
520end
521if h_eq_slice cmd h_cmd_asgn
522if not h_read_ints line pos args 2
523h_emit_err HIVE_ERR_BAD_ARG
524ret -1
525end
526if or lt get args 0 1 lt get args 1 1
527h_emit_err HIVE_ERR_BAD_ARG
528ret -1
529end
530rc = hive_assign_nearest uint get args 0 ulong get args 1 asg
531if ne rc HIVE_OK
532h_emit_err rc
533ret -1
534end
535h_must hive_write_all 1 h_ok_asgn
536h_must hive_write_i32 1 get args 0
537h_must hive_write_all 1 h_k_aid
538h_must hive_write_u64 1 ulong get asg 0
539h_must hive_write_all 1 h_nl
540ret 1
541end
542if h_eq_slice cmd h_cmd_tick
543if not h_read_ints line pos args 1
544h_emit_err HIVE_ERR_BAD_ARG
545ret -1
546end
547if lt get args 0 0
548h_emit_err HIVE_ERR_BAD_ARG
549ret -1
550end
551rec = hive_tick ulong get args 0
552h_must hive_write_all 1 h_ok_tick
553h_must hive_write_u64 1 ulong rec
554h_must hive_write_all 1 h_nl
555ret 1
556end
557if h_eq_slice cmd h_cmd_stat
558if h_cmd_do_stat
559ret 1
560end
561ret -1
562end
563ret 0
564end
565
566fn h_dispatch_line line view uchar * to bool
567pos = new ulong 1 0
568tok = new ulong 2 0
569cbuf = new uchar 16 0
570if not h_next_tok line pos tok
571ret true
572end
573cs = get tok 0
574cl = get tok 1
575if eq get line cs 35
576ret true
577end
578if gt cl 16
579h_emit_err HIVE_ERR_BAD_ARG
580ret false
581end
582i = ulong 0
583while lt i cl bound 16
584put cbuf i get line add cs i
585i = add i 1
586end
587r1 = h_exec_geom part cbuf 0 cl line pos
588if ne r1 0
589ret gt r1 0
590end
591r2 = h_exec_grid part cbuf 0 cl line pos
592if ne r2 0
593ret gt r2 0
594end
595r3 = h_exec_task part cbuf 0 cl line pos
596if ne r3 0
597ret gt r3 0
598end
599h_emit_err HIVE_ERR_BAD_ARG
600ret false
601end
602
603fn h_find_nl buf view uchar * start ulong to ulong
604n = len buf
605p = start
606while lt p n bound 65536
607if eq get buf p 10
608ret p
609end
610p = add p 1
611end
612ret n
613end
614
615fn main to int
616raw = new uchar 65536 0
617rc0 = hive_init 6 64 64
618if ne rc0 HIVE_OK
619ret 2
620end
621tot = ulong 0
622while lt tot 65536 bound 65536
623k = read 0 raw tot sub 65536 tot
624if lt k 0
625ret 2
626end
627if eq k 0
628break
629end
630tot = add tot ulong k
631end
632had_err = false
633pos = ulong 0
634while lt pos tot bound 65536
635lend = h_find_nl part raw 0 tot pos
636if not h_dispatch_line part raw pos sub lend pos
637had_err = true
638end
639pos = add lend 1
640end
641if had_err
642ret 1
643end
644ret 0
645end