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/src/language/omega_lex.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 / src / language / omega_lex.om` 635 lines ยท 11199 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Pure seeded OM-2 Surface Language V0 lexer: a faithful port of src/language/omega_lex.c 3 note (with the src/language/lang_core.om classifiers it calls). Same tokens, same columns, same 4 note error texts and return codes. Depends on src/visor/visor.om. 5 note 6 note The line is olx_line[0..olx_llen), a C string: olw_eval_sbuf stops it at the first NUL. 7 note Token kinds are the OmegaTokKind values of omega_lex.h: 8 note 0=EOF 1=IDENT 2=INT 3=LET 4=FN 5=REQUIRES 6=ENSURES 7=TRUE 8=FALSE 9 note 9=( 10=) 11={ 12=} 13=: 14=, 15=-> 16== 17=+ 18=- 19=* 20=/ 21=& 22=| 10 note 23=== 24=!= 25=< 26=<= 27=> 28=>= 29=! 11 note Errors are "column N: msg" in olx_err[0..olx_elen) (the 512-byte err of the console). 12 13 const OLX_MAX_TOKENS ulong 256 14 const OLX_LINE_MAX ulong 1024 15 16 global olx_line uchar 4104 17 global olx_llen ulong 1 18 global olx_err uchar 512 19 global olx_elen ulong 1 20 global olx_tcnt ulong 1 21 global olx_tkind ulong 256 22 global olx_tcol ulong 256 23 global olx_tval ulong 256 24 global olx_tstr uchar 16384 25 global olx_tslen ulong 256 26 global olx_np ulong 1 27 28 note ---- message builder (the msg[] of a snprintf) ---- 29 global olm_b uchar 512 30 global olm_n ulong 1 31 32 fn olm_reset to void 33 put olm_n 0 0 34 end 35 36 fn olm_c c ulong to void 37 n = get olm_n 0 38 if lt n 511 39 put olm_b n uchar c 40 put olm_n 0 add n 1 41 end 42 end 43 44 fn olm_t s view uchar * to void 45 n = len s 46 for i ulong n bound 512 47 olm_c ulong get s i 48 end 49 end 50 51 global olm_d uchar 24 52 53 fn olm_u v ulong to void 54 k = ulong 0 55 x = v 56 while gt x 9 bound 24 57 put olm_d k uchar add 48 rem x 10 58 x = div x 10 59 k = add k 1 60 end 61 put olm_d k uchar add 48 x 62 k = add k 1 63 while gt k 0 bound 24 64 k = sub k 1 65 olm_c ulong get olm_d k 66 end 67 end 68 69 fn olm_hexd d ulong to void 70 if lt d 10 71 olm_c add 48 d 72 else 73 olm_c add 87 d 74 end 75 end 76 77 note the text of token t (its word) 78 fn olm_tstr t ulong to void 79 n = get olx_tslen t 80 b = mul t 64 81 for i ulong n bound 64 82 olm_c ulong get olx_tstr add b i 83 end 84 end 85 86 note snprintf truncation: keep at most m bytes of the message 87 fn olm_cap m ulong to void 88 if gt get olm_n 0 m 89 put olm_n 0 m 90 end 91 end 92 93 text olx_t_col "column " 94 text olx_t_colsp ": " 95 global olx_mtmp uchar 512 96 97 note set_err(err, 512, col, msg): "column N: msg", at most 511 bytes 98 fn olx_fail col ulong to void 99 put olx_elen 0 0 100 m = get olm_n 0 101 for i ulong m bound 512 102 put olx_mtmp i get olm_b i 103 end 104 olm_reset 105 olm_t olx_t_col 106 olm_u col 107 olm_t olx_t_colsp 108 for i ulong m bound 512 109 olm_c ulong get olx_mtmp i 110 end 111 n = get olm_n 0 112 for i ulong n bound 512 113 put olx_err i get olm_b i 114 end 115 put olx_elen 0 n 116 end 117 118 fn olx_failt col ulong s view uchar * to void 119 olm_reset 120 olm_t s 121 olx_fail col 122 end 123 124 note ---- omega_language_tok_text ---- 125 text olx_k0 "end of line" 126 text olx_k1 "name" 127 text olx_k2 "integer" 128 text olx_k3 "let" 129 text olx_k4 "fn" 130 text olx_k5 "requires" 131 text olx_k6 "ensures" 132 text olx_k7 "true" 133 text olx_k8 "false" 134 text olx_k9 "(" 135 text olx_k10 ")" 136 text olx_k11 "{" 137 text olx_k12 "}" 138 text olx_k13 ":" 139 text olx_k14 "," 140 text olx_k15 "->" 141 text olx_k16 "=" 142 text olx_k17 "+" 143 text olx_k18 "-" 144 text olx_k19 "*" 145 text olx_k20 "/" 146 text olx_k21 "&" 147 text olx_k22 "|" 148 text olx_k23 "==" 149 text olx_k24 "!=" 150 text olx_k25 "<" 151 text olx_k26 "<=" 152 text olx_k27 ">" 153 text olx_k28 ">=" 154 text olx_k29 "!" 155 156 fn olm_tok_hi k ulong to void 157 if eq k 15 158 olm_t olx_k15 159 ret 160 end 161 if eq k 16 162 olm_t olx_k16 163 ret 164 end 165 if eq k 17 166 olm_t olx_k17 167 ret 168 end 169 if eq k 18 170 olm_t olx_k18 171 ret 172 end 173 if eq k 19 174 olm_t olx_k19 175 ret 176 end 177 if eq k 20 178 olm_t olx_k20 179 ret 180 end 181 if eq k 21 182 olm_t olx_k21 183 ret 184 end 185 if eq k 22 186 olm_t olx_k22 187 ret 188 end 189 if eq k 23 190 olm_t olx_k23 191 ret 192 end 193 if eq k 24 194 olm_t olx_k24 195 ret 196 end 197 if eq k 25 198 olm_t olx_k25 199 ret 200 end 201 if eq k 26 202 olm_t olx_k26 203 ret 204 end 205 if eq k 27 206 olm_t olx_k27 207 ret 208 end 209 if eq k 28 210 olm_t olx_k28 211 ret 212 end 213 if eq k 29 214 olm_t olx_k29 215 ret 216 end 217 olm_c 63 218 end 219 220 fn olm_tok k ulong to void 221 if gt k 14 222 olm_tok_hi k 223 ret 224 end 225 if eq k 0 226 olm_t olx_k0 227 ret 228 end 229 if eq k 1 230 olm_t olx_k1 231 ret 232 end 233 if eq k 2 234 olm_t olx_k2 235 ret 236 end 237 if eq k 3 238 olm_t olx_k3 239 ret 240 end 241 if eq k 4 242 olm_t olx_k4 243 ret 244 end 245 if eq k 5 246 olm_t olx_k5 247 ret 248 end 249 if eq k 6 250 olm_t olx_k6 251 ret 252 end 253 if eq k 7 254 olm_t olx_k7 255 ret 256 end 257 if eq k 8 258 olm_t olx_k8 259 ret 260 end 261 if eq k 9 262 olm_t olx_k9 263 ret 264 end 265 if eq k 10 266 olm_t olx_k10 267 ret 268 end 269 if eq k 11 270 olm_t olx_k11 271 ret 272 end 273 if eq k 12 274 olm_t olx_k12 275 ret 276 end 277 if eq k 13 278 olm_t olx_k13 279 ret 280 end 281 olm_t olx_k14 282 end 283 284 note ---- classifiers (lang_core.om) ---- 285 fn olx_ch i ulong to ulong 286 if ge i get olx_llen 0 287 ret 0 288 end 289 ret ulong get olx_line i 290 end 291 292 fn olx_is_id_start c ulong to bool 293 if and ge c 97 le c 122 294 ret true 295 end 296 if and ge c 65 le c 90 297 ret true 298 end 299 ret eq c 95 300 end 301 302 fn olx_is_id_char c ulong to bool 303 if olx_is_id_start c 304 ret true 305 end 306 ret and ge c 48 le c 57 307 end 308 309 fn olx_dval c ulong to ulong 310 if and ge c 48 le c 57 311 ret sub c 48 312 end 313 if and ge c 97 le c 102 314 ret add sub c 97 10 315 end 316 if and ge c 65 le c 70 317 ret add sub c 65 10 318 end 319 ret 99 320 end 321 322 fn olx_tok_eq tidx ulong s view uchar * to bool 323 n = len s 324 if ne get olx_tslen tidx n 325 ret false 326 end 327 base = mul tidx 64 328 for i ulong n bound 64 329 if ne get olx_tstr add base i get s i 330 ret false 331 end 332 end 333 ret true 334 end 335 336 note lang_om_keyword: let fn requires ensures true false, else IDENT 337 fn olx_keyword t ulong to ulong 338 if olx_tok_eq t olx_k3 339 ret 3 340 end 341 if olx_tok_eq t olx_k4 342 ret 4 343 end 344 if olx_tok_eq t olx_k5 345 ret 5 346 end 347 if olx_tok_eq t olx_k6 348 ret 6 349 end 350 if olx_tok_eq t olx_k7 351 ret 7 352 end 353 if olx_tok_eq t olx_k8 354 ret 8 355 end 356 ret 1 357 end 358 359 text olx_e_hex "'0x' needs hex digits after it" 360 text olx_e_bin "'0b' needs binary digits (0 or 1) after it" 361 text olx_e_suffix "a number runs straight into letters or digits it cannot use (V0 has no literal suffixes; write `7: u64`)" 362 text olx_e_wide "integer literal does not fit in 64 bits" 363 364 note lex_int at pos for token t: 0 with the end in olx_np, or the C return code 365 fn olx_lex_int pos ulong t ulong to long 366 slen = get olx_llen 0 367 col = add pos 1 368 base = ulong 10 369 p = pos 370 if lt add p 1 slen 371 if eq olx_ch p 48 372 nx = olx_ch add p 1 373 if eq nx 120 374 base = 16 375 p = add p 2 376 else 377 if eq nx 98 378 base = 2 379 p = add p 2 380 end 381 end 382 end 383 end 384 start = p 385 v = ulong 0 386 ovf = ulong 0 387 maxu = ulong 0xffffffffffffffff 388 while lt p slen bound 4104 389 d = olx_dval olx_ch p 390 if ge d base 391 break 392 end 393 if gt v div sub maxu d base 394 ovf = 1 395 else 396 v = add mul v base d 397 end 398 p = add p 1 399 end 400 if eq p start 401 if eq base 16 402 olx_failt col olx_e_hex 403 else 404 olx_failt col olx_e_bin 405 end 406 ret -1 407 end 408 if lt p slen 409 if olx_is_id_char olx_ch p 410 olx_failt add p 1 olx_e_suffix 411 ret -1 412 end 413 end 414 if ne ovf 0 415 olx_failt col olx_e_wide 416 ret -2 417 end 418 put olx_np 0 p 419 put olx_tkind t 2 420 put olx_tval t v 421 ret 0 422 end 423 424 note lang_om_punct: kind * 4 + length, or 2 (string), 3 (list), 1 (unknown) as -2, -3, -1 425 fn olx_punct c ulong d ulong to long 426 if eq c 40 427 ret 37 428 end 429 if eq c 41 430 ret 41 431 end 432 if eq c 123 433 ret 45 434 end 435 if eq c 125 436 ret 49 437 end 438 if eq c 58 439 ret 53 440 end 441 if eq c 44 442 ret 57 443 end 444 if eq c 43 445 ret 69 446 end 447 if eq c 42 448 ret 77 449 end 450 if eq c 47 451 ret 81 452 end 453 if eq c 38 454 ret 85 455 end 456 if eq c 124 457 ret 89 458 end 459 if eq c 45 460 if eq d 62 461 ret 62 462 end 463 ret 73 464 end 465 if eq c 61 466 if eq d 61 467 ret 94 468 end 469 ret 65 470 end 471 if eq c 33 472 if eq d 61 473 ret 98 474 end 475 ret 117 476 end 477 if eq c 60 478 if eq d 61 479 ret 106 480 end 481 ret 101 482 end 483 if eq c 62 484 if eq d 61 485 ret 114 486 end 487 ret 109 488 end 489 if or eq c 34 eq c 39 490 ret -2 491 end 492 if or eq c 91 eq c 93 493 ret -3 494 end 495 ret -1 496 end 497 498 text olx_e_long "line is too long for V0" 499 text olx_e_cmt "comment is never closed with */" 500 text olx_e_many "too many tokens on one line" 501 text olx_e_name "name is longer than 63 characters" 502 text olx_e_str "strings and characters are not part of Omega V0" 503 text olx_e_list "lists/containers are not part of Omega V0" 504 text olx_e_chr "unexpected character '" 505 text olx_e_byte "unexpected byte 0x" 506 507 note the identifier at i for token k: 0 with the end in olx_np, or -1 508 fn olx_lex_ident i ulong k ulong to long 509 j = add i 1 510 while olx_is_id_char olx_ch j bound 1100 511 j = add j 1 512 end 513 n = sub j i 514 if ge n 64 515 olx_failt add i 1 olx_e_name 516 ret -1 517 end 518 tb = mul k 64 519 for q ulong n bound 64 520 put olx_tstr add tb q get olx_line add i q 521 end 522 put olx_tslen k n 523 put olx_tkind k olx_keyword k 524 put olx_np 0 j 525 ret 0 526 end 527 528 note one token at i (byte c) into token k: 0 with the end in olx_np, or the C return code 529 fn olx_lex_tok i ulong k ulong c ulong to long 530 col = add i 1 531 put olx_tkind k 0 532 put olx_tcol k col 533 put olx_tval k 0 534 put olx_tslen k 0 535 if and ge c 48 le c 57 536 ret olx_lex_int i k 537 end 538 if olx_is_id_start c 539 ret olx_lex_ident i k 540 end 541 pk = olx_punct c olx_ch add i 1 542 if ge pk 0 543 pu = ulong pk 544 put olx_tkind k div pu 4 545 put olx_np 0 add i rem pu 4 546 ret 0 547 end 548 if eq pk -2 549 olx_failt col olx_e_str 550 ret -2 551 end 552 if eq pk -3 553 olx_failt col olx_e_list 554 ret -2 555 end 556 olm_reset 557 if and ge c 32 lt c 127 558 olm_t olx_e_chr 559 olm_c c 560 olm_c 39 561 else 562 olm_t olx_e_byte 563 olm_hexd shr c 4 564 olm_hexd and c 15 565 end 566 olx_fail col 567 ret -1 568 end 569 570 note the block comment opened at i: the position after its */, or -1 571 fn olx_skip_cmt i ulong to long 572 j = add i 2 573 while ne olx_ch j 0 bound 1100 574 if and eq olx_ch j 42 eq olx_ch add j 1 47 575 break 576 end 577 j = add j 1 578 end 579 if eq olx_ch j 0 580 olx_failt add i 1 olx_e_cmt 581 ret -1 582 end 583 ret long add j 2 584 end 585 586 note omega_language_lex(olx_line, toks, 256, &count): 0 with olx_tcnt tokens before the EOF 587 note token at index olx_tcnt, or the C return code with olx_err set 588 fn olx_lex_line to long 589 put olx_tcnt 0 0 590 put olx_elen 0 0 591 slen = get olx_llen 0 592 if gt slen OLX_LINE_MAX 593 olx_failt OLX_LINE_MAX olx_e_long 594 ret -3 595 end 596 i = ulong 0 597 k = ulong 0 598 while le i slen bound 1100 599 c = olx_ch i 600 if or or eq c 32 eq c 9 or eq c 13 eq c 10 601 i = add i 1 602 continue 603 end 604 if and eq c 47 eq olx_ch add i 1 47 605 break 606 end 607 if and eq c 47 eq olx_ch add i 1 42 608 e = olx_skip_cmt i 609 if lt e 0 610 ret -1 611 end 612 i = ulong e 613 continue 614 end 615 if eq c 0 616 break 617 end 618 if ge add k 1 OLX_MAX_TOKENS 619 olx_failt add i 1 olx_e_many 620 ret -3 621 end 622 rc = olx_lex_tok i k c 623 if ne rc 0 624 ret rc 625 end 626 i = get olx_np 0 627 k = add k 1 628 end 629 put olx_tkind k 0 630 put olx_tcol k add i 1 631 put olx_tval k 0 632 put olx_tslen k 0 633 put olx_tcnt 0 k 634 ret 0 635 end