Skip to content

Commit 974d76c

Browse files
committed
[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).
1 parent 799e2d1 commit 974d76c

33 files changed

+758
-206
lines changed

crypto/smartcont/tolk-stdlib/common.tolk

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fun stringHash(s: slice): int
205205
/// That is, if [hash] is computed as the hash of some data, these data are hashed twice,
206206
/// the second hashing occurring inside `CHKSIGNS`.
207207
@pure
208-
fun isSignatureValid(hash: int, signature: slice, publicKey: int): int
208+
fun isSignatureValid(hash: int, signature: slice, publicKey: int): bool
209209
asm "CHKSIGNU";
210210

211211
/// Checks whether [signature] is a valid Ed25519-signature of the data portion of `slice data` using `publicKey`,
@@ -214,7 +214,7 @@ fun isSignatureValid(hash: int, signature: slice, publicKey: int): int
214214
/// The verification of Ed25519 signatures is the standard one,
215215
/// with sha256 used to reduce [data] to the 256-bit number that is actually signed.
216216
@pure
217-
fun isSliceSignatureValid(data: slice, signature: slice, publicKey: int): int
217+
fun isSliceSignatureValid(data: slice, signature: slice, publicKey: int): bool
218218
asm "CHKSIGNS";
219219

220220
/// Generates a new pseudo-random unsigned 256-bit integer x.
@@ -259,14 +259,14 @@ fun randomizeByLogicalTime(): void
259259
/// otherwise the computation is aborted before visiting the `(maxCells + 1)`-st cell and
260260
/// a zero flag is returned to indicate failure. If [c] is `null`, returns `x = y = z = 0`.
261261
@pure
262-
fun calculateCellSize(c: cell, maxCells: int): (int, int, int, int)
262+
fun calculateCellSize(c: cell, maxCells: int): (int, int, int, bool)
263263
asm "CDATASIZEQ NULLSWAPIFNOT2 NULLSWAPIFNOT";
264264

265265
/// Similar to [calculateCellSize], but accepting a `slice` [s] instead of a `cell`.
266266
/// The returned value of `x` does not take into account the cell that contains the `slice` [s] itself;
267267
/// however, the data bits and the cell references of [s] are accounted for in `y` and `z`.
268268
@pure
269-
fun calculateSliceSize(s: slice, maxCells: int): (int, int, int, int)
269+
fun calculateSliceSize(s: slice, maxCells: int): (int, int, int, bool)
270270
asm "SDATASIZEQ NULLSWAPIFNOT2 NULLSWAPIFNOT";
271271

272272
/// A non-quiet version of [calculateCellSize] that throws a cell overflow exception (`8`) on failure.
@@ -382,7 +382,7 @@ fun loadCoins(mutate self: slice): int
382382

383383
/// Loads bool (-1 or 0) from a slice
384384
@pure
385-
fun loadBool(mutate self: slice): int
385+
fun loadBool(mutate self: slice): bool
386386
asm( -> 1 0) "1 LDI";
387387

388388
/// Shifts a slice pointer to [len] bits forward, mutating the slice.
@@ -482,7 +482,7 @@ fun storeCoins(mutate self: builder, x: int): self
482482
/// Stores bool (-1 or 0) into a builder.
483483
/// Attention: true value is `-1`, not 1! If you pass `1` here, TVM will throw an exception.
484484
@pure
485-
fun storeBool(mutate self: builder, x: int): self
485+
fun storeBool(mutate self: builder, x: bool): self
486486
asm(x self) "1 STI";
487487

488488
/// Stores dictionary (represented by TVM `cell` or `null`) into a builder.
@@ -529,22 +529,22 @@ fun getRemainingBitsAndRefsCount(self: slice): (int, int)
529529

530530
/// Checks whether a slice is empty (i.e., contains no bits of data and no cell references).
531531
@pure
532-
fun isEndOfSlice(self: slice): int
532+
fun isEndOfSlice(self: slice): bool
533533
asm "SEMPTY";
534534

535535
/// Checks whether a slice has no bits of data.
536536
@pure
537-
fun isEndOfSliceBits(self: slice): int
537+
fun isEndOfSliceBits(self: slice): bool
538538
asm "SDEMPTY";
539539

540540
/// Checks whether a slice has no references.
541541
@pure
542-
fun isEndOfSliceRefs(self: slice): int
542+
fun isEndOfSliceRefs(self: slice): bool
543543
asm "SREMPTY";
544544

545545
/// Checks whether data parts of two slices coinside.
546546
@pure
547-
fun isSliceBitsEqual(self: slice, b: slice): int
547+
fun isSliceBitsEqual(self: slice, b: slice): bool
548548
asm "SDEQ";
549549

550550
/// Returns the number of cell references already stored in a builder.
@@ -621,10 +621,10 @@ fun parseStandardAddress(s: slice): (int, int)
621621
fun createAddressNone(): slice
622622
asm "b{00} PUSHSLICE";
623623

624-
/// Returns if a slice pointer contains an empty address (`-1` for true, `0` for false, as always).
624+
/// Returns if a slice pointer contains an empty address.
625625
/// In other words, a slice starts with two `0` bits (TL addr_none$00).
626626
@pure
627-
fun addressIsNone(s: slice): int
627+
fun addressIsNone(s: slice): bool
628628
asm "2 PLDU" "0 EQINT";
629629

630630

@@ -677,8 +677,8 @@ fun loadMessageFlags(mutate self: slice): int
677677
/// Having msgFlags (4 bits), check that a message is bounced.
678678
/// Effectively, it's `msgFlags & 1` (the lowest bit present).
679679
@pure
680-
fun isMessageBounced(msgFlags: int): int
681-
asm "1 PUSHINT" "AND";
680+
fun isMessageBounced(msgFlags: int): bool
681+
asm "2 PUSHINT" "MODR";
682682

683683
/// Skip 0xFFFFFFFF prefix (when a message is bounced).
684684
@pure

0 commit comments

Comments
 (0)