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

413 lines · 10475 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Parse bounded loop headers. The maximum spelling after "for" is seven
3note tokens: NAME in - INT .. - INT. used identifies the first unconsumed
4note token even on refusal. Partial results mirror the source-order AST writes.
5note bridge c-type imprec OscImportParse
6note bridge c-type forrange OscForRange
7note bridge c-define OSC_FOR_WINDOW 7
8note bridge c-define OSC_FOR_NAME 1
9note bridge c-define OSC_FOR_IN 2
10note bridge c-define OSC_FOR_INT 3
11note bridge c-define OSC_FOR_OVERFLOW 4
12note bridge c-define OSC_FOR_DOTS 5
13note bridge c-define OSC_FOR_TRIPS 6
14note bridge c-define OSC_BOUND_KEYWORD 1
15note bridge c-define OSC_BOUND_INT 2
16note bridge c-define OSC_BOUND_RANGE 3
17note bridge c-define OSC_IMPORT_POSITION 1
18note bridge c-define OSC_IMPORT_NAME 2
19note bridge c-define OSC_IMPORT_FULL 3
20note bridge c-define OSC_IMPORT_DUP 4
21note bridge c-define OSC_IMPORT_SEMI 5
22note bridge c-type stmtplan OscStmtPlan
23note bridge c-define OSC_ST_CALL 70
24note bridge c-define OSC_ST_EXPR_A 71
25note bridge c-define OSC_ST_EXPR_B 72
26note bridge c-define OSC_ST_BORROW_A 73
27note bridge c-define OSC_ST_INDEX 74
28note bridge c-define OSC_ST_STORE 75
29om2
30
31layout forrange 24
32field used uint at 0 little
33field lower_at uint at 4 little
34field low ulong at 8 little
35field high ulong at 16 little
36end
37
38note The longest name-led statement is a subscripted store: index, ], =,
39note value, ;. Each step is punctuation to consume or a child AST operation.
40layout stmtplan 32
41field skip uint at 0 little
42field steps uint at 4 little
43field actions uint 5 at 8 little
44field nodekind uint at 28 little
45end
46
47layout imprec 20
48field started uint at 0 little
49field stride uint at 4 little
50field nameoff uint at 8 little
51field match uint at 12 little
52field nextkind uint at 16 little
53end
54
55const NAME ushort 1
56const INTEGER ushort 2
57const BOUND ushort 12
58const IN ushort 14
59const SEMI ushort 37
60const DOTDOT ushort 40
61const MINUS ushort 43
62const SIGN ulong 0x8000000000000000
63
64const OSC_FOR_NAME ulong 1
65const OSC_FOR_IN ulong 2
66const OSC_FOR_INT ulong 3
67const OSC_FOR_OVERFLOW ulong 4
68const OSC_FOR_DOTS ulong 5
69const OSC_FOR_TRIPS ulong 6
70
71fn range_end kinds view ushort 7 values view ulong 7 range edit forrange upper ulong to ulong
72pos = ulong get range used
73negative = eq get kinds pos MINUS
74if negative
75 pos = add pos 1
76 put range used uint pos
77end
78if ne get kinds pos INTEGER
79 ret OSC_FOR_INT
80end
81v = get values pos
82limit = sub SIGN 1
83if negative
84 limit = SIGN
85end
86if gt v limit
87 ret OSC_FOR_OVERFLOW
88end
89if negative
90 v = sub ulong 0 v
91end
92if ne upper 0
93 put range high v
94else
95 put range low v
96end
97put range used uint add pos 1
98ret 0
99end
100
101export fn osc_for_range kinds view ushort 7 values view ulong 7 limit ulong range edit forrange to ulong
102put range used 0
103put range lower_at 0
104put range low 0
105put range high 0
106if ne get kinds 0 NAME
107 ret OSC_FOR_NAME
108end
109put range used 1
110if ne get kinds 1 IN
111 ret OSC_FOR_IN
112end
113put range used 2
114put range lower_at 2
115status = range_end kinds values range 0
116if ne status 0
117 ret status
118end
119pos = ulong get range used
120if ne get kinds pos DOTDOT
121 ret OSC_FOR_DOTS
122end
123put range used uint add pos 1
124status = range_end kinds values range 1
125if ne status 0
126 ret status
127end
128lo = get range low
129hi = get range high
130note Flipping the sign bit orders the signed ends. Subtract in ulong so that
131note the full INT64_MIN to INT64_MAX distance is defined before the limit test.
132if or lt xor hi SIGN xor lo SIGN gt sub hi lo limit
133 ret OSC_FOR_TRIPS
134end
135ret 0
136end
137
138note Both while and arena require "bound INT".
139const OSC_BOUND_KEYWORD ulong 1
140const OSC_BOUND_INT ulong 2
141const OSC_BOUND_RANGE ulong 3
142
143export fn osc_read_bound keyword ulong kind ulong value ulong limit ulong to ulong
144if ne keyword ulong BOUND
145 ret OSC_BOUND_KEYWORD
146end
147if ne kind ulong INTEGER
148 ret OSC_BOUND_INT
149end
150if or eq value 0 gt value limit
151 ret OSC_BOUND_RANGE
152end
153ret 0
154end
155
156fn same_import table view uchar * old ulong dest ulong width ulong to bool
157j = ulong 0
158while lt j width
159 if ne get table add old j get table add dest j
160 ret false
161 end
162 j = add j 1
163end
164ret eq get table add old width 0
165end
166
167fn record_import spelling view uchar * table edit uchar * dest ulong to ulong
168width = len spelling
169capacity = sub len table dest
170if ge width capacity
171 width = sub capacity 1
172end
173i = ulong 0
174while lt i width
175 put table add dest i get spelling i
176 i = add i 1
177end
178put table add dest width 0
179ret width
180end
181
182note Match and record "import NAME ;". C owns the table and supplies its
183note byte layout. Installing the candidate name before duplicate/semicolon
184note checks preserves the visible partial declaration on either refusal.
185const OSC_IMPORT_POSITION ulong 1
186const OSC_IMPORT_NAME ulong 2
187const OSC_IMPORT_FULL ulong 3
188const OSC_IMPORT_DUP ulong 4
189const OSC_IMPORT_SEMI ulong 5
190
191export fn osc_read_import state edit imprec kind ulong spelling view uchar * table edit uchar * to ulong
192put state match 0
193stride = ulong get state stride
194nameoff = ulong get state nameoff
195if ne get state started 0
196 ret OSC_IMPORT_POSITION
197end
198if ne kind ulong NAME
199 ret OSC_IMPORT_NAME
200end
201note The caller passes populated entries plus one writable slot, or an empty
202note view when full. Each stored name is terminated within its own record.
203if eq len table 0
204 ret OSC_IMPORT_FULL
205end
206slot = sub len table stride
207dest = add slot nameoff
208width = record_import spelling table dest
209base = ulong 0
210while lt base slot
211 if same_import table add base nameoff dest width
212 put state match uint div base stride
213 ret OSC_IMPORT_DUP
214 end
215 base = add base stride
216end
217if ne get state nextkind uint SEMI
218 ret OSC_IMPORT_SEMI
219end
220ret 0
221end
222
223note Keep the last complete token on capacity refusal. Parentheses suppress
224note the space immediately after an opening or before a closing parenthesis.
225note The token ABI stores its offset and byte length as unsigned 32-bit fields.
226fn clause_spelling dst edit uchar 256 used ulong space bool source view uchar * offset_and_len ulong to ulong
227 first = shr offset_and_len 32
228 width = and offset_and_len 0xffffffff
229 if or ge used len dst gt first len source
230 ret len dst
231 end
232 if gt width sub len source first
233 ret len dst
234 end
235 room = sub sub len dst 1 used
236 if space
237 if eq room 0
238 ret len dst
239 end
240 room = sub room 1
241 end
242 if gt width room
243 ret len dst
244 end
245 if space
246 put dst used 32
247 used = add used 1
248 end
249 for i ulong width
250 put dst add used i get source add first i
251 end
252 used = add used width
253 put dst used 0
254 ret used
255end
256
257export fn osc_clause_word dst edit uchar 256 used ulong prev ulong kind ulong word view uchar * to ulong
258 if ge len word len dst
259 ret len dst
260 end
261 space = and ne used 0 and ne prev 30 ne kind 31
262 ret clause_spelling dst used space word len word
263end
264
265fn clause_token_field tokens view uchar * pos ulong width ulong to ulong
266 value = ulong 0
267 for i ulong width
268 value = or value shl ulong get tokens add pos i mul i 8
269 end
270 ret value
271end
272
273fn clause_segment tokens view uchar * pos ulong to ulong
274 offset = clause_token_field tokens add pos 12 4
275 width = clause_token_field tokens add pos 16 4
276 ret or shl offset 32 width
277end
278
279export fn osc_clause_text dst edit uchar 256 tokens view uchar * source view uchar * to ulong
280 used = ulong 0
281 prev = ulong 0
282 put dst 0 0
283 if ne rem len tokens 32 0
284 ret len dst
285 end
286 pos = ulong 0
287 while lt pos len tokens
288 kind = clause_token_field tokens pos 2
289 used = clause_spelling dst used and ne used 0 and ne prev 30 ne kind 31 source clause_segment tokens pos
290 if eq used len dst
291 ret used
292 end
293 prev = kind
294 pos = add pos 32
295 end
296 ret used
297end
298
299note Each parser node starts with absent children, sibling and checker
300note symbols. Its source token index and coordinates survive every later pass.
301fn node_word node edit uchar 104 pos ulong width ulong value ulong to void
302 for i ulong width
303 put node add pos i uchar and shr value mul i 8 255
304 end
305end
306
307export fn osc_new_node node out uchar 104 kind ulong token ulong line ulong col ulong to void
308 for i ulong 104
309 put node i 0
310 end
311 node_word node 0 2 kind
312 node_word node 4 4 line
313 node_word node 8 4 col
314 for i ulong 16
315 put node add 12 i 255
316 end
317 node_word node 28 4 token
318 for i ulong 8
319 put node add 88 i 255
320 end
321end
322
323const LPAREN uint 30
324const RPAREN uint 31
325const LBRACK uint 34
326const RBRACK uint 35
327const ASSIGN uint 41
328const AMP uint 47
329const DOT uint 63
330const RETURN uint 15
331const RESERVED uint 3
332const CALL_CHILD uint 70
333const EXPR_A uint 71
334const EXPR_B uint 72
335const BORROW_A uint 73
336const INDEX_CHILD uint 74
337const STORE_VALUE uint 75
338note Node IDs come from OscNodeKind, checked in the parser bridge.
339const N_ASSIGN uint 8
340const N_STORE uint 9
341const N_RETURN uint 13
342const N_CALLSTMT uint 14
343const N_FSTORE uint 26
344const N_PFREE uint 31
345
346fn is_free word view uchar * to bool
347if ne len word 4
348 ret false
349end
350ret and eq get word 0 102 and eq get word 1 114 and eq get word 2 101 eq get word 3 101
351end
352
353fn end_statement plan edit stmtplan count ulong to ulong
354put plan actions count uint SEMI
355put plan steps uint add count 1
356ret 0
357end
358
359fn rhs_action kind ulong to uint
360if eq uint kind AMP
361 ret BORROW_A
362end
363ret EXPR_A
364end
365
366export fn osc_statement_plan plan edit stmtplan kind ulong nextkind ulong third ulong spelling view uchar * to ulong
367if eq uint kind RETURN
368 put plan nodekind N_RETURN
369 put plan skip 1
370 if eq uint nextkind uint SEMI
371 ret end_statement plan 0
372 end
373 put plan actions 0 rhs_action nextkind
374 ret end_statement plan 1
375end
376if eq uint nextkind LPAREN
377 put plan nodekind N_CALLSTMT
378 put plan skip 0
379 put plan actions 0 CALL_CHILD
380 ret end_statement plan 1
381end
382if eq uint nextkind DOT
383 if and eq uint third RESERVED is_free spelling
384 put plan nodekind N_PFREE
385 put plan skip 3
386 put plan actions 0 LPAREN
387 put plan actions 1 EXPR_A
388 put plan actions 2 RPAREN
389 ret end_statement plan 3
390 end
391 put plan nodekind N_FSTORE
392 put plan skip 0
393 put plan actions 0 ASSIGN
394 put plan actions 1 EXPR_B
395 ret end_statement plan 2
396end
397if eq uint nextkind ASSIGN
398 put plan nodekind N_ASSIGN
399 put plan skip 2
400 put plan actions 0 rhs_action third
401 ret end_statement plan 1
402end
403if eq uint nextkind LBRACK
404 put plan nodekind N_STORE
405 put plan skip 2
406 put plan actions 0 INDEX_CHILD
407 put plan actions 1 RBRACK
408 put plan actions 2 ASSIGN
409 put plan actions 3 STORE_VALUE
410 ret end_statement plan 4
411end
412ret 1
413end