harec

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

commit 171bc0b0d1d5940b779ccab776bca38d4a59f639
parent 3774f6384d8d1c4a6951ead5c6d2dc5a67977d1c
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 11 Apr 2021 10:26:13 -0400

parse: fix append variadic operator placement

Diffstat:
Msrc/parse.c | 19++++++++++++++-----
Mtests/19-append.ha | 8++++----
2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/parse.c b/src/parse.c @@ -1267,19 +1267,28 @@ parse_allocation_expression(struct lexer *lexer) if (lex(lexer, &tok) == T_ELLIPSIS) { exp->append.variadic = parse_expression(lexer); - if (lex(lexer, &tok) != T_COMMA) { - unlex(lexer, &tok); - } - want(lexer, T_RPAREN, &tok); break; } *next = xcalloc(1, sizeof(struct ast_append_values)); unlex(lexer, &tok); (*next)->expr = parse_expression(lexer); - if (lex(lexer, &tok) != T_COMMA) { + + lex(lexer, &tok); + if (tok.token == T_ELLIPSIS) { + exp->append.variadic = (*next)->expr; + free(*next); + *next = NULL; + if (lex(lexer, &tok) != T_COMMA) { + unlex(lexer, &tok); + } + want(lexer, T_RPAREN, &tok); + break; + } else if (tok.token != T_COMMA) { unlex(lexer, &tok); want(lexer, T_RPAREN, &tok); + break; } + next = &(*next)->next; } break; diff --git a/tests/19-append.ha b/tests/19-append.ha @@ -12,7 +12,7 @@ fn simple() void = { let y: [][]int = alloc([[1]], 1z); let z = [2, 3, 4]; - append(y, ...y); + append(y, y...); append(y, z); assert(len(y) == 3); assert(len(y[0]) == 1 && len(y[1]) == 1 && len(y[2]) == 3); @@ -25,15 +25,15 @@ fn simple() void = { fn variadic() void = { let x: []int = alloc([1, 2], 2z); - append(x, 4, ...[8, 16, 32, 64]); + append(x, 4, [8, 16, 32, 64]...); assert(len(x) == 7); assert(x[0] == 1 && x[1] == 2 && x[2] == 4 && x[3] == 8 && x[4] == 16); assert(x[5] == 32 && x[6] == 64); let y: []int = alloc([1, 2, 3], 3); let z = [128, 256]; - append(y, ...x); - append(y, ...z); + append(y, x...); + append(y, z...); assert(len(y) == 12); assert(y[0] == 1 && y[1] == 2 && y[2] == 3 && y[3] == 1 && y[4] == 2); assert(y[5] == 4 && y[6] == 8 && y[7] == 16 && y[8] == 32);