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_type.om

188 lines · 4849 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Parse a scalar, fixed array, reference body or parameter type. The longest
3note production is "& mut [ scalar ; INT ]". Input beyond EOF is padded with OT_EOF.
4note used counts consumed tokens, including on failure. Named references stop
5note at NAME so the caller can resolve it against the current declaration table.
6note bridge c-define OSC_TYPE_WINDOW 7
7note bridge c-define OSC_TYPE_SCALAR 1
8note bridge c-define OSC_TYPE_ARRAY 2
9note bridge c-define OSC_TYPE_REF_BODY 3
10note bridge c-define OSC_TYPE_PARAM 4
11note bridge c-define OSC_TYPE_EXPECT_SCALAR 1
12note bridge c-define OSC_TYPE_SLICE_ESCAPE 2
13note bridge c-define OSC_TYPE_EXPECT_OPEN 3
14note bridge c-define OSC_TYPE_EXPECT_SEMI 4
15note bridge c-define OSC_TYPE_EXPECT_LENGTH 5
16note bridge c-define OSC_TYPE_ZERO_LENGTH 6
17note bridge c-define OSC_TYPE_LONG_ARRAY 7
18note bridge c-define OSC_TYPE_EXPECT_CLOSE 8
19note bridge c-define OSC_TYPE_NAMED_REF 9
20note bridge c-define OSC_TYPE_BARE_NAME 10
21note bridge c-define OSC_TYPE_LOCAL_ONLY 11
22note bridge c-type typerec OscParsedType
23om2
24
25layout typerec 24
26field used uint at 0 little
27field scalar uint at 4 little
28field refkind uint at 8 little
29field elem uint at 12 little
30field count ulong at 16 little
31end
32
33note Token and type IDs are the public enums in osc_lex.h and osc_ir.h.
34const OT_NAME ushort 1
35const OT_INT ushort 2
36const OT_MUT ushort 6
37const OT_OWN ushort 7
38const OT_U8 ushort 21
39const OT_I64 ushort 28
40const OT_BOOL ushort 29
41const OT_LBRACK ushort 34
42const OT_RBRACK ushort 35
43const OT_SEMI ushort 37
44const OT_AMP ushort 47
45const OT_HANDLE ushort 66
46const OT_POOL ushort 65
47const OT_BYTES ushort 68
48const OT_CELLS ushort 69
49const OSC_TYPE_SCALAR ulong 1
50const OSC_TYPE_ARRAY ulong 2
51const OSC_TYPE_REF_BODY ulong 3
52const OSC_TYPE_PARAM ulong 4
53const OSC_TYPE_EXPECT_SCALAR ulong 1
54const OSC_TYPE_SLICE_ESCAPE ulong 2
55const OSC_TYPE_EXPECT_OPEN ulong 3
56const OSC_TYPE_EXPECT_SEMI ulong 4
57const OSC_TYPE_EXPECT_LENGTH ulong 5
58const OSC_TYPE_ZERO_LENGTH ulong 6
59const OSC_TYPE_LONG_ARRAY ulong 7
60const OSC_TYPE_EXPECT_CLOSE ulong 8
61const OSC_TYPE_NAMED_REF ulong 9
62const OSC_TYPE_BARE_NAME ulong 10
63const OSC_TYPE_LOCAL_ONLY ulong 11
64const TYPE_BOOL uint 1
65const TYPE_U8 uint 2
66const TYPE_REF uint 10
67const TYPE_BYTES uint 11
68const TYPE_CELLS uint 12
69const REF_OWN uint 1
70const REF_SHARED uint 2
71const REF_MUT uint 3
72
73fn type_scalar kind ushort to uint
74if eq kind OT_BOOL
75 ret TYPE_BOOL
76end
77if and ge kind OT_U8 le kind OT_I64
78 ret add TYPE_U8 uint sub kind OT_U8
79end
80ret 0
81end
82
83fn type_scalar_error kind ushort to ulong
84if or eq kind OT_BYTES eq kind OT_CELLS
85 ret OSC_TYPE_SLICE_ESCAPE
86end
87ret OSC_TYPE_EXPECT_SCALAR
88end
89
90fn type_array kinds view ushort 7 values view ulong 7 pos ulong limit ulong result edit typerec to ulong
91if ne get kinds pos OT_LBRACK
92 ret OSC_TYPE_EXPECT_OPEN
93end
94pos = add pos 1
95put result used uint pos
96kind = get kinds pos
97scalar = type_scalar kind
98if eq scalar 0
99 ret type_scalar_error kind
100end
101pos = add pos 1
102put result used uint pos
103if ne get kinds pos OT_SEMI
104 ret OSC_TYPE_EXPECT_SEMI
105end
106pos = add pos 1
107put result used uint pos
108if ne get kinds pos OT_INT
109 ret OSC_TYPE_EXPECT_LENGTH
110end
111size = get values pos
112if eq size 0
113 ret OSC_TYPE_ZERO_LENGTH
114end
115if gt size limit
116 ret OSC_TYPE_LONG_ARRAY
117end
118pos = add pos 1
119put result used uint pos
120if ne get kinds pos OT_RBRACK
121 ret OSC_TYPE_EXPECT_CLOSE
122end
123put result used uint add pos 1
124put result scalar TYPE_REF
125put result elem scalar
126put result count size
127ret 0
128end
129
130export fn osc_read_type kinds view ushort 7 values view ulong 7 form ulong refkind ulong limit ulong result edit typerec to ulong
131put result used 0
132put result scalar 0
133put result refkind uint refkind
134put result elem 0
135put result count 0
136pos = ulong 0
137kind = get kinds pos
138if eq form OSC_TYPE_PARAM
139 if eq kind OT_OWN
140 put result refkind REF_OWN
141 pos = 1
142 form = OSC_TYPE_REF_BODY
143 end
144 if eq kind OT_AMP
145 put result refkind REF_SHARED
146 pos = 1
147 if eq get kinds pos OT_MUT
148 put result refkind REF_MUT
149 pos = 2
150 end
151 form = OSC_TYPE_REF_BODY
152 end
153 put result used uint pos
154 if eq form OSC_TYPE_PARAM
155 if eq kind OT_NAME
156 ret OSC_TYPE_BARE_NAME
157 end
158 if or eq kind OT_HANDLE eq kind OT_POOL
159 ret OSC_TYPE_LOCAL_ONLY
160 end
161 if or eq kind OT_BYTES eq kind OT_CELLS
162 if eq kind OT_BYTES
163 put result scalar TYPE_BYTES
164 else
165 put result scalar TYPE_CELLS
166 end
167 put result used 1
168 ret 0
169 end
170 end
171end
172if eq form OSC_TYPE_REF_BODY
173 if eq get kinds pos OT_NAME
174 ret OSC_TYPE_NAMED_REF
175 end
176 form = OSC_TYPE_ARRAY
177end
178if eq form OSC_TYPE_ARRAY
179 ret type_array kinds values pos limit result
180end
181scalar = type_scalar kind
182if eq scalar 0
183 ret type_scalar_error kind
184end
185put result scalar scalar
186put result used 1
187ret 0
188end