Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

2.2 Operators

Binary operators

Binary operators are operators that take two operands. They are used in the following way:

do say (1 + 2) // 1 on the left, 2 on the right

Here is a list of all binary operators:

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
^ Bitwise XOR
& Bitwise AND
| Bitwise OR
<< Bitwise left shift
>> Bitwise right shift
** power
^/ Root of
>| max
<| min
== Equality
!= Inequality
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
&& Logical AND
|| Logical OR
# Hashing
-> Object subscript
?? Null coalescing
?-> Safe object subscripting
:> Range operator
:< Reverse range operator
:>= Inclusive range operator
:<= Reverse inclusive range operator
=> Lambda function arrow

Unary operators

Unary operators are operators that take one operand (to the right). They are used in the following way:

do say (-1) // 1 on the right

Here is a list of all unary operators:

Operator Meaning
- Negation
! Logical NOT
~ Bitwise NOT
^/ Square root
!- absolute
* Copy
>| max (of array)
<| min (of array)

Ternary operators

Ternary operators are operators that take three operands. They are used in the following way:

do say (true ? 1 : 2) // 1 on the left

assigment operators

Assigment operators are operators that take two operands the left is always an expression from a set keyword. They are used in the following way:

set number += 1 // 1 on the right

Here is a list of all assigment operators:

Operator Meaning
= Assigment
+= Addition
-= Subtraction
*= Multiplication
/= Division
%= Modulo
^= Bitwise XOR
&= Bitwise AND
|= Bitwise OR
<<= Bitwise left shift
>>= Bitwise right shift
**= Power
^/= Root of
>|= Max
<|= Min
??= Null coalescing
++ increment
-- decrement

Operator precedence

Operator precedence is the order in which operators are evaluated.
Operators with higher precedence are evaluated first.
Operators with the same precedence are evaluated from left to right.
Here is an example:

do say (1 + 2 * 3) // 7

here's a full table of operator precedence:

Level Operators Meaning
0 () Grouping, this is always evaluated first
1 #, ->, ?-> Hashing arrays, subscripting objects
2 !, ~, ^/, !-, *, **, >|, <| Unary operators, and POW, MAX, MIN
3 *, /, % Multiplication, division and modulo
4 +, - Addition and subtraction
5 <<, >> Bitwise shift
6 <, >, <=, >= Comparison
7 ==, != Equality
8 & Bitwise AND
9 ^ Bitwise XOR
10 | Bitwise OR
11 && Logical AND
12 ||, ??, |> Logical OR, NULL coalesce and pipeline operator
13 ? :, :>, :<, :>=, :<= Ternary operator and range operators

Addition operator

The addition operator (+) is used to add two numbers together.
It can also be used to concatenate two strings.
Or to join two arrays or objects together.
Example:

do say (1 + 2) // 3
do say ("Hello " + "world!") // Hello world!
do say ([1, 2, 3] + [4, 5, 6]) // [1, 2, 3, 4, 5, 6]
do say ({"key1": 1} + {"key2": 2}) // {"key1": 1, "key2": 2}

Subtraction oparator

The subtraction operator (-) is used to subtract two numbers.
But it can also be used to remove elements from an array, object or string.
Example:

do say (1 - 2) // -1
do say ("Hello" - 3) // He
do say ([1, 2, 3] - 1) // [1, 2]
do say ({"key1": 1, "key2": 2} - "key2") // {"key1": 1}

Hashing operator

The hashing operator (#) is used to get an element from an array.
Example:

do say ([1, 2, 3]#0) // 1

Object Subscripting

The object subscripting operator (->) is used to get an element from an object.
It converts the right operand to a string, or it takes an identifier.
Example:

make object person = {
    "name": "John",
    "age": 30
}
do say (person->name) // John

You can also safely get an element from an object by using the safe object subscripting operator (?->).

make object person = null
do say (person?->name) // null, instead of crash

Power and root

The power operator (**) is used to raise a number to a power.
The root operator (^/) is used to get the root of a number.
Example:

do sayln (2 ** 3) // 8
do sayln (3 ^/ 8) // 2

The root operator can take a second argument to specify the root. (left one)
However, to just get the square root, you can use the root operator as a unary operator.
Example:

do sayln (^/81) // 9

Using root assign, the right side is the root.
Example:

make int number = 81
set number ^= 4 // 3

Max and min

The max operator (>|) is used to get the largest number.
The min operator (<|) is used to get the smallest number.
Example:

do sayln (5 >| 1) // 5
do sayln (5 <| 1) // 1

Abs

The abs operator (!-) is used to get the absolute value of a number.
Example:

do sayln (!- -5) // 5

Info!
There's alternative functions for the above mentioned operators.
POW, SQRT, MAX, MIN, ABS.

Increment and decrement

The increment operator (++) is used to increase a number by 1.
The decrement operator (--) is used to decrease a number by 1.
You can only use these operators in set statements.
Example:

make int number = 5
set number++ // number is now 6
set number-- // number is now 5

Null coalescing

The null coalescing operator (??) is used to return the right operand if the left operand is null.
Example:

make number = null
do say (number ?? 5) // 5
set number ??= 5 // number is null, so it is set to 5
set number ??= 10 //  10 is not set, because number is not null anymore
do sayln(number) // 5

Range operators

The range operator (:>) is used to create a range of numbers.
The reverse range operator (:<) is used to create a range of numbers in reverse.
The inclusive range operator (:>=) is used to create a range of numbers including the last number.
The reverse inclusive range operator (:<=) is used to create a range of numbers in reverse including the last number.
Example:

do sayln (0 :> 5) // [0, 1, 2, 3, 4]
do sayln (5 :< 0) // [5, 4, 3, 2, 1]
do sayln (0 :>= 5) // [0, 1, 2, 3, 4, 5]
do sayln (5 :<= 0) // [5, 4, 3, 2, 1, 0]

Pipeline operator

The pipeline operator (|>) is used to pipe the value into the input of a function.
Example:

do sayln (-5 |> abs) // 5

It calls the right operand with the left operand as input.
It errors when the argument is incorrect/not enough or it's not a function.

Ternary operators

The ternary operator (?:) is used to return a value based on a condition.
It takes three operands, the condition, the value if true and the value if false.
Example:

do say (true ? 1 : 2) // 1
do say (false ? 1 : 2) // 2

The rest of the operators are pretty self explanatory. and behave the same as in other languages.