Skip to content

Expressions

Operators

Arithmetic Operators

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulo a % b
++ Increment ++a or a++
-- Decrement --a or a--

Bitwise Operators

Operator Description Example
and Bitwise AND a and b
or Bitwise OR a or b
xor Bitwise XOR a xor b
not Bitwise NOT not a
nand Bitwise NAND a nand b
nor Bitwise NOR a nor b
lshift Left shift a lshift b
rshift Right shift a rshift b

Logical Operators

Operator Description Example
&& Logical AND a && b
\|\| Logical OR a \|\| b
! Logical NOT !a

Comparison Operators

Operator Description Example
is Equal to a is b
isnt Not equal to a isnt b
is equal to Equal to (verbose) a is equal to b
isnt equal to Not equal to (verbose) a isnt equal to b
is less than Less than a is less than b
is greater than Greater than a is greater than b
is less than or equal to Less than or equal a is less than or equal to b
is greater than or equal to Greater than or equal a is greater than or equal to b
< Less than a < b
> Greater than a > b
<= Less than or equal a <= b
>= Greater than or equal a >= b

Assignment Operators

Operator Description Example
= Assignment a = b
+= Addition assignment a += b
-= Subtraction assignment a -= b
*= Multiplication assignment a *= b
/= Division assignment a /= b
%= Modulo assignment a %= b

Type Operators

Operator Description Example
as Type cast a as int32
sizeof Size in bytes sizeof(Type)
alignof Alignment requirement alignof(Type)
typeof Type reflection typeof(instance)
addressof Address of variable addressof(a)

Memory Operators

Operator Description Example
new Heap allocation new T
delete Heap deallocation delete ptr
construct Placement construction construct T at addr
destruct Placement destruction destruct T at addr
move Transfer ownership move(ptr)

Member Access Operators

Operator Description Example
. Member access object.member
-> Pointer member access ptr->member
:: Scope resolution namespace::symbol

Other Operators

Operator Description Example
? : Ternary conditional condition ? a : b
${} String interpolation "value: ${x}"

Operator Precedence

From highest to lowest precedence:

Precedence Operators Associativity Notes
1 :: Left to right Scope resolution
2 () [] . -> ++ -- (postfix) Left to right Postfix
3 ++ -- (prefix) ! not + - (unary) addressof sizeof alignof typeof Right to left Prefix / Unary
4 as Left to right Type cast
5 * / % Left to right Multiplicative
6 + - Left to right Additive
7 lshift rshift and or xor nand nor Left to right Bitwise
8 < > <= >= is less than is less than or equal to is greater than is greater than or equal to Left to right Relational
9 is isnt is equal to isnt equal to Left to right Equality
10 && Left to right Logical AND
11 \|\| Left to right Logical OR
12 ? : Right to left Ternary conditional
13 = += -= *= /= %= Right to left Assignment

Example Code

import std.print;

