Skip to main content

Statements

We can think of Hybrix programs as being made of three kinds of layers:

  • Definitions such as module, class, and type, and func which describe the overall structure of your program.

  • Statements such as if, loop, and --> which are the step-by-step actions performed by your program.

  • Expressions such as x + 1 which are parts of statements.

Expressions always go inside statements, and statements always go inside definitions. func definitions always go inside class or module definitions.

Below is a small complete program illustrating these distinctions:

class rectangle               # class definition
var width: int # variable definition
var height: int # variable definition

func get_area(): int # function definition
var area: int # variable definition
# ".width * .height" is an expression
.width * .height -> area # assignment statement
return area # return statement
end func
end class

module main # module definition
func start() # function definition
var r: rectangle # variable definition
# "new rectangle()" and "r" are expressions
new rectangle() -> r # assignment statement
# "3" and "r.width" are expressions
3 -> r.width # assignment statement
5 -> r.height # assignment statement

console::init() # function call statement
console::print_int( # function call statement (spans 3 lines)
| r.get_area()
|)
end func
end module

Let's summarize the kinds of Hybrix statements.

Assignment statement

Example: x + z -> z

Assignment statements use the -> and <- arrow operators. The arrow points to a variable where the value will be stored. In this documentation, we usually show the arrows pointing to the right, but the direction does not affect the meaning. For example:

module main
func start()
var x: int

# Put six into the variable x
1 + 2 + 3 -> x

# Put six into the variable x
x <- 1 + 2 + 3
end func
end module

The arrows can be as long as you like. This also does not affect the meaning:

module main
func start()
var x: int

# Put six into the variable x
1 + 2 + 3 -------------------> x

# Put six into the variable x
x <------------------------------- 1 + 2 + 3
end func
end module

Many programming languages use = for assignments. In Hybrix, = has a very different meaning: it is used to test whether two values are equal.

For example, if we write x = 1, it does not change the value of x. It asks "Does x equal 1?" If so, then x = 1 has the value true; otherwise, the value is false. Also, x = 1 is an expression, not a statement—you cannot write x = 1 on a line by itself.

This distinction is especially clear when you consider an assignment such as x + 1 --> x. It means "get the value of x, add one, and put the sum back into x." This statement increases the value of x by one. Whereas x + 1 = x will always be false, because it's impossible for a number to equal itself plus 1.

Function call statement

Example: console::print("hello")

A function call can be a statement by itself. This is possible even if the func normally returns a value; the return value will simply be discarded:

module main
# Define a function called "get_max":
func get_max(x: int, y: int): int

# If x is bigger, then x is the result
if x > y
do return x

# Otherwise y is the result
return y
end func

func start()
var n: int

# Assignment statement containing a function call expression
main::get_max(1, 5) -> n

# Function call statement; the return value is discarded
main::get_max(1, 5)
end func
end module

Return statement

Example: return y

Above, the statement return y causes the function get_max() to return, using y as the return value. See Functions for details.

Loop, drop, lift

Example:

module main
func start()
var i: int
0 -> i

console::init()

# This loop prints: "1 3 5 7 9"
loop
i + 1 -> i

if i % 2 = 0
do lift # skip even numbers

if i > 10
do drop # stop after 10

console::print_int(i)
console::print(" ")
end loop

end func
end module

The drop and lift statements are used inside a loop compound statement. See Loops for details.

If statements

Example:

. . .
if x = 1 then
console::print("one")
elsif x = 2
console::print("two")
else
console::print("many")
end if
. . .
. . .
if x = 1
do console::print("one")
. . .

if statements are explained in the If statements section.

Break statement

The break statement is used for debugging. It causes the Hybrix debugger to stop on that line, just like a breakpoint.

Example:

module main
func start()
var x: int
1 -> x

break

2 -> x
end func
end module