Variables in Alusus is just a places that allocated in the memory for store data when we execute the program. And the type we
gave it to the variables determines the data type that could be stores in the allocated memory for that variable.
To defined a variable we use the reserved word `def` followed by the variable name then a `:` and finally the variable's name.
def variable_name: Data_type;
def variable_name: Data_type = value;
variable_name = value
import "Srl/Console.alusus"; use Srl; def age: Int = 40; Console.print("I am %d years old.\n", age); /* I am 40 years old. */
The basic types for variables in Alusus are almost the same as any programming language, and they are:
`Bool`, `Float`, `Char`, `Word`, `Int`, in addition to `ArchInt` and `ArchWord`.
Note: Alusus defines `int` as an alias for `Int`, which means there is not difference between `int` and `Int` when defining an integer variable, and the same holds for other basic types, which means `char` is an alias for `Char` and `word` is an alias for `Word` etc.
def my_number: int // 32 bits
def my_int_number: int[16] // 16 bits
def my_int_number: ArchInt;
import "Srl/Console.alusus"; use Srl; def a: Int = 3; // define an integer variable and assign a value directly to it. def b:int; // define another integer variable. b=8 // assign the value 8 to variable `b` def sum:int=a+b; // store the result of sum in a new integer variable. Console.print(sum); // print the result
def my_float_number: float = value;
import "Srl/Console.alusus"; use Srl; def x1: float[64]=0.3; def x2: float[64]=0.6; def sum: float[64]=x1+x2; Console.print("x1+x2 = %f",sum); // x1+x2 = 0.900000
Console.print("x1+x2 = %0.1f",sum); // x1+x2 = 0.9 Console.print("x1+x2 = %0.2f",sum); // x1+x2 = 0.90
import "Srl/Console.alusus"; use Srl.Console; def float_num: float; // by default it will be 32 bit float_num=getFloat (); // enter the number print("%f", float_num~cast[Float[64]]); // cast on print
def a: char = 65; Console.print("%c", a); // A
Note: It is also possible to define constants in the same way, but putting the value itself instead of the type, this possible for integers, floating point numbers, and strings. As shown in the next example:
def hello: "Hello World"; def pi: 3.141592; def daysPerWeek: 7;
Class types are types defined by the user (or a standard library) using the `class` keyword. Among the most important class types are `Array` and `String`. Instances of class types are called objects. We will discuss these types later in this tutorial.