6. Operations

Jam supports a large set of standard operations. These operations link directly to methods defining their behaviour.

There are two kinds of operators, binary and unary. Binary operations require exactly two values, while unary require exactly one.

6.1. Operators

Binary Operator Meaning
== Equality
!= Inverse Equality
< Smaller than
<= Smaller than or equal to
> Larger than
>= Larger than or equal to
is Identity Equivalence
!is Inverse Identity Equivalence
+ Addition
- Subtraction
* Multiplication
/ Division
// Integer Division
** Exponentiation
% Modulo
in Containment
!in Inverse Containment
&& Logical And
|| Logical Or
^^ Logical Exclusive Or
& Bitwise And
| Bitwise Or
^ Bitwise Exclusive Or
: Type of
Unary Operator Meaning
! Logical Not
~ Bitwise Inverse

6.2. Order of Operations

For Binary Operations, Order of Operations is ranked using the PEDMAS system. The operations in descending rank order are Parenthesis, Exponents, Division, Multiplication, Addition, Subtraction. A group of operations of the same rank will be executed from left to right.

6.3. Indexing

Indexing is an operation on a value that maps one or more values to another value. This includes using an integer to map the index of an element in an array to the element itself.

6.4. Type of

The “Type of” operation is a constraint on the type system that the LHS must be of the type given on the RHS. This can be used to override type inference for Variables.

6.4.1. Syntax

UnaryOperator        ::=  "~" | "!"
MathematicalOperator ::=  "+" | "-" | "*" | "/" | "//" | "**" | "%"
EquivalenceOperator  ::=  "==" | "!="
RelationalOperator   ::=  "<" | "<=" | ">" | ">="
IdentityOperator     ::=  "is" | "!is"
ContainmentOperator  ::=  "in" | "!in"
LogicalOperator      ::=  "&&" | "||" | "^^"
BitwiseOperator      ::=  "&" | "|" | "^"
ComparisonOperator   ::=  EquivalenceOperator | RelationalOperator | IdentityOperator
ComparisonOperation  ::=  Value [ ComparisonOperator Value ]+
Operator             ::=  MathematicalOperator | ComparisonOperator | ContainmentOperator | LogicalOperator | BitwiseOperator
BinaryOperation      ::=  ComparisonOperation | ( Value BinaryOperator Value )
UnaryOperation       ::=  [UnaryOperator] Value
IndexOperation       ::=  Value "[" [ Value "," ]* Value "]"
Operation            ::=  UnaryOperation | BinaryOperation | IndexOperation

6.4.2. Examples

6.5. Mathematical

# Addition
size = 5 + 3 #=> 8
total_volume = 6.8 + 2.3 #=> 9.1

# Subtraction
size = 3 - 5 #=> -2
total_volume = 6.8 - 2.3 #=> 4.5

# Multiplication
area = 5 * 3 #=> 15
depreciation_rate = 2 * -5.3 #=> -10.6

# Division
ratio = 5.0 / 2.0 #=> 2.5

# Integer Division
portion = 10 // 3 #=> 3
half = 8 // 4 #=> 2

# Exponentiation
y_coord = x_coord**2
decay_rate = 2**-2 #=> 0.25

# Modulo
TODO

6.6. Equivalence

# Equality
if num_sides == 4
    puts("Shape is a square")
end

# Inverse Equality
if num_eyes != 2
    puts("Not human")
end

6.7. Relational

# Smaller than
while count < 10
    puts(count)
    count += 1
end

# Smaller than or equal to
#TODO

# Larger than
if score > high_score
    high_score = score
end

# Larger than or equal to
#TODO

6.8. Identity

# Identity Equivalence
#TODO

# Inverse Identity Equivalence
#TODO

6.9. Containment

#TODO

6.10. Logical

# And
if total_score >= 50 && final_exam_score >= 50
    return true #passed
else
    return false #failed
end

# Or
if rank > 5 || difficulty_setting == 2
    difficulty = 2
end

# Exclusive Or
#TODO

# Logical Not
#TODO

6.11. Bitwise

# And
#TODO

# Or
#TODO

# Exclusive Or
#TODO

# Bitwise Inverse
TODO

6.12. Indexing

# Arrays
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
fifth_prime = primes[4]

# Associative Arrays
emergency_numbers = {
    $AUS -> "000"
    $USA -> "911"
}
aus_emergency_number = emergency_numbers[$AUS]

6.13. Type of

result:Int = long_math_function() # Ensures `result` is of type `Int`