Skip to main content

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.

OperatorSymbolFormatconstraintsresult typeresult type is const
PostIncrement++x++x is not const//
PostDecrement--x--x is not const//
Bracket[]x[y]variesYes if x is const
Parentheses()x(y, z, ...)x is a function or typevariesvaries
Dot.x.yvariesYes if x is const
WithParentheses()(x)same as xif x is const
PreIncrement++++xx is not const//
PreDecrement----xx is not const//
UnaryPlus++xsame as xYes
UnaryMinus--xsame as xYes
LogicalNot!!xsame as xYes
BitwiseNot~~xsame as xYes
Multiply*x * yx and y of the same typesame as xYes
Divide/x / yx and y of the same typesame as xYes
Modulo%x % yx and y of the same typesame as xYes
Add+x + yx and y of the same typesame as xYes
Subtract-x - yx and y of the same typesame as xYes
ShiftLeft<<x << yx and y of the same typesame as xYes
ShiftRight>>x >> yx and y of the same typesame as xYes
LessThan<x < yx and y are boolboolYes
GreaterThan>x > yx and y are boolboolYes
LessThanOrEqual<=x <= yx and y are boolboolYes
GreaterThanOrEqual>=x >= yx and y are boolboolYes
Equal==x == yx and y are boolboolYes
NotEqual!=x != yx and y are boolboolYes
BitwiseAnd&x & yx and y of the same typesame as xYes
BitwiseXor^x ^ yx and y of the same typesame as xYes
BitwiseOr|x | yx and y of the same typesame as xYes
LogicalAnd&&x && yx and y are boolboolYes
LogicalOr||x || yx and y are boolboolYes
TernaryConditional? :x ? y : zx is bool
y and z of the same type
same as yYes if either y or z is const
Assignment=x = yx is not const
x and y of the same type
//
AssignmentAdd+=x += yx is not const
x and y of the same type
//
AssignmentSubtract-=x -= yx is not const
x and y of the same type
//
AssignmentMultiply*=x *= yx is not const
x and y of the same type
//
AssignmentDivide/=x /= yx is not const
x and y of the same type
//
AssignmentModulo%=x %= yx is not const
x and y of the same type
//
AssignmentShiftLeft<<=x <<= yx is not const
x and y of the same type
//
AssignmentShiftRight>>=x >>= yx is not const
x and y of the same type
//
AssignmentBitwiseAnd&=x &= yx is not const
x and y of the same type
//
AssignmentBitwiseXor^=x ^= yx is not const
x and y of the same type
//
AssignmentBitwiseOr|=x |= yx is not const
x and y of the same type
//