| 1 | // Copyright (c) 2020-2024 Joe Conigliaro. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module ast |
| 5 | |
| 6 | import v2.token |
| 7 | |
| 8 | pub const empty_expr = Expr(EmptyExpr(0)) |
| 9 | pub const empty_stmt = Stmt(EmptyStmt(0)) |
| 10 | |
| 11 | type EmptyExpr = u8 |
| 12 | type EmptyStmt = u8 |
| 13 | |
| 14 | pub type Expr = ArrayInitExpr |
| 15 | | AsCastExpr |
| 16 | | AssocExpr |
| 17 | | BasicLiteral |
| 18 | | CallExpr |
| 19 | | CallOrCastExpr |
| 20 | | CastExpr |
| 21 | | ComptimeExpr |
| 22 | | EmptyExpr |
| 23 | | FieldInit |
| 24 | | FnLiteral |
| 25 | | GenericArgOrIndexExpr |
| 26 | | GenericArgs |
| 27 | | Ident |
| 28 | | IfExpr |
| 29 | | IfGuardExpr |
| 30 | | IndexExpr |
| 31 | | InfixExpr |
| 32 | | InitExpr |
| 33 | | Keyword |
| 34 | | KeywordOperator |
| 35 | | LambdaExpr |
| 36 | | LifetimeExpr |
| 37 | | LockExpr |
| 38 | | MapInitExpr |
| 39 | | MatchExpr |
| 40 | | ModifierExpr |
| 41 | | OrExpr |
| 42 | | ParenExpr |
| 43 | | PostfixExpr |
| 44 | | PrefixExpr |
| 45 | | RangeExpr |
| 46 | | SelectExpr |
| 47 | | SelectorExpr |
| 48 | | SqlExpr |
| 49 | | StringInterLiteral |
| 50 | | StringLiteral |
| 51 | | Tuple |
| 52 | | Type |
| 53 | | UnsafeExpr |
| 54 | |
| 55 | // TODO: decide if this going to be done like this (FieldInit) |
| 56 | |
| 57 | pub type Stmt = AsmStmt |
| 58 | | AssertStmt |
| 59 | | AssignStmt |
| 60 | | BlockStmt |
| 61 | | ComptimeStmt |
| 62 | | ConstDecl |
| 63 | | DeferStmt |
| 64 | | Directive |
| 65 | | EmptyStmt |
| 66 | | EnumDecl |
| 67 | | ExprStmt |
| 68 | | FlowControlStmt |
| 69 | | FnDecl |
| 70 | | ForInStmt |
| 71 | | ForStmt |
| 72 | | GlobalDecl |
| 73 | | ImportStmt |
| 74 | | InterfaceDecl |
| 75 | | LabelStmt |
| 76 | | ModuleStmt |
| 77 | | ReturnStmt |
| 78 | | StructDecl |
| 79 | | TypeDecl |
| 80 | | []Attribute |
| 81 | |
| 82 | // pub type Decl = ConstDecl | EnumDecl | FnDecl | GlobalDecl |
| 83 | // | InterfaceDecl | StructDecl | TypeDecl |
| 84 | |
| 85 | // TODO: (re)implement nested sumtype like TS (was removed from v) |
| 86 | // currently need to cast to type in parser.type. Should I leave like |
| 87 | // this or add these directly to Expr until nesting is implemented? |
| 88 | pub type Type = AnonStructType |
| 89 | | ArrayFixedType |
| 90 | | ArrayType |
| 91 | | ChannelType |
| 92 | | FnType |
| 93 | | GenericType |
| 94 | | MapType |
| 95 | | NilType |
| 96 | | NoneType |
| 97 | | OptionType |
| 98 | | PointerType |
| 99 | | ResultType |
| 100 | | ThreadType |
| 101 | | TupleType |
| 102 | |
| 103 | pub fn (exprs []Expr) name_list() string { |
| 104 | mut out := '' |
| 105 | for i := 0; i < exprs.len; i++ { |
| 106 | out = out + exprs[i].name() |
| 107 | if i < exprs.len - 1 { |
| 108 | out = out + ',' |
| 109 | } |
| 110 | } |
| 111 | return out |
| 112 | } |
| 113 | |
| 114 | pub fn (t Type) name() string { |
| 115 | return match t { |
| 116 | GenericType { |
| 117 | '${t.name.name()}[${t.params.name_list()}]' |
| 118 | } |
| 119 | else { |
| 120 | 'Type' |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // TODO: fix this, should be only what is needed |
| 126 | pub fn (expr Expr) name() string { |
| 127 | return match expr { |
| 128 | AsCastExpr { |
| 129 | '${expr.expr.name()} as ${expr.typ.name()}' |
| 130 | } |
| 131 | BasicLiteral { |
| 132 | expr.value |
| 133 | } |
| 134 | CallExpr { |
| 135 | // TODO: |
| 136 | '${expr.lhs.name()}()' |
| 137 | } |
| 138 | CallOrCastExpr { |
| 139 | '${expr.lhs.name()}(${expr.expr.name()})' |
| 140 | } |
| 141 | EmptyExpr { |
| 142 | // TODO: |
| 143 | 'EmptyExpr' |
| 144 | } |
| 145 | GenericArgs { |
| 146 | '${expr.lhs.name()}[${expr.args.name_list()}]' |
| 147 | } |
| 148 | Ident { |
| 149 | expr.name |
| 150 | } |
| 151 | IndexExpr { |
| 152 | '${expr.lhs.name()}[${expr.expr.name()}]' |
| 153 | } |
| 154 | InfixExpr { |
| 155 | '${expr.lhs.name()} ${expr.op} ${expr.rhs.name()}' |
| 156 | } |
| 157 | Keyword { |
| 158 | expr.tok.str() |
| 159 | } |
| 160 | LifetimeExpr { |
| 161 | '^' + expr.name |
| 162 | } |
| 163 | ModifierExpr { |
| 164 | '${expr.kind} ${expr.expr.name()}' |
| 165 | } |
| 166 | ParenExpr { |
| 167 | '(${expr.expr.name()})' |
| 168 | } |
| 169 | PrefixExpr { |
| 170 | '${expr.op}${expr.expr.name()}' |
| 171 | } |
| 172 | SelectorExpr { |
| 173 | expr.lhs.name() + '.' + expr.rhs.name |
| 174 | } |
| 175 | StringLiteral { |
| 176 | "'${expr.value}'" |
| 177 | } |
| 178 | Type { |
| 179 | 'Type' |
| 180 | } |
| 181 | UnsafeExpr { |
| 182 | // TODO: |
| 183 | 'UnsafeExpr' |
| 184 | } |
| 185 | else { |
| 186 | 'Expr' |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | pub fn (expr Expr) pos() token.Pos { |
| 192 | return match expr { |
| 193 | AsCastExpr { |
| 194 | expr.pos |
| 195 | } |
| 196 | ArrayInitExpr { |
| 197 | expr.pos |
| 198 | } |
| 199 | AssocExpr { |
| 200 | expr.pos |
| 201 | } |
| 202 | BasicLiteral { |
| 203 | expr.pos |
| 204 | } |
| 205 | CallExpr { |
| 206 | expr.pos |
| 207 | } |
| 208 | CallOrCastExpr { |
| 209 | expr.pos |
| 210 | } |
| 211 | CastExpr { |
| 212 | expr.pos |
| 213 | } |
| 214 | ComptimeExpr { |
| 215 | expr.pos |
| 216 | } |
| 217 | FnLiteral { |
| 218 | expr.pos |
| 219 | } |
| 220 | GenericArgOrIndexExpr { |
| 221 | expr.pos |
| 222 | } |
| 223 | GenericArgs { |
| 224 | expr.pos |
| 225 | } |
| 226 | Ident { |
| 227 | expr.pos |
| 228 | } |
| 229 | IfExpr { |
| 230 | expr.pos |
| 231 | } |
| 232 | IfGuardExpr { |
| 233 | expr.pos |
| 234 | } |
| 235 | IndexExpr { |
| 236 | expr.pos |
| 237 | } |
| 238 | InfixExpr { |
| 239 | expr.pos |
| 240 | } |
| 241 | InitExpr { |
| 242 | expr.pos |
| 243 | } |
| 244 | KeywordOperator { |
| 245 | expr.pos |
| 246 | } |
| 247 | LifetimeExpr { |
| 248 | expr.pos |
| 249 | } |
| 250 | LambdaExpr { |
| 251 | expr.pos |
| 252 | } |
| 253 | LockExpr { |
| 254 | expr.pos |
| 255 | } |
| 256 | MapInitExpr { |
| 257 | expr.pos |
| 258 | } |
| 259 | MatchExpr { |
| 260 | expr.pos |
| 261 | } |
| 262 | ModifierExpr { |
| 263 | expr.pos |
| 264 | } |
| 265 | OrExpr { |
| 266 | expr.pos |
| 267 | } |
| 268 | ParenExpr { |
| 269 | expr.pos |
| 270 | } |
| 271 | PostfixExpr { |
| 272 | expr.pos |
| 273 | } |
| 274 | PrefixExpr { |
| 275 | expr.pos |
| 276 | } |
| 277 | RangeExpr { |
| 278 | expr.pos |
| 279 | } |
| 280 | SelectExpr { |
| 281 | expr.pos |
| 282 | } |
| 283 | SelectorExpr { |
| 284 | expr.pos |
| 285 | } |
| 286 | SqlExpr { |
| 287 | expr.pos |
| 288 | } |
| 289 | StringInterLiteral { |
| 290 | expr.pos |
| 291 | } |
| 292 | StringLiteral { |
| 293 | expr.pos |
| 294 | } |
| 295 | Tuple { |
| 296 | expr.pos |
| 297 | } |
| 298 | UnsafeExpr { |
| 299 | expr.pos |
| 300 | } |
| 301 | else { |
| 302 | // Default for expressions without pos field (EmptyExpr, FieldInit, Keyword, Type) |
| 303 | token.Pos{} |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // File (AST container) |
| 309 | pub struct File { |
| 310 | pub: |
| 311 | attributes []Attribute |
| 312 | mod string |
| 313 | name string |
| 314 | stmts []Stmt |
| 315 | imports []ImportStmt |
| 316 | selector_names map[int]string |
| 317 | } |
| 318 | |
| 319 | pub enum Language { |
| 320 | v |
| 321 | c |
| 322 | js |
| 323 | } |
| 324 | |
| 325 | pub fn (lang Language) str() string { |
| 326 | return match lang { |
| 327 | .v { 'V' } |
| 328 | .c { 'C' } |
| 329 | .js { 'JS' } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | pub enum DeferMode { |
| 334 | scoped // default: defer to end of current scope |
| 335 | function // defer(fn): defer to end of function |
| 336 | } |
| 337 | |
| 338 | // Expressions |
| 339 | pub struct ArrayInitExpr { |
| 340 | pub mut: |
| 341 | typ Expr = empty_expr |
| 342 | exprs []Expr |
| 343 | init Expr = empty_expr |
| 344 | cap Expr = empty_expr |
| 345 | len Expr = empty_expr |
| 346 | update_expr Expr = empty_expr // `a` in `[...a, 3, 4]` |
| 347 | pos token.Pos |
| 348 | } |
| 349 | |
| 350 | pub struct AsCastExpr { |
| 351 | pub: |
| 352 | expr Expr |
| 353 | typ Expr |
| 354 | pos token.Pos |
| 355 | } |
| 356 | |
| 357 | pub struct AssocExpr { |
| 358 | pub: |
| 359 | typ Expr |
| 360 | expr Expr |
| 361 | fields []FieldInit |
| 362 | pos token.Pos |
| 363 | } |
| 364 | |
| 365 | pub struct BasicLiteral { |
| 366 | pub: |
| 367 | kind token.Token |
| 368 | value string |
| 369 | pos token.Pos |
| 370 | } |
| 371 | |
| 372 | pub struct CallExpr { |
| 373 | pub mut: |
| 374 | lhs Expr |
| 375 | pub: |
| 376 | args []Expr |
| 377 | pos token.Pos |
| 378 | } |
| 379 | |
| 380 | pub struct CallOrCastExpr { |
| 381 | pub mut: |
| 382 | lhs Expr |
| 383 | expr Expr |
| 384 | pub: |
| 385 | pos token.Pos |
| 386 | } |
| 387 | |
| 388 | pub struct CastExpr { |
| 389 | pub mut: |
| 390 | typ Expr |
| 391 | expr Expr |
| 392 | pub: |
| 393 | pos token.Pos |
| 394 | } |
| 395 | |
| 396 | pub struct ComptimeExpr { |
| 397 | pub: |
| 398 | expr Expr |
| 399 | pos token.Pos |
| 400 | } |
| 401 | |
| 402 | pub struct FieldDecl { |
| 403 | pub: |
| 404 | name string |
| 405 | typ Expr = empty_expr // can be empty as used for const (unless we use something else) |
| 406 | value Expr = empty_expr |
| 407 | attributes []Attribute |
| 408 | is_public bool |
| 409 | is_mut bool |
| 410 | is_module_mut bool |
| 411 | is_interface_method bool |
| 412 | } |
| 413 | |
| 414 | pub struct FieldInit { |
| 415 | pub: |
| 416 | name string |
| 417 | pub mut: |
| 418 | value Expr |
| 419 | } |
| 420 | |
| 421 | // anon fn / closure |
| 422 | pub struct FnLiteral { |
| 423 | pub: |
| 424 | typ FnType |
| 425 | captured_vars []Expr |
| 426 | stmts []Stmt |
| 427 | pos token.Pos |
| 428 | } |
| 429 | |
| 430 | pub struct GenericArgs { |
| 431 | pub: |
| 432 | lhs Expr |
| 433 | args []Expr // concrete types and lifetimes |
| 434 | pos token.Pos |
| 435 | } |
| 436 | |
| 437 | pub struct GenericArgOrIndexExpr { |
| 438 | pub: |
| 439 | lhs Expr |
| 440 | expr Expr |
| 441 | pos token.Pos |
| 442 | } |
| 443 | |
| 444 | pub struct Ident { |
| 445 | pub mut: |
| 446 | pos token.Pos |
| 447 | name string |
| 448 | } |
| 449 | |
| 450 | pub struct IfExpr { |
| 451 | pub mut: |
| 452 | cond Expr = empty_expr |
| 453 | else_expr Expr = empty_expr |
| 454 | stmts []Stmt |
| 455 | pos token.Pos |
| 456 | } |
| 457 | |
| 458 | pub struct IfGuardExpr { |
| 459 | pub: |
| 460 | stmt AssignStmt |
| 461 | pos token.Pos |
| 462 | } |
| 463 | |
| 464 | pub struct InfixExpr { |
| 465 | pub mut: |
| 466 | op token.Token |
| 467 | lhs Expr |
| 468 | rhs Expr |
| 469 | pos token.Pos |
| 470 | } |
| 471 | |
| 472 | pub struct IndexExpr { |
| 473 | pub mut: |
| 474 | lhs Expr |
| 475 | expr Expr |
| 476 | is_gated bool |
| 477 | pos token.Pos |
| 478 | } |
| 479 | |
| 480 | pub struct InitExpr { |
| 481 | pub mut: |
| 482 | typ Expr |
| 483 | fields []FieldInit |
| 484 | pos token.Pos |
| 485 | } |
| 486 | |
| 487 | pub struct Keyword { |
| 488 | pub: |
| 489 | tok token.Token |
| 490 | } |
| 491 | |
| 492 | pub struct KeywordOperator { |
| 493 | pub: |
| 494 | op token.Token |
| 495 | exprs []Expr |
| 496 | pos token.Pos |
| 497 | } |
| 498 | |
| 499 | pub struct Tuple { |
| 500 | pub: |
| 501 | exprs []Expr |
| 502 | pos token.Pos |
| 503 | } |
| 504 | |
| 505 | pub struct LambdaExpr { |
| 506 | pub: |
| 507 | args []Ident |
| 508 | expr Expr |
| 509 | pos token.Pos |
| 510 | } |
| 511 | |
| 512 | pub struct LockExpr { |
| 513 | pub: |
| 514 | lock_exprs []Expr |
| 515 | rlock_exprs []Expr |
| 516 | stmts []Stmt |
| 517 | pos token.Pos |
| 518 | } |
| 519 | |
| 520 | pub struct MapInitExpr { |
| 521 | pub: |
| 522 | typ Expr = empty_expr |
| 523 | keys []Expr |
| 524 | vals []Expr |
| 525 | pos token.Pos |
| 526 | } |
| 527 | |
| 528 | pub struct MatchBranch { |
| 529 | pub: |
| 530 | cond []Expr |
| 531 | stmts []Stmt |
| 532 | pos token.Pos |
| 533 | } |
| 534 | |
| 535 | pub struct MatchExpr { |
| 536 | pub: |
| 537 | expr Expr |
| 538 | branches []MatchBranch |
| 539 | pos token.Pos |
| 540 | } |
| 541 | |
| 542 | // TODO: possibly merge modifiers into a bitfield where |
| 543 | // multiple modifiers are used. this could also be used |
| 544 | // for struct decl `mut:`, `pub mut:` etc. consider this |
| 545 | // [flag] |
| 546 | // pub enum Modifier { |
| 547 | // .atomic |
| 548 | // .global |
| 549 | // .mutable |
| 550 | // .public |
| 551 | // .shared |
| 552 | // .static |
| 553 | // .volatile |
| 554 | // } |
| 555 | |
| 556 | pub struct ModifierExpr { |
| 557 | pub mut: |
| 558 | kind token.Token |
| 559 | expr Expr |
| 560 | pos token.Pos |
| 561 | } |
| 562 | |
| 563 | // pub fn (expr ModifierExpr) unwrap() Expr { |
| 564 | // return expr.expr |
| 565 | // } |
| 566 | |
| 567 | pub struct OrExpr { |
| 568 | pub: |
| 569 | expr Expr |
| 570 | stmts []Stmt |
| 571 | pos token.Pos |
| 572 | } |
| 573 | |
| 574 | pub struct Parameter { |
| 575 | pub mut: |
| 576 | name string |
| 577 | typ Expr |
| 578 | is_mut bool |
| 579 | pos token.Pos |
| 580 | } |
| 581 | |
| 582 | pub struct LifetimeExpr { |
| 583 | pub: |
| 584 | name string |
| 585 | pos token.Pos |
| 586 | } |
| 587 | |
| 588 | // name_str returns the parameter name. |
| 589 | pub fn (p Parameter) name_str() string { |
| 590 | return p.name |
| 591 | } |
| 592 | |
| 593 | pub struct ParenExpr { |
| 594 | pub: |
| 595 | expr Expr |
| 596 | pos token.Pos |
| 597 | } |
| 598 | |
| 599 | pub struct PostfixExpr { |
| 600 | pub: |
| 601 | op token.Token |
| 602 | expr Expr |
| 603 | pos token.Pos |
| 604 | } |
| 605 | |
| 606 | pub struct PrefixExpr { |
| 607 | pub mut: |
| 608 | op token.Token |
| 609 | expr Expr |
| 610 | pos token.Pos |
| 611 | } |
| 612 | |
| 613 | pub struct RangeExpr { |
| 614 | pub: |
| 615 | op token.Token // `..` exclusive | `...` inclusive |
| 616 | start Expr |
| 617 | end Expr |
| 618 | pos token.Pos |
| 619 | } |
| 620 | |
| 621 | pub struct SelectExpr { |
| 622 | pub: |
| 623 | pos token.Pos |
| 624 | stmt Stmt |
| 625 | stmts []Stmt |
| 626 | next Expr = empty_expr |
| 627 | } |
| 628 | |
| 629 | pub struct SelectorExpr { |
| 630 | pub mut: |
| 631 | lhs Expr |
| 632 | rhs Ident |
| 633 | pos token.Pos |
| 634 | } |
| 635 | |
| 636 | pub fn (se SelectorExpr) leftmost() Expr { |
| 637 | if se.lhs is SelectorExpr { |
| 638 | return se.lhs.leftmost() |
| 639 | } |
| 640 | return se.lhs |
| 641 | } |
| 642 | |
| 643 | // pub fn (expr Expr) str() string { |
| 644 | // return 'Expr.str() - ${expr.type_name()}' |
| 645 | // } |
| 646 | |
| 647 | pub fn (se SelectorExpr) name() string { |
| 648 | return se.lhs.name() + '.' + se.rhs.name |
| 649 | } |
| 650 | |
| 651 | pub enum StringLiteralKind { |
| 652 | c |
| 653 | js |
| 654 | raw |
| 655 | v |
| 656 | } |
| 657 | |
| 658 | pub fn (s StringLiteralKind) str() string { |
| 659 | return match s { |
| 660 | .c { 'c' } |
| 661 | .js { 'js' } |
| 662 | .raw { 'r' } |
| 663 | .v { 'v' } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // TODO: allow overriding this method in main v compiler |
| 668 | // that is why this method was renamed from `from_string` |
| 669 | @[direct_array_access] |
| 670 | pub fn StringLiteralKind.from_string_tinyv(s string) StringLiteralKind { |
| 671 | match s[0] { |
| 672 | `c` { |
| 673 | return .c |
| 674 | } |
| 675 | `j` { |
| 676 | if s[1] == `s` { |
| 677 | return .js |
| 678 | } |
| 679 | } |
| 680 | `r` { |
| 681 | return .raw |
| 682 | } |
| 683 | else {} |
| 684 | } |
| 685 | |
| 686 | return .v |
| 687 | } |
| 688 | |
| 689 | // NOTE: I'm using two nodes StringLiteral & StringInterLiteral |
| 690 | // to avoid the extra array allocations when not needed. |
| 691 | pub struct StringLiteral { |
| 692 | pub: |
| 693 | kind StringLiteralKind |
| 694 | value string |
| 695 | pos token.Pos |
| 696 | } |
| 697 | |
| 698 | pub struct StringInterLiteral { |
| 699 | pub: |
| 700 | kind StringLiteralKind |
| 701 | values []string |
| 702 | inters []StringInter |
| 703 | pos token.Pos |
| 704 | } |
| 705 | |
| 706 | pub struct StringInter { |
| 707 | pub: |
| 708 | format StringInterFormat |
| 709 | width int |
| 710 | precision int |
| 711 | expr Expr |
| 712 | // TEMP: prob removed once individual |
| 713 | // fields are set, precision etc |
| 714 | format_expr Expr = empty_expr |
| 715 | resolved_fmt string // resolved sprintf format specifier (e.g. '%d', '%s', '%lld'), set by transformer |
| 716 | } |
| 717 | |
| 718 | pub enum StringInterFormat { |
| 719 | unformatted |
| 720 | binary |
| 721 | character |
| 722 | decimal |
| 723 | exponent |
| 724 | exponent_short |
| 725 | float |
| 726 | hex |
| 727 | octal |
| 728 | pointer_address |
| 729 | string |
| 730 | } |
| 731 | |
| 732 | pub fn StringInterFormat.from_u8(c u8) StringInterFormat { |
| 733 | return match c { |
| 734 | `b` { .binary } |
| 735 | `c` { .character } |
| 736 | `d` { .decimal } |
| 737 | `e`, `E` { .exponent } |
| 738 | `g`, `G` { .exponent_short } |
| 739 | `f`, `F` { .float } |
| 740 | `x`, `X` { .hex } |
| 741 | `o` { .octal } |
| 742 | `p` { .pointer_address } |
| 743 | `s` { .string } |
| 744 | else { .unformatted } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | pub fn (sif StringInterFormat) str() string { |
| 749 | return match sif { |
| 750 | .unformatted { '' } |
| 751 | .binary { 'b' } |
| 752 | .character { 'c' } |
| 753 | .decimal { 'd' } |
| 754 | .exponent { 'e' } |
| 755 | .exponent_short { 'g' } |
| 756 | .float { 'f' } |
| 757 | .hex { 'x' } |
| 758 | .octal { 'o' } |
| 759 | .pointer_address { 'p' } |
| 760 | .string { 's' } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | pub struct SqlExpr { |
| 765 | pub: |
| 766 | expr Expr |
| 767 | table_name string |
| 768 | is_count bool |
| 769 | is_create bool |
| 770 | pos token.Pos |
| 771 | } |
| 772 | |
| 773 | pub struct UnsafeExpr { |
| 774 | pub: |
| 775 | stmts []Stmt |
| 776 | pos token.Pos |
| 777 | } |
| 778 | |
| 779 | // Statements |
| 780 | pub struct AsmStmt { |
| 781 | pub: |
| 782 | arch string |
| 783 | } |
| 784 | |
| 785 | pub struct AssertStmt { |
| 786 | pub: |
| 787 | expr Expr |
| 788 | extra Expr = empty_expr |
| 789 | } |
| 790 | |
| 791 | pub struct AssignStmt { |
| 792 | pub: |
| 793 | op token.Token |
| 794 | lhs []Expr |
| 795 | rhs []Expr |
| 796 | pos token.Pos |
| 797 | } |
| 798 | |
| 799 | pub fn (attributes []Attribute) has(name string) bool { |
| 800 | for attribute in attributes { |
| 801 | if attribute.name == name { |
| 802 | return true |
| 803 | } |
| 804 | // Also check value when it's a simple identifier (e.g., @[flag]) |
| 805 | if attribute.name == '' { |
| 806 | if attribute.value is Ident && attribute.value.name == name { |
| 807 | return true |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | return false |
| 812 | } |
| 813 | |
| 814 | pub struct Attribute { |
| 815 | pub: |
| 816 | name string |
| 817 | value Expr |
| 818 | comptime_cond Expr |
| 819 | pos token.Pos |
| 820 | } |
| 821 | |
| 822 | pub struct BlockStmt { |
| 823 | pub: |
| 824 | stmts []Stmt |
| 825 | } |
| 826 | |
| 827 | pub struct ComptimeStmt { |
| 828 | pub: |
| 829 | stmt Stmt |
| 830 | } |
| 831 | |
| 832 | pub struct ConstDecl { |
| 833 | pub: |
| 834 | is_public bool |
| 835 | fields []FieldInit |
| 836 | } |
| 837 | |
| 838 | pub struct DeferStmt { |
| 839 | pub: |
| 840 | mode DeferMode |
| 841 | stmts []Stmt |
| 842 | } |
| 843 | |
| 844 | // #flag / #include |
| 845 | pub struct Directive { |
| 846 | pub: |
| 847 | name string |
| 848 | value string |
| 849 | ct_cond string // optional comptime condition e.g. 'linux', 'darwin' for `#include linux <pty.h>` |
| 850 | } |
| 851 | |
| 852 | pub struct EnumDecl { |
| 853 | pub: |
| 854 | attributes []Attribute |
| 855 | is_public bool |
| 856 | name string |
| 857 | as_type Expr = empty_expr |
| 858 | fields []FieldDecl |
| 859 | } |
| 860 | |
| 861 | pub struct ExprStmt { |
| 862 | pub mut: |
| 863 | expr Expr |
| 864 | } |
| 865 | |
| 866 | pub struct FlowControlStmt { |
| 867 | pub: |
| 868 | op token.Token |
| 869 | label string |
| 870 | } |
| 871 | |
| 872 | // enum FnArributes { |
| 873 | // method |
| 874 | // static |
| 875 | // } |
| 876 | |
| 877 | pub struct FnDecl { |
| 878 | pub: |
| 879 | attributes []Attribute |
| 880 | is_public bool |
| 881 | is_method bool |
| 882 | is_static bool |
| 883 | receiver Parameter |
| 884 | language Language = .v |
| 885 | name string |
| 886 | typ FnType |
| 887 | stmts []Stmt |
| 888 | pos token.Pos |
| 889 | } |
| 890 | |
| 891 | pub struct ForStmt { |
| 892 | pub mut: |
| 893 | init Stmt = empty_stmt // initialization |
| 894 | cond Expr = empty_expr // condition |
| 895 | post Stmt = empty_stmt // post iteration (afterthought) |
| 896 | stmts []Stmt |
| 897 | } |
| 898 | |
| 899 | // NOTE: used as the initializer for ForStmt |
| 900 | pub struct ForInStmt { |
| 901 | pub mut: |
| 902 | // key string |
| 903 | // value string |
| 904 | // value_is_mut bool |
| 905 | // expr Expr |
| 906 | // TODO: |
| 907 | key Expr = empty_expr |
| 908 | value Expr |
| 909 | expr Expr |
| 910 | } |
| 911 | |
| 912 | pub struct GlobalDecl { |
| 913 | pub: |
| 914 | attributes []Attribute |
| 915 | fields []FieldDecl |
| 916 | is_public bool |
| 917 | } |
| 918 | |
| 919 | pub struct ImportStmt { |
| 920 | pub: |
| 921 | name string |
| 922 | alias string |
| 923 | is_aliased bool |
| 924 | symbols []Expr |
| 925 | } |
| 926 | |
| 927 | pub struct InterfaceDecl { |
| 928 | pub: |
| 929 | is_public bool |
| 930 | attributes []Attribute |
| 931 | name string |
| 932 | generic_params []Expr |
| 933 | embedded []Expr |
| 934 | fields []FieldDecl |
| 935 | } |
| 936 | |
| 937 | pub struct LabelStmt { |
| 938 | pub: |
| 939 | name string |
| 940 | stmt Stmt = empty_stmt |
| 941 | } |
| 942 | |
| 943 | pub struct ModuleStmt { |
| 944 | pub: |
| 945 | name string |
| 946 | } |
| 947 | |
| 948 | pub struct ReturnStmt { |
| 949 | pub: |
| 950 | exprs []Expr |
| 951 | } |
| 952 | |
| 953 | pub struct StructDecl { |
| 954 | pub: |
| 955 | attributes []Attribute |
| 956 | is_public bool |
| 957 | is_union bool |
| 958 | implements []Expr |
| 959 | embedded []Expr |
| 960 | language Language = .v |
| 961 | name string |
| 962 | generic_params []Expr |
| 963 | fields []FieldDecl |
| 964 | pos token.Pos |
| 965 | } |
| 966 | |
| 967 | pub struct TypeDecl { |
| 968 | pub: |
| 969 | is_public bool |
| 970 | language Language |
| 971 | name string |
| 972 | generic_params []Expr |
| 973 | base_type Expr = empty_expr |
| 974 | variants []Expr |
| 975 | } |
| 976 | |
| 977 | // Type Nodes |
| 978 | pub struct ArrayType { |
| 979 | pub: |
| 980 | elem_type Expr = empty_expr |
| 981 | } |
| 982 | |
| 983 | pub struct ArrayFixedType { |
| 984 | pub: |
| 985 | len Expr = empty_expr |
| 986 | elem_type Expr = empty_expr |
| 987 | } |
| 988 | |
| 989 | pub struct ChannelType { |
| 990 | pub: |
| 991 | // s255: cap defaults to empty_expr (like ThreadType/OptionType/etc.). The |
| 992 | // parser omits cap for a bare `chan T` (type.v: `ChannelType{elem_type: ...}`), |
| 993 | // which previously left it a zero-valued Expr (invalid sum-type tag, null |
| 994 | // payload). Encoding that via FlatBuilder.add_expr crashes the arm64 self-host |
| 995 | // — an exhaustive match on the unmatched tag falls into the first arm |
| 996 | // (ArrayInitExpr) and derefs the null payload (same class as s251). |
| 997 | cap Expr = empty_expr |
| 998 | elem_type Expr = empty_expr |
| 999 | } |
| 1000 | |
| 1001 | pub struct ThreadType { |
| 1002 | pub: |
| 1003 | elem_type Expr = empty_expr |
| 1004 | } |
| 1005 | |
| 1006 | pub struct FnType { |
| 1007 | pub: |
| 1008 | generic_params []Expr |
| 1009 | params []Parameter |
| 1010 | pub mut: |
| 1011 | return_type Expr = empty_expr |
| 1012 | } |
| 1013 | |
| 1014 | pub fn (ft &FnType) str() string { |
| 1015 | mut s := 'fn(' |
| 1016 | for i := 0; i < ft.params.len; i++ { |
| 1017 | param := ft.params[i] |
| 1018 | s = s + param.name + param.typ.name() |
| 1019 | if i < ft.params.len - 1 { |
| 1020 | s = s + ', ' |
| 1021 | } |
| 1022 | } |
| 1023 | if ft.return_type is EmptyExpr { |
| 1024 | s = s + ')' |
| 1025 | } else { |
| 1026 | s = s + ') ' + ft.return_type.name() |
| 1027 | } |
| 1028 | return s |
| 1029 | } |
| 1030 | |
| 1031 | pub fn (ident &Ident) str() string { |
| 1032 | return ident.name.clone() |
| 1033 | } |
| 1034 | |
| 1035 | // pub fn (expr Expr) str() string { |
| 1036 | // return match expr { |
| 1037 | // Ident { |
| 1038 | // expr.name |
| 1039 | // } |
| 1040 | // SelectorExpr { |
| 1041 | // expr.lhs.str() + '.' + expr.rhs.name |
| 1042 | // } |
| 1043 | // else { |
| 1044 | // 'missing Expr.str() for ${expr.type_name()}' |
| 1045 | // // expr.str() |
| 1046 | // } |
| 1047 | // } |
| 1048 | // } |
| 1049 | |
| 1050 | pub struct AnonStructType { |
| 1051 | pub: |
| 1052 | generic_params []Expr |
| 1053 | embedded []Expr |
| 1054 | fields []FieldDecl |
| 1055 | } |
| 1056 | |
| 1057 | pub struct GenericType { |
| 1058 | pub: |
| 1059 | name Expr = empty_expr |
| 1060 | params []Expr |
| 1061 | } |
| 1062 | |
| 1063 | pub struct MapType { |
| 1064 | pub: |
| 1065 | key_type Expr = empty_expr |
| 1066 | value_type Expr = empty_expr |
| 1067 | } |
| 1068 | |
| 1069 | pub struct NilType {} |
| 1070 | |
| 1071 | pub struct NoneType {} |
| 1072 | |
| 1073 | pub struct OptionType { |
| 1074 | pub: |
| 1075 | base_type Expr = empty_expr |
| 1076 | } |
| 1077 | |
| 1078 | pub struct PointerType { |
| 1079 | pub: |
| 1080 | base_type Expr = empty_expr |
| 1081 | lifetime string |
| 1082 | } |
| 1083 | |
| 1084 | pub struct ResultType { |
| 1085 | pub: |
| 1086 | base_type Expr = empty_expr |
| 1087 | } |
| 1088 | |
| 1089 | pub struct TupleType { |
| 1090 | pub: |
| 1091 | types []Expr |
| 1092 | } |
| 1093 | |