Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.6 Extensions and loops

Extensions

Extension keywords are used to extend the functionality of a line of code.
There are 10 extensions:

when
if
else
while
for
then
catch
unless
until

Extensions are used to do conditional statements, loops and error handling.
Extensions are always followed by some kind of expression or a block of code.

when and else

when is used to create a conditional statement.
when is followed by a boolean expression, a block of code and an optional else block.
The boolean expression is evaluated and if it is true, the block of code is executed.
If the boolean expression is false, the else block is executed.
Example of a when statement with an else extension:

do {
    do say("Access granted")
} when (password == "1234") else {
    do say("Access denied")
}

when encapsulates the entire block of code, if the condition is true, the block is executed.
Otherwise, nothing before it is executed.

do say("Will not run") then say("Will also not run") when false
do say("Will run") when true

while loops

while is used to create a loop.
while is followed by a boolean expression and a block of code.
The boolean expression is evaluated and if it is true, the block of code is executed.
The boolean expression is evaluated again and if it is true, the block of code is executed again.
This process repeats until the boolean expression is false.
Example of a while loop:

make int i = 0
do {
    do sayln(i)
    set i++
} while i < 10

This loop will print the numbers 0 to 9 to the console.
The variable i is incremented by 1 each time the loop is executed.
The loop will stop when i is equal to 10.

Just like with when, while encapsulates the entire block of code.

make int i = 0
do sayln(i) then {set i++} while i < 10

for

do sayln(i) for i in range(100)

for loops are used to iterate over an array, in this example, the range(100) function returns an array of numbers from 0 to 99.
The for loop will iterate over the array and assign the current value to the variable i.
The variable i is then printed to the console.
The variable i is created for the loop body, and is not accessible outside of the loop.
You can not use a variable, which means the loop will just iterate over the array and do nothing with the value:

do sayln("Hello world!") for range(10)

then

then is used to immediately execute a function after the previous function has finished.
then is followed by a function call.
Example of a then statement:

do sayln("Hello world!") then sayln("Hello again!")

This will print Hello world! and then Hello again! to the console.
The second say function is executed after the first say function has finished.

if

if condition then sayln("Access granted") else if other_condition sayln("AdminAcess granted")

If is also a master keyword, but behaves as an extension when used after an else.
if is used to create a conditional statement.
if is followed by a boolean expression, a then extension and an optional else extension.
The boolean expression is evaluated and if it is true, the block of code is executed.
If the boolean expression is false, the else block is executed.
If only encapsulates the next statement, not the entire block.

if true then sayln("Will print when the condition is true") then sayln("Will always print regardless")
make bool result = listen("True or False?\n") == "true" 
if result then {
    do say("Hello, World!")
} else if !result then {
    do say("Goodbye, World!")
} else {
    do say("This should never happen.")
}

catch

catch is used to catch errors.
catch is followed by a block of code.
Example of a catch statement:

do {
    do say(undfined_variable) // This will throw an error
} catch {
    do say("An error occured!\n Code: " + _)
}

If an error occurs in the block of code, An error occured! will be printed to the console.
The error code is stored in the _ variable.
More information about error handling can be found in 1.10 Error handling.

Extension chaining

Extensions can be chained together.
Example of a chained statement:

do {
    do say("Access granted")
} then login() when password == "1234" else {
    do say("Access denied")
}

This will call the login function after the say function has finished.
This only happens when when is true, the when statement includes the entire block
If the password is not 1234, Access denied will be printed to the console.

Info!
You can learn more about extensions in 2.3 Extensions