Skip to main content

Operators

Swirl supports all traditional operators as they are found in C, with additional operators like exponentiation (**) and the following -

  • alignof: yields the alignment requirement of a type in bytes.
  • sizeof: yields the number of bytes that the type will occupy in memory.

Both the above operators can act on both, expressions and types. Types can be used in expression context by preceding them with the type keyword. E.g.

let s = sizeof type i32;  // evaluates to 4
let t = sizeof 8888; // evaluates to 4 since integer literals default to an i32 type

All the other operators are listed in the table below.

OperatorUsageAssociativityPrecedence (Descending order)
.Member accessLeftA
[]Indexing, can be used on pointers too.LeftB
&xAddress takingLeftC
+xUnary plusLeftC
-xUnary minusLeftC
*xDereferenceLeftC
!xLogical notLeftC
~xBitwise NOTLeftC
asType casting, e.g. 34 as i64LeftD
**Exponentiation, base appears on the left, exponent on the right.RightE
*MultiplicationLeftF
/DivisionLeftF
%ModulusLeftF
+AdditionLeftG
-SubtractionLeftG
<<Bitwise left shiftLeftH
>>Bitwise right shiftLeftH
>Greater thanLeftI
>=Greater than or equalLeftI
<Less thanLeftI
<=Less than or equalLeftI
==EqualityLeftJ
!=InequalityLeftJ
&Bitwise ANDLeftK
^Bitwise XORLeftL
|Bitwise ORLeftM
&&Logical ANDLeftN
||Logical ORLeftO
=AssignmentRightP
+=Add assignmentRightP
-=Subtract assignmentRightP
*=Multiply assignmentRightP
/=Divide assignmentRightP
%=Modulus assignmentRightP
**=Exponentiation assignmentRightP
|=Bitwise OR assignmentRightP
&=Bitwise AND assignmentRightP
^=Bitwise XOR assignmentRightP
<<=Left shift assignmentRightP
>>=Right shift assignmentRightP