Skip to content

Operators

ataylorg edited this page Nov 28, 2017 · 3 revisions

Assignment Operators

Operator Explanation
+= Adds the two operands together, and then assign the result of the addition to the left operand.
-= Subtract the right operand from the left operand, and then assign the result of the subtraction to the left operand.
*= Multiply the two operands together, and then assign the result of the multiplication to the left operand.
/= Divide the left operand by the right operand, and assign the result of the division to the left operand.
%= Perform modular division on the two operands, and assign the result of the division to the left operand.
<<= Perform a left shift operation on the left operand, shifting by the number of bits specified by the right operand, and assign the result of the shift to the left operand.
>>= Perform a right shift operation on the left operand, shifting by the number of bits specified by the right operand, and assign the result of the shift to the left operand.
&= Perform a bitwise conjunction operation on the two operands, and assign the result of the operation to the left operand.
^= Performs a bitwise exclusive disjunction operation on the two operands, and assign the result of the operation to the left operand.
|= Performs a bitwise inclusive disjunction operation on the two operands, and assign the result of the operation to the left operand.

Incrementing and Decrementing

The increment operator ++ adds 1 to its operand. The operand must be either an int, float, Image or Pixel. If the ++ is before the operand as in ++i * 5, ++i is incremented before the other operation. If the increment operator is after such as in i++ * 5, then the increment happens after.

int i = 2;
Console.log(i++ * 5) // Prints 10
i = 2;
Console.log(++i * 5) // Prints 15

Arithmetic Operators

The following arithmetic operators are utilized in PixMix

+ addition
- subtraction
* multiplication
/ division
% modulo
++ prefix and postfix incrementation
-- prefix and postfix decrementation
() control arithmetic preference

Comparison Operators

For comparing equivalent values, the keyword is is used. Conversely, to determine if two values are not equal, the keyword not is used. Greater than, less than, and their variants use standard symbols (<, >, <=, >=).

int x = 1;
int y = 3;

if x is y
    Console.log(“X is the same as Y”);
if x not y
    Console.log(“X and Y are not the same”);
if x <= y
    Console.log(“X is less than Y”);

Logical Operators

The conjunction operator in PixMix is the keyword and while the disjunction operator is the keyword or. To negate a logical expression, use the keyword not. Logic can be nested using parentheses.

if x is 5 and y > 3
    Console.log(“X is 5 and Y is greater than 3);
if x is 5 or y is 3
    Console.log(“X is 5 or Y is 3);
if x is 5 and (y is 3 or z is 5)
    Console.log(“X is 5 and either Y is 3 or Z is 5);

Bit Shifting

The left-shift operator << is used to shift its operand’s bits to the left, while the right-shift operator >> shifts to the right. The second operand denotes the number of bit places to shift by. Bit shifted off the left or right sides are discarded.

int x = 47; 	// x is 00101111 in binary
x << 1;		// x is 01011110 in binary, or 94 in decimal
x >> 1;		// x is 00101111, or 47, again

Bitwise Logical Operators

& Conjunction -- When both bits are 1, the result is 1, otherwise it’s 0.
10110 & 10101 = 10100
| Inclusive Disjunction -- When both bits are 0, the result is 0, otherwise it’s 1.
10110 | 10101 = 10111
^ Exclusive Disjunction -- When bits are different, the result is 1, otherwise it’s 0.
10110 ^ 10101 = 00011
~ Negation -- Reverses each bit, 1s become 0s, 0s become 1s.
~001011 = 110100

Type Casts

You can use a type cast to explicitly cause an expression to be of a specified data type. A type cast consists of a type specifier enclosed in parentheses, followed by an expression. To ensure proper casting, you should also enclose the expression that follows the type specifier in parentheses.

float x;
int y = 7;
int z = 3;
x = (float) (y / z);

Member Access Expressions

You can use the member access operator . to access the members of a structure or union variable. You put the name of the structure variable on the left side of the operator, and the name of the member on the right side.

Object point = {
    int x, y;
}

point.x = 0
point.y = 5

Conditional Expressions

Pixmix uses a Java-like ternary operator. The following code:

bool x = true

if true
    Console.log("True")
else
    Console.log("False")

can be written using the conditional operator as:

bool x = true
Console.log(x ? "True" : "False") // True would be printed to the console

Operator Precedence

When an expression contains multiple operators. The operators are grouped based on rules of precedence. The order of precedence in PixMix follows the order of operations in C. Notably, PixMix evaluates from right to left when multiple assignment statements appear as subexpressions in a single larger expression.

The following is a list of types of expressions, presented in order of highest precedence first. Sometimes two or more operators have equal precedence; all those operators are applied from left to right unless stated otherwise.

  1. Function calls, array subscripting, and membership access operator expressions.
  2. Unary operators, including logical negation, bitwise complement, increment, decrement, unary positive, unary negative and type casting. When several unary operators are consecutive, the later ones are nested within the earlier ones: !-x means !(-x).
  3. Multiplication, division, and modular division expressions.
  4. Addition and subtraction expressions.
  5. Bitwise shifting expressions.
  6. Greater-than, less-than, greater-than-or-equal-to, and less-than-or-equal-to
  7. expressions.
  8. Equal-to and not-equal-to expressions.
  9. Bitwise AND expressions.
  10. Bitwise exclusive OR expressions.
  11. Bitwise inclusive OR expressions.
  12. Logical AND expressions.
  13. Logical OR expressions.
  14. Conditional expressions (using ?:). When used as subexpressions, these are evaluated right to left.
  15. All assignment expressions, including compound assignment. When multiple assignment statements appear as subexpressions in a single larger expression, they are evaluated right to left.