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