Expressions
Literals
Number Literals
Integer literals are by default regarded as of type int32.
To force a specific type, append a suffix of "u" (for unsigned types) or "i" (for signed types), plus a bit-width. e.g., 100u8, 1000i64.
Hex literals can also be followed by the same suffices.
Bigint literals ends with suffix 'ib', without a bit-width.
Floating point literals uses the suffix of 'f', followed by the bit-width.
Other Literals
String literals are characters surrounded by pair of quotation marks '"' and are of type string.
Address literals are base32 characters of length 58 followed by :ed25519.
Hash literals are base32 characters of length 52 followed by :hash.
"Hello world!" // string literal
vffgwr07yq323axszgxbr2qp9azzbyjjm844s90z8ack63s6hrch683z48:ed25519 // address literal
ccnwe8x9sig7gb98zkb6gh@qarffhvf6c3ok9433@tz9ne4mb6qi:hash // hash literal
Unary Operators
PREDA supports the following unary operators: increment (++), decrement (--), negation (-), bitwise negation (~) and logical negation (!).
Binary Operators
PREDA supports the following binary operators: addition (+), subtraction (-), multiplication (*), division (/), modulo (%), left-shift (<<), right-shift (>>), bitwise and (&), bitwise or (|), bitwise exclusive or (^).
The above operator can be combine with assignment operator (=) to form compound assignment operators, like addition assignment (+=), left shift assignment (<<=), etc.
Besides, there are also logical binary operators less than(<), greater than (>), less than or equal(<=), greater than or equal(>=), equal(==), not equal(!=), logical and (&&) and logical or (||)。
The Conditional Operator
The conditional operator is a ternary operator used in expressions in the format:
condition ? expression1 : expression2
It takes the result of expression1 if condition is satisfied, otherwise result of expression2. expression1 and expression2 must have the same result type.
Operator Precedence
The following table lists all operators sorted from higher to lower precedence.
Operator | Symbol | Format | constraints | result type | result type is const |
---|---|---|---|---|---|
PostIncrement | ++ | x++ | x is not const | / | / |
PostDecrement | -- | x-- | x is not const | / | / |
Bracket | [] | x[y] | varies | Yes if x is const | |
Parentheses | () | x(y, z, ...) | x is a function or type | varies | varies |
Dot | . | x.y | varies | Yes if x is const | |
WithParentheses | () | (x) | same as x | if x is const | |
PreIncrement | ++ | ++x | x is not const | / | / |
PreDecrement | -- | --x | x is not const | / | / |
UnaryPlus | + | +x | same as x | Yes | |
UnaryMinus | - | -x | same as x | Yes | |
LogicalNot | ! | !x | same as x | Yes | |
BitwiseNot | ~ | ~x | same as x | Yes | |
Multiply | * | x * y | x and y of the same type | same as x | Yes |
Divide | / | x / y | x and y of the same type | same as x | Yes |
Modulo | % | x % y | x and y of the same type | same as x | Yes |
Add | + | x + y | x and y of the same type | same as x | Yes |
Subtract | - | x - y | x and y of the same type | same as x | Yes |
ShiftLeft | << | x << y | x and y of the same type | same as x | Yes |
ShiftRight | >> | x >> y | x and y of the same type | same as x | Yes |
LessThan | < | x < y | x and y are bool | bool | Yes |
GreaterThan | > | x > y | x and y are bool | bool | Yes |
LessThanOrEqual | <= | x <= y | x and y are bool | bool | Yes |
GreaterThanOrEqual | >= | x >= y | x and y are bool | bool | Yes |
Equal | == | x == y | x and y are bool | bool | Yes |
NotEqual | != | x != y | x and y are bool | bool | Yes |
BitwiseAnd | & | x & y | x and y of the same type | same as x | Yes |
BitwiseXor | ^ | x ^ y | x and y of the same type | same as x | Yes |
BitwiseOr | | | x | y | x and y of the same type | same as x | Yes |
LogicalAnd | && | x && y | x and y are bool | bool | Yes |
LogicalOr | || | x || y | x and y are bool | bool | Yes |
TernaryConditional | ? : | x ? y : z | x is bool y and z of the same type | same as y | Yes if either y or z is const |
Assignment | = | x = y | x is not const x and y of the same type | / | / |
AssignmentAdd | += | x += y | x is not const x and y of the same type | / | / |
AssignmentSubtract | -= | x -= y | x is not const x and y of the same type | / | / |
AssignmentMultiply | *= | x *= y | x is not const x and y of the same type | / | / |
AssignmentDivide | /= | x /= y | x is not const x and y of the same type | / | / |
AssignmentModulo | %= | x %= y | x is not const x and y of the same type | / | / |
AssignmentShiftLeft | <<= | x <<= y | x is not const x and y of the same type | / | / |
AssignmentShiftRight | >>= | x >>= y | x is not const x and y of the same type | / | / |
AssignmentBitwiseAnd | &= | x &= y | x is not const x and y of the same type | / | / |
AssignmentBitwiseXor | ^= | x ^= y | x is not const x and y of the same type | / | / |
AssignmentBitwiseOr | |= | x |= y | x is not const x and y of the same type | / | / |