Operators

Arithmetic

OperatorOperationExample
+Addition3 + 47
-Subtraction10 - 37
*Multiplication4 * 520
/Division (float)7 / 23.5
//Integer division7 // 23
%Modulo7 % 31
**Exponentiation2 ** 8256
- (unary)Negate-5-5

Comparison

OperatorMeaningExample
==Equal3 == 3yep
!=Not equal3 != 4yep
<Less than2 < 5yep
>Greater than5 > 2yep
<=Less or equal3 <= 3yep
>=Greater or equal4 >= 5nope

Logical

OperatorMeaningExample
andBoth trueyep and nopenope
orEither trueyep or nopeyep
notInvertnot yepnope

Short-circuit evaluation: and stops at the first nope, or stops at the first yep.

Bitwise

OperatorMeaningExample
&Bitwise AND0b1010 & 0b11000b1000
`\`Bitwise OR`0b1010 \0b01010b1111`
^Bitwise XOR0b1010 ^ 0b11000b0110
~Bitwise NOT~5-6
<<Left shift1 << 416
>>Right shift16 >> 24

Pipeline |>

Passes the left side as the first argument to the right side:

lhj
forge result = [3, 1, 2] |> sorted |> reversed

Equivalent to:

lhj
forge result = reversed(sorted([3, 1, 2]))

Pipelines chain left-to-right, which reads more naturally for data transforms.

Range ..

Creates an inclusive integer range:

lhj
forge r = 1..10    ## 1, 2, 3, ..., 10
march i in 1..5:
    echo i         ## 1 2 3 4 5

Arrow assignment <-

Reassigns a variable:

lhj
forge x = 1
x <- x + 1

Membership in

Tests whether a value is in a sequence:

lhj
echo 3 in [1, 2, 3, 4]        ## yep
echo "key" in {"key": "val"}   ## yep
echo "x" in "lexer"            ## yep

Operator precedence

From highest to lowest:

  1. () — grouping
  2. ** — exponentiation (right-associative)
  3. Unary -, not, ~
  4. *, /, //, %
  5. +, -, .. (range)
  6. <<, >>
  7. &
  8. ^
  9. |
  10. ==, !=, <, >, <=, >=, in
  11. and
  12. or
  13. |> — pipeline (lowest)

When in doubt, use parentheses.