FunC Update 2022.05
TON FoundationWe are glad to present you new features of the FunC programming language:
Compiler directives
Those are keywords that start with #
and instruct compiler to do some actions, checks or adjust parameters.
Introducing the first derivatives - #pragma
and #include
.
#pragma version;
Used to enforce specific version of FunC compiler when compiling the file.
Version is specified in semver format, that is a.b.c
, where a
is major version, b
is minor, and c
is patch.
Comparison operators =
, >
, <
, >=
, <=
, ^
can be used.
#include "filename.fc";
The #include
directive allows to include another FunC source code file that will be parsed at the place of include.
String literals
Strings in FunC are quoted in double quotes "
like "this is a string"
. Special symbols like \n
and multi-line strings are not supported. Optionally string literals may specify type after them such as "string"u
.
The following strings types are supported:
- without type - used for asm function definitions and to define a slice const by ASCII string,
s
- defines a raw slice const by its contents (hex-encoded and optionally bit-padded),a
- creates a slice const containingMsgAddressInt
structure from specified address,u
- creates an int const that corresponds to hex values of provided ASCII string,h
- creates an int const that is first 32 bits of SHA256 hash of the string,H
- creates an int const that is all 256 bits of SHA256 hash of the string,c
- creates an int const that is crc32 value of the string.
For example, following values result in the corresponding consts:
"string"
becomesx{737472696e67}
slice const,"abcdef"s
becomesx{abcdef}
slice const,"Ef8zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzM0vF"a
becomesx{9FE6666666666666666666666666666666666666666666666666666666666666667_}
slice const (addr_std$10 anycast:none$0 workchain_id:int8=0xFF address:bits256=0x33...33
),"NstK"u
becomes0x4e73744b
int const,"transfer(slice, int)"h
becomes0x7a62e8a8
int const,"transfer(slice, int)"H
becomes0x7a62e8a8ebac41bd6de16c65e7be363bc2d2cbc6a0873778dead4795c13db979
int const,"transfer(slice, int)"c
becomes2235694568
int const.
Constants
FunC allows to define compile-time constants that are substituted and precalculated during compilation.
Constants are defined as const optional-type identifier = value-or-expression;
optional-type
can be used to force a specific type of constant and for better readability.
As of now int
and slice
types are supported.
value-or-expression
can be a literal or a pre-computable expression of literals and constants.
For example, constants can be defined as following:
const int101 = 101;
definesint101
constant that is equivalent to numeric literal101
,const str1 = "const1", str2 = "aabbcc"s;
defines two constants that are equal to their corresponding strings,const int int240 = ((int1 + int2) * 10) << 3;
definesint240
constant that equals to the result of calculation,const slice str2r = str2;
definesstr2r
constant that is equal to value ofstr2
constant.
Since numeric constants are substituted during compilation all optimization and pre-computations performed during compilation are successfully performed (unlike old method defining constants via inline asm PUSHINT
s).
To start using new features you need to use the func
compiler from the master branch https://github.com/ton-blockchain/ton.