Statements
We can think of Hybrix programs as being made of three kinds of layers:
-
Definitions such as
MODULE,CLASS, andTYPE, andFUNCwhich 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 + 1which 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
DROP and LIFT are simple statements that can occur inside 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