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 / src / adapters / zig-u64-import.om

992 lines · 14923 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Handwritten bounded Zig 0.17 u64 wrapping-expression importer, profile zig-u64-wrapping-pure-v1.
3note No Zig compiler, parser library or host fallback. Spec: spec/universal/zig-u64-v1.md.
4note SHA256 definitions are the unchanged, pinned source-aware vendor core prepended at build.
5global Src uchar 4096
6global Extra uchar 1
7global Out uchar 8192
8global Slen ulong 1
9global Cur ulong 1
10global Tk ulong 1
11global Ts ulong 1
12global Te ulong 1
13global Tv ulong 1
14global Npar ulong 1
15global Fnoff ulong 1
16global Fnlen ulong 1
17global Poff ulong 6
18global Plen ulong 6
19global Pref ulong 6
20global Nop ulong 1
21global Beg ulong 64
22global Ends ulong 64
23global Nu ulong 64
24text Fixed "u64 pub fn return as _"
25text Keys "addrspace align allowzero and anyframe anytype asm break callconv catch comptime const continue defer else enum errdefer error export extern fn for if inline noalias noinline nosuspend opaque or orelse packed pub resume return linksection struct suspend switch test threadlocal try union unreachable var volatile while async await usingnamespace main"
26text Prims "anyerror anyframe anyopaque bool c_int c_long c_longdouble c_longlong c_char c_short c_uint c_ulong c_ulonglong c_ushort comptime_float comptime_int f128 f16 f32 f64 f80 false isize noreturn null true type undefined usize void"
27text Entry "calc omega_"
28text ESyn "ZIG_IMPORT_SYNTAX\n"
29text EType "ZIG_IMPORT_UNSUPPORTED_TYPE\n"
30text ECap "ZIG_IMPORT_LIMIT\n"
31text EArith "ZIG_IMPORT_UNSUPPORTED_ARITHMETIC\n"
32text EForm "ZIG_IMPORT_UNSUPPORTED_CONSTRUCT\n"
33text EName "ZIG_IMPORT_UNKNOWN_IDENTIFIER\n"
34text ESig "ZIG_IMPORT_UNSUPPORTED_SIGNATURE\n"
35text ELex "ZIG_IMPORT_UNSUPPORTED_SOURCE_FORM\n"
36text EIO "ZIG_IMPORT_IO\n"
37
38fn die code ulong to void
39if eq code 65
40w = write 2 ESyn 0 len ESyn
41end
42if eq code 66
43w = write 2 EType 0 len EType
44end
45if eq code 67
46w = write 2 ECap 0 len ECap
47end
48if eq code 68
49w = write 2 EArith 0 len EArith
50end
51if eq code 69
52w = write 2 EForm 0 len EForm
53end
54if eq code 70
55w = write 2 EName 0 len EName
56end
57if eq code 71
58w = write 2 ESig 0 len ESig
59end
60if eq code 72
61w = write 2 ELex 0 len ELex
62end
63if eq code 77
64w = write 2 EIO 0 len EIO
65end
66exit int code
67end
68
69fn alpha c uchar to bool
70ret or or and ge c 65 le c 90 and ge c 97 le c 122 eq c 95
71end
72
73fn digit c uchar to bool
74ret and ge c 48 le c 57
75end
76
77fn ident c uchar to bool
78ret or alpha c digit c
79end
80
81fn srcdig p ulong to bool
82if ge p get Slen 0
83ret false
84end
85ret digit get Src p
86end
87
88fn srcid p ulong to bool
89if ge p get Slen 0
90ret false
91end
92ret ident get Src p
93end
94
95fn srcis p ulong c ulong to bool
96if ge p get Slen 0
97ret false
98end
99ret eq ulong get Src p c
100end
101
102note Zig std.ascii.isWhitespace restricted to the bytes the profile admits: space, tab, LF, CR.
103fn wsat p ulong to bool
104if ge p get Slen 0
105ret false
106end
107c = get Src p
108ret or or eq c 32 eq c 9 or eq c 10 eq c 13
109end
110
111note Strict UTF-8 (no overlong, surrogate or above U+10FFFF) over the whole source.
112fn utf8 to bool
113n = get Slen 0
114pos = ulong 0
115while lt pos n
116c = ulong get Src pos
117pos = add pos 1
118if ge c 128
119extra = ulong 0
120v = ulong 0
121low = ulong 0
122if and ge c 194 le c 223
123extra = 1
124v = and c 31
125low = 128
126end
127if and ge c 224 le c 239
128extra = 2
129v = and c 15
130low = 2048
131end
132if and ge c 240 le c 244
133extra = 3
134v = and c 7
135low = 65536
136end
137if eq extra 0
138ret false
139end
140if gt extra sub n pos
141ret false
142end
143for j ulong extra
144c = ulong get Src pos
145if or lt c 128 gt c 191
146ret false
147end
148v = or shl v 6 and c 63
149pos = add pos 1
150end
151if lt v low
152ret false
153end
154if gt v 1114111
155ret false
156end
157if and ge v 55296 le v 57343
158ret false
159end
160end
161end
162ret true
163end
164
165note Ordinary line comment. Doc comments (/// not followed by /, and //!) refuse 72. Comment bytes are
166note those the Zig 0.17 tokenizer admits: 0x20..0x7E, UTF-8 sequences, and CR only before LF.
167fn linecom to void
168p = get Cur 0
169if srcis add p 2 33
170die 72
171end
172if srcis add p 2 47
173if not srcis add p 3 47
174die 72
175end
176end
177p = add p 2
178more = ulong 1
179while and eq more 1 lt p get Slen 0
180c = get Src p
181if eq c 10
182more = 0
183else
184if eq c 13
185if not srcis add p 1 10
186die 72
187end
188else
189if or lt c 32 eq c 127
190die 72
191end
192end
193p = add p 1
194end
195end
196put Cur 0 p
197end
198
199fn spaces to void
200more = ulong 1
201while eq more 1
202if ge get Cur 0 get Slen 0
203ret
204end
205p = get Cur 0
206if wsat p
207put Cur 0 add p 1
208else
209if and srcis p 47 srcis add p 1 47
210linecom
211else
212more = 0
213end
214end
215end
216end
217
218fn idlex to void
219p = add get Cur 0 1
220while srcid p
221p = add p 1
222end
223put Tk 0 256
224put Cur 0 p
225put Te 0 p
226end
227
228note Builtin @name: token 260 covering "@name"; a quoted identifier @"..." refuses 69.
229fn atlex to void
230p = add get Cur 0 1
231if not srcid p
232die 69
233end
234if srcdig p
235die 69
236end
237while srcid p
238p = add p 1
239end
240put Tk 0 260
241put Cur 0 p
242put Te 0 p
243end
244
245note Decimal literal: 0 or [1-9][0-9]*; no _, prefixes, exponent or fraction; value at most 2^64-1.
246fn numlex to void
247p = get Cur 0
248if eq get Src p 48
249if srcid add p 1
250die 68
251end
252end
253v = ulong 0
254while srcdig p
255d = sub ulong get Src p 48
256if gt v 1844674407370955161
257die 67
258end
259if and eq v 1844674407370955161 gt d 5
260die 67
261end
262v = add mul v 10 d
263p = add p 1
264end
265if srcid p
266die 68
267end
268if srcis p 46
269die 68
270end
271put Tk 0 257
272put Tv 0 v
273put Cur 0 p
274put Te 0 p
275end
276
277note + - * followed by % are the wrapping operators 261/262/263; followed by %= they are compound
278note assignment (264). Every other form keeps its single-byte code and refuses as arithmetic.
279fn punct p ulong c ulong to void
280k = c
281n = ulong 1
282if and or or eq c 43 eq c 45 eq c 42 srcis add p 1 37
283n = 2
284if eq c 43
285k = 261
286end
287if eq c 45
288k = 262
289end
290if eq c 42
291k = 263
292end
293if srcis add p 2 61
294k = 264
295n = 3
296end
297end
298put Tk 0 k
299put Cur 0 add p n
300put Te 0 add p n
301end
302
303fn lexnext to void
304spaces
305p = get Cur 0
306put Ts 0 p
307if ge p get Slen 0
308put Tk 0 0
309put Te 0 p
310ret
311end
312c = get Src p
313if alpha c
314idlex
315else
316if digit c
317numlex
318else
319if gt c 127
320die 69
321end
322if eq c 64
323atlex
324else
325punct p ulong c
326end
327end
328end
329end
330
331fn word off ulong n ulong to bool
332if or ne get Tk 0 256 ne sub get Te 0 get Ts 0 n
333ret false
334end
335for i ulong n
336if ne get Src add get Ts 0 i get Fixed add off i
337ret false
338end
339end
340ret true
341end
342
343fn keyeq off ulong n ulong to bool
344if ne sub get Te 0 get Ts 0 n
345ret false
346end
347for i ulong n
348if ne get Src add get Ts 0 i get Keys add off i
349ret false
350end
351end
352ret true
353end
354
355fn primeq off ulong n ulong to bool
356if ne sub get Te 0 get Ts 0 n
357ret false
358end
359for i ulong n
360if ne get Src add get Ts 0 i get Prims add off i
361ret false
362end
363end
364ret true
365end
366
367fn keychar p ulong to bool
368if ge p len Keys
369ret false
370end
371ret ne get Keys p 32
372end
373
374fn primchar p ulong to bool
375if ge p len Prims
376ret false
377end
378ret ne get Prims p 32
379end
380
381fn iskey to bool
382if ne get Tk 0 256
383ret false
384end
385p = ulong 0
386while lt p len Keys
387b = p
388while keychar p
389p = add p 1
390end
391if keyeq b sub p b
392ret true
393end
394p = add p 1
395end
396ret false
397end
398
399note Zig primitive names: the fixed list, and every [iu][0-9]+ integer type name (std/zig/primitives.zig).
400fn isprim to bool
401if ne get Tk 0 256
402ret false
403end
404s = get Ts 0
405n = sub get Te 0 s
406c = get Src s
407if and ge n 2 or eq c 105 eq c 117
408q = ulong 1
409for i ulong sub n 1
410if not digit get Src add add s 1 i
411q = 0
412end
413end
414if eq q 1
415ret true
416end
417end
418p = ulong 0
419while lt p len Prims
420b = p
421while primchar p
422p = add p 1
423end
424if primeq b sub p b
425ret true
426end
427p = add p 1
428end
429ret false
430end
431
432fn entryok to bool
433n = sub get Te 0 get Ts 0
434if eq n 4
435for i ulong 4
436if ne get Src add get Ts 0 i get Entry i
437ret false
438end
439end
440ret true
441end
442if le n 6
443ret false
444end
445for i ulong 6
446if ne get Src add get Ts 0 i get Entry add i 5
447ret false
448end
449end
450ret true
451end
452
453fn nameok to void
454if ne get Tk 0 256
455die 69
456end
457if gt sub get Te 0 get Ts 0 32
458die 71
459end
460c = get Src get Ts 0
461if or lt c 97 gt c 122
462die 71
463end
464if iskey
465die 71
466end
467if isprim
468die 71
469end
470end
471
472fn need k ulong to void
473if ne get Tk 0 k
474die 65
475end
476lexnext
477end
478
479fn badarith to bool
480k = get Tk 0
481if or or eq k 43 eq k 45 eq k 42
482ret true
483end
484if or eq k 47 eq k 37
485ret true
486end
487if or eq k 60 eq k 62
488ret true
489end
490if or or eq k 38 eq k 124 eq k 94
491ret true
492end
493if or or eq k 126 eq k 33 eq k 61
494ret true
495end
496ret eq k 264
497end
498
499note Expect token k; an operator here is unsupported arithmetic (68), a separator or end is syntax (65).
500fn closer k ulong to void
501t = get Tk 0
502if eq t k
503lexnext
504ret
505end
506if badarith
507die 68
508end
509if or or eq t 0 eq t 41 or eq t 44 or eq t 125 eq t 59
510die 65
511end
512if eq t 256
513if iskey
514die 69
515end
516die 65
517end
518if eq t 257
519die 65
520end
521die 69
522end
523
524fn same off ulong count ulong to bool
525if ne count sub get Te 0 get Ts 0
526ret false
527end
528for i ulong count
529if ne get Src add off i get Src add get Ts 0 i
530ret false
531end
532end
533ret true
534end
535
536fn leput off ulong v ulong n ulong to void
537for i ulong n
538put Out add off i uchar and shr v mul i 8 255
539end
540end
541
542fn emitop op ulong a ulong b ulong imm ulong lo ulong hi ulong to ulong
543i = get Nop 0
544if ge i 64
545die 67
546end
547p = add 57 mul i 20
548for j ulong 20
549put Out add p j 0
550end
551put Out p 6
552put Out add p 1 1
553put Out add p 2 uchar op
554put Out add p 3 1
555if ne op 9
556put Out add p 4 1
557end
558if ne op 1
559put Out add p 7 1
560end
561leput add p 8 a 2
562leput add p 10 b 2
563leput add p 12 imm 8
564put Beg i lo
565put Ends i hi
566put Nu i 0
567put Nop 0 add i 1
568ret i
569end
570
571note A bare literal (op 1, Nu = 1) is comptime_int in Zig; a typed constant is @as(u64, literal).
572fn literal typed ulong to ulong
573node = emitop 1 65535 65535 get Tv 0 get Ts 0 get Te 0
574if eq typed 0
575put Nu node 1
576end
577lexnext
578ret node
579end
580
581fn identprim to ulong
582if word 21 1
583die 69
584end
585for j ulong get Npar 0
586if gt get Plen j 0
587if same get Poff j get Plen j
588put Pref j add get Pref j 1
589lexnext
590ret j
591end
592end
593end
594if iskey
595die 69
596end
597if isprim
598die 66
599end
600lexnext
601if or or eq get Tk 0 40 eq get Tk 0 46 eq get Tk 0 58
602die 69
603end
604die 70
605ret 0
606end
607
608note @as(u64, LITERAL): a typed constant. Its parenthesis counts toward the depth limit.
609fn builtin depth ulong to ulong
610s = get Ts 0
611if ne sub get Te 0 s 3
612die 69
613end
614if or ne get Src add s 1 97 ne get Src add s 2 115
615die 69
616end
617if gt add depth 1 16
618die 67
619end
620lexnext
621if ne get Tk 0 40
622die 69
623end
624lexnext
625if not word 0 3
626if eq get Tk 0 256
627die 66
628end
629die 69
630end
631lexnext
632need 44
633if ne get Tk 0 257
634if or or eq get Tk 0 41 eq get Tk 0 0 eq get Tk 0 44
635die 65
636end
637die 69
638end
639node = literal 1
640closer 41
641ret node
642end
643
644fn primary depth ulong to ulong
645if gt depth 16
646die 67
647end
648t = get Tk 0
649if eq t 257
650ret literal 0
651end
652if eq t 256
653ret identprim
654end
655if eq t 260
656ret builtin depth
657end
658if eq t 40
659lexnext
660node = expr add depth 1
661closer 41
662ret node
663end
664if or or eq t 261 eq t 262 eq t 263
665die 68
666end
667if badarith
668die 68
669end
670if or or eq t 0 eq t 41 or eq t 44 or eq t 125 eq t 59
671die 65
672end
673die 69
674ret 0
675end
676
677note Zig requires whitespace on both sides of a binary operator or on neither (std/zig/Parse.zig).
678fn opws to void
679wb = ulong 0
680if wsat sub get Ts 0 1
681wb = 1
682end
683wa = ulong 0
684if wsat get Te 0
685wa = 1
686end
687if ne wb wa
688die 65
689end
690end
691
692note Both operands untyped comptime_int literals: Zig evaluates that operation exactly at compile time, not
693note modulo 2^64, so it is refused (68); at least one operand must be u64.
694fn binop code ulong left ulong right ulong lo ulong hi ulong to ulong
695if and eq get Nu left 1 eq get Nu right 1
696die 68
697end
698ret emitop code left right 0 lo hi
699end
700
701fn term depth ulong to ulong
702left = primary depth
703while eq get Tk 0 263
704opws
705lo = get Ts 0
706hi = get Te 0
707lexnext
708right = primary depth
709left = binop 4 left right lo hi
710end
711ret left
712end
713
714fn expr depth ulong to ulong
715left = term depth
716while or eq get Tk 0 261 eq get Tk 0 262
717opws
718code = sub get Tk 0 259
719lo = get Ts 0
720hi = get Te 0
721lexnext
722right = term depth
723left = binop code left right lo hi
724end
725ret left
726end
727
728fn ptype to void
729if not word 0 3
730if iskey
731die 69
732end
733t = get Tk 0
734if or eq t 256 or eq t 42 or eq t 63 eq t 91
735die 66
736end
737if eq t 33
738die 69
739end
740die 65
741end
742lexnext
743end
744
745note A parameter is a name or the discard _; a named parameter must be used (Zig rejects an unused one).
746fn param to void
747j = get Npar 0
748if ge j 6
749die 67
750end
751if word 21 1
752put Poff j get Ts 0
753put Plen j 0
754else
755if iskey
756lexnext
757if eq get Tk 0 58
758die 71
759end
760die 69
761end
762nameok
763if same get Fnoff 0 get Fnlen 0
764die 71
765end
766for k ulong j
767if gt get Plen k 0
768if same get Poff k get Plen k
769die 71
770end
771end
772end
773put Poff j get Ts 0
774put Plen j sub get Te 0 get Ts 0
775end
776put Pref j 0
777x = emitop 9 65535 65535 j get Ts 0 get Te 0
778put Npar 0 add j 1
779lexnext
780need 58
781ptype
782end
783
784fn params to void
785if eq get Tk 0 41
786lexnext
787ret
788end
789more = ulong 1
790while eq more 1
791param
792if eq get Tk 0 44
793lexnext
794if eq get Tk 0 41
795die 65
796end
797else
798more = 0
799end
800end
801need 41
802end
803
804fn header to void
805lexnext
806if not word 4 3
807die 69
808end
809lexnext
810if not word 8 2
811die 69
812end
813lexnext
814nameok
815if not entryok
816die 71
817end
818put Fnoff 0 get Ts 0
819put Fnlen 0 sub get Te 0 get Ts 0
820lexnext
821need 40
822params
823ptype
824need 123
825end
826
827fn parse to void
828put Cur 0 0
829header
830if eq get Tk 0 125
831die 65
832end
833if not word 11 6
834die 69
835end
836lexnext
837root = expr 0
838closer 59
839closer 125
840if ne get Tk 0 0
841die 69
842end
843for j ulong get Npar 0
844if and gt get Plen j 0 eq get Pref j 0
845die 71
846end
847end
848z = emitop 1 65535 65535 0 0 0
849root = emitop 2 root z 0 0 0
850end
851
852fn parent to void
853put Out 25 212
854put Out 26 237
855put Out 27 97
856put Out 28 72
857put Out 29 191
858put Out 30 169
859put Out 31 124
860put Out 32 198
861put Out 33 147
862put Out 34 211
863put Out 35 250
864put Out 36 68
865put Out 37 149
866put Out 38 226
867put Out 39 215
868put Out 40 226
869put Out 41 230
870put Out 42 138
871put Out 43 186
872put Out 44 4
873put Out 45 96
874put Out 46 122
875put Out 47 126
876put Out 48 150
877put Out 49 221
878put Out 50 154
879put Out 51 156
880put Out 52 84
881put Out 53 191
882put Out 54 144
883put Out 55 115
884put Out 56 168
885end
886
887fn envelope to ulong
888n = get Nop 0
889sem = add 44 mul 20 n
890prov = add add 74 mul 8 n get Slen 0
891put Out 0 79
892put Out 1 77
893put Out 2 83
894put Out 3 66
895put Out 4 1
896leput 5 sem 4
897leput 9 prov 4
898put Out 13 79
899put Out 14 77
900put Out 15 83
901put Out 16 65
902put Out 17 2
903put Out 18 6
904put Out 19 8
905put Out 20 1
906put Out 21 0
907put Out 22 1
908leput 23 n 2
909parent
910s = add 13 sem
911put Out s 79
912put Out add s 1 77
913put Out add s 2 83
914put Out add s 3 80
915put Out add s 4 1
916put Out add s 5 1
917leput add s 6 get Slen 0 4
918hinit
919for i ulong sem
920hbyte get Out add 13 i
921end
922finish
923for i ulong 32
924put Out add add s 10 i get Hash i
925end
926hinit
927for i ulong get Slen 0
928hbyte get Src i
929end
930finish
931for i ulong 32
932put Out add add s 42 i get Hash i
933end
934for i ulong n
935leput add add s 74 mul i 8 get Beg i 4
936leput add add s 78 mul i 8 get Ends i 4
937end
938b = add add s 74 mul n 8
939for i ulong get Slen 0
940put Out add b i get Src i
941end
942ret add add 13 sem prov
943end
944
945fn main to int
946n = ulong 0
947more = ulong 1
948while and eq more 1 lt n 4096
949r = read 0 Src n sub 4096 n
950if lt r 0
951die 77
952end
953if eq r 0
954more = 0
955else
956n = add n ulong r
957end
958end
959if eq n 4096
960r = read 0 Extra 0 1
961if lt r 0
962die 77
963end
964if gt r 0
965die 67
966end
967end
968put Slen 0 n
969for i ulong n
970if eq get Src i 0
971die 65
972end
973end
974if not utf8
975die 72
976end
977if and srcis 0 239 and srcis 1 187 srcis 2 191
978die 72
979end
980constants
981parse
982n = envelope
983sent = ulong 0
984while lt sent n
985r = write 1 Out sent sub n sent
986if le r 0
987die 77
988end
989sent = add sent ulong r
990end
991ret 0
992end