Skip to content

Latest commit

 

History

History
344 lines (251 loc) · 7 KB

File metadata and controls

344 lines (251 loc) · 7 KB

Operators Reference

A comprehensive guide to all operators in Pome, their usage, and precedence.

Arithmetic Operators

Arithmetic operators perform mathematical calculations:

var a = 10;
var b = 3;

print(a + b);   // Addition: 13
print(a - b);   // Subtraction: 7
print(a * b);   // Multiplication: 30
print(a / b);   // Division: 3.333...
print(a % b);   // Modulo (remainder): 1

Division

Division returns a decimal result:

print(10 / 3);    // Output: 3.333...
print(10 / 2);    // Output: 5
print(-10 / 3);   // Output: -3.333...

Modulo

Modulo returns the remainder:

print(10 % 3);   // Output: 1
print(15 % 5);   // Output: 0
print(-10 % 3);  // Output: -1

Comparison Operators

Comparison operators return boolean values:

print(5 == 5);   // Equal: true
print(5 == 3);   // Equal: false
print(5 != 3);   // Not equal: true
print(5 > 3);    // Greater than: true
print(5 < 3);    // Less than: false
print(5 >= 5);   // Greater or equal: true
print(3 <= 5);   // Less or equal: true

Type Consideration

Pome does not perform automatic type coercion for comparisons. When comparing values of different types, the comparison will generally evaluate to false unless values are explicitly converted to compatible types first. For predictable results, ensure both sides of a comparison have the same type.

print(5 == "5");      // false (different types)
print(true == 1);       // false (different types)
print(nil == false);    // false

Logical Operators

Logical operators work with boolean values:

var a = true;
var b = false;

print(a and b);   // Logical AND: false
print(a or b);    // Logical OR: true
print(not a);     // Logical NOT: false

AND Operator

Returns true only if both operands are true:

print(true and true);    // true
print(true and false);   // false
print(false and true);   // false
print(false and false);  // false

Short-circuits: If the first operand is false, the second is not evaluated.

var x = nil;
// Safe: len(x) is not evaluated because x != nil is false
if (x != nil and len(x) > 0) {
    // ...
}

OR Operator

Returns true if at least one operand is true:

print(true or true);    // true
print(true or false);   // true
print(false or true);   // true
print(false or false);  // false

Short-circuits: If the first operand is true, the second is not evaluated.

var x = nil;
// Safe: isValid(x) is only called if x is NOT nil (Wait, logic check: if x != nil is true, it short circuits. If false, it evals right. So x IS nil on right. Bad example for OR short circuit safety unless inverted).
// Better example:
var config = {debug: true};
// If config.debug is true, the second part is skipped.
if (config.debug or expensiveCheck()) {
    print("Debug or check passed");
}

NOT Operator

Inverts a boolean value:

print(not true);   // false
print(not false);  // true
print(not nil);    // true (nil is falsy)
print(not 0);      // true (0 is falsy)

String Operators

The + operator concatenates strings:

print("Hello" + " " + "World");  // Output: Hello World
print("Count: " + 5);            // Output: Count: 5

Assignment Operators

The = operator assigns values to variables:

var x = 10;
x = 20;

Compound Assignment

Pome supports compound assignment operators for concise updates:

Operator Description Equivalent
+= Add and assign x = x + y
-= Subtract and assign x = x - y
*= Multiply and assign x = x * y
/= Divide and assign x = x / y
%= Modulo and assign x = x % y
var x = 10;
x += 5;  // x is now 15
x *= 2;  // x is now 30

Operator Precedence

Operators are evaluated in this order (highest to lowest):

Precedence Operator Associativity
1 () [] . Left to right
2 ! - (unary) Right to left
3 ^ Left to right
4 * / % Left to right
5 + - Left to right
6 < <= > >= Left to right
7 == != Left to right
8 and Left to right
9 or Left to right
10 ?: (ternary) Right to left
11 = += -= *= /= %= Right to left

Examples

// Multiplication before addition
print(2 + 3 * 4);  // Output: 14 (not 20)

// Parentheses override precedence
print((2 + 3) * 4);  // Output: 20

// Comparison before AND
print(true and 5 > 3);  // Output: true

// AND before OR
print(true or false and false);  // Output: true (and is evaluated first)
print((true or false) and false);  // Output: false

Unary Operators

Unary Minus

Negates a number:

var x = 10;
print(-x);  // Output: -10
print(-(-x));  // Output: 10

Logical NOT

Inverts a boolean:

print(!true);   // Output: false
print(!false);  // Output: true

Ternary Operator

The conditional operator chooses between two values:

var age = 25;
var status = age >= 18 ? "adult" : "minor";
print(status);  // Output: adult

Syntax: condition ? valueIfTrue : valueIfFalse

Nested Ternary

var score = 85;
var grade = score >= 90 ? "A" : 
            score >= 80 ? "B" : 
            score >= 70 ? "C" : 
            score >= 60 ? "D" : "F";
print(grade);  // Output: B

Comparison with Different Types

String vs Number

print("10" == 10);    // May vary
print("abc" == 123);  // false

Nil Comparisons

print(nil == nil);      // true
print(nil == false);    // false
print(nil == 0);        // false

Common Operator Patterns

Checking Bounds

var value = 50;
if (value > 0 and value < 100) {
    print("In range");
}

Checking Multiple Conditions

var x = 5;
if (x > 0 and x < 10 or x == 100) {
    print("Valid");
}

Safe Property Access

var user = {};
if (user != nil and user.name != nil) {
    print(user.name);
}

Toggle Booleans

var isActive = true;
isActive = not isActive;  // Now false

Default Values

var input = nil;
var result = input != nil ? input : "default";

Operator Tips

  1. Use parentheses for clarity: Even when not required, they make code easier to read

    // Less clear
    if (x > 5 and y < 10 or z == 3) { }
    
    // Clearer
    if ((x > 5 and y < 10) or z == 3) { }
    
  2. Be aware of operator precedence: Unexpected results come from forgetting it

    print(2 + 3 * 4);  // 14, not 20
    
  3. Use short-circuit evaluation strategically:

    // This won't crash if x is nil
    if (x != nil and x > 10) { }
    
  4. Be explicit when converting types:

    // Pome does not implicitly convert non-string values when concatenating.
    // Ensure `myVar` is a string before concatenation.
    print("Value: " + myVar);  // myVar must be a string
    

Next: Module System
Back to: Documentation Home