Skip to content

Commit

Permalink
Merge pull request #1545 from ton-blockchain/tolk-v0.9
Browse files Browse the repository at this point in the history
Tolk v0.9: nullable types `T?`, null safety, control flow, smart casts
  • Loading branch information
EmelyanenkoK authored Mar 5, 2025
2 parents faf5811 + f67e9f4 commit 6eeae5d
Show file tree
Hide file tree
Showing 110 changed files with 5,722 additions and 1,821 deletions.
22 changes: 11 additions & 11 deletions crypto/smartcont/tolk-stdlib/common.tolk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Standard library for Tolk (LGPL licence).
// It contains common functions that are available out of the box, the user doesn't have to import anything.
// More specific functions are required to be imported explicitly, like "@stdlib/tvm-dicts".
tolk 0.8
tolk 0.9

/**
Tuple manipulation primitives.
Expand Down Expand Up @@ -139,7 +139,7 @@ fun getMyOriginalBalance(): int
/// `int` — balance in nanotoncoins;
/// `cell` — a dictionary with 32-bit keys representing the balance of "extra currencies".
@pure
fun getMyOriginalBalanceWithExtraCurrencies(): [int, cell]
fun getMyOriginalBalanceWithExtraCurrencies(): [int, cell?]
asm "BALANCE";

/// Returns the logical time of the current transaction.
Expand All @@ -154,7 +154,7 @@ fun getCurrentBlockLogicalTime(): int

/// Returns the value of the global configuration parameter with integer index `i` as a `cell` or `null` value.
@pure
fun getBlockchainConfigParam(x: int): cell
fun getBlockchainConfigParam(x: int): cell?
asm "CONFIGOPTPARAM";

/// Returns the persistent contract storage cell. It can be parsed or modified with slice and builder primitives later.
Expand Down Expand Up @@ -291,7 +291,7 @@ fun calculateSliceSizeStrict(s: slice, maxCells: int): (int, int, int)
/// otherwise the returned value is one plus the maximum of depths of cells referred to from [c].
/// If [c] is a `null` instead of a cell, returns zero.
@pure
fun getCellDepth(c: cell): int
fun getCellDepth(c: cell?): int
asm "CDEPTH";

/// Returns the depth of `slice` [s].
Expand Down Expand Up @@ -417,12 +417,12 @@ fun getLastBits(self: slice, len: int): slice
/// Loads a dictionary (TL HashMapE structure, represented as TVM cell) from a slice.
/// Returns `null` if `nothing` constructor is used.
@pure
fun loadDict(mutate self: slice): cell
fun loadDict(mutate self: slice): cell?
asm( -> 1 0) "LDDICT";

/// Preloads a dictionary (cell) from a slice.
@pure
fun preloadDict(self: slice): cell
fun preloadDict(self: slice): cell?
asm "PLDDICT";

/// Loads a dictionary as [loadDict], but returns only the remainder of the slice.
Expand All @@ -433,12 +433,12 @@ fun skipDict(mutate self: slice): self
/// Loads (Maybe ^Cell) from a slice.
/// In other words, loads 1 bit: if it's true, loads the first ref, otherwise returns `null`.
@pure
fun loadMaybeRef(mutate self: slice): cell
fun loadMaybeRef(mutate self: slice): cell?
asm( -> 1 0) "LDOPTREF";

/// Preloads (Maybe ^Cell) from a slice.
@pure
fun preloadMaybeRef(self: slice): cell
fun preloadMaybeRef(self: slice): cell?
asm "PLDOPTREF";

/// Loads (Maybe ^Cell), but returns only the remainder of the slice.
Expand Down Expand Up @@ -497,13 +497,13 @@ fun storeBool(mutate self: builder, x: bool): self
/// Stores dictionary (represented by TVM `cell` or `null`) into a builder.
/// In other words, stores a `1`-bit and a reference to [c] if [c] is not `null` and `0`-bit otherwise.
@pure
fun storeDict(mutate self: builder, c: cell): self
fun storeDict(mutate self: builder, c: cell?): self
asm(c self) "STDICT";

/// Stores (Maybe ^Cell) into a builder.
/// In other words, if cell is `null`, store '0' bit; otherwise, store '1' and a ref to [c].
@pure
fun storeMaybeRef(mutate self: builder, c: cell): self
fun storeMaybeRef(mutate self: builder, c: cell?): self
asm(c self) "STOPTREF";

/// Concatenates two builders.
Expand Down Expand Up @@ -661,7 +661,7 @@ fun reserveToncoinsOnBalance(nanoTonCoins: int, reserveMode: int): void

/// Similar to [reserveToncoinsOnBalance], but also accepts a dictionary extraAmount (represented by a cell or null)
/// with extra currencies. In this way currencies other than Toncoin can be reserved.
fun reserveExtraCurrenciesOnBalance(nanoTonCoins: int, extraAmount: cell, reserveMode: int): void
fun reserveExtraCurrenciesOnBalance(nanoTonCoins: int, extraAmount: cell?, reserveMode: int): void
asm "RAWRESERVEX";


Expand Down
2 changes: 1 addition & 1 deletion crypto/smartcont/tolk-stdlib/gas-payments.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// A part of standard library for Tolk
tolk 0.8
tolk 0.9

/**
Gas and payment related primitives.
Expand Down
11 changes: 6 additions & 5 deletions crypto/smartcont/tolk-stdlib/lisp-lists.tolk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// A part of standard library for Tolk
tolk 0.8
tolk 0.9

/**
Lisp-style lists are nested 2-elements tuples: `(1, (2, (3, null)))` represents list `[1, 2, 3]`.
Expand All @@ -14,17 +14,18 @@ fun createEmptyList(): tuple
/// Adds an element to the beginning of lisp-style list.
/// Note, that it does not mutate the list: instead, it returns a new one (it's a lisp pattern).
@pure
fun listPrepend<X>(head: X, tail: tuple): tuple
fun listPrepend<X>(head: X, tail: tuple?): tuple
asm "CONS";

/// Extracts the head and the tail of lisp-style list.
@pure
fun listSplit<X>(list: tuple): (X, tuple)
fun listSplit<X>(list: tuple): (X, tuple?)
asm "UNCONS";

/// Extracts the tail and the head of lisp-style list.
/// After extracting the last element, tuple is assigned to null.
@pure
fun listNext<X>(mutate self: tuple): X
fun listNext<X>(mutate self: tuple?): X
asm( -> 1 0) "UNCONS";

/// Returns the head of lisp-style list.
Expand All @@ -34,5 +35,5 @@ fun listGetHead<X>(list: tuple): X

/// Returns the tail of lisp-style list.
@pure
fun listGetTail(list: tuple): tuple
fun listGetTail(list: tuple): tuple?
asm "CDR";
Loading

0 comments on commit 6eeae5d

Please sign in to comment.