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 / om / compiler / osc_literal.om

753 lines · 12904 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Decode the spelling of one OSC integer token. Syntax takes precedence over overflow.
3note bridge c-define OSC_LITERAL_MALFORMED 1
4note bridge c-define OSC_LITERAL_OVERFLOW 2
5note The lexer and literal decoder share this unit so integer tokens need no second bridge call.
6note bridge c-type lexstate OscLexState
7note bridge c-define OSC_LEX_COMMENT 3
8note bridge c-define OSC_LEX_FULL 4
9note bridge c-define OSC_LEX_NAME_LONG 5
10note bridge c-define OSC_LEX_BAD_BYTE 6
11om2
12
13layout lexstate 40
14field pos ulong at 0 little
15field start ulong at 8 little
16field value ulong at 16 little
17field line uint at 24 little
18field col uint at 28 little
19field width uint at 32 little
20field kind uint at 36 little
21end
22
23note Wire image of a host OscToken. The C adapter asserts its offsets and size.
24layout osctoken 32
25field kind ushort at 0 little
26field line uint at 4 little
27field col uint at 8 little
28field off uint at 12 little
29field width uint at 16 little
30field value ulong at 24 little
31end
32
33fn literal_digit c ulong to ulong
34if and ge c 48 le c 57
35 ret sub c 48
36end
37if and ge c 65 le c 70
38 ret add 10 sub c 65
39end
40if and ge c 97 le c 102
41 ret add 10 sub c 97
42end
43ret 16
44end
45
46export fn osc_parse_literal spelling view uchar * value out ulong 1 to ulong
47put value 0 0
48radix = ulong 10
49pos = ulong 0
50if ge len spelling 2
51 if eq get spelling 0 48
52 prefix = get spelling 1
53 if eq prefix 120
54 radix = 16
55 pos = 2
56 end
57 if eq prefix 98
58 radix = 2
59 pos = 2
60 end
61 end
62end
63if eq pos len spelling
64 ret 1
65end
66acc = ulong 0
67overflow = false
68note The input span bounds the scan, including arbitrarily many leading zeroes.
69while lt pos len spelling bound 9223372036854775807
70 digit = literal_digit ulong get spelling pos
71 if ge digit radix
72 ret 1
73 end
74 if gt acc div sub 0xffffffffffffffff digit radix
75 overflow = true
76 end
77 acc = add mul acc radix digit
78 pos = add pos 1
79end
80if overflow
81 ret 2
82end
83put value 0 acc
84ret 0
85end
86
87note OscTokKind values from src/compiler/osc_lex.h. The oracle exercises every spelling.
88const OT_EOF uint 0
89const OT_NAME uint 1
90const OT_INT uint 2
91const OT_RESERVED uint 3
92const OT_FN uint 4
93const OT_LET uint 5
94const OT_MUT uint 6
95const OT_OWN uint 7
96const OT_ALLOC uint 8
97const OT_IF uint 9
98const OT_ELSE uint 10
99const OT_WHILE uint 11
100const OT_BOUND uint 12
101const OT_FOR uint 13
102const OT_IN uint 14
103const OT_RETURN uint 15
104const OT_REQUIRES uint 16
105const OT_ENSURES uint 17
106const OT_AS uint 18
107const OT_TRUE uint 19
108const OT_FALSE uint 20
109const OT_U8 uint 21
110const OT_U16 uint 22
111const OT_U32 uint 23
112const OT_U64 uint 24
113const OT_I8 uint 25
114const OT_I16 uint 26
115const OT_I32 uint 27
116const OT_I64 uint 28
117const OT_BOOL uint 29
118const OT_LPAREN uint 30
119const OT_RPAREN uint 31
120const OT_LBRACE uint 32
121const OT_RBRACE uint 33
122const OT_LBRACK uint 34
123const OT_RBRACK uint 35
124const OT_COMMA uint 36
125const OT_SEMI uint 37
126const OT_COLON uint 38
127const OT_ARROW uint 39
128const OT_DOTDOT uint 40
129const OT_ASSIGN uint 41
130const OT_PLUS uint 42
131const OT_MINUS uint 43
132const OT_STAR uint 44
133const OT_SLASH uint 45
134const OT_PERCENT uint 46
135const OT_AMP uint 47
136const OT_PIPE uint 48
137const OT_CARET uint 49
138const OT_TILDE uint 50
139const OT_BANG uint 51
140const OT_ANDAND uint 52
141const OT_OROR uint 53
142const OT_SHL uint 54
143const OT_SHR uint 55
144const OT_EQ uint 56
145const OT_NE uint 57
146const OT_LT uint 58
147const OT_LE uint 59
148const OT_GT uint 60
149const OT_GE uint 61
150const OT_STRUCT uint 62
151const OT_DOT uint 63
152const OT_ARENA uint 64
153const OT_POOL uint 65
154const OT_HANDLE uint 66
155const OT_IMPORT uint 67
156const OT_BYTES uint 68
157const OT_CELLS uint 69
158
159fn lex_digit c uchar to bool
160ret and ge c 48 le c 57
161end
162
163fn lex_alpha c uchar to bool
164if eq c 95
165 ret true
166end
167ret or and ge c 65 le c 90 and ge c 97 le c 122
168end
169
170note Function-mode units cannot contain text globals. Compare exact ASCII spellings
171note packed most significant byte first. Identifiers contain no zero bytes.
172fn lex_word word view uchar * to uint
173if gt len word 10
174 ret OT_NAME
175end
176key = ulong 0
177tail = uint 0
178index = ulong 0
179while lt index len word bound 10
180 if lt index 8
181 key = or shl key 8 ulong get word index
182 else
183 tail = or shl tail 8 uint get word index
184 end
185 index = add index 1
186end
187if ne tail 0
188 note capability
189 if and eq key 0x6361706162696c69 eq tail 0x7479
190 ret OT_RESERVED
191 end
192 note authority
193 if and eq key 0x617574686f726974 eq tail 0x79
194 ret OT_RESERVED
195 end
196 ret OT_NAME
197end
198note fn
199if eq key 0x666e
200 ret OT_FN
201end
202note let
203if eq key 0x6c6574
204 ret OT_LET
205end
206note mut
207if eq key 0x6d7574
208 ret OT_MUT
209end
210note own
211if eq key 0x6f776e
212 ret OT_OWN
213end
214note alloc
215if eq key 0x616c6c6f63
216 ret OT_ALLOC
217end
218note if
219if eq key 0x6966
220 ret OT_IF
221end
222note else
223if eq key 0x656c7365
224 ret OT_ELSE
225end
226note while
227if eq key 0x7768696c65
228 ret OT_WHILE
229end
230note bound
231if eq key 0x626f756e64
232 ret OT_BOUND
233end
234note for
235if eq key 0x666f72
236 ret OT_FOR
237end
238note in
239if eq key 0x696e
240 ret OT_IN
241end
242note return
243if eq key 0x72657475726e
244 ret OT_RETURN
245end
246note requires
247if eq key 0x7265717569726573
248 ret OT_REQUIRES
249end
250note ensures
251if eq key 0x656e7375726573
252 ret OT_ENSURES
253end
254note as
255if eq key 0x6173
256 ret OT_AS
257end
258note true
259if eq key 0x74727565
260 ret OT_TRUE
261end
262note false
263if eq key 0x66616c7365
264 ret OT_FALSE
265end
266note u8
267if eq key 0x7538
268 ret OT_U8
269end
270note u16
271if eq key 0x753136
272 ret OT_U16
273end
274note u32
275if eq key 0x753332
276 ret OT_U32
277end
278note u64
279if eq key 0x753634
280 ret OT_U64
281end
282note i8
283if eq key 0x6938
284 ret OT_I8
285end
286note i16
287if eq key 0x693136
288 ret OT_I16
289end
290note i32
291if eq key 0x693332
292 ret OT_I32
293end
294note i64
295if eq key 0x693634
296 ret OT_I64
297end
298note bool
299if eq key 0x626f6f6c
300 ret OT_BOOL
301end
302note struct
303if eq key 0x737472756374
304 ret OT_STRUCT
305end
306note arena
307if eq key 0x6172656e61
308 ret OT_ARENA
309end
310note pool
311if eq key 0x706f6f6c
312 ret OT_POOL
313end
314note handle
315if eq key 0x68616e646c65
316 ret OT_HANDLE
317end
318note import
319if eq key 0x696d706f7274
320 ret OT_IMPORT
321end
322note bytes
323if eq key 0x6279746573
324 ret OT_BYTES
325end
326note cells
327if eq key 0x63656c6c73
328 ret OT_CELLS
329end
330note free
331if eq key 0x66726565
332 ret OT_RESERVED
333end
334note drop
335if eq key 0x64726f70
336 ret OT_RESERVED
337end
338note delete
339if eq key 0x64656c657465
340 ret OT_RESERVED
341end
342note dealloc
343if eq key 0x6465616c6c6f63
344 ret OT_RESERVED
345end
346note effect
347if eq key 0x656666656374
348 ret OT_RESERVED
349end
350note cap
351if eq key 0x636170
352 ret OT_RESERVED
353end
354note mint
355if eq key 0x6d696e74
356 ret OT_RESERVED
357end
358note revoke
359if eq key 0x7265766f6b65
360 ret OT_RESERVED
361end
362note grant
363if eq key 0x6772616e74
364 ret OT_RESERVED
365end
366note publish
367if eq key 0x7075626c697368
368 ret OT_RESERVED
369end
370note promote
371if eq key 0x70726f6d6f7465
372 ret OT_RESERVED
373end
374note admin
375if eq key 0x61646d696e
376 ret OT_RESERVED
377end
378note syscall
379if eq key 0x73797363616c6c
380 ret OT_RESERVED
381end
382note io
383if eq key 0x696f
384 ret OT_RESERVED
385end
386note print
387if eq key 0x7072696e74
388 ret OT_RESERVED
389end
390note extern
391if eq key 0x65787465726e
392 ret OT_RESERVED
393end
394note unsafe
395if eq key 0x756e73616665
396 ret OT_RESERVED
397end
398note asm
399if eq key 0x61736d
400 ret OT_RESERVED
401end
402note loop
403if eq key 0x6c6f6f70
404 ret OT_RESERVED
405end
406note match
407if eq key 0x6d61746368
408 ret OT_RESERVED
409end
410note enum
411if eq key 0x656e756d
412 ret OT_RESERVED
413end
414note var
415if eq key 0x766172
416 ret OT_RESERVED
417end
418note const
419if eq key 0x636f6e7374
420 ret OT_RESERVED
421end
422note self
423if eq key 0x73656c66
424 ret OT_RESERVED
425end
426note rec
427if eq key 0x726563
428 ret OT_RESERVED
429end
430note break
431if eq key 0x627265616b
432 ret OT_RESERVED
433end
434note continue
435if eq key 0x636f6e74696e7565
436 ret OT_RESERVED
437end
438ret OT_NAME
439end
440
441fn lex_pair key uint to uint
442note ->
443if eq key 0x2d3e
444 ret OT_ARROW
445end
446note ..
447if eq key 0x2e2e
448 ret OT_DOTDOT
449end
450note &&
451if eq key 0x2626
452 ret OT_ANDAND
453end
454note ||
455if eq key 0x7c7c
456 ret OT_OROR
457end
458note <<
459if eq key 0x3c3c
460 ret OT_SHL
461end
462note >>
463if eq key 0x3e3e
464 ret OT_SHR
465end
466note ==
467if eq key 0x3d3d
468 ret OT_EQ
469end
470note !=
471if eq key 0x213d
472 ret OT_NE
473end
474note <=
475if eq key 0x3c3d
476 ret OT_LE
477end
478note >=
479if eq key 0x3e3d
480 ret OT_GE
481end
482ret OT_EOF
483end
484
485fn lex_single key uint to uint
486note (
487if eq key 0x28
488 ret OT_LPAREN
489end
490note )
491if eq key 0x29
492 ret OT_RPAREN
493end
494note {
495if eq key 0x7b
496 ret OT_LBRACE
497end
498note }
499if eq key 0x7d
500 ret OT_RBRACE
501end
502note [
503if eq key 0x5b
504 ret OT_LBRACK
505end
506note ]
507if eq key 0x5d
508 ret OT_RBRACK
509end
510note ,
511if eq key 0x2c
512 ret OT_COMMA
513end
514note ;
515if eq key 0x3b
516 ret OT_SEMI
517end
518note :
519if eq key 0x3a
520 ret OT_COLON
521end
522note =
523if eq key 0x3d
524 ret OT_ASSIGN
525end
526note +
527if eq key 0x2b
528 ret OT_PLUS
529end
530note -
531if eq key 0x2d
532 ret OT_MINUS
533end
534note *
535if eq key 0x2a
536 ret OT_STAR
537end
538note /
539if eq key 0x2f
540 ret OT_SLASH
541end
542note %
543if eq key 0x25
544 ret OT_PERCENT
545end
546note &
547if eq key 0x26
548 ret OT_AMP
549end
550note |
551if eq key 0x7c
552 ret OT_PIPE
553end
554note ^
555if eq key 0x5e
556 ret OT_CARET
557end
558note ~
559if eq key 0x7e
560 ret OT_TILDE
561end
562note !
563if eq key 0x21
564 ret OT_BANG
565end
566note <
567if eq key 0x3c
568 ret OT_LT
569end
570note >
571if eq key 0x3e
572 ret OT_GT
573end
574note .
575if eq key 0x2e
576 ret OT_DOT
577end
578ret OT_EOF
579end
580
581note Leave pos at the first token byte. Only LF advances the line number.
582note On an unfinished block comment, report the position of its opening slash.
583fn lex_trivia src view uchar * cursor edit lexstate to bool
584pos = get cursor pos
585line = get cursor line
586col = get cursor col
587while lt pos len src bound 9223372036854775807
588 c = get src pos
589 if eq c 10
590 line = add line 1
591 col = 1
592 pos = add pos 1
593 continue
594 end
595 if or eq c 32 or eq c 9 eq c 13
596 col = add col 1
597 pos = add pos 1
598 continue
599 end
600 if ne c 47
601 break
602 end
603 if eq add pos 1 len src
604 break
605 end
606 nextbyte = get src add pos 1
607 if eq nextbyte 47
608 while lt pos len src bound 9223372036854775807
609 if eq get src pos 10
610 break
611 end
612 pos = add pos 1
613 col = add col 1
614 end
615 continue
616 end
617 if ne nextbyte 42
618 break
619 end
620 put cursor line line
621 put cursor col col
622 pos = add pos 2
623 col = add col 2
624 while true bound 9223372036854775807
625 if eq pos len src
626 ret false
627 end
628 c = get src pos
629 pos = add pos 1
630 col = add col 1
631 if eq c 42
632 if lt pos len src
633 if eq get src pos 47
634 pos = add pos 1
635 col = add col 1
636 break
637 end
638 end
639 end
640 if eq c 10
641 line = add line 1
642 col = 1
643 end
644 end
645end
646put cursor pos pos
647put cursor line line
648put cursor col col
649ret true
650end
651
652note On entry col is the previous token's start column and width its byte length.
653note Tokens never cross a line. Trivia updates both coordinates before the capacity check.
654export fn osc_lex_next src view uchar * cursor edit lexstate room ulong namemax ulong to ulong
655value = new ulong 1 0
656put cursor col add get cursor col get cursor width
657if not lex_trivia src cursor
658 ret 3
659end
660if eq room 0
661 ret 4
662end
663start = get cursor pos
664put cursor start start
665put cursor width 0
666put cursor value 0
667put cursor kind OT_EOF
668if eq start len src
669 ret 0
670end
671c = get src start
672pos = add start 1
673if or lex_alpha c lex_digit c
674 while lt pos len src bound 9223372036854775807
675 ch = get src pos
676 if not or lex_alpha ch lex_digit ch
677 break
678 end
679 pos = add pos 1
680 end
681 size = sub pos start
682 put cursor pos pos
683 put cursor width uint size
684 if lex_digit c
685 status = osc_parse_literal part src start size value
686 if ne status 0
687 ret status
688 end
689 put cursor value get value 0
690 put cursor kind OT_INT
691 else
692 if gt ulong uint size namemax
693 ret 5
694 end
695 put cursor kind lex_word part src start size
696 end
697 ret 0
698end
699kind = OT_EOF
700if lt pos len src
701 pair = or shl uint c 8 uint get src pos
702 kind = lex_pair pair
703end
704if ne kind OT_EOF
705 pos = add pos 1
706else
707 kind = lex_single uint c
708 if eq kind OT_EOF
709 ret 6
710 end
711end
712put cursor kind kind
713put cursor pos pos
714put cursor width uint sub pos start
715ret 0
716end
717
718fn save_token dest edit osctoken cursor view lexstate to void
719 put dest kind ushort get cursor kind
720 put dest line get cursor line
721 put dest col get cursor col
722 put dest off uint get cursor start
723 put dest width get cursor width
724 put dest value get cursor value
725end
726
727note Refusals preserve completed tokens and the partially written failing
728note token. Capacity and unterminated comments create no token.
729export fn osc_lex_stream src view uchar * cursor edit lexstate dest edit uchar * count edit ulong 1 to ulong
730 token = new uchar 32 0
731 n = ulong 0
732 cap = div len dest 32
733 while true bound 4294967296
734 status = osc_lex_next src cursor sub cap n 63
735 if and ne status 3 ne status 4
736 save_token token cursor
737 offset = mul n 32
738 for j ulong 32
739 put dest add offset j get token j
740 end
741 end
742 if ne status 0
743 put count 0 n
744 ret status
745 end
746 n = add n 1
747 if eq get cursor kind OT_EOF
748 put count 0 n
749 ret 0
750 end
751 end
752 trap 1
753end