Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.3 Variables and expressions

Generic Variables

Variables are used to store data.
Variables can be created with the make keyword.
Variables can be set with the set keyword.

make int number = 5
set number = 10

Variables have a type, this type is set when the variable is created and cannot be changed.
The type of the variable is used to determine what operations can be performed on the variable and what data is stored.

make var value = 10 // 5
set value = "Hello!" // OK

The type of the variable above is var, this is a generic type that can store any type of data.
The value of the variable is first set to 10, then it is set to "Hello!". You can also leave out the type of the variable, in which case it will default to var.

Expressions

Expressions are used to perform operations with variables and values.
here's an example of a simple math expression:

make int number = 5 // 5
make int bigger = number + 5 // 5 + 5
do say (number + bigger) // 5 + 10 = 15

This expression adds 5 to the variable number.
The result of the expression is stored in a new variable bigger.
The value of bigger is now 10, and the value of number has not changed
The expression number + bigger is then passed into the say function.
The say function prints the value of the expression to the console.

Expressions can be used in many places, such as function arguments, variable declarations and more.
Expressions can be used to perform math operations, string operations, boolean operations and more.
here's a more complex example:

make int number = 5
do say (number + 5 * 2 - 10 / 2) // 10

You can observe how the order of operations is followed.
The expression is evaluated from left to right.
For more information about operators and operator precedence, go to 2.2 Operators.

Generic types

Not all types behave the same when used in expressions, a good example are strings.
Strings are blocks of text, So operations like addition and substraction work differently.
here's an example:

make string name = "Robotnik"
do say (name + " is the best") // Robotnik is the best

Additionally, any type can be concatenated to a string.

make int number = 5
do say ("The number is " + number) // The number is 5

You can also use other operators on strings.

make string name = "Robotnik" // 8 characters long
do say (name - 4) // "Robo"

Parentheses

Parentheses can be used to change the order of operations.
here's an example:

make int number = 5
do say (number + 5 * 2 - 10 / 2) // 10
do say ((number + 5) * 2 - 10 / 2) // 15

Parentheses first!