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 / c-i64-import.om

868 lines · 12796 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Handwritten bounded C17 int64 expression importer for profile S2 (spec/universal/translation-s2-v1.md).
3note No C compiler runtime or host parser. Emits source-aware v3 profile 10 records.
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
20global F ulong 3
21text Header "#include <stdint.h>\n"
22text Words "int64_t uint64_t return void auto break case char const continue default do double else enum extern float for goto if inline int long register restrict short signed sizeof static struct switch typedef union unsigned volatile while main _Bool _Complex _Imaginary _Alignas _Alignof _Atomic _Generic _Noreturn _Static_assert _Thread_local alignas alignof bool constexpr false nullptr static_assert thread_local true typeof typeof_unqual"
23text Sfx "_t_MAX_MIN_WIDTH_C"
24text EType "C_IMPORT_UNSUPPORTED_TYPE\n"
25text ESyn "C_IMPORT_SYNTAX\n"
26text ECap "C_IMPORT_LIMIT\n"
27text EArith "C_IMPORT_UNSUPPORTED_ARITHMETIC\n"
28text EForm "C_IMPORT_UNSUPPORTED_CONSTRUCT\n"
29text EName "C_IMPORT_UNKNOWN_IDENTIFIER\n"
30text ESig "C_IMPORT_UNSUPPORTED_SIGNATURE\n"
31text EHead "C_IMPORT_UNSUPPORTED_PREPROCESSOR\n"
32text EIO "C_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 EHead 0 len EHead
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 notnl p ulong to bool
92if ge p get Slen 0
93ret false
94end
95ret ne get Src p 10
96end
97
98fn wrdchar p ulong to bool
99if ge p len Words
100ret false
101end
102ret ne get Words p 32
103end
104
105fn linecom to void
106p = add get Cur 0 2
107while notnl p
108p = add p 1
109end
110put Cur 0 p
111end
112
113fn blockcom to void
114p = add get Cur 0 2
115while lt add p 1 get Slen 0
116a = get Src p
117b = get Src add p 1
118if and eq a 42 eq b 47
119put Cur 0 add p 2
120ret
121end
122p = add p 1
123end
124die 65
125end
126
127fn spaces to void
128more = ulong 1
129while eq more 1
130if ge get Cur 0 get Slen 0
131ret
132end
133p = get Cur 0
134c = get Src p
135if or or eq c 32 eq c 9 or eq c 10 eq c 13
136put Cur 0 add p 1
137else
138if and eq c 47 lt add p 1 get Slen 0
139d = get Src add p 1
140if eq d 47
141linecom
142else
143if eq d 42
144blockcom
145else
146more = 0
147end
148end
149else
150more = 0
151end
152end
153end
154end
155
156fn idlex to void
157p = add get Cur 0 1
158while srcid p
159p = add p 1
160end
161put Tk 0 256
162put Cur 0 p
163put Te 0 p
164end
165
166fn numlex to void
167p = get Cur 0
168if eq get Src p 48
169if srcdig add p 1
170die 68
171end
172end
173v = ulong 0
174while srcdig p
175d = sub ulong get Src p 48
176if gt v 922337203685477580
177die 67
178end
179if and eq v 922337203685477580 gt d 7
180die 67
181end
182v = add mul v 10 d
183p = add p 1
184end
185if gt add p 2 get Slen 0
186die 68
187end
188a = get Src p
189b = get Src add p 1
190if or ne a 76 ne b 76
191die 68
192end
193p = add p 2
194put Tk 0 257
195put Tv 0 v
196put Cur 0 p
197put Te 0 p
198end
199
200fn lexnext to void
201spaces
202p = get Cur 0
203put Ts 0 p
204if ge p get Slen 0
205put Tk 0 0
206put Te 0 p
207ret
208end
209c = get Src p
210if alpha c
211idlex
212else
213if digit c
214numlex
215else
216if gt c 127
217die 69
218end
219put Tk 0 ulong c
220put Cur 0 add p 1
221put Te 0 add p 1
222end
223end
224end
225
226fn word off ulong n ulong to bool
227if or ne get Tk 0 256 ne sub get Te 0 get Ts 0 n
228ret false
229end
230for i ulong n
231if ne get Src add get Ts 0 i get Words add off i
232ret false
233end
234end
235ret true
236end
237
238fn reserved to bool
239p = ulong 0
240while lt p len Words
241b = p
242while wrdchar p
243p = add p 1
244end
245if word b sub p b
246ret true
247end
248p = add p 1
249end
250ret false
251end
252
253fn typeprefix to bool
254n = sub get Te 0 get Ts 0
255p = get Ts 0
256if ge n 3
257if eq get Src p 105
258if and eq get Src add p 1 110 eq get Src add p 2 116
259ret true
260end
261end
262end
263if ge n 4
264if eq get Src p 117
265if and eq get Src add p 1 105 eq get Src add p 2 110
266ret eq get Src add p 3 116
267end
268end
269end
270ret false
271end
272
273note tokends: the current identifier is longer than, and ends with, the k bytes Sfx[o, o+k).
274fn tokends o ulong k ulong to bool
275s = get Ts 0
276e = get Te 0
277if le sub e s k
278ret false
279end
280b = sub e k
281for i ulong k
282if ne get Src add b i get Sfx add o i
283ret false
284end
285end
286ret true
287end
288
289fn upperword to bool
290s = get Ts 0
291n = sub get Te 0 s
292for i ulong n
293c = ulong get Src add s i
294if and ge c 97 le c 122
295ret false
296end
297end
298ret true
299end
300
301note fnameok (amendment A8): the function name is any C identifier of at most 63 bytes that is not a keyword
302note (Words), not a C17 7.1.3 reserved identifier (__x, _X), not a *_t typedef name and not an upper-case
303note *_MAX/_MIN/_WIDTH/_C macro name of the <stdint.h> namespace. Refusal 71.
304fn fnameok to void
305if ne get Tk 0 256
306die 69
307end
308if gt sub get Te 0 get Ts 0 63
309die 71
310end
311if reserved
312die 71
313end
314if eq get Src get Ts 0 95
315d = ulong get Src add get Ts 0 1
316if or eq d 95 and ge d 65 le d 90
317die 71
318end
319end
320if tokends 0 2
321die 71
322end
323if upperword
324if or or tokends 2 4 tokends 6 4 or tokends 10 6 tokends 16 2
325die 71
326end
327end
328end
329
330fn nameok to void
331if ne get Tk 0 256
332die 69
333end
334if gt sub get Te 0 get Ts 0 32
335die 71
336end
337c = get Src get Ts 0
338if or lt c 97 gt c 122
339die 71
340end
341if typeprefix
342die 71
343end
344if reserved
345die 71
346end
347end
348
349fn need k ulong to void
350if ne get Tk 0 k
351die 65
352end
353lexnext
354end
355
356fn ctype to void
357if not word 0 7
358die 66
359end
360lexnext
361end
362
363fn same off ulong count ulong to bool
364if ne count sub get Te 0 get Ts 0
365ret false
366end
367for i ulong count
368if ne get Src add off i get Src add get Ts 0 i
369ret false
370end
371end
372ret true
373end
374
375fn findpar to ulong
376for j ulong get Npar 0
377if same get Poff j get Plen j
378ret j
379end
380end
381die 70
382ret 0
383end
384
385fn leput off ulong v ulong n ulong to void
386for i ulong n
387put Out add off i uchar and shr v mul i 8 255
388end
389end
390
391fn setf ty ulong at ulong ob ulong to void
392put F 0 ty
393put F 1 at
394put F 2 ob
395end
396
397note emitop: one v3 record with dialect 1, version 1, the type/attribute/obligations last given to setf.
398fn emitop op ulong a ulong b ulong imm ulong lo ulong hi ulong to ulong
399i = get Nop 0
400if ge i 64
401die 67
402end
403p = add 57 mul i 20
404for j ulong 20
405put Out add p j 0
406end
407put Out p 1
408put Out add p 1 1
409put Out add p 2 uchar op
410put Out add p 3 uchar get F 0
411put Out add p 4 uchar get F 1
412put Out add p 7 uchar get F 2
413leput add p 8 a 2
414leput add p 10 b 2
415leput add p 12 imm 8
416put Beg i lo
417put Ends i hi
418put Nop 0 add i 1
419ret i
420end
421
422fn needw k ulong to void
423if ne get Tk 0 k
424die 68
425end
426lexnext
427end
428
429note castu: the exact "(uint64_t)" of the wrapping form; any other spelling is unsupported arithmetic.
430fn castu to void
431needw 40
432if not word 8 8
433die 68
434end
435lexnext
436needw 41
437end
438
439note wrapform: after "(" and "int64_t": ")((uint64_t)A OP (uint64_t)B)", OP one of + - *, attribute 5.
440fn wrapform depth ulong to ulong
441lexnext
442needw 41
443needw 40
444castu
445a = primary add depth 1
446k = get Tk 0
447code = ulong 0
448if eq k 43
449code = 2
450end
451if eq k 45
452code = 3
453end
454if eq k 42
455code = 4
456end
457if eq code 0
458die 68
459end
460lo = get Ts 0
461hi = get Te 0
462lexnext
463castu
464b = primary add depth 1
465needw 41
466setf 5 5 1
467ret emitop code a b 0 lo hi
468end
469
470fn primary depth ulong to ulong
471if gt depth 16
472die 67
473end
474if eq get Tk 0 257
475setf 5 0 0
476node = emitop 1 65535 65535 get Tv 0 get Ts 0 get Te 0
477lexnext
478ret node
479end
480if eq get Tk 0 256
481node = findpar
482lexnext
483ret node
484end
485if eq get Tk 0 40
486lexnext
487if word 0 7
488ret wrapform depth
489end
490if word 8 8
491die 68
492end
493node = condx add depth 1
494need 41
495ret node
496end
497die 68
498ret 0
499end
500
501fn term depth ulong to ulong
502left = primary depth
503while eq get Tk 0 42
504lo = get Ts 0
505hi = get Te 0
506lexnext
507right = primary depth
508setf 5 6 33
509left = emitop 4 left right 0 lo hi
510end
511ret left
512end
513
514fn addx depth ulong to ulong
515left = term depth
516while or eq get Tk 0 43 eq get Tk 0 45
517code = ulong 2
518if eq get Tk 0 45
519code = 3
520end
521lo = get Ts 0
522hi = get Te 0
523lexnext
524right = term depth
525setf 5 6 33
526left = emitop code left right 0 lo hi
527end
528ret left
529end
530
531fn nextc to ulong
532p = get Te 0
533if ge p get Slen 0
534ret 0
535end
536ret ulong get Src p
537end
538
539note cmpop: the comparison at the current token as opcode 10..15 (== != < <= > >=), or 0. A two-character
540note operator must be adjacent; its first character is consumed here, the caller consumes the last.
541fn cmpop to ulong
542k = get Tk 0
543c = nextc
544if eq k 60
545if eq c 60
546die 68
547end
548if eq c 61
549lexnext
550ret 13
551end
552ret 12
553end
554if eq k 62
555if eq c 62
556die 68
557end
558if eq c 61
559lexnext
560ret 15
561end
562ret 14
563end
564if eq k 61
565if ne c 61
566die 69
567end
568lexnext
569ret 10
570end
571if eq k 33
572if ne c 61
573die 68
574end
575lexnext
576ret 11
577end
578ret 0
579end
580
581fn iscmp to bool
582k = get Tk 0
583if or eq k 60 eq k 62
584ret true
585end
586ret or eq k 61 eq k 33
587end
588
589note condx: additive [cmp additive "?" condx ":" condx]; a comparison exists only as a ?: condition.
590fn condx depth ulong to ulong
591left = addx depth
592if eq get Tk 0 63
593die 68
594end
595lo = get Ts 0
596code = cmpop
597if eq code 0
598ret left
599end
600hi = get Te 0
601lexnext
602right = addx depth
603if iscmp
604die 68
605end
606if ne get Tk 0 63
607die 68
608end
609setf 6 0 0
610c = emitop code left right 0 lo hi
611qlo = get Ts 0
612qhi = get Te 0
613lexnext
614x = condx depth
615need 58
616y = condx depth
617setf 5 0 0
618ret emitop 16 c x y qlo qhi
619end
620
621fn badarith to bool
622k = get Tk 0
623if or eq k 47 eq k 37
624ret true
625end
626if or eq k 38 eq k 124
627ret true
628end
629if or eq k 94 eq k 126
630ret true
631end
632ret false
633end
634
635fn parse to void
636if lt get Slen 0 len Header
637die 72
638end
639for i ulong len Header
640if ne get Src i get Header i
641die 72
642end
643end
644put Cur 0 len Header
645lexnext
646ctype
647fnameok
648lexnext
649need 40
650if word 24 4
651lexnext
652need 41
653else
654more = ulong 1
655while eq more 1
656ctype
657nameok
658j = get Npar 0
659if ge j 6
660die 67
661end
662for k ulong j
663if same get Poff k get Plen k
664die 71
665end
666end
667put Poff j get Ts 0
668put Plen j sub get Te 0 get Ts 0
669setf 5 0 1
670x = emitop 9 65535 65535 j get Ts 0 get Te 0
671put Npar 0 add j 1
672lexnext
673if eq get Tk 0 44
674lexnext
675else
676more = 0
677end
678end
679need 41
680end
681need 123
682if not word 17 6
683die 69
684end
685lexnext
686root = condx 0
687if badarith
688die 68
689end
690need 59
691need 125
692if ne get Tk 0 0
693die 69
694end
695setf 5 0 0
696z = emitop 1 65535 65535 0 0 0
697setf 5 5 1
698root = emitop 2 root z 0 0 0
699end
700
701fn parent to void
702put Out 25 212
703put Out 26 237
704put Out 27 97
705put Out 28 72
706put Out 29 191
707put Out 30 169
708put Out 31 124
709put Out 32 198
710put Out 33 147
711put Out 34 211
712put Out 35 250
713put Out 36 68
714put Out 37 149
715put Out 38 226
716put Out 39 215
717put Out 40 226
718put Out 41 230
719put Out 42 138
720put Out 43 186
721put Out 44 4
722put Out 45 96
723put Out 46 122
724put Out 47 126
725put Out 48 150
726put Out 49 221
727put Out 50 154
728put Out 51 156
729put Out 52 84
730put Out 53 191
731put Out 54 144
732put Out 55 115
733put Out 56 168
734end
735
736fn envelope to ulong
737n = get Nop 0
738sem = add 44 mul 20 n
739prov = add add 74 mul 8 n get Slen 0
740put Out 0 79
741put Out 1 77
742put Out 2 83
743put Out 3 66
744put Out 4 1
745leput 5 sem 4
746leput 9 prov 4
747put Out 13 79
748put Out 14 77
749put Out 15 83
750put Out 16 65
751put Out 17 3
752put Out 18 1
753put Out 19 10
754put Out 20 1
755put Out 21 0
756put Out 22 1
757leput 23 n 2
758parent
759s = add 13 sem
760put Out s 79
761put Out add s 1 77
762put Out add s 2 83
763put Out add s 3 80
764put Out add s 4 1
765put Out add s 5 0
766leput add s 6 get Slen 0 4
767hinit
768for i ulong sem
769hbyte get Out add 13 i
770end
771finish
772for i ulong 32
773put Out add add s 10 i get Hash i
774end
775hinit
776for i ulong get Slen 0
777hbyte get Src i
778end
779finish
780for i ulong 32
781put Out add add s 42 i get Hash i
782end
783for i ulong n
784leput add add s 74 mul i 8 get Beg i 4
785leput add add s 78 mul i 8 get Ends i 4
786end
787b = add add s 74 mul n 8
788for i ulong get Slen 0
789put Out add b i get Src i
790end
791ret add add 13 sem prov
792end
793
794fn triend c uchar to bool
795if or eq c 61 eq c 47
796ret true
797end
798if or eq c 40 eq c 39
799ret true
800end
801if or eq c 41 eq c 33
802ret true
803end
804if or eq c 60 eq c 62
805ret true
806end
807ret eq c 45
808end
809
810fn phasecheck to void
811for i ulong get Slen 0
812c = get Src i
813if eq c 92
814die 72
815end
816if and eq c 63 lt add i 2 get Slen 0
817if eq get Src add i 1 63
818if triend get Src add i 2
819die 72
820end
821end
822end
823end
824end
825
826fn main to int
827n = ulong 0
828more = ulong 1
829while and eq more 1 lt n 4096
830r = read 0 Src n sub 4096 n
831if lt r 0
832die 77
833end
834if eq r 0
835more = 0
836else
837n = add n ulong r
838end
839end
840if eq n 4096
841r = read 0 Extra 0 1
842if lt r 0
843die 77
844end
845if gt r 0
846die 67
847end
848end
849put Slen 0 n
850for i ulong n
851if eq get Src i 0
852die 65
853end
854end
855phasecheck
856constants
857parse
858n = envelope
859sent = ulong 0
860while lt sent n
861r = write 1 Out sent sub n sent
862if le r 0
863die 77
864end
865sent = add sent ulong r
866end
867ret 0
868end