Skip to content

Commit 7a1602f

Browse files
committed
[Tolk] Support syntax tensorVar.0 and tupleVar.0
It works both for reading and writing: > var t = (1, 2); > t.0; // 1 > t.0 = 5; > t; // (5, 2) It also works for typed/untyped tuples, producing INDEX and SETINDEX. Global tensors and tuples works. Nesting `t.0.1.2` works. `mutate` works. Even mixing tuples inside tensors inside a global for writing works.
1 parent 565bc59 commit 7a1602f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1119
-338
lines changed

crypto/smartcont/tolk-stdlib/common.tolk

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,32 @@ fun tuplePush<T>(mutate self: tuple, value: T): void
2121
asm "TPUSH";
2222

2323
/// Returns the first element of a non-empty tuple.
24+
/// `t.0` is actually the same as `t.tupleFirst()`
2425
@pure
25-
fun tupleFirst<T>(t: tuple): T
26+
fun tupleFirst<T>(self: tuple): T
2627
asm "FIRST";
2728

2829
/// Returns the [`index`]-th element of a tuple.
30+
/// `t.i` is actually the same as `t.tupleAt(i)`
2931
@pure
30-
fun tupleAt<T>(t: tuple, index: int): T
32+
fun tupleAt<T>(self: tuple, index: int): T
33+
builtin;
34+
35+
/// Sets the [`index`]-th element of a tuple to a specified value
36+
/// (element with this index must already exist, a new element isn't created).
37+
/// `t.i = value` is actually the same as `t.tupleSetAt(value, i)`
38+
@pure
39+
fun tupleSetAt<T>(mutate self: tuple, value: T, index: int): void
3140
builtin;
3241

3342
/// Returns the size of a tuple (elements count in it).
3443
@pure
35-
fun tupleSize(t: tuple): int
44+
fun tupleSize(self: tuple): int
3645
asm "TLEN";
3746

3847
/// Returns the last element of a non-empty tuple.
3948
@pure
40-
fun tupleLast<T>(t: tuple): T
49+
fun tupleLast<T>(self: tuple): T
4150
asm "LAST";
4251

4352

tolk-tester/tests/a10.tolk

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ fun testStartBalanceCodegen2() {
7878
return first;
7979
}
8080

81+
global cur: [int, int, int];
82+
global next: [int, int, int];
83+
84+
@method_id(95)
85+
fun test95() {
86+
cur = [1, 2, 3];
87+
next = [2, 3, 4];
88+
(cur, next) = (next, [3, 4, 5]);
89+
return (cur, next);
90+
}
91+
8192
/**
8293
method_id | in | out
8394
@testcase | 0 | 101 15 | 100 1
@@ -90,6 +101,7 @@ fun testStartBalanceCodegen2() {
90101
@testcase | 89 | 4 | 1 4 1 4
91102
@testcase | 91 | | 10
92103
@testcase | 92 | | 10 32
104+
@testcase | 95 | | [ 2 3 4 ] [ 3 4 5 ]
93105

94106
@fif_codegen
95107
"""
@@ -104,9 +116,9 @@ fun testStartBalanceCodegen2() {
104116
testDumpDontPolluteStack PROC:<{
105117
...
106118
DUMPSTK
107-
x{6d79} PUSHSLICE // f s _5
119+
x{6d79} PUSHSLICE // f s '5
108120
STRDUMP DROP
109-
SBITS // f _6
121+
SBITS // f '6
110122
}>
111123
"""
112124

@@ -127,4 +139,20 @@ fun testStartBalanceCodegen2() {
127139
FIRST // first
128140
}>
129141
"""
142+
143+
@fif_codegen
144+
"""
145+
test95 PROC:<{
146+
...
147+
next GETGLOB // '10
148+
3 PUSHINT // '10 '12=3
149+
4 PUSHINT // '10 '12=3 '13=4
150+
5 PUSHINT // '10 '12=3 '13=4 '14=5
151+
TRIPLE // '15 '16
152+
next SETGLOB
153+
cur SETGLOB
154+
cur GETGLOB // '17
155+
next GETGLOB // '17 '18
156+
}>
157+
"""
130158
*/

