Skip to content

Commit a010537

Browse files
committed
[Tolk] Support default values for fields, simplify constants
Now fields can have default values, even complex ones: > tensor: (int, coins) = (2, ton("0.05")) Initial value must be a constant expression. Global constants can also now be of any type, including tensors and nullables. Constant evaluator was almost dropped off, evaluation is now done at IR (Ops) level.
1 parent 9e6c1fc commit a010537

27 files changed

+266
-244
lines changed

tolk-tester/tests/constants-tests.tolk

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const true3 = true1 && true2;
2929
const false1 = !true;
3030
const false2 = false1 || false;
3131

32+
const tens1 = (1, 2);
33+
const tens2: (int, int, (int8, int16)) = (tens1.1, tens1.0 << 2, tens1);
34+
35+
const intOrN: int? = null;
36+
const int32Or64: int32 | int64 = 7 as int64;
37+
3238
fun iget1(): int { return int1; }
3339
fun iget2(): int { return int2; }
3440
fun iget3(): int { return int1+int2; }
@@ -71,6 +77,17 @@ fun test3() {
7177
return (false1, false2);
7278
}
7379

80+
@method_id(104)
81+
fun test4() {
82+
__expect_type(tens1, "(int, int)");
83+
return (tens1.0, tens2.2);
84+
}
85+
86+
@method_id(105)
87+
fun test5() {
88+
return (intOrN == null, int32Or64 is int32, int32Or64);
89+
}
90+
7491
fun main() {
7592
var i1: int = iget1();
7693
var i2: int = iget2();
@@ -98,6 +115,8 @@ fun main() {
98115
@testcase | 101 | | 0 -1
99116
@testcase | 102 | | -1 -1 -1
100117
@testcase | 103 | | 0 0
118+
@testcase | 104 | | 1 1 2
119+
@testcase | 105 | | -1 0 7 48
101120

102-
@code_hash 28102194299745406750019953961984060488024870092664444642078578246708959881688
121+
@code_hash 80040709432962217077682091261201772251141677197885524779745956896218368868623
103122
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct A {
2+
ok: coins = ton("0.05"),
3+
err: int = now(),
4+
}
5+
6+
/**
7+
@compilation_should_fail
8+
@stderr not a constant expression
9+
@stderr err: int
10+
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct A {
2+
tens: (int, int) = (1, 2 + now()),
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr not a constant expression
8+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct A {
2+
a: int = 1 << CCC;
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr undefined symbol `CCC`
8+
@stderr 1 << CCC
9+
*/

tolk-tester/tests/invalid-syntax/err-3150.tolk

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct A {
2+
v1 = 0,
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr expected `: <type>`, got `=`
8+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct A {
2+
v1: int = "asdf";
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr can not assign `slice` to `int`
8+
@stderr v1: int = "asdf"
9+
*/

tolk-tester/tests/match-by-expr-tests.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ fun main() {}
155155
100 PUSHINT // '1=100
156156
}>ELSE<{ // x
157157
DUP // x x
158-
2 EQINT // x '6
158+
2 EQINT // x '8
159159
IF:<{ // x
160160
DROP //
161161
200 PUSHINT // '1=200
162162
}>ELSE<{ // x
163-
3 EQINT // '9
163+
3 EQINT // '13
164164
IF:<{ //
165165
300 PUSHINT // '1=300
166166
}>ELSE<{ //

tolk-tester/tests/struct-tests.tolk

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,38 @@ fun test31() {
428428
return (s1, s2, s3, 777, s1 == null, s2 == null, s3 == null, 777, s1!.x == null, s2!.x == null);
429429
}
430430

431+
struct PointDef0 {
432+
x: int = 0,
433+
y: int = 0,
434+
}
435+
436+
@method_id(132)
437+
fun test32() {
438+
var p1: PointDef0 = { x: 10, y: 20 };
439+
var p2: PointDef0 = { x: 10 };
440+
var p3: PointDef0 = { y: 20 };
441+
var p4: PointDef0 = { };
442+
return (p1, p2, p3, p4, PointDef0{});
443+
}
431444

445+
struct WithDefaults {
446+
f1: (bool, int) = (true, 0),
447+
f2: int,
448+
f3: slice? = stringHexToSlice("010203"),
449+
f4: PointDef0? = null,
450+
f5: int32 | int64 = 0 as int32,
451+
}
452+
453+
@method_id(133)
454+
fun test33(): WithDefaults {
455+
var w1: WithDefaults = { f2: 0 };
456+
assert(w1.f1.0 && w1.f1.1 == 0 && w1.f3!.getRemainingBitsCount() == 24 && w1.f4 == null && w1.f5 is int32) throw 100;
457+
var w2: WithDefaults? = { f1: (false, 55), f2: 10, f5: 8 as int64 };
458+
assert(w2.f1.0 != true && w2.f3!.getRemainingBitsCount() == 24 && w2.f4 == null && w2.f5 is int64 && w2.f5 == 8) throw 100;
459+
var w3: (int, WithDefaults) = (0, { f2: 7, f4: {y: 20} });
460+
assert(w3.1.f4 != null && w3.1.f4.x == 0 && w3.1.f4.y == 20) throw 100;
461+
return { f2: 5, f3: null };
462+
}
432463

433464

434465

@@ -480,6 +511,8 @@ type PointAlias = Point;
480511
@testcase | 130 | -1 | 140 777 (null) 140 777 0 777 0 0
481512
@testcase | 130 | 0 | 139 777 4 1 777 139 777 0 0
482513
@testcase | 131 | | 5 141 (null) 141 (null) 0 777 0 0 -1 777 0 -1
514+
@testcase | 132 | | 10 20 10 0 0 20 0 0 0 0
515+
@testcase | 133 | | -1 0 5 (null) (null) (null) 0 0 46
483516

484517
@fif_codegen
485518
"""

tolk-tester/tests/use-before-declare.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const first = 1;
5858
"""
5959
test2 PROC:<{
6060
//
61-
2 PUSHINT // '0=2
61+
2 PUSHINT // '2
6262
}>
6363
"""
6464
*/

0 commit comments

Comments
 (0)