The Hyper Programming Language

Binary Operator Expressions

General

Binary operator expressions are expressions that consist of a left operand, followed by a binary operator and a right operand. An important thing to note is that the two binary operators that work on pointers behave different from the other binary operators. Those two operators required pointer types as operands, while the other operators expect non-pointer types.

Binary operators

Hyper has the following binary operators:

Operator Type Short-circuit Meaning
=$ Pointer No Pointer equality
!$ Pointer No Pointer inequality
+ Non-pointer No Addition
- Non-pointer No Subtraction
* Non-pointer No Multiplication
/ Non-pointer No Division
% Non-pointer No Modulo
& Non-pointer Yes Logical AND
| Non-pointer Yes Logical OR
-> Non-pointer Yes Logical consequence
= Non-pointer No Equality
!= Non-pointer No Inequality
< Non-pointer No Less than
<= Non-pointer No Less than or equal
> Non-pointer No Greater than
>= Non-pointer No Greater than or equal

The first two operators in the table, '$$' and '!$' are pointer operators, and they require their operands to have a pointer type. If a non-pointer type is used, then that operand is required to be addressable and then the compiler automatically takes the address of that expression to use for the pointer operator. All the other operators in the table work on non-pointer types, and automatically dereference any operand pointer type.

The '=' operator is the equality operator, and is not an assignment. The difference between '=' as assignment or as equality is based on the fact that an expression is not a statement.

The '&' and '|' operators are short-circuited boolean operators, like their counterparts '&&' and '||' from Java and C++. Their left operand is guaranteed to be evaluated before the right operand, and evaluation of the right operand is skipped in case the result of the operation is dominated by the left operand (false for '&' and true for '|'). Hyper does not have bitwise operators.

The '->' operator is also a short-circuited boolean operator. It expresses logical consequence. The expression a -> b is equivalent to !a | b. This means that the left operand is guaranteed to be evaluated first, and that the right operand is only evaluated if the left operand evaluates to true.

Short-circuited operators
Operator Left operand Right operand Result
& true true true
true false false
false not evaluated false
| false false false
false true true
true not evaluated true
-> true true true
true false false
false not evaluated true

Example

procedure p()
  var a & b & c & d : int
  a = 12
  b = 9
  c = a + 8 * b   # c becomes 12 + (8 * 9) = 12 + 72 = 84
  d = c - a / b + a % b   # d becomes 84 - (12 / 9) + (12 % 9) = 84 - 1 + 3 = 86
  var p : * int = b   # p points to b
  var s : bool
  s = a = b  # s becomes false, because a is not equal to b
  s = a > b  # s becomes true, because 12 > 9
  s = p !$ c  # s becomes true, because p doesn't point to c
  s = p =$ b  # s becomes true, because p points to b
  s = p =$ 9  # ERROR : '9' is not addressable
  s = p = 9   # s becomes true, because p points to b, which contains the value 9
  s = p =$ b -> p = b  # s becomes true, because true -> true = true
end

See also


Valid HTML 4.01 Strict Valid CSS!