harec

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

commit 0e12d1e84cf7f0be0a9b94ee97352ca4d72d9219
parent 059db20030fa0fc6c1e516d071b758cf3475b960
Author: Alexey Yerin <yyp@disroot.org>
Date:   Mon, 19 Apr 2021 22:18:23 +0300

Update match according to a recent spec change

Diffstat:
Msrc/parse.c | 17+++++++++++++----
Mtests/18-match.ha | 50+++++++++++++++++++++++++-------------------------
2 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/src/parse.c b/src/parse.c @@ -1801,6 +1801,16 @@ parse_match_expression(struct lexer *lexer) struct token tok2 = {0}; struct identifier ident = {0}; switch (lex(lexer, &tok)) { + case T_NULL: + _case->name = "_"; + unlex(lexer, &tok); + _case->type = parse_type(lexer); + break; + case T_UNDERSCORE: + _case->name = "_"; + want(lexer, T_COLON, &tok); + _case->type = parse_type(lexer); + break; case T_NAME: switch (lex(lexer, &tok2)) { case T_COLON: @@ -1822,8 +1832,7 @@ parse_match_expression(struct lexer *lexer) _case->type->alias.name = tok.name; break; default: - synassert(false, &tok, T_COLON, - T_DOUBLE_COLON, T_CASE, T_EOF); + synassert(false, &tok, T_DOUBLE_COLON, T_CASE, T_EOF); break; } break; @@ -1843,8 +1852,8 @@ parse_match_expression(struct lexer *lexer) } break; default: - unlex(lexer, &tok); - _case->type = parse_type(lexer); + synassert(false, &tok, T_NAME, T_NULL, T_UNDERSCORE, + T_TIMES, T_EOF); break; } diff --git a/tests/18-match.ha b/tests/18-match.ha @@ -3,9 +3,9 @@ fn tagged() void = { let expected: [_]size = [1, 2, 5]; for (let i = 0z; i < len(cases); i += 1) { let y: size = match (cases[i]) { - int => 1, - uint => 2, - s: str => len(s), + _: int => 1, + _: uint => 2, + s: str => len(s), }; assert(y == expected[i]); }; @@ -15,9 +15,9 @@ fn termination() void = { let x: (int | uint | str) = 1337i; for (true) { let y: int = match (x) { - int => 42, - uint => abort(), - str => break, + _: int => 42, + _: uint => abort(), + _: str => break, }; assert(y == 42); x = "hi"; @@ -27,8 +27,8 @@ fn termination() void = { fn default() void = { let x: (int | uint | str) = 1337u; let y: int = match (x) { - int => 42, - * => 24, + _: int => 42, + * => 24, }; assert(y == 24); }; @@ -59,8 +59,8 @@ fn alias() void = { let expected = [42, 24]; for (let i = 0z; i < len(cases); i += 1) { let y: int = match (cases[i]) { - foo => 42, - bar => 24, + _: foo => 42, + _: bar => 24, }; assert(y == expected[i]); }; @@ -102,24 +102,24 @@ fn transitivity() void = { let x: (foobar | int) = 10; match (x) { i: int => assert(i == 10), - foo => abort(), - bar => abort(), + _: foo => abort(), + _: bar => abort(), }; x = foo; let visit = false; match (x) { - int => abort(), - foo => visit = true, - bar => abort(), + _: int => abort(), + _: foo => visit = true, + _: bar => abort(), }; assert(visit); x = bar; visit = false; match (x) { - int => abort(), - foo => abort(), - foobar => visit = true, + _: int => abort(), + _: foo => abort(), + _: foobar => visit = true, }; assert(visit); @@ -129,25 +129,25 @@ fn transitivity() void = { visit = true; assert(z is bar); }, - int => abort(), + _: int => abort(), }; assert(visit); let y: foobarbaz = 10; visit = false; match (y) { - baz => visit = true, - foo => abort(), - bar => abort(), + _: baz => visit = true, + _: foo => abort(), + _: bar => abort(), }; assert(visit); y = foo; visit = false; match (y) { - baz => abort(), - foo => visit = true, - bar => abort(), + _: baz => abort(), + _: foo => visit = true, + _: bar => abort(), }; assert(visit); };