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