Skip to content

Commit d54e708

Browse files
committed
[Tolk] Support trailing comma in various places
Trailing comma is now allowed in: * tensors * tuples * function calls * function parameters Note, that `(5)` is not a tensor, it's just integer `5`. With a trailing comma `(5,)` it's still `(5)`.
1 parent 8194d1f commit d54e708

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

tolk-tester/tests/a6.tolk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fun f(a: int, b: int, c: int, d: int, e: int, f: int): (int, int) {
44
__expect_type(D, "int");
55
__expect_type(D*D, "int");
66
__expect_type(calc_phi, "() -> int");
7-
return (Dx/D,Dy/D);
7+
return (Dx/D,Dy/D,);
88
};;;;
99

1010
fun calc_phi(): int {
@@ -16,7 +16,7 @@ fun calc_phi(): int {
1616
do {
1717
(p,q)=(q,p+q);
1818
} while (q <= n); //;;
19-
return mulDivRound(p, n, q);
19+
return mulDivRound(p, n, q,);
2020
}
2121

2222
fun calc_sqrt2(): int {
@@ -31,10 +31,10 @@ fun calc_sqrt2(): int {
3131
return mulDivRound(p, n, q);
3232
}
3333

34-
fun calc_root(m: int) {
34+
fun calc_root(m: int,) {
3535
var base: int=1;
3636
repeat(70) { base *= 10; }
37-
var (a, b, c) = (1,0,-m);
37+
var (a, b, c) = (1,0,-m,);
3838
var (p1, q1, p2, q2) = (1, 0, 0, 1);
3939
do {
4040
var k: int=-1;

tolk-tester/tests/allow_post_modification.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fun test_call_1(x: int): (int, int, int, int, int, int, int) {
4747
return foo1(x, x.`~inc`(x / 20), x, x = x * 2, x, x += 1, x);
4848
}
4949

50-
fun foo2(x1: int, x2: int, x3456: (int, int, int, int), x7: int): (int, int, int, int, int, int, int) {
50+
fun foo2(x1: int, x2: int, x3456: (int, int, int, int), x7: int, ): (int, int, int, int, int, int, int) {
5151
var (x3: int, x4: int, x5: int, x6: int) = x3456;
5252
return (x1, x2, x3, x4, x5, x6, x7);
5353
}

tolk-tester/tests/assignment-tests.tolk

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fun extractFromTypedTuple(params: [int]) {
55

66
@method_id(101)
77
fun test101(x: int) {
8-
var params = [x];
8+
var params = [x, ];
99
return extractFromTypedTuple(params);
1010
}
1111

@@ -38,7 +38,7 @@ fun getTensor_1X(x: int) {
3838
}
3939
fun getTuple_12() {
4040
callOrder.tuplePush(110);
41-
return [1, 2];
41+
return [1, 2, ];
4242
}
4343
fun getTuple_1X(x: int) {
4444
callOrder.tuplePush(111);
@@ -133,7 +133,7 @@ fun test108() {
133133
fun test109() {
134134
callOrder = createEmptyTuple();
135135
var x = 0;
136-
[getTuple_12().0, getTuple_1X(x = getIntValue5()).1, getTuple_1X(x += 10).0] = [getIntValue5(), getIntValue5(), getIntValueX(x)];
136+
[getTuple_12().0, getTuple_1X(x = getIntValue5()).1, getTuple_1X(x += 10).0] = [getIntValue5(), getIntValue5(), getIntValueX(x), ];
137137
return (callOrder, x);
138138
}
139139

@@ -203,9 +203,18 @@ fun test116() {
203203
return (a, b, c, d, rhs2);
204204
}
205205

206+
@method_id(117)
207+
fun test117() {
208+
var c = [((1, ), ), ];
209+
__expect_type(c, "[int]");
210+
c.0 = 10;
211+
c.0 = (20, );
212+
return c;
213+
}
214+
206215

207216

208-
fun main(value: int) {
217+
fun main(value: int, ) {
209218
var (x: int?, y) = (autoInferIntNull(value), autoInferIntNull(value * 2));
210219
if (x == null && y == null) { return null; }
211220
return x == null || y == null ? -1 : x! + y!;
@@ -231,6 +240,7 @@ fun main(value: int) {
231240
@testcase | 114 | | 13 [ 1 14 ] 1 3
232241
@testcase | 115 | | [ 101 111 ] 9 9
233242
@testcase | 116 | | 1 2 3 4 [ 1 2 3 4 ]
243+
@testcase | 117 | | [ 20 ]
234244

235245

236246
@fif_codegen

tolk-tester/tests/inference-tests.tolk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ fun test1(x: int, y: int) {
3030
__expect_type(beginCell(), "builder");
3131
__expect_type(beginCell().endCell(), "cell");
3232
}
33+
__expect_type((1, 2), "(int, int)");
34+
__expect_type((1, 2, ), "(int, int)");
35+
__expect_type((1, ), "int");
36+
__expect_type((((1,)),), "int");
37+
__expect_type([1, 2], "[int, int]");
38+
__expect_type([1, 2, ], "[int, int]");
39+
__expect_type([1, ], "[int]");
40+
__expect_type([((1, ), ), ], "[int]");
41+
__expect_type([[[1, ], ], ], "[[[int]]]");
3342
}
3443

3544
fun test2(x: int, y: bool) {

tolk-tester/tests/no-spaces.tolk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const int10:int=10;
33
fun just10(): int { return int10; }
44
fun eq(v: int): int { return`v`; }
55

6-
@method_id(101) fun `get_-1` (): int {return-1;}
7-
@method_id(102) fun `get_--1` (): int {return--1;}
8-
@method_id(103) fun `get_---1`(): int {return---1;}
9-
@method_id(104) fun `get_+++1`(): int {return+++1;}
10-
@method_id(105) fun `get_+-+1`(): int {return+-+1;}
6+
@method_id(101,) fun `get_-1` (): int {return-1;}
7+
@method_id(102,) fun `get_--1` (): int {return--1;}
8+
@method_id(103,) fun `get_---1`(): int {return---1;}
9+
@method_id(104,) fun `get_+++1`(): int {return+++1;}
10+
@method_id(105,) fun `get_+-+1`(): int {return+-+1;}
1111

1212
global `some()var`:int;
1313

tolk-tester/tests/nullable-tensors.tolk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,13 @@ fun test135() {
415415
t1!.0 == null, t2!.0 == null, e1!.1.0 == null, e1!.1.1 == null, e2!.1.0 == null, e2!.1.1 == null);
416416
}
417417

418+
@method_id(136, )
419+
fun test136(x: int?, ) {
420+
var t1 = (x, ); // it's not a tensor with 1 element
421+
__expect_type(t1, "int?", );
422+
return ((t1, t1 == null, ), ); // testing trailing comma everywhere :)
423+
}
424+
418425

419426

420427
fun main(){}
@@ -466,6 +473,8 @@ fun main(){}
466473
@testcase | 133 | | 60
467474
@testcase | 134 | | 11 21 -1
468475
@testcase | 135 | | [ 10 ] [ (null) ] (null) 777 10 -1 (null) -1 (null) 0 777 10 -1 (null) -1 (null) 0 777 0 0 -1 0 0 -1 0 0 -1 777 0 -1 0 0 -1 0
476+
@testcase | 136 | 9 | 9 0
477+
@testcase | 136 | null | (null) -1
469478

470479
@fif_codegen
471480
"""

tolk/ast-from-tokens.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ static V<ast_parameter_list> parse_parameter_list(Lexer& lex) {
236236
params.push_back(parse_parameter(lex, true));
237237
while (lex.tok() == tok_comma) {
238238
lex.next();
239+
if (lex.tok() == tok_clpar) { // trailing comma
240+
break;
241+
}
239242
params.push_back(parse_parameter(lex, false));
240243
}
241244
}
@@ -266,6 +269,9 @@ static V<ast_argument_list> parse_argument_list(Lexer& lex) {
266269
args.push_back(parse_argument(lex));
267270
while (lex.tok() == tok_comma) {
268271
lex.next();
272+
if (lex.tok() == tok_clpar) { // trailing comma
273+
break;
274+
}
269275
args.push_back(parse_argument(lex));
270276
}
271277
}
@@ -311,9 +317,15 @@ static AnyExprV parse_expr100(Lexer& lex) {
311317
std::vector<AnyExprV> items(1, first);
312318
while (lex.tok() == tok_comma) {
313319
lex.next();
320+
if (lex.tok() == tok_clpar) { // trailing comma
321+
break;
322+
}
314323
items.emplace_back(parse_expr(lex));
315324
}
316325
lex.expect(tok_clpar, "`)`");
326+
if (items.size() == 1) { // we can reach here for 1 element with trailing comma: `(item, )`
327+
return items[0]; // then just return item, not a 1-element tensor,
328+
} // since 1-element tensors won't be type compatible with item's type
317329
return createV<ast_tensor>(loc, std::move(items));
318330
}
319331
case tok_opbracket: {
@@ -325,6 +337,9 @@ static AnyExprV parse_expr100(Lexer& lex) {
325337
std::vector<AnyExprV> items(1, parse_expr(lex));
326338
while (lex.tok() == tok_comma) {
327339
lex.next();
340+
if (lex.tok() == tok_clbracket) { // trailing comma
341+
break;
342+
}
328343
items.emplace_back(parse_expr(lex));
329344
}
330345
lex.expect(tok_clbracket, "`]`");
@@ -952,6 +967,9 @@ static V<ast_annotation> parse_annotation(Lexer& lex) {
952967
args.push_back(parse_expr(lex));
953968
while (lex.tok() == tok_comma) {
954969
lex.next();
970+
if (lex.tok() == tok_clpar) { // trailing comma
971+
break;
972+
}
955973
args.push_back(parse_expr(lex));
956974
}
957975
lex.expect(tok_clpar, "`)`");

0 commit comments

Comments
 (0)