PreviousNext
Help > BASICS > Identifiers > Variables and Data Types
Variables and Data Types

Variable is an identifier which represents some data. It can be read or written, located at a specific location in the memory, or consist of several elements of the same type. In the last case the variable is called “array”.

There are several data types in Rittle:

byte               - 8 bits, integer                    range -128  …  +128

small              - 16 bits, integer                 range -32,768   …   +32,767

int                   - 32 bits, integer                 range -2,147,483,6478   …   +2,147,483,647

big                  - 64 bits, integer                 range -9,223,372,036,854,775,808   …  +9,223,372,036,854,775,807

real                 - 64 bits, floating point      range 10−308 …  10308

text                - unlimited sequence of 8-bit ASCII characters ending with NUL (ASCII code 0)

func                - special type used to pass function names as parameters to functions

any                 - morphing into any supported data type

 

The small type can be declared also as sint16. Similarly, the int type can be declared as sint32. And big can be declared as sint64.

And finally, the byte type can be declared with the more technical sint8.

 

In a typical scenario the types “byte” and “small” have practical meaning only when applied to variables that are directly linked to hardware (i.e. MCU registers for instance). Using them elsewhere in a program offers no considerable improvement in speed or memory consumption, but could be a potential source of problems coming from erroneous use of numbers outside of their limited range.

 

Respectively the type “big” should be used when there is an actual need to work with exceptionally big integer numbers. Therefore, for the majority of application scenarios, especially with the popular 32-bit system architectures, the type “int” is the recommended type for work with integer numbers.

 

The type “any” is most flexible. It can represent data in any other type depending on the context.

Declaring variables is one of the most complex and important aspect of any language, and Rittle makes no exception from that rule. The declaration statement is composed from several sections and the overall format is this:

 

var [role] type name [ [size [:size …]] ] [maxlen [length] ] [at address] [, name …] [= value, …];

 

The parameters “address” and “size” can be only numeric constants or variable names, while “value” can be a composite expression.

role” is optional specific term which will be explained later with the functions.

type” is one of the valid data types.

name” is a unique and valid identifier name.

maxlen” is valid only when the type is text. It defines a maximum length for the variable. Note that the length parameter also needs to be enclosed in square brackets. All operations that produce a longer result will lead to the variable being automatically truncated to the specified maximum length.

The var-statement must be completed with a ‘;’ character, just like any other statement in Rittle;

Let’s consider the declaration in more details.

The first word var informs that one or more variables will be declared in this statement. This is the keyword in Rittle that makes declarations possible.

After var follows an optional section, which tells the compiler where that variable is located in the physical memory. Generally, this refers to specific hardware registers or areas of memory which are fixed by the hardware. Unless there is a very good reason why a variable should be directed to a fixed address, there is no need to use at otherwise.

var byte reg at 0x82a0;

This example declares a variable with name “reg”, which is of type byte and is located at fixed memory address 0x82a0.

Address specifier works with multiple variables by declaring all of them pointing to the same address. It also works with arrays by specifying the initial address of the array.

Declaring arrays is done by using indicating the number of elements in the array:

var real a [10];

This declares an array of 10 elements of type real as a variable with name “a”;

Multi-dimensional arrays are declared in the same way by simply adding the sizes and separating them with a ‘:’ character:

var real a [10:10:10] at 0x400000;

The statement above declares a three-dimensional array of real numbers (1000 elements in total) as variable with name “a”, and the array resides in a fixed memory location starting from address 0x400000;

It is important to remember that indexes for arrays always start from 0. Thus, an array with 10 elements will have valid indexes [0] through [9]. The same applies to multi-dimensional arrays – the index for any dimension always starts from 0.

Variables can be also declared in multiples within the same var-statement, if all of them are of the same type. Their names are separated by a ‘,’ character:

var small second, minute, hour, day, month;

The last section in a var-statement allows variables to be initialised during the declaration. Initialisation values follow after a ‘=’ character. In a simple form:

 var int x = -1;

This statement declares a variable “x” and assigns it a value of -1.

Initialisation of arrays during their declaration is not possible. It is a good practice to have arrays individually declared each in its own “var” statement.