1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

[Tolk] bool type (-1/0 int under the hood)

Comparison operators `== / >= /...` return `bool`.
Logical operators `&& ||` return bool.
Constants `true` and `false` have the `bool` type.
Lots of stdlib functions return `bool`, not `int`.

Operator `!x` supports both `int` and `bool`.
Condition of `if` accepts both `int` and `bool`.
Arithmetic operators are restricted to integers.
Logical `&&` and `||` accept both `bool` and `int`.

No arithmetic operations with bools allowed (only bitwise and logical).
This commit is contained in:
tolk-vm 2025-01-13 15:21:24 +07:00
parent 799e2d1265
commit 974d76c5f6
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
33 changed files with 764 additions and 212 deletions

View file

@ -83,7 +83,7 @@ fun test_if_else(x: int): (int, int, int, int, int) {
return (x.`~inc`(8), x + 1, x = 1, x <<= 3, x);
} else {
xx = 9;
return (x, x.`~inc`(-4), x.`~inc`(-1), x >= 1, x = x + xx);
return (x, x.`~inc`(-4), x.`~inc`(-1), (x >= 1) as int, x = x + xx);
}
}

View file

@ -1,20 +1,20 @@
fun lshift(): int {
fun lshift(): bool {
return (1 << 0) == 1;
}
fun rshift(): int {
fun rshift(): bool {
return (1 >> 0) == 1;
}
fun lshift_var(i: int): int {
fun lshift_var(i: int): bool {
return (1 << i) == 1;
}
fun rshift_var(i: int): int {
fun rshift_var(i: int): bool {
return (1 >> i) == 1;
}
fun main(x: int): int {
fun main(x: int): bool {
if (x == 0) {
return lshift();
} else if (x == 1) {
@ -31,12 +31,71 @@ fun main(x: int): int {
}
@method_id(11)
fun is_claimed(index: int): int {
fun is_claimed(index: int): bool {
var claim_bit_index: int = index % 256;
var mask: int = 1 << claim_bit_index;
return (255 & mask) == mask;
}
@method_id(12)
fun bit_not(i: int, b: bool): (int, bool, bool, bool, int, bool) {
var i2 = ~i;
var b2 = !b;
var (i3: int, b3: bool) = (i2, b2);
return (i3, b3, !i, !b, ~~~i, !!!b);
}
@method_id(13)
fun boolWithBitwiseConst() {
var found = true;
return (found & false, found | true, found ^ true, found & found);
}
global g14: int;
fun getBool() { return (g14 += 1) > 2; }
@method_id(14)
fun boolWithBitwise(b: bool) {
g14 = 0;
return (b & getBool(), !b & getBool(), b | getBool(), !b | getBool(), b ^ getBool(), !b & getBool(), g14);
}
@method_id(15)
fun boolWithBitwiseSet(b1: bool, b2: bool) {
b1 &= b2;
b2 |= true;
b1 |= b1 == false;
b2 ^= (b1 ^= b2);
return (b1, b2);
}
@method_id(16)
fun testDoUntilCodegen(i: bool, n: int) {
var cnt = 0;
do { cnt += 1; } while (i);
do { cnt += 1; } while (!!i);
do { cnt += 1; } while (n);
return (cnt, !i, !n);
}
@method_id(17)
fun testConstNegateCodegen() {
return (!0, !1, !true, !false, !!true, !!false);
}
@method_id(18)
fun testBoolNegateOptimized(x: bool) {
return (x, !x, !!x, !!!x, !!!!true);
}
fun eqX(x: bool) { return x; }
@method_id(19)
fun testBoolCompareOptimized(x: bool) {
return (x == true, x != true, eqX(x) == false, eqX(x) != false, !!(x == !false));
}
/**
method_id | in | out
@ -50,4 +109,96 @@ fun is_claimed(index: int): int {
@testcase | 11 | 1 | -1
@testcase | 11 | 256 | -1
@testcase | 11 | 8 | 0
@testcase | 12 | 0 0 | -1 -1 -1 -1 -1 -1
@testcase | 12 | -1 -1 | 0 0 0 0 0 0
@testcase | 12 | 7 0 | -8 -1 0 -1 -8 -1
@testcase | 14 | -1 | 0 0 -1 -1 0 0 6
@testcase | 14 | 0 | 0 0 -1 -1 -1 -1 6
@testcase | 15 | -1 -1 | 0 -1
@testcase | 15 | -1 0 | 0 -1
@testcase | 16 | 0 0 | 3 -1 -1
@testcase | 17 | | -1 0 0 -1 -1 0
@testcase | 18 | 0 | 0 -1 0 -1 -1
@testcase | 18 | -1 | -1 0 -1 0 -1
@testcase | 19 | 0 | 0 -1 -1 0 0
@testcase | 19 | -1 | -1 0 0 -1 -1
@fif_codegen
"""
boolWithBitwiseConst PROC:<{
//
0 PUSHINT // _3
-1 PUSHINT // _3 _5
0 PUSHINT // _3 _5 _7
-1 PUSHINT // _3 _5 _7 _8
}>
"""
@fif_codegen
"""
testDoUntilCodegen PROC:<{
// i n
0 PUSHINT // i n cnt=0
UNTIL:<{
INC // i n cnt
s2 PUSH // i n cnt i
NOT // i n cnt _6
}> // i n cnt
UNTIL:<{
INC // i n cnt
s2 PUSH // i n cnt i
NOT // i n cnt _9
}> // i n cnt
UNTIL:<{
INC // i n cnt
OVER // i n cnt n
0 EQINT // i n cnt _12
}> // i n cnt
s0 s2 XCHG // cnt n i
NOT // cnt n _13
SWAP // cnt _13 n
0 EQINT // cnt _13 _14
}>
"""
@fif_codegen
"""
testConstNegateCodegen PROC:<{
//
TRUE // _0
FALSE // _0 _1
FALSE // _0 _1 _2
TRUE // _0 _1 _2 _3
TRUE // _0 _1 _2 _3 _4
FALSE // _0 _1 _2 _3 _4 _5
}>
"""
@fif_codegen
"""
testBoolNegateOptimized PROC:<{
// x
DUP // x x
NOT // x _1
OVER // x _1 x
NOT // x _1 _2
s2 s(-1) PUXC
TRUE // x _1 x _2 _3
}>
"""
@fif_codegen
"""
testBoolCompareOptimized PROC:<{
// x
DUP // x x
NOT // x _1
OVER // x _1 x
eqX CALLDICT // x _1 _2
NOT // x _1 _3
s2 PUSH // x _1 _3 x
eqX CALLDICT // x _1 _3 _4
s3 PUSH // x _1 _3 _4 x
}>
"""
*/

View file

@ -1,6 +1,6 @@
global op: (int, int) -> int;
fun check_assoc(a: int, b: int, c: int): int {
fun check_assoc(a: int, b: int, c: int): bool {
return op(op(a, b), c) == op(a, op(b, c));
}
@ -8,8 +8,9 @@ fun unnamed_args(_: int, _: slice, _: int) {
return true;
}
fun main(x: int, y: int, z: int): int {
fun main(x: int, y: int, z: int): bool {
op = `_+_`;
if (0) { return null; }
return check_assoc(x, y, z);
}

View file

@ -2,7 +2,7 @@ fun check_assoc(op: (int, int) -> int, a: int, b: int, c: int) {
return op(op(a, b), c) == op(a, op(b, c));
}
fun main(x: int, y: int, z: int): int {
fun main(x: int, y: int, z: int): bool {
return check_assoc(`_+_`, x, y, z);
}

View file

@ -162,8 +162,8 @@ fun test13() {
}
@method_id(114)
fun test110(x: int) {
var s = beginCell().storeBool(x < 0).storeBool(0).storeBool(x).endCell().beginParse();
fun test110(x: bool) {
var s = beginCell().storeBool(x == true).storeBool(false).storeBool(x).endCell().beginParse();
return (s.loadBool(), s.loadBool(), s.loadBool());
}
@ -179,15 +179,15 @@ fun test111() {
if (s.addressIsNone()) {
s.skipBits(2);
}
if (s.loadBool() == 0) {
assert(s.loadBool() == 0) throw 444;
if (s.loadBool() == false) {
assert(!s.loadBool()) throw 444;
s.skipBouncedPrefix();
}
var op2 = s.loadMessageOp();
var q2 = s.loadMessageQueryId();
s.skipBits(64);
s.assertEndOfSlice();
assert(isMessageBounced(0x001)) throw 444;
assert(isMessageBounced(0x001) && !isMessageBounced(0x002)) throw 444;
return (op1, q1, op2, q2);
}

View file

@ -1,7 +1,7 @@
@method_id(101)
fun test1(): int {
var x = false;
if (x == true) {
var x: int = false as int;
if (x == true as int) {
x= 100500;
}
return x;

View file

@ -11,7 +11,7 @@ fun prepareDict_3_30_4_40_5_x(valueAt5: int): cell {
fun lookupIdxByValue(idict32: cell, value: int): int {
var cur_key = -1;
do {
var (cur_key redef, cs: slice, found: int) = idict32.iDictGetNext(32, cur_key);
var (cur_key redef, cs: slice, found: bool) = idict32.iDictGetNext(32, cur_key);
// one-line condition (via &) doesn't work, since right side is calculated immediately
if (found) {
if (cs.loadInt(32) == value) {

View file

@ -0,0 +1,8 @@
fun failMathOnBoolean(c: cell) {
return (null == c) * 10;
}
/**
@compilation_should_fail
@stderr can not apply operator `*` to `bool` and `int`
*/

View file

@ -0,0 +1,11 @@
fun failBitwiseNotOnBool() {
var eq = 1 == 0;
if (~eq) {
return 0;
}
}
/**
@compilation_should_fail
@stderr can not apply operator `~` to `bool`
*/

View file

@ -1,9 +1,9 @@
fun main() {
var tri: (int, bool) = (10, false);
var tri: (int, int) = (10, false);
return;
}
/**
@compilation_should_fail
@stderr bool type is not supported yet
@stderr can not assign `(int, bool)` to variable of type `(int, int)`
*/

View file

@ -4,5 +4,5 @@ fun failWhenTernaryConditionNotInt(cs: slice) {
/**
@compilation_should_fail
@stderr condition of ternary operator must be an integer
@stderr can not use `slice` as a boolean condition
*/

View file

@ -1,14 +1,14 @@
import "imports/use-dicts.tolk"
fun simpleAllConst() {
return (!0, !!0 & !false, !!!0, !1, !!1, !-1, !!-1, (!5 == 0) == !0, !0 == true);
return (!0, !!0 & !false, !!!0, !1, !!1, !-1, !!-1, (!5 as int == 0) == !0, !0 == true);
}
fun compileTimeEval1(x: int) {
// todo now compiler doesn't understand that bool can't be equal to number other than 0/-1
// (but understands that it can't be positive)
// that's why for now, the last condition is evaluated at runtime
return (!x, !x > 10, !x < 10, !!x == 5, !x == -10);
return (!x, !x as int > 10, (!x as int) < 10, !!x as int == 5, !x as int == -10);
}
@method_id(101)
@ -23,13 +23,13 @@ fun withAndOr(x: int, y: int, z: int) {
var return_at_end = -1;
if (!x & !y) {
if (!z & !y) { return 10; }
else if (z | !!y) { return_at_end = 20; }
else if ((z != 0) | !!y) { return_at_end = 20; }
} else if (!!x & !!y & !z) {
if (!z & (x > 10)) { return_at_end = 30; }
if ((x != 11) & !z) { return 40; }
return_at_end = 50;
} else {
return_at_end = !x ? !y : !z | 1;
return_at_end = !x ? !y as int : (!z as int) | 1;
}
return return_at_end;
}
@ -124,6 +124,31 @@ fun testLogicalOps2(first: int) {
return (s.getRemainingBitsCount(), sum);
}
@method_id(112)
fun mixLogicalIntsAndBools(first: int, cond: bool) {
return (
(first && cond) || (!first && cond),
((first & -1) & cond as int) == ((first && true) && cond) as int,
7 && cond,
first || cond || !cond || alwaysThrows(),
cond || first || !first || alwaysThrows()
);
}
@method_id(113)
fun testConvertIfToIfnot(x: bool) {
assert(!!(x == false), 100);
assert(!x, 100);
if (x == !!false) {
return 1;
}
if (!!(x != !false)) {
return 1;
}
assert(!!x, 100);
return -4;
}
fun main() {
}
@ -160,18 +185,21 @@ fun main() {
@testcase | 110 | 500 | -1 -1 0 -1 -1 3
@testcase | 111 | 0 | 32 4
@testcase | 111 | -1 | 0 8
@testcase | 112 | 5 0 | 0 -1 0 -1 -1
@testcase | 112 | 0 -1 | -1 -1 -1 -1 -1
@testcase | 113 | 0 | 1
@fif_codegen
"""
simpleAllConst PROC:<{
//
-1 PUSHINT
TRUE
0 PUSHINT
-1 PUSHINT
0 PUSHINT
-1 PUSHINT
0 PUSHINT
-1 PUSHINT
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE
TRUE
}>
@ -293,4 +321,27 @@ These are moments of future optimizations. For now, it's more than enough.
}>
"""
@fif_codegen
"""
testConvertIfToIfnot PROC:<{
// x
DUP // x x
100 THROWIF
DUP // x x
100 THROWIF
DUP // x x
IFNOTJMP:<{ // x
DROP //
1 PUSHINT // _7=1
}> // x
DUP // x x
IFNOTJMP:<{ // x
DROP //
1 PUSHINT // _8=1
}> // x
100 THROWIFNOT
-4 PUSHINT // _12=-4
}>
"""
*/

View file

@ -22,7 +22,7 @@ global `some()var`:int;
return `a`*-1*-(1)*---(1)*+just10()+-`just10`()*m1*-m1+-eq(m1)----0x1;
}
@method_id(112) fun `bitwise~ops`(flags:int):[int,int] {
@method_id(112) fun `bitwise~ops`(flags:int):[bool,bool] {
return[
(just10()-3==just10()-(4)--1)|((2==2)&(eq(eq(10)) -3==just10()--13)),
((flags&0xFF)!=0)

View file

@ -73,7 +73,7 @@ fun test7() {
var b = beginCell().storeMaybeRef(null);
var s = b.endCell().beginParse();
var c = s.loadMaybeRef();
return (null == c) * 10 + (b != null);
return (null == c) as int * 10 + (b != null) as int;
}
fun main() {
@ -139,7 +139,7 @@ fun main() {
10 MULCONST // b _13
SWAP // _13 b
ISNULL // _13 _14
0 EQINT // _13 _15
NOT // _13 _15
ADD // _16
}>
"""

View file

@ -1,4 +1,4 @@
fun justTrue(): int { return true; }
fun justTrue(): bool { return true; }
fun unary_minus_1(a: int, b: int, c: int): int{return -(a+b) *c;}
fun unary_minus_2(a: int, b: int, c: int): int{return(-(a+b))*c;}
@ -6,17 +6,17 @@ fun unary_minus_3(a: int, b: int, c: int): int{return-((a+b) *c);}
@method_id(101)
fun test1(x: int, y: int, z: int): int {
fun test1(x: int, y: int, z: int): bool {
return (x > 0) & (y > 0) & (z > 0);
}
@method_id(102)
fun test2(x: int, y: int, z: int): int {
return x > (0 & (y > 0) & (z > 0));
fun test2(x: int, y: int, z: int): bool {
return x > (0 & (y > 0) as int & (z > 0) as int);
}
@method_id(103)
fun test3(x: int, y: int, z: int): int {
fun test3(x: int, y: int, z: int): bool {
if ((x < 0) | (y < 0)) {
return z < 0;
}
@ -24,29 +24,29 @@ fun test3(x: int, y: int, z: int): int {
}
@method_id(104)
fun test4(x: int, y: int, mode: int): int {
fun test4(x: int, y: int, mode: int): bool {
if (mode == 1) {
return (x == 10) | (y == 20);
} if (mode == 2) {
return (x == 10) | (y == 20);
} else {
return x == (10 | (y == 20));
return x == (10 | (y == 20) as int);
}
}
@method_id(105)
fun test5(status: int): int {
return justTrue() & (status == 1) & ((justTrue() & status) == 1);
fun test5(status: int): bool {
return justTrue() & (status == 1) & ((justTrue() as int & status) == 1);
}
@method_id(106)
fun test6(a: int, b: int, c: int): int {
fun test6(a: int, b: int, c: int): bool {
return (unary_minus_1(a,b,c) == unary_minus_2(a,b,c)) & (unary_minus_1(a,b,c) == unary_minus_3(a,b,c));
}
@method_id(107)
fun test7(b: int): int {
var a = b == 3 ? 3 : b == 4 ? 4 : (b == 5) & 1 ? 5 : 100;
var a = b == 3 ? 3 : b == 4 ? 4 : (b == 5) & true ? 5 : 100;
return a;
}
@ -56,14 +56,14 @@ fun test8(b: int): int {
return a;
}
fun `_<p`(a: int, b: int): int { return true; }
fun `_<p`(a: int, b: int): bool { return true; }
fun main() {
// ok to parse
var c = [
(3 & 3) > 0, 3 & (3 > 0), 3 & (`_<_`(3, 0)),
3 & `_<p`(3, 0), (1 & 2) ^ (3 | 4),
1 & ((1) == 1)
(3 & 3) > 0, 3 & (3 > 0) as int, 3 & (`_<_`(3, 0)),
3 & `_<p`(3, 0) as int, (1 & 2) ^ (3 | 4),
true & ((1) == 1)
];
}

View file

@ -661,7 +661,7 @@ fun fixed248_pow(x: int, y: int): int {
return 1 << 248; // x^0 = 1
}
if (x <= 0) {
var bad: int = (x | y) < 0;
var bad: int = ((x | y) < 0) as int;
return 0 >> bad; // 0^y = 0 if x=0 and y>=0; "out of range" exception otherwise
}
var (l, s) = log2_aux_f256(x);
@ -677,7 +677,7 @@ fun fixed248_pow(x: int, y: int): int {
// now log_2(x^y) = y*log_2(x) = q + ll, ss integer, ll fixed257, -1/2<=ll<1/2
var sq: int = q + 248;
if (sq <= 0) {
return -(sq == 0); // underflow
return -((sq == 0) as int); // underflow
}
y = expm1_f257(mulrshiftr256(ll, log2_const_f256()));
return (y ~>> (9 - q)) - (-1 << sq);
@ -986,7 +986,7 @@ fun tset<X>(mutate self: tuple, idx: int, value: X): void
// fixed256 acos_prepare_slow(fixed255 x);
@inline
fun acos_prepare_slow_f255(x: int): int {
x -= (x == 0);
x -= (x == 0) as int;
var t: int = 1;
repeat (255) {
t = t * sign(x) * 2 + 1; // decode Gray code (sign(x_0), sign(x_1), ...)

View file

@ -38,7 +38,7 @@ fun foo_until(x: int): int {
}
@method_id(4)
fun test4(x: int): (int, int) {
fun test4(x: int): (int, bool) {
var s = 0;
var reached = false;
do {

View file

@ -6,7 +6,7 @@ fun main(x: int): int {
if (i > 5) {
return 1;
}
var f: int = (i * i == 64);
var f: bool = (i * i == 64);
} while (!f);
return -1;
}

View file

@ -4,7 +4,7 @@ fun test(y: int): int {
if (y > 0) {
return 1;
}
return x > 0;
return x > 0 ? -1 : 0;
}
@method_id(2)