Skip to content

Commit 4743108

Browse files
committed
feat: operators, expressions, reordering
Fix #2419
1 parent c2b5d4a commit 4743108

File tree

2 files changed

+147
-57
lines changed

2 files changed

+147
-57
lines changed

docs/src/content/docs/book/learn-tact-in-y-minutes.mdx

+141-54
Original file line numberDiff line numberDiff line change
@@ -427,57 +427,139 @@ Read more: [Structs and Messages](/book/structs-and-messages).
427427
## Operators
428428

429429
```tact
430-
// Let's omit the type ascriptions and let the compiler infer the types
431-
let a = 5;
432-
let b = 4;
433-
434-
// Common arithmetic operators have predictable precedences
435-
a + b - a * b / a % b; // 9
436-
437-
// You can change order of operations with parentheses
438-
(a + (b - a)) * b / (a % b); // 16
439-
440-
// The % is the modulo, not the remainder operator
441-
1 % a; // 1
442-
1 % -a; // -4
443-
444-
// Shifts
445-
a << 2; // 20
446-
b >> 2; // 1
447-
448-
// Relations
449-
// TODO
450-
// > >= < <=
451-
452-
// Logical checks
453-
// TODO
454-
// !, !!, &&, ||, ==, !=
455-
// Equality == !=
456-
// Logical AND &&
457-
// Logical OR ||
458-
// Unary postfix !!
459-
// Unary prefix !
460-
// Ternary ?:
461-
462-
// TODO
463-
// Unary prefix ~
464-
// Bitwise AND &
465-
// Bitwise XOR ^
466-
// Bitwise OR |
467-
468-
// TODO
469-
// Assignment = and augmented assignment operators
470-
// Most of the above have augmented assignment versions,
471-
// see the "Statements" section below.
430+
// Let's omit the type ascriptions and let the compiler infer the types.
431+
let five = 5; // = is an assignment operator,
432+
// but it can be a part of the assignment statement only,
433+
// because there is no assignment expression
434+
let four = 4;
435+
436+
// Most operators below have augmented assignment versions, like +=, -=, etc.
437+
// See the "Statements" section below for more info.
438+
439+
// Common arithmetic operators have predictable precedences.
440+
five + four - five * four / five % four; // 9
441+
442+
// You can change order of operations with parentheses.
443+
(five + (four - five)) * four / (five % four); // 16
444+
445+
// The % is the modulo, not the remainder operator.
446+
1 % five; // 1
447+
1 % -five; // -4
448+
449+
// Negation and bitwise NOT.
450+
-five; // -5: negation of 5
451+
~five; // -6: bitwise NOT of 5
452+
-(~five); // 6: bitwise NOT, then negation
453+
~(-five); // 4: negation, then bitwise NOT
454+
455+
// Bitwise shifts.
456+
five << 2; // 20
457+
four >> 2; // 1
458+
-four >> 2; // -1, because negation is applied first
459+
// and >> performs arithmetic or sign-propagating right shift
460+
461+
// Other common bitwise operators.
462+
five & four; // 4, due to bitwise AND
463+
five | four; // 5, due to bitwise OR
464+
five ^ four; // 1, due to bitwise XOR
465+
466+
// Relations.
467+
five == four; // false
468+
five != four; // true
469+
five > four; // true
470+
five < four; // false
471+
five - 1 >= four; // true
472+
five - 1 <= four; // true
473+
474+
// Logical checks.
475+
!(five == 5); // false, because of the inverse ! operator
476+
false && five == 5; // false, because && is short-circuited
477+
true || five != 5; // true, because || is also short-circuited
478+
479+
// Non-null assertion operator raises
480+
// a compilation error if the value is null, but does nothing otherwise.
481+
five!!; // 5
482+
483+
// Ternary operator ?: is right-associative.
484+
false ? 1 : (false ? 2 : 3); // 3
485+
false ? 1 : true ? 2 : 3; // 2
472486
```
473487

474488
Read more: [Operators](/book/operators).
475489

476490
## Expressions
477491

