harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit dd73e7df284e61e2fb1e371210dbe5956db84bcb
parent 33c9e86661a4f2cf628e07d72be451f99c0d4f61
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Tue,  7 Dec 2021 11:12:02 +0000

parse: update match case binding syntax

Signed-off-by: Eyal Sawady <ecs@d2evs.net>

Diffstat:
Msrc/parse.c | 32+++++---------------------------
Mtests/18-match.ha | 26+++++++++++++-------------
Mtests/26-gen.ha | 6+++---
3 files changed, 21 insertions(+), 43 deletions(-)

diff --git a/src/parse.c b/src/parse.c @@ -1842,35 +1842,13 @@ parse_match_expression(struct lexer *lexer) *next_case = xcalloc(1, sizeof(struct ast_match_case)); want(lexer, T_CASE, &tok); - struct token tok2 = {0}; - struct identifier ident = {0}; struct ast_type *type = NULL; switch (lex(lexer, &tok)) { - case T_NAME: - switch (lex(lexer, &tok2)) { - case T_COLON: - _case->name = tok.name; // Assumes ownership - _case->type = parse_type(lexer); - break; - case T_DOUBLE_COLON: - ident.ns = xcalloc(1, sizeof(struct identifier)); - ident.ns->name = tok.name; // Assumes ownership - parse_identifier(lexer, &ident, false); - _case->type = mktype(&tok.loc); - _case->type->storage = STORAGE_ALIAS; - _case->type->alias = ident; - break; - case T_ARROW: - unlex(lexer, &tok2); - _case->type = mktype(&tok.loc); - _case->type->storage = STORAGE_ALIAS; - _case->type->alias.name = tok.name; - break; - default: - synassert(false, &tok, T_COLON, - T_DOUBLE_COLON, T_ARROW, T_EOF); - break; - } + case T_LET: + want(lexer, T_NAME, &tok); + _case->name = tok.name; + want(lexer, T_COLON, NULL); + _case->type = parse_type(lexer); break; case T_ARROW: // Default case diff --git a/tests/18-match.ha b/tests/18-match.ha @@ -18,7 +18,7 @@ fn tagged() void = { yield 1; case uint => yield 2; - case s: str => + case let s: str => yield len(s); }; assert(y == expected[i]); @@ -56,7 +56,7 @@ fn pointer() void = { let x = 42; let y: nullable *int = &x; let z: int = match (y) { - case y: *int => + case let y: *int => yield *y; case null => abort(); @@ -90,18 +90,18 @@ fn alias() void = { fn tagged_result() void = { let x: (int | uint) = 42i; let y: (int | uint) = match (x) { - case x: int => + case let x: int => yield x; - case x: uint => + case let x: uint => yield x; }; assert(y is int); x = 42u; y = match (x) { - case x: int => + case let x: int => yield x; - case x: uint => + case let x: uint => yield x; }; assert(y is uint); @@ -113,7 +113,7 @@ fn implicit_cast() void = { let a: (int | foobar) = match (y) { case null => yield foo; - case z: *int => + case let z: *int => yield *z; }; assert(a is foobar); @@ -122,7 +122,7 @@ fn implicit_cast() void = { fn transitivity() void = { let x: (foobar | int) = 10; match (x) { - case i: int => + case let i: int => assert(i == 10); case foo => abort(); @@ -155,7 +155,7 @@ fn transitivity() void = { visit = false; match (x) { - case z: (foo | bar) => + case let z: (foo | bar) => visit = true; assert(z is bar); case int => @@ -193,15 +193,15 @@ fn numeric() void = { let visit = true; let x: integer = 1337i; match (x) { - case s: signed => + case let s: signed => match (s) { - case i: int => + case let i: int => visit = true; assert(i == 1337); case => abort(); }; - case u: unsigned => + case let u: unsigned => abort(); }; assert(visit); @@ -210,7 +210,7 @@ fn numeric() void = { fn alignment_conversion() void = { let x: align_8 = 1234i; match (x) { - case y: align_4 => + case let y: align_4 => assert(y as int == 1234); case => abort(); diff --git a/tests/26-gen.ha b/tests/26-gen.ha @@ -23,7 +23,7 @@ export fn main() void = { let x: (void | int) = 10; match (x) { - case i: int => + case let i: int => assert(i == 10); case void => abort(); @@ -34,7 +34,7 @@ export fn main() void = { let p = match (p) { case void => abort(); - case p: u64 => + case let p: u64 => yield p: uintptr: *int; }; assert(*p == 0); @@ -44,7 +44,7 @@ export fn main() void = { let p = match (thing) { case int => abort(); - case p: *int => + case let p: *int => yield p; }; *p = 0;