tolk-tester/tests/a6_1.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fun main(a: int, b: int, c: int, d: int, e: int, f: int): (int, int) {
77

88
@method_id(101)
99
fun testDivMod(x: int, y: int) {
10-
return [divMod(x, y), modDiv(x, y), mulDivMod(x, y, 10)];
10+
return (divMod(x, y), modDiv(x, y), mulDivMod(x, y, 10));
1111
}
1212

1313
/**
@@ -18,5 +18,5 @@ fun testDivMod(x: int, y: int) {
1818
@testcase | 0 | 448 -433 -444 792 150012 -356232 | -218 -572
1919
@testcase | 0 | -40 -821 433 -734 -721629 -741724 | -206 889
2020
@testcase | 0 | -261 -98 -494 868 -166153 733738 | 263 995
21-
@testcase | 101 | 112 3 | [ 37 1 1 37 33 6 ]
21+
@testcase | 101 | 112 3 | 37 1 1 37 33 6
2222
*/

tolk-tester/tests/allow_post_modification.tolk

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ fun test_if_else(x: int): (int, int, int, int, int) {
8989

9090
@method_id(21)
9191
fun test_assign_with_inner(x: int) {
92-
return (x, x += 10, [(x, x += 20, eq(x -= 50), x)], eq2((x, x *= eq(x /= 2))));
92+
var result = (x, x += 10, [x, x += 20, eq(x -= 50), x], eq2((x, x *= eq(x /= 2))));
93+
return result;
9394
}
9495

9596
@method_id(22)
9697
fun test_assign_with_mutate(x: int) {
97-
return (x, mul2(mutate x, x += 5), x.`~inc`(mul2(mutate x, x)), x);
98+
var (result, _) = ((x, mul2(mutate x, x += 5), x.`~inc`(mul2(mutate x, x)), x), 0);
99+
return result;
98100
}
99101

100102
@method_id(23)
@@ -138,5 +140,12 @@ fun main() {
138140
inc CALLDICT // self newY
139141
}>
140142
"""
143+
144+
@fif_codegen
145+
"""
146+
test_assign_tensor_global PROC:<{
147+
// x.0 x.1
148+
"""
149+
141150
@code_hash 7627024945492125068389905298530400936797031708759561372406088054030801992712
142151
*/

tolk-tester/tests/bit-operators.tolk

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ fun testBoolCompareOptimized(x: bool) {
127127
"""
128128
boolWithBitwiseConst PROC:<{
129129
//
130-
0 PUSHINT // _3
131-
-1 PUSHINT // _3 _5
132-
0 PUSHINT // _3 _5 _7
133-
-1 PUSHINT // _3 _5 _7 _8
130+
0 PUSHINT // '3
131+
-1 PUSHINT // '3 '5
132+
0 PUSHINT // '3 '5 '7
133+
-1 PUSHINT // '3 '5 '7 '8
134134
}>
135135
"""
136136

@@ -142,35 +142,35 @@ fun testBoolCompareOptimized(x: bool) {
142142
UNTIL:<{
143143
INC // i n cnt
144144
s2 PUSH // i n cnt i
145-
NOT // i n cnt _6
145+
NOT // i n cnt '6
146146
}> // i n cnt
147147
UNTIL:<{
148148
INC // i n cnt
149149
s2 PUSH // i n cnt i
150-
NOT // i n cnt _9
150+
NOT // i n cnt '9
151151
}> // i n cnt
152152
UNTIL:<{
153153
INC // i n cnt
154154
OVER // i n cnt n
155-
0 EQINT // i n cnt _12
155+
0 EQINT // i n cnt '12
156156
}> // i n cnt
157157
s0 s2 XCHG // cnt n i
158-
NOT // cnt n _13
159-
SWAP // cnt _13 n
160-
0 EQINT // cnt _13 _14
158+
NOT // cnt n '13
159+
SWAP // cnt '13 n
160+
0 EQINT // cnt '13 '14
161161
}>
162162
"""
163163

164164
@fif_codegen
165165
"""
166166
testConstNegateCodegen PROC:<{
167167
//
168-
TRUE // _0
169-
FALSE // _0 _1
170-
FALSE // _0 _1 _2
171-
TRUE // _0 _1 _2 _3
172-
TRUE // _0 _1 _2 _3 _4
173-
FALSE // _0 _1 _2 _3 _4 _5
168+
TRUE // '0
169+
FALSE // '0 '1
170+
FALSE // '0 '1 '2
171+
TRUE // '0 '1 '2 '3
172+
TRUE // '0 '1 '2 '3 '4
173+
FALSE // '0 '1 '2 '3 '4 '5
174174
}>
175175
"""
176176

@@ -179,11 +179,11 @@ fun testBoolCompareOptimized(x: bool) {
179179
testBoolNegateOptimized PROC:<{
180180
// x
181181
DUP // x x
182-
NOT // x _1
183-
OVER // x _1 x
184-
NOT // x _1 _2
182+
NOT // x '1
183+
OVER // x '1 x
184+
NOT // x '1 '2
185185
s2 s(-1) PUXC
186-
TRUE // x _1 x _2 _3
186+
TRUE // x '1 x '2 '3
187187
}>
188188
"""
189189

@@ -192,13 +192,13 @@ fun testBoolCompareOptimized(x: bool) {
192192
testBoolCompareOptimized PROC:<{
193193
// x
194194
DUP // x x
195-
NOT // x _1
196-
OVER // x _1 x
197-
eqX CALLDICT // x _1 _2
198-
NOT // x _1 _3
199-
s2 PUSH // x _1 _3 x
200-
eqX CALLDICT // x _1 _3 _4
201-
s3 PUSH // x _1 _3 _4 x
195+
NOT // x '1
196+
OVER // x '1 x
197+
eqX CALLDICT // x '1 '2
198+
NOT // x '1 '3
199+
s2 PUSH // x '1 '3 x
200+
eqX CALLDICT // x '1 '3 '4
201+
s3 PUSH // x '1 '3 '4 x
202202
}>
203203
"""
204204
*/

tolk-tester/tests/cells-slices.tolk

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,16 @@ Note, that since 'compute-asm-ltr' became on be default, chaining methods codege
216216
"""
217217
test6 PROC:<{
218218
//
219-
NEWC // _0
220-
1 PUSHINT // _0 _1=1
221-
SWAP // _1=1 _0
222-
32 STU // _0
223-
2 PUSHINT // _0 _4=2
224-
SWAP // _4=2 _0
225-
32 STU // _0
226-
3 PUSHINT // _0 _7=3
227-
SWAP // _7=3 _0
228-
32 STU // _0
219+
NEWC // '0
220+
1 PUSHINT // '0 '1=1
221+
SWAP // '1=1 '0
222+
32 STU // '0
223+
2 PUSHINT // '0 '4=2
224+
SWAP // '4=2 '0
225+
32 STU // '0
226+
3 PUSHINT // '0 '7=3
227+
SWAP // '7=3 '0
228+
32 STU // '0
229229
}>
230230
"""
231231
*/

tolk-tester/tests/codegen_check_demo.tolk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Below, I just give examples of @fif_codegen tag:
3535
"""
3636
main PROC:<{
3737
// s
38-
17 PUSHINT // s _1=17
38+
17 PUSHINT // s '1=17
3939
OVER // s z=17 t
4040
WHILE:<{
4141
...
@@ -63,7 +63,7 @@ main PROC:<{
6363
@fif_codegen
6464
"""
6565
OVER
66-
0 GTINT // s z t _5
66+
0 GTINT // s z t '5
6767
"""
6868

6969
@fif_codegen
@@ -83,7 +83,7 @@ FALSE
8383
}>
8484
"""
8585

86-
@fif_codegen NOT // _8
86+
@fif_codegen NOT // '8
8787
@fif_codegen main PROC:<{
8888

8989
@fif_codegen_avoid PROCINLINE

tolk-tester/tests/generics-1.tolk

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ fun getTwo<X>(): X { return 2 as X; }
1414
fun takeInt(a: int) { return a; }
1515

1616
@method_id(102)
17-
fun test102(): (int, int, int, [(int, int)]) {
17+
fun test102(): (int, int, int, [int, int]) {
1818
var a: int = getTwo();
1919
var _: int = getTwo();
2020
var b = getTwo() as int;
2121
var c: int = 1 ? getTwo() : getTwo();
2222
var c redef = getTwo();
23-
return (eq1<int>(a), eq2<int>(b), takeInt(getTwo()), [(getTwo(), getTwo())]);
23+
var ab_tens = (0, (1, 2));
24+
ab_tens.0 = getTwo();
25+
ab_tens.1.1 = getTwo();
26+
var ab_tup = [0, [1, 2]];
27+
ab_tup.0 = getTwo();
28+
ab_tup.1.1 = getTwo();
29+
return (eq1<int>(a), eq2<int>(b), takeInt(getTwo()), [getTwo(), ab_tens.1.1]);
2430
}
2531

2632
@method_id(103)
@@ -43,9 +49,9 @@ fun manyEq<T1, T2, T3>(a: T1, b: T2, c: T3): [T1, T2, T3] {
4349
fun test104(f: int) {
4450
var result = (
4551
manyEq(1 ? 1 : 1, f ? 0 : null, !f ? getTwo() as int : null),
46-
manyEq((f ? null as int : eq2(2), beginCell().storeBool(true).endCell().beginParse().loadBool()), 0, eq4(f))
52+
manyEq(f ? null as int : eq2(2), beginCell().storeBool(true).endCell().beginParse().loadBool(), eq4(f))
4753
);
48-
__expect_type(result, "([int, int, int], [(int, bool), int, int])");
54+
__expect_type(result, "([int, int, int], [int, bool, int])");
4955
return result;
5056
}
5157

@@ -74,7 +80,8 @@ fun test106() {
7480
return [
7581
abstractTransform(cellToSlice, calcLoad32, c),
7682
abstractTransform(calcYPlus1<int>, calcYPlus1<int>, 0),
77-
abstractTransform(calcTensorPlus1, calcTensorMul2, (2, 2))
83+
abstractTransform(calcTensorPlus1, calcTensorMul2, (2, 2)).0,
84+
abstractTransform(calcTensorPlus1, calcTensorMul2, (2, 2)).1
7885
];
7986
}
8087

@@ -135,7 +142,7 @@ fun main(x: int): (int, [[int, int]]) {
135142
@testcase | 101 | 0 | 0 0 0 [ 0 0 ] 0 0 0 [ 0 0 ] 0 0 0 []
136143
@testcase | 102 | | 2 2 2 [ 2 2 ]
137144
@testcase | 103 | 0 | 0 100 100
138-
@testcase | 104 | 0 | [ 1 (null) 2 ] [ 2 -1 0 0 ]
145+
@testcase | 104 | 0 | [ 1 (null) 2 ] [ 2 -1 0 ]
139146
@testcase | 105 | | 3
140147
@testcase | 106 | | [ 106 2 6 6 ]
141148
@testcase | 107 | | 6 6 1 1 6 6

tolk-tester/tests/if_stmt.tolk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ fun main() {
5454
test3 PROC:<{
5555
// x
5656
DUP // x x
57-
20 NEQINT // x _2
57+
20 NEQINT // x '2
5858
IFNOTJMP:<{ // x
5959
DROP //
60-
20 PUSHINT // _3=20
60+
20 PUSHINT // '3=20
6161
}> // x
6262
DUP // x x
63-
50 EQINT // x _5
63+
50 EQINT // x '5
6464
IFNOTJMP:<{ // x
6565
"""
6666
*/

0 commit comments

Comments
 (0)