website is under construction


An operator is something that takes one or more values (or expressions) and yields another value (so that the construction itself becomes an expression)

Operators can be grouped according to the number of values they take. Unary operators take only one value, for example ! (the logical not operator). Binary operators take two values, such as the familiar arithmetical operators + (plus) and - (minus). The majority of Ghost's operators fall into this category.

Precedence and Associativity

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication (*) operator has a higher precedence than the addition (+) operator. Parentheses may be used to force precedence, if necessary. For example, (1 + 5) * 3 evaluates to 18.

When operators have equal precedence their associativity decides how the operators are grouped. For example, = is left-associative, so 1 - 2 - 3 is grouped as (1 - 2) - 3 and evaluates to -4. = on the other hand is right-associative, so a = b = c is grouped as a = (b = c).

Use of parentheses, even when not strictly necessary, can often increase readability of the code by making grouping explicit rather than relying on the implicit operator precedence and associativity.

The following table summarizes the operator precedence in Ghost, from highest to lowest. Operators in the same box have the same precedence.

1() .Grouping, Method callLeft
3* / %Multiply, Divide, ModuloLeft
4+ -Add, SubtractLeft
5< <= > >=ComparisonLeft
6== !=Equals, Not equalLeft
7andLogical andLeft
8orLogical orLeft

Arithmetic Operators

Remember basic arithmetic from school? These work just like those.

-aNegationOpposite of a.
a + bAdditionSum of a and b.
a - bSubtractionDifference of a and b.
a * bMultiplicationProduct of a and b.
a / bDivisionQuotient of a and b.
a % bModuloRemainder of a divided by b.

The result of the modulo operator (%) has the same sign as the dividend - that is, the result of a % b will have the same sign as a. For example:

print(5 % 3) // >> 2
print(5 % -3) // >> 2
print(-5 % 3) // >> -2
print(-5 % -3) // >> -2

Assignment Operator

The assignment operator is =. This declares and assigns the value of the expression on the right.

message = "Hello, world!"

print(message) // >> Hello, world!

Comparison Operators

Comparison operators, as their name implies, allow you to compare two values.

a == bEqualtrue if a is equal to b.
a != bNot equaltrue if a is not equal to b.
a < bLess thantrue if a is less than b.
a > bGreater thantrue if a is greater than b.
a <= bLess than or equal totrue if a is less than or equal to b.
a >= bGreater than or equal totrue if a is greater than or equal to b.