Operators
Also known as: operators arithmetic plus minus multiply divide modulo remainder power exponent assignment && || ! not and or logical boolean
● Beginner6 minBefore this: VariablesSpec 1.8
Operators compute with values: + adds, == compares, and combines two conditions, ?? picks a default.
Every operator in Kavach, group by group: what it is called, what it does in plain words, and an example with its result. The results below were printed by the real compiler.
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
+ | Addition | Adds two numbers | 7 + 2 | 9 |
- | Subtraction | Takes the second number from the first | 7 - 2 | 5 |
* | Multiplication | Multiplies two numbers | 7 * 2 | 14 |
/ | Division | Divides; two whole numbers give a whole number, the fraction cut off | 7 / 2 | 3 |
/ | Division | With floats, the exact result | 7.0 / 2.0 | 3.5 |
% | Remainder | What is left over after dividing | 7 % 2 | 1 |
** | Power | Raises the first number to the power of the second | 2 ** 10 | 1024 |
- | Negation | Turns a number to its opposite sign | -(7 + 2) | -9 |
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
== | Equal to | True when both sides are the same | 5 == 5 | true |
!= | Not equal to | True when the sides differ | 5 != 3 | true |
< | Less than | True when the left is smaller | 3 < 5 | true |
<= | Less than or equal to | True when the left is smaller or the same | 5 <= 5 | true |
> | Greater than | True when the left is bigger | 7 > 9 | false |
>= | Greater than or equal to | True when the left is bigger or the same | 9 >= 9 | true |
a < b < c | Chain | Both comparisons at once, as in mathematics | 1 < 5 < 10 | true |
| Chain | Means | Result |
|---|---|---|
a == b != c | Three values: a == b and b != c | true |
c < a <= b | Three values: c < a and a <= b | true |
a == b != c < d | Four values: a == b and b != c and c < d | true |
a == b != c < d > e | Five values: a == b and b != c and c < d and d > e | true |
e < c < a == b < d | Any order: e < c and c < a and a == b and b < d | true |
a != b == c | Only neighbours: a != b and b == c: a and c are never compared | false |
a != c != b | Only neighbours: a != c and c != b: true, although a and b are equal | true |
e < c < a == b < d < f == g | Seven values: e < c and c < a and a == b and b < d and d < f and f == g | true |
(a == b) != (c == d) | Grouped: Each bracket is worked out first, to true or false; then the two answers are compared | true |
(c < a) == (e < d) | Grouped: true == true: both brackets give the same answer | true |
(a < b) == (d > f) == (e > c) | Grouped chain: Three answers, compared as a chain of their own | true |
a == b == c or d == e == d | Chains with or: True when either chain is true; here neither is | false |
a == b == 5 or d == e == d | Chains with or: The first chain is true, so the whole is true | true |
c < a == b and d > e < c | Chains with and: (c < a == b) and (d > e < c): both chains must be true | true |
not (a == b == c) | A chain turned around: a == b == c is false, so not makes it true | true |
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
and | And | True only when both sides are true | true and false | false |
or | Or | True when at least one side is true | true or false | true |
not | Not | Turns true into false, and false into true | not true | false |
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
= | Assign | Gives the variable a new value | x = 4 | 4 |
+= | Add and assign | x += 3 is x = x + 3 | x += 3 | 13 |
-= | Subtract and assign | x -= 3 is x = x - 3 | x -= 3 | 7 |
*= | Multiply and assign | x *= 3 is x = x * 3 | x *= 3 | 30 |
/= | Divide and assign | x /= 3 is x = x / 3 | x /= 3 | 3 |
%= | Remainder and assign | x %= 3 is x = x % 3 | x %= 3 | 1 |
**= | Power and assign | x **= 3 is x = x ** 3 | x **= 3 | 1000 |
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
?? | Default | The value if there is one, otherwise the one on the right | nickname ?? "friend" | friend |
?. | Read through | Reads a field when the value is there, and gives none when it isn't | found?.name | Asha |
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
.. | Range | From the first number up to, but not including, the last | 0..3 | [0, 1, 2] |
..= | Range, end included | From the first number up to and including the last | 0..=3 | [0, 1, 2, 3] |
| Operator | Name | What it does | Example | Result |
|---|---|---|---|---|
& | And | Keeps the bits set in both numbers | 12 & 10 | 8 |
| | Or | Keeps the bits set in either number | 12 | 10 | 14 |
^ | Exclusive or | Keeps the bits set in exactly one of the numbers | 12 ^ 10 | 6 |
~ | Not | Flips every bit | ~0 | -1 |
<< | Shift left | Moves the bits left: each step doubles the number | 1 << 4 | 16 |
>> | Shift right | Moves the bits right: each step halves the number, keeping its sign | 32 >> 2 | 8 |
Comparisons chain with any mix of == != < <= > >= and any number of values: each part compares its two neighbours, every value is worked out once, and the chain stops at the first part that is false. Brackets group as usual: (a == b) is worked out first, to true or false, and that answer is then compared as one value. Whole chains join with and, or and not, with no brackets needed, since comparisons go first: a == b == c or d == e == d. There is no === or !==: == and != already compare contents, and types never mix.
Because a bracket gives a Bool, comparing it with a number is refused: (a < b) < c would ask whether true is less than 3.
fn main() {
const a = 5
const b = 5
const c = 3
print((a < b) < c)
}The compiler refused the program:
kavach run group-chain.kav
error: expected `Bool`, found `Int`Logic is written in words, and, or, not, so a condition reads like a sentence. When operators mix, precedence decides which goes first; when in doubt, use brackets.