Conditionals
You can use conditionals to control the flow of the program by taking advantage of builtin functionality. Of course, the simplest and most straightforward way to do this is to use if-else statements, in the form of @if
, @else
, and @elseif
. They run whatever code is in their block (between the curly braces) if the condition given turns up true.
Basic conditions
These work mostly like they do in other languages, but a couple notes is that you do not need brackets, and it doesn't support mathematical comparisons in the usual way. You can get around the latter by using a boolean builtin function, which we will expand on.
Also, instead of the or
and and
logical operators being ||
and &&
, they are |
and &
instead, and the negation operator is !
. The order of operations is not
then and
then or
, and you can use square brackets to control the order further.
First here's an example:
<SWITCHES>
# Arbitrary switches for reference in conditions #
$do_1
$do_2
<MACRO>
# @if, @elseif, @else blocks #
@if $do_1 & $do_2 {
# This happens if $do_1 and $do_2 is true #
}
@elseif $do_1 | $do_2 {
# This happens if $do_1 or $do_2 is true #
}
@elseif $do_1 | !$do_2 {
# This happens if $do_1 is true or $do_2 is false #
}
@elseif [$do_1 & !$do_2] | $do2 {
# This happens if $do_1 is true and $do_2 is false, or $do2 is true #
}
@else {
# Happens if none of the other conditions are true #
}
How to do mathematical comparisons
You can use @e
, a builtin boolean function, which means that it returns true or false, and it can only be used within the condition part of an @if
or @elseif
statement.
As @e
is implemented, which is relatively easy to customize, it uses the expr-eval package. @e
takes one argument and has expr-eval
interpret it as an expression.
They use and
, or
, and not
as the logical operators, literally written out. They also have the standard ==
, <
, >
, <=
, >=
comparison operators.
One thing to note about @e
and conditional expressions in general is that they're stripped of all their spaces, so the whitespace and new lines do not affect how it is interpreted.
You can check their documentation for more details and functions you can use within their expressions, but for the most important basics, here are some examples of it in use:
<VECTORS>
$my_num1 = 5
$my_num2 = 10
$my_vector = 0,1,2,3,4,5
<MACRO>
@if @e[$my_num1 < $my_num2] {
# Will run if $my_num1:0 is less than $my_num2:0 #
# 5 < 10, so this runs #
}
@if @e[$my_num1 + 5 == $my_num2] {
# Will run if $my_num1:0 + 5 is equal to $my_num2:0 #
# 10 == 10, so this runs #
}
@if @e[$my_num1 == 5 and $my_num2 == 10] {
# Will run if both $my_num1 is 5 and $my_num2 is 10 #
# Both are true, so this runs #
}
@if @e[$my_num1 < $my_vector:2] {
# Will run if $my_num1:0 is less than $my_vector:2 #
# 5 is not less than 2, so it does not run #
}