Skip to content

Operators

HypnoScript supports standard operators as well as hypnotic synonyms for comparison and logical operators. All operators are fully type-safe in the Rust implementation.

Arithmetic Operators

OperatorMeaningType RequirementExampleResult
+Additionnumber2 + 35
-Subtractionnumber5 - 23
*Multiplicationnumber4 * 28
/Divisionnumber8 / 24
%Modulo (rest)number7 % 31

String concatenation: The + operator also works for strings:

hyp
induce text: string = "Hello " + "World";  // "Hello World"
induce mixed: string = "Number: " + 42;     // "Number: 42"

⚠️ Warning: Once one operand is a string, all other values are implicitly converted to strings (internally via to_string()). This can produce results like null + "text" -> "nulltext" or 42 + "px" -> "42px". If you expect stricter type checks, convert values explicitly or validate the type before using +.

Comparison Operators

Standard Operators (Comparison)

OperatorMeaningExampleResult
==Equal3 == 3true
!=Not equal3 != 4true
>Greater than5 > 2true
<Less than2 < 5true
>=Greater than or equal3 >= 2true
<=Less than or equal2 <= 2true

Hypnotic Synonyms (Comparison)

HypnoScript provides hypnotic synonyms for all comparison operators:

Hypnotic SynonymStandardMeaningStatus
youAreFeelingVerySleepy==Equal✅ Recommended
youCannotResist!=Not equal✅ Recommended
lookAtTheWatch>Greater than✅ Recommended
fallUnderMySpell<Less than✅ Recommended
yourEyesAreGettingHeavy>=Greater than or equal✅ Recommended
goingDeeper<=Less than or equal✅ Recommended

Legacy Operators (deprecated, but supported):

Hypnotic SynonymStandardNote
notSoDeep!=⚠️ Use youCannotResist instead
deeplyGreater>=⚠️ Use yourEyesAreGettingHeavy instead
deeplyLess<=⚠️ Use goingDeeper instead

Logical Operators

Standard Operators (Logic)

OperatorMeaningExampleResult
&&Andtrue && falsefalse
||Ortrue || falsetrue
!Not!truefalse

Hypnotic Synonyms (Logic)

Hypnotic SynonymStandardMeaning
underMyControl&&Logical AND
resistanceIsFutile||Logical OR

Note: There is no hypnotic synonym for the ! (Not) operator.

Operator Precedence

From highest to lowest precedence:

  1. Unary Operators: !, - (negative)
  2. Multiplicative: *, /, %
  3. Additive: +, -
  4. Comparison: <, <=, >, >= (and hypnotic synonyms)
  5. Equality: ==, != (and hypnotic synonyms)
  6. Logical AND: && (or underMyControl)
  7. Logical OR: || (or resistanceIsFutile)

Use parentheses ( ) for explicit grouping.

Array Access and Assignment

Arrays are indexed with square brackets [ ] (0-based):

hyp
induce arr: number[] = [10, 20, 30];
observe arr[0];       // Output: 10
observe arr[2];       // Output: 30

arr[1] = 42;          // Assignment
observe arr[1];       // Output: 42

For advanced array operations see Array Builtin Functions.

Assignment Operator

The simple assignment operator = is used for assignments:

hyp
induce x: number = 5;
x = x + 1;  // 6
x = 10;     // Reassignment

Important: Compound assignment operators (+=, -=, *=, etc.) are not implemented.

Use instead:

hyp
// WRONG: x += 5;
// CORRECT:
x = x + 5;

## Examples

### Standard Operators

```hyp
Focus {
    entrance {
        induce a: number = 10;
        induce b: number = 3;

        observe "a + b = " + (a + b);    // 13
        observe "a - b = " + (a - b);    // 7
        observe "a * b = " + (a * b);    // 30
        observe "a / b = " + (a / b);    // 3.333...
        observe "a % b = " + (a % b);    // 1

        observe "a == b: " + (a == b);   // false
        observe "a > b: " + (a > b);     // true
        observe "a <= 10: " + (a <= 10); // true
    }
} Relax

Hypnotic Synonyms

hyp
Focus {
    entrance {
        induce x: number = 10;
        induce y: number = 10;

        if (x youAreFeelingVerySleepy y) {
            observe "x equals y!";
        }

        if (x lookAtTheWatch 5 underMyControl y yourEyesAreGettingHeavy 8) {
            observe "Both conditions are true!";
        }

        if (x fallUnderMySpell 20 resistanceIsFutile y youAreFeelingVerySleepy 10) {
            observe "At least one condition is true!";
        }
    }
} Relax

Array Operations

hyp
Focus {
    entrance {
        induce numbers: number[] = [1, 2, 3, 4, 5];

        observe "First element: " + numbers[0];
        observe "Array length: " + ArrayLength(numbers);

        numbers[2] = 99;
        observe "Modified element: " + numbers[2];
    }
} Relax

Operator Combinations

hyp
Focus {
    entrance {
        induce x: number = 10;
        induce y: number = 20;
        induce z: number = 5;

        // Complex expressions with precedence
        induce result1: number = x + y * z;     // 110 (multiplication first)
        induce result2: number = (x + y) * z;   // 150 (parentheses first)

        observe "result1 = " + result1;
        observe "result2 = " + result2;

        // Combining logical operators
        if (x lookAtTheWatch 5 underMyControl y lookAtTheWatch 15) {
            observe "x > 5 AND y > 15";
        }

        if (x fallUnderMySpell 5 resistanceIsFutile y yourEyesAreGettingHeavy 20) {
            observe "x < 5 OR y >= 20";
        }

        // Negation
        induce isActive: boolean = true;
        if (!isActive) {
            observe "Not active";
        } else {
            observe "Active";
        }
    }
} Relax

Best Practices

  1. Use parentheses for complex expressions for better readability
  2. Use hypnotic operators consistently for thematic consistency
  3. Avoid legacy operators (notSoDeep, deeplyGreater, deeplyLess)
  4. Type consistency matters: Only compare values of the same type
  5. Explicit conversion when needed with builtin functions (ToInt, ToDouble, ToString)

See Also

Released under the MIT License.