Skip to content

Operators ​

Project Links: GitHub · Compiler Source

Overview ​

MaìLang provides a rich set of operators, including arithmetic, comparison, logical, bitwise, and assignment operators.

Arithmetic Operators ​

OperatorNameDescriptionExample
+AdditionAdd two numbers or concatenate strings1 + 2 = 3, "a" + "b" = "ab"
-SubtractionSubtract two numbers5 - 3 = 2
*MultiplicationMultiply two numbers or repeat a string4 * 3 = 12, "ha" * 3 = "hahaha"
/DivisionDivide two numbers10 / 3 = 3 (integers), 10.0 / 3.0 = 3.333...
%ModuloRemainder10 % 3 = 1
**ExponentiationPower2 ** 3 = 8
-NegationUnary operator-5

Type Behavior ​

int + int = int       // 1 + 2 = 3
int + float = float   // 1 + 2.0 = 3.0
float + float = float // 1.0 + 2.0 = 3.0
str + str = str       // "a" + "b" = "ab"
str + any = str       // "a" + 42 = "a42"
int * int = int       // 3 * 4 = 12
str * int = str       // "ha" * 3 = "hahaha"

Comparison Operators ​

OperatorNameDescriptionExample
==EqualValues are equal1 == 1 → true
!=Not equalValues are not equal1 != 2 → true
<Less thanLeft is less than right1 < 2 → true
<=Less than or equalLeft is less than or equal to right1 <= 1 → true
>Greater thanLeft is greater than right2 > 1 → true
>=Greater than or equalLeft is greater than or equal to right2 >= 2 → true

Comparison Rules ​

int vs int        // compared by value
float vs float    // compared by value
int vs float      // int is converted to float, then compared
str vs str        // lexicographic comparison
bool vs bool      // true > false
null vs null      // always equal

Logical Operators ​

OperatorNameDescriptionExample
&&Logical ANDBoth are truetrue && false → false
||Logical OREither is truetrue || false → true
!Logical NOTNegation!true → false

Short-Circuit Evaluation ​

// && short-circuits: if the left side is false, the right side does not run
false && expensive_function()  // expensive_function does not run

// || short-circuits: if the left side is true, the right side does not run
true || expensive_function()   // expensive_function does not run

Truth Table ​

&& | true | false
---|------|------
true  | true  | false
false | false | false

|| | true | false
---|------|------
true  | true  | true
false | true  | false

Bitwise Operators ​

OperatorNameDescriptionExample
&Bitwise AND1 only when both bits are 10b1010 & 0b1100 → 0b1000
|Bitwise OR1 when either bit is 10b1010 | 0b1100 → 0b1110
^Bitwise XOR1 when the bits differ0b1010 ^ 0b1100 → 0b0110
~Bitwise NOTInvert all bits~0b1010 → ...11110101
<<Left shiftShift left by the given count1 << 3 → 8
>>Right shiftShift right by the given count8 >> 2 → 2

Examples ​

// Set bits
let flags = 0b0000
flags = flags | 0b0001  // set bit 0
flags = flags | 0b0010  // set bit 1

// Clear bits
flags = flags & ~0b0001  // clear bit 0

// Toggle bits
flags = flags ^ 0b0001  // toggle bit 0

// Test bits
if flags & 0b0001 != 0 {
    // bit 0 is set
}

Assignment Operators ​

OperatorEquivalentExample
=—x = 10
+=x = x + yx += 5
-=x = x - yx -= 3
*=x = x * yx *= 2
/=x = x / yx /= 4
%=x = x % yx %= 3
&=x = x & yx &= 0xFF
|=x = x | yx |= 0x01
^=x = x ^ yx ^= 0xFF
<<=x = x << yx <<= 1
>>=x = x >> yx >>= 1

Operator Precedence ​

From highest to lowest:

PrecedenceOperatorsAssociativityDescription
15()—Parentheses
14. [] ()leftMember access, indexing, function call
13**rightExponentiation
12- ! ~rightUnary operators
11* / %leftMultiplicative
10+ -leftAdditive
9<< >>leftShift
8&leftBitwise AND
7^leftBitwise XOR
6|leftBitwise OR
5== != < <= > >=leftComparison
4&&leftLogical AND
3||leftLogical OR
2= += -= etc.rightAssignment
1,leftComma

Next Steps ​