int32 main()
{
    // ============================================
    // Arithmetic Operators
    // ============================================
    var int32 a = 10;
    var int32 b = 3;

    int32 sum        = a + b;   // 13
    int32 diff       = a - b;   // 7
    int32 product    = a * b;   // 30
    int32 quotient   = a / b;   // 3
    int32 remainder  = a % b;   // 1

    var int32 counter = 0;
    ++counter;                  // prefix increment: counter = 1
    counter++;                  // postfix increment: counter = 2
    --counter;                  // prefix decrement: counter = 1


    // ============================================
    // Bitwise Operators (parentheses required when mixing)
    // ============================================
    int32 flags = 0b11001100;
    int32 mask  = 0b00001111;

    int32 masked   = flags and mask;           // 0b00001100 = 12
    int32 combined = flags or mask;            // 0b11001111 = 207
    int32 toggled  = flags xor mask;           // 0b11000011 = 195
    int32 inverted = not flags;                // bitwise NOT
    int32 negated  = flags nand mask;          // NOT (flags AND mask)
    int32 neither  = flags nor mask;           // NOT (flags OR mask)

    int32 shifted_left  = 1 lshift 4;          // 16
    int32 shifted_right = 256 rshift 2;        // 64

    int32 complex = flags and mask or toggled xor inverted; // (((flags and mask) or toggled) xor inverted)


    // ============================================
    // Logical Operators
    // ============================================
    bool is_valid   = true;
    bool is_enabled = false;

    bool both    = is_valid && is_enabled;     // false
    bool either  = is_valid || is_enabled;     // true
    bool negated_bool = !is_valid;             // false


    // ============================================
    // Comparison Operators (symbolic)
    // ============================================
    bool less      = a < b;                    // false
    bool greater   = a > b;                    // true
    bool leq       = a <= b;                   // false
    bool geq       = a >= b;                   // true


    // ============================================
    // Comparison Operators (readable)
    // ============================================
    if (a is b)
    {
        std::print("a equals b");
    }

    if (a isnt b)
    {
        std::print("a does not equal b");
    }

    if (a is greater than b)
    {
        std::print("a is greater than b");
    }

    if (a is less than b)
    {
        std::print("a is less than b");
    }

    if (a is greater than or equal to b)
    {
        std::print("a is greater than or equal to b");
    }

    if (a is less than or equal to b)
    {
        std::print("a is less than or equal to b");
    }

    // Verbose equality forms
    if (a is equal to b)
    {
        std::print("a is equal to b");
    }

    if (a isnt equal to b)
    {
        std::print("a is not equal to b");
    }


    // ============================================
    // Assignment Operators
    // ============================================
    var int32 x = 10;
    x += 5;                                    // x = 15
    x -= 3;                                    // x = 12
    x *= 2;                                    // x = 24
    x /= 4;                                    // x = 6
    x %= 4;                                    // x = 2


    // ============================================
    // Type Operators
    // ============================================
    int32 value = 42;
    float32 converted = value as float32;      // 42.0f32

    int32 size = sizeof(int64);                // 8
    int32 alignment = alignof(float64);        // 8
    int32* ptr = addressof(value);             // address of value

    // Reflection
    var ascii* type_name = typeof(value).name();
    bool is_int = typeof(value).is_struct();


    // ============================================
    // Memory Operators
    // ============================================
    var int32* var heap_int = new int32;
    var int32* var heap_array = new int32[10];
    var int32* var aligned_ptr = new<16> int32;

    // With initializer
    var Point* var point = new Point with { _x: 10, _y: 20 };

    // Placement construction
    ubyte buffer[64];
    construct Point at addressof(buffer) with { _x: 5, _y: 5 };
    destruct Point at addressof(buffer);

    // Ownership transfer
    var int32 unique* var owner = new int32;
    var int32 unique* var new_owner = move(owner);

    delete heap_int;
    delete heap_array;
    delete aligned_ptr;
    delete point;
    delete new_owner;


    // ============================================
    // Member Access Operators
    // ============================================
    Point local_point = { _x: 1, _y: 2 };
    int32 px = local_point._x;                 // direct access

    var Point* var point_ptr = addressof(local_point);
    int32 py = point_ptr->_y;                  // pointer access

    std::print("Accessed via ::");             // scope resolution


    // ============================================
    // Ternary Operator
    // ============================================
    int32 max_val = (a is greater than b) ? a : b;
    int32 min_val = (a is less than b) ? a : b;


    // ============================================
    // String Interpolation
    // ============================================
    std::print("Sum: ${sum}, Product: ${product}");
    std::print("a=${a}, b=${b}, max=${max_val}");


    // ============================================
    // Precedence Demonstration
    // ============================================

    // Multiplicative > Additive
    int32 p1 = 2 + 3 * 4;                      // 2 + (3 * 4) = 14

    // Additive > Bitwise
    int32 p2 = 2 + 3 and 0xFF;                 // (2 + 3) and 0xFF = 5

    // Bitwise > Relational (FIXED from C++)
    bool p3 = flags and mask is greater than 0; // (flags and mask) is greater than 0

    // Relational > Equality
    bool p4 = a is greater than b is true;     // (a is greater than b) is true

    // Equality > Logical AND
    bool p5 = a is 10 && b is 3;               // (a is 10) && (b is 3)

    // Logical AND > Logical OR
    bool p6 = true || false && false;          // true || (false && false) = true

    // Logical OR > Ternary
    int32 p7 = true || false ? 1 : 0;          // (true || false) ? 1 : 0 = 1

    // Ternary > Assignment
    var int32 p8 = 0;
    p8 = true ? 42 : 0;                        // p8 = (true ? 42 : 0) = 42


    return 0;
}