expr.ha (13784B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use hare::lex; 5 6 // An identifier access expression. 7 // 8 // foo 9 export type access_identifier = ident; 10 11 // An index access expression. 12 // 13 // foo[0] 14 export type access_index = struct { 15 object: *expr, 16 index: *expr, 17 }; 18 19 // A struct field access expression. 20 // 21 // foo.bar 22 export type access_field = struct { 23 object: *expr, 24 field: str, 25 }; 26 27 // A tuple field access expression. 28 // 29 // foo.1 30 export type access_tuple = struct { 31 object: *expr, 32 value: *expr, 33 }; 34 35 // An access expression. 36 export type access_expr = (access_identifier | access_index | access_field 37 | access_tuple); 38 39 // An align expression. 40 // 41 // align(int) 42 export type align_expr = *_type; 43 44 // The form of an allocation expression. 45 // 46 // alloc(foo) // OBJECT 47 // alloc(foo...) // COPY 48 export type alloc_form = enum { 49 OBJECT, 50 COPY, 51 }; 52 53 // An allocation expression. 54 // 55 // alloc(foo) 56 // alloc(foo...) 57 // alloc(foo, bar) 58 export type alloc_expr = struct { 59 init: *expr, 60 form: alloc_form, 61 capacity: nullable *expr, 62 }; 63 64 // An append expression. 65 // 66 // append(foo, bar); 67 // append(foo, bar...); 68 // append(foo, [0...], bar); 69 export type append_expr = struct { 70 object: *expr, 71 value: *expr, 72 length: nullable *expr, 73 variadic: bool, 74 is_static: bool, 75 }; 76 77 // An assertion expression. 78 // 79 // assert(foo) 80 // assert(foo, "error") 81 // abort() 82 // abort("error") 83 export type assert_expr = struct { 84 cond: nullable *expr, 85 message: nullable *expr, 86 is_static: bool, 87 }; 88 89 // An assignment expression. 90 // 91 // foo = bar 92 export type assign_expr = struct { 93 op: (binarithm_op | void), 94 object: *expr, 95 value: *expr, 96 }; 97 98 // A binary arithmetic operator 99 export type binarithm_op = enum { 100 // TODO: Rehome this with the checked AST? 101 102 BAND, // & 103 BOR, // | 104 DIV, // / 105 GT, // > 106 GTEQ, // >= 107 LAND, // && 108 LEQUAL, // == 109 LESS, // < 110 LESSEQ, // <= 111 LOR, // || 112 LSHIFT, // << 113 LXOR, // ^^ 114 MINUS, // - 115 MODULO, // % 116 NEQUAL, // != 117 PLUS, // + 118 RSHIFT, // >> 119 TIMES, // * 120 BXOR, // ^ 121 }; 122 123 // A binary arithmetic expression. 124 // 125 // foo * bar 126 export type binarithm_expr = struct { 127 op: binarithm_op, 128 lvalue: *expr, 129 rvalue: *expr, 130 }; 131 132 // A single variable biding. 133 // 134 // foo: int = bar 135 // (foo, foo2): int = bar 136 export type binding = struct { 137 name: (str | binding_unpack), 138 _type: nullable *_type, 139 init: *expr, 140 }; 141 142 // Tuple unpacking binding. 143 // 144 // (foo, _, bar) 145 export type binding_unpack = [](str | void); 146 147 // The kind of binding expression being used. 148 export type binding_kind = enum { 149 CONST, 150 DEF, 151 LET, 152 }; 153 154 // A variable binding expression. 155 // 156 // let foo: int = bar, ... 157 export type binding_expr = struct { 158 is_static: bool, 159 kind: binding_kind, 160 bindings: []binding, 161 }; 162 163 // A break expression. The label is set to empty string if absent. 164 // 165 // break :label 166 export type break_expr = label; 167 168 // A function call expression. 169 // 170 // foo(bar) 171 export type call_expr = struct { 172 lvalue: *expr, 173 variadic: bool, 174 args: []*expr, 175 }; 176 177 // The kind of cast expression being used. 178 export type cast_kind = enum { 179 // TODO: Should this be rehomed with the checked AST? 180 181 CAST, 182 ASSERTION, 183 TEST, 184 }; 185 186 // A cast expression. 187 // 188 // foo: int 189 // foo as int 190 // foo is int 191 export type cast_expr = struct { 192 kind: cast_kind, 193 value: *expr, 194 _type: *_type, 195 }; 196 197 // A compound expression. 198 // 199 // { 200 // foo; 201 // bar; 202 // // ... 203 // } 204 export type compound_expr = struct { 205 exprs: []*expr, 206 label: label, 207 }; 208 209 // An array literal. 210 // 211 // [foo, bar, ...] 212 export type array_literal = struct { 213 expand: bool, 214 values: []*expr, 215 }; 216 217 // A single struct field and value. 218 // 219 // foo: int = 10 220 export type struct_value = struct { 221 name: str, 222 _type: nullable *_type, 223 init: *expr, 224 }; 225 226 // A struct literal. 227 // 228 // struct { foo: int = bar, struct { baz = quux }, ... } 229 export type struct_literal = struct { 230 autofill: bool, 231 alias: ident, // [] for anonymous 232 fields: [](struct_value | *struct_literal), 233 }; 234 235 // A tuple literal. 236 // 237 // (foo, bar, ...) 238 export type tuple_literal = []*expr; 239 240 // The value "null". 241 export type _null = void; 242 243 // A scalar value. 244 export type value = (bool | done | _null | str | rune | void); 245 246 // An integer or float literal. 247 export type number_literal = struct { 248 suff: lex::ltok, 249 value: (i64 | u64 | f64), 250 sign: bool, // true if negative, false otherwise 251 }; 252 253 // A literal expression. 254 export type literal_expr = (value | array_literal | number_literal | 255 struct_literal | tuple_literal); 256 257 // A continue expression. The label is set to empty string if absent. 258 // 259 // continue :label 260 export type continue_expr = label; 261 262 // A deferred expression. 263 // 264 // defer foo 265 export type defer_expr = *expr; 266 267 // A delete expression. 268 // 269 // delete(foo[10]) 270 // delete(foo[4..42]) 271 export type delete_expr = struct { 272 object: *expr, 273 is_static: bool, 274 }; 275 276 // The kind of for expression being used. 277 export type for_kind = enum { 278 ACCUMULATOR, 279 EACH_VALUE, 280 EACH_POINTER, 281 ITERATOR, 282 }; 283 284 // A for loop. 285 // 286 // for (let foo = 0; foo < bar; baz) quux 287 // for (let line => next_line()) quux 288 // for (let number .. [1, 2, 3]) quux 289 // for (let ptr &.. [1, 2, 3]) quux 290 export type for_expr = struct { 291 kind: for_kind, 292 bindings: nullable *expr, 293 cond: nullable *expr, 294 afterthought: nullable *expr, 295 body: *expr, 296 label: label, 297 }; 298 299 // A free expression. 300 // 301 // free(foo) 302 export type free_expr = *expr; 303 304 // An if or if..else expression. 305 // 306 // if (foo) bar else baz 307 export type if_expr = struct { 308 cond: *expr, 309 tbranch: *expr, 310 fbranch: nullable *expr, 311 }; 312 313 // An insert expression. 314 // 315 // insert(foo[0], bar); 316 // insert(foo[0], bar...); 317 // insert(foo[0], [0...], bar); 318 export type insert_expr = append_expr; 319 320 // :label. The ":" character is not included. 321 export type label = str; 322 323 // A length expression. 324 // 325 // len(foo) 326 export type len_expr = *expr; 327 328 // A match case. 329 // 330 // case type => exprs 331 // case let name: type => exprs 332 export type match_case = struct { 333 name: str, 334 _type: nullable *_type, // null for default case 335 exprs: []*expr, 336 }; 337 338 // A match expression. 339 // 340 // match (foo) { case int => bar; ... } 341 export type match_expr = struct { 342 value: *expr, 343 cases: []match_case, 344 label: label, 345 }; 346 347 // An offset expression. 348 // 349 // offset(foo.bar) 350 export type offset_expr = *expr; 351 352 // An error propagation expression. 353 // 354 // foo? 355 export type propagate_expr = *expr; 356 357 // An error assertion expression. 358 // 359 // foo! 360 export type error_assert_expr = *expr; 361 362 // A return statement. 363 // 364 // return foo 365 export type return_expr = nullable *expr; 366 367 // A size expression. 368 // 369 // size(int) 370 export type size_expr = *_type; 371 372 // A slicing expression. 373 // 374 // foo[bar..baz] 375 export type slice_expr = struct { 376 object: *expr, 377 start: nullable *expr, 378 end: nullable *expr, 379 }; 380 381 // A switch case. 382 // 383 // case value => exprs 384 export type switch_case = struct { 385 options: []*expr, // [] for default case 386 exprs: []*expr, 387 }; 388 389 // A switch expression. 390 // 391 // switch (foo) { case bar => baz; ... } 392 export type switch_expr = struct { 393 value: *expr, 394 cases: []switch_case, 395 label: label, 396 }; 397 398 // A unary operator 399 export type unarithm_op = enum { 400 // TODO: Should this be rehomed with the checked AST? 401 402 ADDR, // & 403 BNOT, // ~ 404 DEREF, // * 405 LNOT, // ! 406 MINUS, // - 407 }; 408 409 // A unary arithmetic expression. 410 // 411 // !example 412 export type unarithm_expr = struct { 413 op: unarithm_op, 414 operand: *expr, 415 }; 416 417 // A vastart expression. 418 // 419 // vastart() 420 export type vastart_expr = void; 421 422 // A vaarg expression. 423 // 424 // vaarg(ap) 425 export type vaarg_expr = *expr; 426 427 // A vaend expression. 428 // 429 // vaend(ap) 430 export type vaend_expr = *expr; 431 432 // A C-style variadic expression. 433 export type variadic_expr = (vastart_expr | vaarg_expr | vaend_expr); 434 435 // A yield expression. 436 // 437 // yield foo 438 export type yield_expr = struct { 439 label: label, 440 value: nullable *expr, 441 }; 442 443 // A Hare expression. 444 export type expr = struct { 445 start: lex::location, 446 end: lex::location, 447 expr: (access_expr | align_expr | alloc_expr | append_expr | 448 assert_expr | assign_expr | binarithm_expr | binding_expr | 449 break_expr | call_expr | cast_expr | literal_expr | 450 continue_expr | defer_expr | delete_expr | for_expr | 451 free_expr | error_assert_expr | if_expr | insert_expr | 452 compound_expr | match_expr | len_expr | size_expr | 453 offset_expr | propagate_expr | return_expr | slice_expr | 454 switch_expr | unarithm_expr | variadic_expr | yield_expr), 455 }; 456 457 // Frees resources associated with a Hare [[expr]]ession. 458 export fn expr_finish(e: nullable *expr) void = match (e) { 459 case null => void; 460 case let e: *expr => 461 match (e.expr) { 462 case let a: access_expr => 463 match (a) { 464 case let i: access_identifier => 465 ident_free(i); 466 case let i: access_index => 467 expr_finish(i.object); 468 free(i.object); 469 expr_finish(i.index); 470 free(i.index); 471 case let f: access_field => 472 expr_finish(f.object); 473 free(f.object); 474 free(f.field); 475 case let t: access_tuple => 476 expr_finish(t.object); 477 free(t.object); 478 expr_finish(t.value); 479 free(t.value); 480 }; 481 case let a: align_expr => 482 type_finish(a); 483 free(a); 484 case let a: alloc_expr => 485 expr_finish(a.init); 486 free(a.init); 487 expr_finish(a.capacity); 488 free(a.capacity); 489 case let a: append_expr => 490 expr_finish(a.object); 491 free(a.object); 492 expr_finish(a.value); 493 free(a.value); 494 expr_finish(a.length); 495 free(a.length); 496 case let a: assert_expr => 497 expr_finish(a.cond); 498 free(a.cond); 499 expr_finish(a.message); 500 free(a.message); 501 case let a: assign_expr => 502 expr_finish(a.object); 503 free(a.object); 504 expr_finish(a.value); 505 free(a.value); 506 case let b: binarithm_expr => 507 expr_finish(b.lvalue); 508 free(b.lvalue); 509 expr_finish(b.rvalue); 510 free(b.rvalue); 511 case let b: binding_expr => 512 for (let i = 0z; i < len(b.bindings); i += 1) { 513 match (b.bindings[i].name) { 514 case let s: str => 515 free(s); 516 case let u: binding_unpack => 517 for (let i = 0z; i < len(u); i += 1) { 518 match (u[i]) { 519 case let s: str => 520 free(s); 521 case => void; 522 }; 523 }; 524 free(u); 525 }; 526 type_finish(b.bindings[i]._type); 527 free(b.bindings[i]._type); 528 expr_finish(b.bindings[i].init); 529 free(b.bindings[i].init); 530 }; 531 free(b.bindings); 532 case let b: break_expr => 533 free(b); 534 case let c: call_expr => 535 expr_finish(c.lvalue); 536 free(c.lvalue); 537 for (let i = 0z; i < len(c.args); i += 1) { 538 expr_finish(c.args[i]); 539 free(c.args[i]); 540 }; 541 free(c.args); 542 case let c: cast_expr => 543 expr_finish(c.value); 544 free(c.value); 545 type_finish(c._type); 546 free(c._type); 547 case let c: compound_expr => 548 for (let i = 0z; i < len(c.exprs); i += 1) { 549 expr_finish(c.exprs[i]); 550 free(c.exprs[i]); 551 }; 552 free(c.exprs); 553 free(c.label); 554 case let c: literal_expr => 555 match (c) { 556 case let a: array_literal => 557 for (let i = 0z; i < len(a.values); i += 1) { 558 expr_finish(a.values[i]); 559 free(a.values[i]); 560 }; 561 free(a.values); 562 case let s: struct_literal => 563 struct_literal_finish(&s); 564 case let t: tuple_literal => 565 for (let i = 0z; i < len(t); i += 1) { 566 expr_finish(t[i]); 567 free(t[i]); 568 }; 569 free(t); 570 case (value | number_literal) => void; 571 }; 572 case let c: continue_expr => 573 free(c); 574 case let d: defer_expr => 575 expr_finish(d); 576 free(d); 577 case let d: delete_expr => 578 expr_finish(d.object); 579 free(d.object); 580 case let e: error_assert_expr => 581 expr_finish(e); 582 free(e); 583 case let f: for_expr => 584 expr_finish(f.bindings); 585 free(f.bindings); 586 expr_finish(f.cond); 587 free(f.cond); 588 expr_finish(f.afterthought); 589 free(f.afterthought); 590 expr_finish(f.body); 591 free(f.body); 592 case let f: free_expr => 593 expr_finish(f); 594 free(f); 595 case let i: if_expr => 596 expr_finish(i.cond); 597 free(i.cond); 598 expr_finish(i.tbranch); 599 free(i.tbranch); 600 expr_finish(i.fbranch); 601 free(i.fbranch); 602 case let e: insert_expr => 603 expr_finish(e.object); 604 free(e.object); 605 expr_finish(e.value); 606 free(e.value); 607 expr_finish(e.length); 608 free(e.length); 609 case let l: len_expr => 610 expr_finish(l); 611 free(l); 612 case let m: match_expr => 613 free(m.label); 614 expr_finish(m.value); 615 free(m.value); 616 for (let i = 0z; i < len(m.cases); i += 1) { 617 free(m.cases[i].name); 618 type_finish(m.cases[i]._type); 619 free(m.cases[i]._type); 620 const exprs = m.cases[i].exprs; 621 for (let i = 0z; i < len(exprs); i += 1) { 622 expr_finish(exprs[i]); 623 free(exprs[i]); 624 }; 625 free(exprs); 626 }; 627 free(m.cases); 628 case let o: offset_expr => 629 expr_finish(o); 630 free(o); 631 case let p: propagate_expr => 632 expr_finish(p); 633 free(p); 634 case let r: return_expr => 635 expr_finish(r); 636 free(r); 637 case let s: size_expr => 638 type_finish(s); 639 free(s); 640 case let s: slice_expr => 641 expr_finish(s.object); 642 free(s.object); 643 expr_finish(s.start); 644 free(s.start); 645 expr_finish(s.end); 646 free(s.end); 647 case let s: switch_expr => 648 free(s.label); 649 expr_finish(s.value); 650 free(s.value); 651 for (let i = 0z; i < len(s.cases); i += 1) { 652 let opts = s.cases[i].options; 653 for (let j = 0z; j < len(opts); j += 1) { 654 expr_finish(opts[j]); 655 free(opts[j]); 656 }; 657 free(opts); 658 659 let exprs = s.cases[i].exprs; 660 for (let j = 0z; j < len(exprs); j += 1) { 661 expr_finish(exprs[j]); 662 free(exprs[j]); 663 }; 664 free(exprs); 665 }; 666 free(s.cases); 667 case let u: unarithm_expr => 668 expr_finish(u.operand); 669 free(u.operand); 670 case let v: variadic_expr => 671 match (v) { 672 case vastart_expr => void; 673 case let v: vaarg_expr => 674 expr_finish(v); 675 free(v); 676 case let v: vaend_expr => 677 expr_finish(v); 678 free(v); 679 }; 680 case let y: yield_expr => 681 free(y.label); 682 expr_finish(y.value); 683 free(y.value); 684 }; 685 }; 686 687 fn struct_literal_finish(s: *struct_literal) void = { 688 ident_free(s.alias); 689 for (let i = 0z; i < len(s.fields); i += 1) { 690 match (s.fields[i]) { 691 case let v: struct_value => 692 free(v.name); 693 type_finish(v._type); 694 free(v._type); 695 expr_finish(v.init); 696 free(v.init); 697 case let c: *struct_literal => 698 struct_literal_finish(c); 699 free(c); 700 }; 701 }; 702 free(s.fields); 703 };