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
omega-mv / om / compiler / osc_statement.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Parse bounded loop headers. The maximum spelling after "for" is seven |
| 3 | note tokens: NAME in - INT .. - INT. used identifies the first unconsumed |
| 4 | note token even on refusal. Partial results mirror the source-order AST writes. |
| 5 | note bridge c-type imprec OscImportParse |
| 6 | note bridge c-type forrange OscForRange |
| 7 | note bridge c-define OSC_FOR_WINDOW 7 |
| 8 | note bridge c-define OSC_FOR_NAME 1 |
| 9 | note bridge c-define OSC_FOR_IN 2 |
| 10 | note bridge c-define OSC_FOR_INT 3 |
| 11 | note bridge c-define OSC_FOR_OVERFLOW 4 |
| 12 | note bridge c-define OSC_FOR_DOTS 5 |
| 13 | note bridge c-define OSC_FOR_TRIPS 6 |
| 14 | note bridge c-define OSC_BOUND_KEYWORD 1 |
| 15 | note bridge c-define OSC_BOUND_INT 2 |
| 16 | note bridge c-define OSC_BOUND_RANGE 3 |
| 17 | note bridge c-define OSC_IMPORT_POSITION 1 |
| 18 | note bridge c-define OSC_IMPORT_NAME 2 |
| 19 | note bridge c-define OSC_IMPORT_FULL 3 |
| 20 | note bridge c-define OSC_IMPORT_DUP 4 |
| 21 | note bridge c-define OSC_IMPORT_SEMI 5 |
| 22 | note bridge c-type stmtplan OscStmtPlan |
| 23 | note bridge c-define OSC_ST_CALL 70 |
| 24 | note bridge c-define OSC_ST_EXPR_A 71 |
| 25 | note bridge c-define OSC_ST_EXPR_B 72 |
| 26 | note bridge c-define OSC_ST_BORROW_A 73 |
| 27 | note bridge c-define OSC_ST_INDEX 74 |
| 28 | note bridge c-define OSC_ST_STORE 75 |
| 29 | om2 |
| 30 | |
| 31 | layout forrange 24 |
| 32 | field used uint at 0 little |
| 33 | field lower_at uint at 4 little |
| 34 | field low ulong at 8 little |
| 35 | field high ulong at 16 little |
| 36 | end |
| 37 | |
| 38 | note The longest name-led statement is a subscripted store: index, ], =, |
| 39 | note value, ;. Each step is punctuation to consume or a child AST operation. |
| 40 | layout stmtplan 32 |
| 41 | field skip uint at 0 little |
| 42 | field steps uint at 4 little |
| 43 | field actions uint 5 at 8 little |
| 44 | field nodekind uint at 28 little |
| 45 | end |
| 46 | |
| 47 | layout imprec 20 |
| 48 | field started uint at 0 little |
| 49 | field stride uint at 4 little |
| 50 | field nameoff uint at 8 little |
| 51 | field match uint at 12 little |
| 52 | field nextkind uint at 16 little |
| 53 | end |
| 54 | |
| 55 | const NAME ushort 1 |
| 56 | const INTEGER ushort 2 |
| 57 | const BOUND ushort 12 |
| 58 | const IN ushort 14 |
| 59 | const SEMI ushort 37 |
| 60 | const DOTDOT ushort 40 |
| 61 | const MINUS ushort 43 |
| 62 | const SIGN ulong 0x8000000000000000 |
| 63 | |
| 64 | const OSC_FOR_NAME ulong 1 |
| 65 | const OSC_FOR_IN ulong 2 |
| 66 | const OSC_FOR_INT ulong 3 |
| 67 | const OSC_FOR_OVERFLOW ulong 4 |
| 68 | const OSC_FOR_DOTS ulong 5 |
| 69 | const OSC_FOR_TRIPS ulong 6 |
| 70 | |
| 71 | fn range_end kinds view ushort 7 values view ulong 7 range edit forrange upper ulong to ulong |
| 72 | pos = ulong get range used |
| 73 | negative = eq get kinds pos MINUS |
| 74 | if negative |
| 75 | pos = add pos 1 |
| 76 | put range used uint pos |
| 77 | end |
| 78 | if ne get kinds pos INTEGER |
| 79 | ret OSC_FOR_INT |
| 80 | end |
| 81 | v = get values pos |
| 82 | limit = sub SIGN 1 |
| 83 | if negative |
| 84 | limit = SIGN |
| 85 | end |
| 86 | if gt v limit |
| 87 | ret OSC_FOR_OVERFLOW |
| 88 | end |
| 89 | if negative |
| 90 | v = sub ulong 0 v |
| 91 | end |
| 92 | if ne upper 0 |
| 93 | put range high v |
| 94 | else |
| 95 | put range low v |
| 96 | end |
| 97 | put range used uint add pos 1 |
| 98 | ret 0 |
| 99 | end |
| 100 | |
| 101 | export fn osc_for_range kinds view ushort 7 values view ulong 7 limit ulong range edit forrange to ulong |
| 102 | put range used 0 |
| 103 | put range lower_at 0 |
| 104 | put range low 0 |
| 105 | put range high 0 |
| 106 | if ne get kinds 0 NAME |
| 107 | ret OSC_FOR_NAME |
| 108 | end |
| 109 | put range used 1 |
| 110 | if ne get kinds 1 IN |
| 111 | ret OSC_FOR_IN |
| 112 | end |
| 113 | put range used 2 |
| 114 | put range lower_at 2 |
| 115 | status = range_end kinds values range 0 |
| 116 | if ne status 0 |
| 117 | ret status |
| 118 | end |
| 119 | pos = ulong get range used |
| 120 | if ne get kinds pos DOTDOT |
| 121 | ret OSC_FOR_DOTS |
| 122 | end |
| 123 | put range used uint add pos 1 |
| 124 | status = range_end kinds values range 1 |
| 125 | if ne status 0 |
| 126 | ret status |
| 127 | end |
| 128 | lo = get range low |
| 129 | hi = get range high |
| 130 | note Flipping the sign bit orders the signed ends. Subtract in ulong so that |
| 131 | note the full INT64_MIN to INT64_MAX distance is defined before the limit test. |
| 132 | if or lt xor hi SIGN xor lo SIGN gt sub hi lo limit |
| 133 | ret OSC_FOR_TRIPS |
| 134 | end |
| 135 | ret 0 |
| 136 | end |
| 137 | |
| 138 | note Both while and arena require "bound INT". |
| 139 | const OSC_BOUND_KEYWORD ulong 1 |
| 140 | const OSC_BOUND_INT ulong 2 |
| 141 | const OSC_BOUND_RANGE ulong 3 |
| 142 | |
| 143 | export fn osc_read_bound keyword ulong kind ulong value ulong limit ulong to ulong |
| 144 | if ne keyword ulong BOUND |
| 145 | ret OSC_BOUND_KEYWORD |
| 146 | end |
| 147 | if ne kind ulong INTEGER |
| 148 | ret OSC_BOUND_INT |
| 149 | end |
| 150 | if or eq value 0 gt value limit |
| 151 | ret OSC_BOUND_RANGE |
| 152 | end |
| 153 | ret 0 |
| 154 | end |
| 155 | |
| 156 | fn same_import table view uchar * old ulong dest ulong width ulong to bool |
| 157 | j = ulong 0 |
| 158 | while 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 |
| 163 | end |
| 164 | ret eq get table add old width 0 |
| 165 | end |
| 166 | |
| 167 | fn record_import spelling view uchar * table edit uchar * dest ulong to ulong |
| 168 | width = len spelling |
| 169 | capacity = sub len table dest |
| 170 | if ge width capacity |
| 171 | width = sub capacity 1 |
| 172 | end |
| 173 | i = ulong 0 |
| 174 | while lt i width |
| 175 | put table add dest i get spelling i |
| 176 | i = add i 1 |
| 177 | end |
| 178 | put table add dest width 0 |
| 179 | ret width |
| 180 | end |
| 181 | |
| 182 | note Match and record "import NAME ;". C owns the table and supplies its |
| 183 | note byte layout. Installing the candidate name before duplicate/semicolon |
| 184 | note checks preserves the visible partial declaration on either refusal. |
| 185 | const OSC_IMPORT_POSITION ulong 1 |
| 186 | const OSC_IMPORT_NAME ulong 2 |
| 187 | const OSC_IMPORT_FULL ulong 3 |
| 188 | const OSC_IMPORT_DUP ulong 4 |
| 189 | const OSC_IMPORT_SEMI ulong 5 |
| 190 | |
| 191 | export fn osc_read_import state edit imprec kind ulong spelling view uchar * table edit uchar * to ulong |
| 192 | put state match 0 |
| 193 | stride = ulong get state stride |
| 194 | nameoff = ulong get state nameoff |
| 195 | if ne get state started 0 |
| 196 | ret OSC_IMPORT_POSITION |
| 197 | end |
| 198 | if ne kind ulong NAME |
| 199 | ret OSC_IMPORT_NAME |
| 200 | end |
| 201 | note The caller passes populated entries plus one writable slot, or an empty |
| 202 | note view when full. Each stored name is terminated within its own record. |
| 203 | if eq len table 0 |
| 204 | ret OSC_IMPORT_FULL |
| 205 | end |
| 206 | slot = sub len table stride |
| 207 | dest = add slot nameoff |
| 208 | width = record_import spelling table dest |
| 209 | base = ulong 0 |
| 210 | while 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 |
| 216 | end |
| 217 | if ne get state nextkind uint SEMI |
| 218 | ret OSC_IMPORT_SEMI |
| 219 | end |
| 220 | ret 0 |
| 221 | end |
| 222 | |
| 223 | note Keep the last complete token on capacity refusal. Parentheses suppress |
| 224 | note the space immediately after an opening or before a closing parenthesis. |
| 225 | note The token ABI stores its offset and byte length as unsigned 32-bit fields. |
| 226 | fn 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 |
| 255 | end |
| 256 | |
| 257 | export 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 |
| 263 | end |
| 264 | |
| 265 | fn 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 |
| 271 | end |
| 272 | |
| 273 | fn 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 |
| 277 | end |
| 278 | |
| 279 | export 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 |
| 297 | end |
| 298 | |
| 299 | note Each parser node starts with absent children, sibling and checker |
| 300 | note symbols. Its source token index and coordinates survive every later pass. |
| 301 | fn 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 |
| 305 | end |
| 306 | |
| 307 | export 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 |
| 321 | end |
| 322 | |
| 323 | const LPAREN uint 30 |
| 324 | const RPAREN uint 31 |
| 325 | const LBRACK uint 34 |
| 326 | const RBRACK uint 35 |
| 327 | const ASSIGN uint 41 |
| 328 | const AMP uint 47 |
| 329 | const DOT uint 63 |
| 330 | const RETURN uint 15 |
| 331 | const RESERVED uint 3 |
| 332 | const CALL_CHILD uint 70 |
| 333 | const EXPR_A uint 71 |
| 334 | const EXPR_B uint 72 |
| 335 | const BORROW_A uint 73 |
| 336 | const INDEX_CHILD uint 74 |
| 337 | const STORE_VALUE uint 75 |
| 338 | note Node IDs come from OscNodeKind, checked in the parser bridge. |
| 339 | const N_ASSIGN uint 8 |
| 340 | const N_STORE uint 9 |
| 341 | const N_RETURN uint 13 |
| 342 | const N_CALLSTMT uint 14 |
| 343 | const N_FSTORE uint 26 |
| 344 | const N_PFREE uint 31 |
| 345 | |
| 346 | fn is_free word view uchar * to bool |
| 347 | if ne len word 4 |
| 348 | ret false |
| 349 | end |
| 350 | ret and eq get word 0 102 and eq get word 1 114 and eq get word 2 101 eq get word 3 101 |
| 351 | end |
| 352 | |
| 353 | fn end_statement plan edit stmtplan count ulong to ulong |
| 354 | put plan actions count uint SEMI |
| 355 | put plan steps uint add count 1 |
| 356 | ret 0 |
| 357 | end |
| 358 | |
| 359 | fn rhs_action kind ulong to uint |
| 360 | if eq uint kind AMP |
| 361 | ret BORROW_A |
| 362 | end |
| 363 | ret EXPR_A |
| 364 | end |
| 365 | |
| 366 | export fn osc_statement_plan plan edit stmtplan kind ulong nextkind ulong third ulong spelling view uchar * to ulong |
| 367 | if 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 |
| 375 | end |
| 376 | if 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 |
| 381 | end |
| 382 | if 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 |
| 396 | end |
| 397 | if 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 |
| 402 | end |
| 403 | if 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 |
| 411 | end |
| 412 | ret 1 |
| 413 | end |