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 / rust-u64-import.om

893 lines · 12721 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Handwritten bounded Rust 2021 u64 wrapping-expression importer, profile rust-u64-wrapping-pure-v1.
3note No Rust compiler, parser library or host fallback. Spec: spec/universal/rust-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 Poff ulong 6
16global Plen ulong 6
17global Nop ulong 1
18global Beg ulong 64
19global Ends ulong 64
20text Fixed "u64 pub fn where as wrapping_add wrapping_sub wrapping_mul"
21text Keys "as break const continue crate else enum extern false fn for if impl in let loop match mod move mut pub ref return self static struct super trait true type unsafe use where while async await dyn abstract become box do final macro override priv typeof unsized virtual yield try union gen safe raw auto default macro_rules main"
22text Prims "u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f16 f32 f64 f128 bool char str"
23text Entry "calc omega_"
24text ESyn "RUST_IMPORT_SYNTAX\n"
25text EType "RUST_IMPORT_UNSUPPORTED_TYPE\n"
26text ECap "RUST_IMPORT_LIMIT\n"
27text EArith "RUST_IMPORT_UNSUPPORTED_ARITHMETIC\n"
28text EForm "RUST_IMPORT_UNSUPPORTED_CONSTRUCT\n"
29text EName "RUST_IMPORT_UNKNOWN_IDENTIFIER\n"
30text ESig "RUST_IMPORT_UNSUPPORTED_SIGNATURE\n"
31text ELex "RUST_IMPORT_UNSUPPORTED_SOURCE_FORM\n"
32text EIO "RUST_IMPORT_IO\n"
33
34fn die code ulong to void
35if eq code 65
36w = write 2 ESyn 0 len ESyn
37end
38if eq code 66
39w = write 2 EType 0 len EType
40end
41if eq code 67
42w = write 2 ECap 0 len ECap
43end
44if eq code 68
45w = write 2 EArith 0 len EArith
46end
47if eq code 69
48w = write 2 EForm 0 len EForm
49end
50if eq code 70
51w = write 2 EName 0 len EName
52end
53if eq code 71
54w = write 2 ESig 0 len ESig
55end
56if eq code 72
57w = write 2 ELex 0 len ELex
58end
59if eq code 77
60w = write 2 EIO 0 len EIO
61end
62exit int code
63end
64
65fn alpha c uchar to bool
66ret or or and ge c 65 le c 90 and ge c 97 le c 122 eq c 95
67end
68
69fn digit c uchar to bool
70ret and ge c 48 le c 57
71end
72
73fn ident c uchar to bool
74ret or alpha c digit c
75end
76
77fn srcdig p ulong to bool
78if ge p get Slen 0
79ret false
80end
81ret digit get Src p
82end
83
84fn srcid p ulong to bool
85if ge p get Slen 0
86ret false
87end
88ret ident get Src p
89end
90
91fn srcis p ulong c ulong to bool
92if ge p get Slen 0
93ret false
94end
95ret eq ulong get Src p c
96end
97
98fn notnl p ulong to bool
99if ge p get Slen 0
100ret false
101end
102ret ne get Src p 10
103end
104
105note Strict UTF-8 (no overlong, surrogate or above U+10FFFF) over the whole source.
106fn utf8 to bool
107n = get Slen 0
108pos = ulong 0
109while lt pos n
110c = ulong get Src pos
111pos = add pos 1
112if ge c 128
113extra = ulong 0
114v = ulong 0
115low = ulong 0
116if and ge c 194 le c 223
117extra = 1
118v = and c 31
119low = 128
120end
121if and ge c 224 le c 239
122extra = 2
123v = and c 15
124low = 2048
125end
126if and ge c 240 le c 244
127extra = 3
128v = and c 7
129low = 65536
130end
131if eq extra 0
132ret false
133end
134if gt extra sub n pos
135ret false
136end
137for j ulong extra
138c = ulong get Src pos
139if or lt c 128 gt c 191
140ret false
141end
142v = or shl v 6 and c 63
143pos = add pos 1
144end
145if lt v low
146ret false
147end
148if gt v 1114111
149ret false
150end
151if and ge v 55296 le v 57343
152ret false
153end
154end
155end
156ret true
157end
158
159note Ordinary line comment. Doc comments (/// not followed by /, and //!) are attribute sugar: 72.
160fn linecom to void
161p = get Cur 0
162if srcis add p 2 33
163die 72
164end
165if srcis add p 2 47
166if not srcis add p 3 47
167die 72
168end
169end
170p = add p 2
171while notnl p
172p = add p 1
173end
174put Cur 0 p
175end
176
177note Ordinary block comment; Rust block comments nest. Doc forms /** x and /*! refuse 72.
178fn blockcom to void
179p = get Cur 0
180if srcis add p 2 33
181die 72
182end
183if srcis add p 2 42
184if lt add p 3 get Slen 0
185if not or srcis add p 3 42 srcis add p 3 47
186die 72
187end
188end
189end
190q = add p 2
191depth = ulong 1
192while gt depth 0
193if ge add q 1 get Slen 0
194die 65
195end
196a = get Src q
197b = get Src add q 1
198if and eq a 47 eq b 42
199depth = add depth 1
200q = add q 2
201else
202if and eq a 42 eq b 47
203depth = sub depth 1
204q = add q 2
205else
206q = add q 1
207end
208end
209end
210put Cur 0 q
211end
212
213fn spaces to void
214more = ulong 1
215while eq more 1
216if ge get Cur 0 get Slen 0
217ret
218end
219p = get Cur 0
220c = get Src p
221if or or eq c 32 eq c 9 or eq c 10 eq c 13
222put Cur 0 add p 1
223else
224if and eq c 47 lt add p 1 get Slen 0
225d = get Src add p 1
226if eq d 47
227linecom
228else
229if eq d 42
230blockcom
231else
232more = 0
233end
234end
235else
236more = 0
237end
238end
239end
240end
241
242fn idlex to void
243p = add get Cur 0 1
244while srcid p
245p = add p 1
246end
247put Tk 0 256
248put Cur 0 p
249put Te 0 p
250end
251
252note Decimal literal with exact suffix u64: 0u64 or [1-9][0-9]*u64; no _, prefixes or other suffixes.
253fn numlex to void
254p = get Cur 0
255if eq get Src p 48
256if srcdig add p 1
257die 68
258end
259end
260v = ulong 0
261while srcdig p
262d = sub ulong get Src p 48
263if gt v 1844674407370955161
264die 67
265end
266if and eq v 1844674407370955161 gt d 5
267die 67
268end
269v = add mul v 10 d
270p = add p 1
271end
272if not srcis p 117
273die 68
274end
275if not srcis add p 1 54
276die 68
277end
278if not srcis add p 2 52
279die 68
280end
281p = add p 3
282if srcid p
283die 68
284end
285put Tk 0 257
286put Tv 0 v
287put Cur 0 p
288put Te 0 p
289end
290
291fn punct p ulong c ulong to void
292k = c
293n = ulong 1
294if and eq c 58 srcis add p 1 58
295k = 258
296n = 2
297end
298if and eq c 45 srcis add p 1 62
299k = 259
300n = 2
301end
302put Tk 0 k
303put Cur 0 add p n
304put Te 0 add p n
305end
306
307fn lexnext to void
308spaces
309p = get Cur 0
310put Ts 0 p
311if ge p get Slen 0
312put Tk 0 0
313put Te 0 p
314ret
315end
316c = get Src p
317if alpha c
318idlex
319else
320if digit c
321numlex
322else
323if gt c 127
324die 69
325end
326punct p ulong c
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
399fn isprim to bool
400if ne get Tk 0 256
401ret false
402end
403p = ulong 0
404while lt p len Prims
405b = p
406while primchar p
407p = add p 1
408end
409if primeq b sub p b
410ret true
411end
412p = add p 1
413end
414ret false
415end
416
417fn entryok to bool
418n = sub get Te 0 get Ts 0
419if eq n 4
420for i ulong 4
421if ne get Src add get Ts 0 i get Entry i
422ret false
423end
424end
425ret true
426end
427if le n 6
428ret false
429end
430for i ulong 6
431if ne get Src add get Ts 0 i get Entry add i 5
432ret false
433end
434end
435ret true
436end
437
438fn nameok to void
439if ne get Tk 0 256
440die 69
441end
442if gt sub get Te 0 get Ts 0 32
443die 71
444end
445c = get Src get Ts 0
446if or lt c 97 gt c 122
447die 71
448end
449if iskey
450die 71
451end
452if isprim
453die 71
454end
455end
456
457fn need k ulong to void
458if ne get Tk 0 k
459die 65
460end
461lexnext
462end
463
464fn badarith to bool
465k = get Tk 0
466if or or eq k 43 eq k 45 eq k 42
467ret true
468end
469if or eq k 47 eq k 37
470ret true
471end
472if or eq k 60 eq k 62
473ret true
474end
475if or or eq k 38 eq k 124 eq k 94
476ret true
477end
478ret word 17 2
479end
480
481note Expect token k; an operator here is unsupported arithmetic (68), a separator or end is syntax (65).
482fn closer k ulong to void
483t = get Tk 0
484if eq t k
485lexnext
486ret
487end
488if badarith
489die 68
490end
491if or or eq t 0 eq t 41 or eq t 44 eq t 125
492die 65
493end
494if or eq t 256 eq t 257
495die 65
496end
497die 69
498end
499
500fn same off ulong count ulong to bool
501if ne count sub get Te 0 get Ts 0
502ret false
503end
504for i ulong count
505if ne get Src add off i get Src add get Ts 0 i
506ret false
507end
508end
509ret true
510end
511
512fn leput off ulong v ulong n ulong to void
513for i ulong n
514put Out add off i uchar and shr v mul i 8 255
515end
516end
517
518fn emitop op ulong a ulong b ulong imm ulong lo ulong hi ulong to ulong
519i = get Nop 0
520if ge i 64
521die 67
522end
523p = add 57 mul i 20
524for j ulong 20
525put Out add p j 0
526end
527put Out p 2
528put Out add p 1 1
529put Out add p 2 uchar op
530put Out add p 3 1
531if ne op 9
532put Out add p 4 1
533end
534if ne op 1
535put Out add p 7 1
536end
537leput add p 8 a 2
538leput add p 10 b 2
539leput add p 12 imm 8
540put Beg i lo
541put Ends i hi
542put Nop 0 add i 1
543ret i
544end
545
546fn methodcode to ulong
547if word 20 12
548ret 2
549end
550if word 33 12
551ret 3
552end
553if word 46 12
554ret 4
555end
556die 68
557ret 0
558end
559
560fn identprim to ulong
561for j ulong get Npar 0
562if same get Poff j get Plen j
563lexnext
564ret j
565end
566end
567if iskey
568die 69
569end
570if isprim
571die 66
572end
573lexnext
574if eq get Tk 0 33
575die 69
576end
577die 70
578ret 0
579end
580
581note u64::wrapping_add(x, y) and the sub/mul forms: same operation, operands in source order.
582fn qualified depth ulong to ulong
583lexnext
584if ne get Tk 0 258
585die 69
586end
587lexnext
588if ne get Tk 0 256
589die 69
590end
591code = methodcode
592lo = get Ts 0
593hi = get Te 0
594lexnext
595if ne get Tk 0 40
596die 69
597end
598lexnext
599a = expr add depth 1
600closer 44
601b = expr add depth 1
602closer 41
603ret emitop code a b 0 lo hi
604end
605
606fn primary depth ulong to ulong
607if gt depth 16
608die 67
609end
610t = get Tk 0
611if eq t 257
612node = emitop 1 65535 65535 get Tv 0 get Ts 0 get Te 0
613lexnext
614ret node
615end
616if eq t 256
617if word 0 3
618ret qualified depth
619end
620ret identprim
621end
622if eq t 40
623lexnext
624node = expr add depth 1
625closer 41
626ret node
627end
628if or or eq t 43 eq t 45 or eq t 42 eq t 33
629die 68
630end
631if or or eq t 0 eq t 41 or eq t 44 eq t 125
632die 65
633end
634die 69
635ret 0
636end
637
638note Postfix chain: receiver.wrapping_add(arg) etc. Receiver is the left operand, argument the right.
639fn expr depth ulong to ulong
640left = primary depth
641while eq get Tk 0 46
642lexnext
643if ne get Tk 0 256
644die 69
645end
646code = methodcode
647lo = get Ts 0
648hi = get Te 0
649lexnext
650if ne get Tk 0 40
651die 69
652end
653lexnext
654right = expr add depth 1
655closer 41
656left = emitop code left right 0 lo hi
657end
658ret left
659end
660
661fn ptype to void
662if not word 0 3
663die 66
664end
665lexnext
666end
667
668fn param to void
669nameok
670j = get Npar 0
671if ge j 6
672die 67
673end
674for k ulong j
675if same get Poff k get Plen k
676die 71
677end
678end
679put Poff j get Ts 0
680put Plen j sub get Te 0 get Ts 0
681x = emitop 9 65535 65535 j get Ts 0 get Te 0
682put Npar 0 add j 1
683lexnext
684need 58
685ptype
686end
687
688fn params to void
689if eq get Tk 0 41
690lexnext
691ret
692end
693more = ulong 1
694while eq more 1
695param
696if eq get Tk 0 44
697lexnext
698if eq get Tk 0 41
699die 65
700end
701else
702more = 0
703end
704end
705need 41
706end
707
708fn header to void
709lexnext
710if word 4 3
711lexnext
712if eq get Tk 0 40
713die 69
714end
715end
716if not word 8 2
717die 69
718end
719lexnext
720nameok
721if not entryok
722die 71
723end
724lexnext
725if eq get Tk 0 60
726die 69
727end
728need 40
729params
730if ne get Tk 0 259
731die 71
732end
733lexnext
734ptype
735if word 11 5
736die 69
737end
738need 123
739end
740
741fn parse to void
742put Cur 0 0
743header
744root = expr 0
745closer 125
746if ne get Tk 0 0
747die 69
748end
749z = emitop 1 65535 65535 0 0 0
750root = emitop 2 root z 0 0 0
751end
752
753fn parent to void
754put Out 25 212
755put Out 26 237
756put Out 27 97
757put Out 28 72
758put Out 29 191
759put Out 30 169
760put Out 31 124
761put Out 32 198
762put Out 33 147
763put Out 34 211
764put Out 35 250
765put Out 36 68
766put Out 37 149
767put Out 38 226
768put Out 39 215
769put Out 40 226
770put Out 41 230
771put Out 42 138
772put Out 43 186
773put Out 44 4
774put Out 45 96
775put Out 46 122
776put Out 47 126
777put Out 48 150
778put Out 49 221
779put Out 50 154
780put Out 51 156
781put Out 52 84
782put Out 53 191
783put Out 54 144
784put Out 55 115
785put Out 56 168
786end
787
788fn envelope to ulong
789n = get Nop 0
790sem = add 44 mul 20 n
791prov = add add 74 mul 8 n get Slen 0
792put Out 0 79
793put Out 1 77
794put Out 2 83
795put Out 3 66
796put Out 4 1
797leput 5 sem 4
798leput 9 prov 4
799put Out 13 79
800put Out 14 77
801put Out 15 83
802put Out 16 65
803put Out 17 1
804put Out 18 2
805put Out 19 2
806put Out 20 1
807put Out 21 0
808put Out 22 1
809leput 23 n 2
810parent
811s = add 13 sem
812put Out s 79
813put Out add s 1 77
814put Out add s 2 83
815put Out add s 3 80
816put Out add s 4 1
817put Out add s 5 1
818leput add s 6 get Slen 0 4
819hinit
820for i ulong sem
821hbyte get Out add 13 i
822end
823finish
824for i ulong 32
825put Out add add s 10 i get Hash i
826end
827hinit
828for i ulong get Slen 0
829hbyte get Src i
830end
831finish
832for i ulong 32
833put Out add add s 42 i get Hash i
834end
835for i ulong n
836leput add add s 74 mul i 8 get Beg i 4
837leput add add s 78 mul i 8 get Ends i 4
838end
839b = add add s 74 mul n 8
840for i ulong get Slen 0
841put Out add b i get Src i
842end
843ret add add 13 sem prov
844end
845
846fn main to int
847n = ulong 0
848more = ulong 1
849while and eq more 1 lt n 4096
850r = read 0 Src n sub 4096 n
851if lt r 0
852die 77
853end
854if eq r 0
855more = 0
856else
857n = add n ulong r
858end
859end
860if eq n 4096
861r = read 0 Extra 0 1
862if lt r 0
863die 77
864end
865if gt r 0
866die 67
867end
868end
869put Slen 0 n
870for i ulong n
871if eq get Src i 0
872die 65
873end
874end
875if not utf8
876die 72
877end
878if and srcis 0 239 and srcis 1 187 srcis 2 191
879die 72
880end
881constants
882parse
883n = envelope
884sent = ulong 0
885while lt sent n
886r = write 1 Out sent sub n sent
887if le r 0
888die 77
889end
890sent = add sent ulong r
891end
892ret 0
893end