Skip to content

Commit 620a12d

Browse files
committed
[Tolk] Support intN and bool constants
Constant evaluator has been rewritten to utilize type inferring mechanism. Now, > const a = 1 as int8; is valid, and constants can not only be int/slice, but also intN/uintN and bool.
1 parent 49cc6d0 commit 620a12d

21 files changed

+238
-88
lines changed

tolk-tester/tests/co1.tolk

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ const str2int = 0xAABBCC;
1616

1717
const nibbles: int = 4;
1818

19+
const strange_zero = (!10 as int);
20+
const strange_minus_1: int = (!0 as int);
21+
22+
const true1 = true;
23+
const true2 = !!true;
24+
const true3 = true1 && true2;
25+
26+
const false1 = !true;
27+
const false2 = false1 || false;
28+
1929
fun iget1(): int { return int1; }
2030
fun iget2(): int { return int2; }
2131
fun iget3(): int { return int1+int2; }
@@ -43,6 +53,21 @@ asm "SDEQ";
4353
fun stslicer(b: builder, s: slice): builder
4454
asm "STSLICER";
4555

56+
@method_id(101)
57+
fun test1() {
58+
return (strange_zero, strange_minus_1);
59+
}
60+
61+
@method_id(102)
62+
fun test2() {
63+
return (true1, true2, true3);
64+
}
65+
66+
@method_id(103)
67+
fun test3() {
68+
return (false1, false2);
69+
}
70+
4671
fun main() {
4772
var i1: int = iget1();
4873
var i2: int = iget2();
@@ -66,7 +91,10 @@ fun main() {
6691
}
6792

6893
/**
69-
@testcase | 0 | | 0
94+
@testcase | 0 | | 0
95+
@testcase | 101 | | 0 -1
96+
@testcase | 102 | | -1 -1 -1
97+
@testcase | 103 | | 0 0
7098

71-
@code_hash 61273295789179921867241079778489100375537711211918844448475493726205774530743
99+
@code_hash 28102194299745406750019953961984060488024870092664444642078578246708959881688
72100
*/

tolk-tester/tests/intN-tests.tolk

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ fun getAnyInt(): int { return 0; }
33
fun getNullableInt32(): int32? { return 32; }
44
fun getNullableVarInt32(): varint32? { return 32; }
55

6+
const someN_u128: uint128 = 128;
7+
const someN_i32: int32 = 20 + (12 as int32) * (1 as uint8);
8+
69

710
fun autoInferInt8(x: int8) {
811
if (random()) { return x; }
@@ -50,7 +53,8 @@ fun test1(x: int8) {
5053

5154
var z = x + y;
5255
__expect_type(z, "int");
53-
return x / y;
56+
__expect_type(someN_u128, "uint128");
57+
return x / y + someN_u128;
5458
}
5559

5660
fun test2(): (uint8, uint8, uint8) {
@@ -60,6 +64,7 @@ fun test2(): (uint8, uint8, uint8) {
6064

6165
fun test3(op: int32, qid: uint64) {
6266
op = qid as int32;
67+
op = someN_i32;
6368

6469
op + qid;
6570
op = op + qid;
@@ -166,7 +171,7 @@ fun main() {
166171

167172
/**
168173
@testcase | 0 | | [ 0 0 0 ]
169-
@testcase | 101 | 0 | 1
174+
@testcase | 101 | 0 | 129
170175
@testcase | 104 | | 64 64 -1 0
171176
@testcase | 105 | | 3 -1 0
172177
@testcase | 114 | 5 | (null) (null) 0

tolk-tester/tests/invalid-cyclic-1.tolk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ const TWO = ONE + 1;
33

44
/**
55
@compilation_should_fail
6-
@stderr const ONE
7-
@stderr undefined symbol `TWO`
6+
@stderr const `TWO` appears, directly or indirectly, in its own initializer
87
*/

tolk-tester/tests/invalid-declaration-13.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const c: slice = 123 + 456;
22

33
/**
44
@compilation_should_fail
5-
@stderr expression type does not match declared type
5+
@stderr can not assign `int` to `slice`
66
@stderr const c
77
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const ttt = 1;
2+
const asdf = ttt<int>;
3+
4+
/**
5+
@compilation_should_fail
6+
@stderr generic T not expected here
7+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const asdf = ttt;
2+
3+
/**
4+
@compilation_should_fail
5+
@stderr undefined symbol `ttt`
6+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fun foo() { return 1; }
2+
3+
const asdf = 1 + foo();
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr not a constant expression
8+
@stderr const asdf
9+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const asdf = 1 + "";
2+
3+
/**
4+
@compilation_should_fail
5+
@stderr can not apply operator `+` to `int` and `slice`
6+
*/

tolk/ast-replacer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class ASTReplacerInFunctionBody : public ASTReplacer {
187187

188188

189189
const std::vector<FunctionPtr>& get_all_not_builtin_functions();
190+
const std::vector<GlobalConstPtr>& get_all_declared_constants();
190191

191192
template<class BodyReplacerT>
192193
void replace_ast_of_all_functions() {

tolk/ast-visitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class ASTVisitorFunctionBody : public ASTVisitor {
180180

181181

182182
const std::vector<FunctionPtr>& get_all_not_builtin_functions();
183+
const std::vector<GlobalConstPtr>& get_all_declared_constants();
183184

184185
template<class BodyVisitorT>
185186
void visit_ast_of_all_functions() {

0 commit comments

Comments
 (0)