478-
```tact
479-
// TODO: ... + now, require, send (first use)
480-
// (move notes here!)
492+
```tact /StdAddress/
493+
// Integer literals.
494+
0; 42; 1_000; 020; // decimal, base 10
495+
0xABC; 0xf; 0x001; // hexadecimal, base 16
496+
0o777; 0o00000001; // octal, base 8
497+
0b111010101111010; // binary, base 2
498+
499+
// Boolean literals.
500+
true; false;
501+
502+
// String literals.
503+
"foo"; "1234";
504+
"\\ \" \n \r \t \v \b \f \x00 through \xFF";
505+
"\u0000 through \uFFFF and \u{0} through \u{FFFFFF}";
506+
507+
// `null` and `self` literals.
508+
null; // not an instance of a primitive type, but
509+
// a special value that represents the intentional absence
510+
// of any other value
511+
512+
self; // used to reference the current contract from within
513+
// and the value of the currently extended type inside
514+
// the extension function. See "Functions" section below for more.
515+
516+
// Identifiers, with usual naming conventions:
517+
// They may contain Latin lowercase letters `a-z`,
518+
// Latin uppercase letters `A-Z`, underscores `_`,
519+
// and digits 0 - 9, but may not start with a digit.
520+
// No other symbols are allowed, and Unicode identifiers are prohibited.
521+
// They also cannot start with __gen or __tact since those prefixes
522+
// are reserved by the Tact compiler.
523+
azAZ09_;
524+
525+
// Instantiations or instance expressions of structs and message structs.
526+
let addr = BasechainAddress{ hash: null, };
527+
528+
// Field access.
529+
addr.hash; // null
530+
self.storageReserve; // 0, a contract-level constant present in all contracts,
531+
// because they implicitly inherit the BaseTrait
532+
// that defines it.
533+
534+
// Extension function calls (methods).
535+
self.storageReserve.toString(); // "0"
536+
self.notify("Cashback".asComment()); // rather expensive,
537+
// use cashback() instead
538+
"hey".asComment(); // allowed on literals
539+
540+
// Global function calls.
541+
now(); // UNIX timestamp in seconds
542+
cashback(sender());
543+
544+
// Some of the functions can be computed at compile-time given enough data.
545+
sha256("hey, I'll produce the SHA256 number at compile-time");
546+
547+
// But there are special, compile-time-only functions.
548+
let _: Address = address("EQDtFpEwcFAEcRe5mLVh2N6C0x-_hJEM7W61_JLnSF74p4q2");
549+
let _: Cell = cell("");
550+
let _: Slice = slice("te6cckEBAQEADgAAGEhlbGxvIHdvcmxkIXgtxbw="); // a Slice with Hello world!
551+
let _: Slice = rawSlice("000DEADBEEF000"); // CS{Cell{03f...430} bits: 588..644; refs: 1..1}
552+
let _: Int = ascii("⚡"); // 14850721 or 0xE29AA1, 3 bytes in total
553+
let _: Int = crc32("000DEADBEEF000"); // 1821923098
554+
let _: Int = ton("1"); // 10^9 nanoToncoin, which is equivalent to one Toncoin,
555+
// the main currency of TON Blockchain
556+
557+
// initOf, which obtains the initial code and initial data
558+
// of the given contract, i.e., it's initial state.
559+
initOf MyContract(); // StateInit{ code, data }
560+
561+
// codeOf, which only obtains the code.
562+
codeOf MyContract;
481563
```
482564

483565
Read more: [Expressions](/book/expressions).
@@ -486,6 +568,7 @@ Read more: [Expressions](/book/expressions).
486568

487569
```tact
488570
// TODO: ... + + exit codes and try-catch (may be separated then)
571+
// assignments and augmented assignments
489572
// (move notes here!)
490573
```
491574

@@ -509,7 +592,13 @@ Read more: [Constants](/book/constants).
509592

510593
Read more: [Functions](/book/functions).
511594

512-
## TODO something about gas usage {#gas}
595+
## Contracts and traits
596+
597+
```tact
598+
// TODO: move notes here
599+
// TODO: contract structure
600+
// TODO: traits
601+
```
513602

514603
## Persistent state {#state}
515604

@@ -534,10 +623,13 @@ contract StateActor(
534623
}
535624
```
536625

537-
## Contract structure {#contract}
626+
## TODO something about gas usage {#gas}
627+
628+
## Message exchange
538629

539630
```tact
540-
// TODO: move notes here
631+
// TODO: ... + message sending + reserve + receiving messages + refunds + child-parent interactions (or earlier)
632+
// (move notes here!)
541633
```
542634

543635
## Deployment
@@ -549,13 +641,8 @@ contract StateActor(
549641

550642
## Debugging
551643

552-
// TODO: require, throw, emit, etc.
553-
554-
## Message exchange
555-
556644
```tact
557-
// TODO: ... + message sending + reserve + receiving messages + refunds + child-parent interactions (or earlier)
558-
// (move notes here!)
645+
// TODO: require, throw, emit, etc.
559646
```
560647

561648
## Example contracts {#examples}

docs/src/content/docs/book/operators.mdx

+6-3
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,12 @@ Can only be applied to values of type [`Int{:tact}`][int]:
321321

322322
```tact
323323
let two: Int = 2;
324-
two >> 1; // 1
325-
4 >> 1; // 2
326-
5 >> 1; // 2, due to flooring of integer values
324+
two >> 1; // 1
325+
-two >> 1; // -1, because >> performs arithmetic shift right,
326+
// which preserves the sign of the left operand
327+
328+
4 >> 1; // 2
329+
5 >> 1; // 2, due to flooring of integer values
327330
328331
pow(2, 254) >> 254; // 1
329332
```

0 commit comments

Comments
 (0)