Operators

Below is a list of all of Sparkle's operators, in order of precedence.

The order of precedence is important to understand how expressions are evaluated. Parentheses can be used to change the order of evaluation.

let x be 42

let expression_a be type of x is equal to Number
let expression_b be type of (x is equal to Number)

print expression_a # true
print expression_b # Boolean

Some of the operators have alternate aliases, for example a plus b and a + b are equivalent.

Returns the negative of the number.

minus a
-a

Errors:

  • The operand is not a number.

Returns the value of the property.

a's b

Errors:

  • The property doesn't exist.

Calls a function, optionally with parameters.

call a [with ___]

Errors:

  • The number of parameters doesn't match with the function definition.
  • Parameters by reference mismatch.

For lists and strings, returns the element at the index. For objects, returns the value of the property with the given name.

a at b

Errors:

  • Index is out of bounds (list/string).
  • Property doesn't exist (object).

For numbers, returns the result of the operation. For strings and plus, returns a concatenation. Multiplication, division, and modulo take precedence over addition, subtraction, and exponentiation.

a times b
a divided by b
a modulo b
a plus b
a minus b
a raised to b
a * b
a / b
a % b
a + b
a - b
a ^ b

Errors:

  • Invalid operand type.
  • Dividing by zero.

Returns the type of the value.

type of a

For strings, returns true if the string is a substring of the other string. For lists, returns true if the element is in the list. For objects, returns true if the property is in the object.

a has b

Compares two values. Between strings, the lexicographical order is used, case-sensitive.

a is greater than b
a is less than b
a is greater or equal to b
a is less or equal to b
a > b
a < b
a >= b
a <= b

Errors:

  • Operand types mismatch.

Checks the equality of two values. For lists and objects, performs a deep equality check. For functions, returns true if they reference the same function.

If the operands are not of the same type, returns false.

a is equal to b
a is not equal to b
a = b
a != b

Returns the result of a logical operation between two boolean values.

a and b
a or b
not a

Errors:

  • Operand not a boolean.

User defined classes may overload the unary minus, arithmetic, comparison, equality, and boolean operators, but they retain their order of precedence.