3.CONSTANTS AND VARIABLES
A Programming Language Manual





3.1 About values and types

Programs work with data.
Each data has a value and a type.

Think about numbers. Numbers can be integers (like 1, 2, 3, etc.) or they can be fractional (like 0.5, 1.0, 2.9, etc), also called floats in some programming languages. For the numbers, the type of data is "integer" or "float" and the values are 1, 2, 0.5, 1.0, etc.

This version of GS9 uses only float numbers. For simplicity integer format is supported, so 5 will be in fact 5.0.

The data that can be used in programs is not limited only to numeric types. Programs can also work with text strings, as you have seen in the simple example from the previous chapter. There, the "Hello World!" was the value of a string data.

When used by a program, fixed values like 1, 2.9, or "Hello World!", are called constants.


3.2 Storing data

To work with data, programs must be able to store the data values in some memory locations for later use.

Imagine I ask you to retain the number 2 in your mental memory, and then I ask you to memorize also the number 3. You have just stored two different values in your memory. Now, if I ask you to add both of them and memorize the result, you will use another memory place to store the value 5.

This is also how the programs work. Those memory locations are called variables, because they can store different values during the program's execution. Of course at one moment, there is only one value in such a variable, but later, the program can change it.

Variables are addressed by the program using their given names. If you want to store an amount of money in a variable, you would probably use a representative name for you, like "money" or "my_money". Then, you can write in the program money=10; to store the numeric value of 10 in the "money" variable, for later use. After setting a new value in a variable, the old value that was stored there is lost.

Variable names can include characters like 'a' to 'z' or 'A' to 'Z', digits '0' to '9' and the underline character '_', but they can't start with a digit. GS9 is a case sensitive language, meaning it does matter if you use lower or upper characters. The "Money" and "money" will be two different variables.

 Here are some examples of variable names.

 There are some restricted names, that you can't choose, called "reserved words", because they are used for commands or similar basic instructions.

Because a variable stores a value, you can say the variable has the type of it's value. Changing the value with one of a different type will change the type of the variable too.

In GS9 you declare (create) a variable at the first moment when you set a value to it. Obviously you can store a different value in the same variable at any time. Let's see another program example.

 As before, edit the program.txt file and write the folowing content:

Run the above program and check the results. On 5 lines you should see the numbers 1, 10, 20, 20.


3.3 Basic data types

Let's now have a look at the basic data types that can be used in GS9 language.

NUMBER

GS9 uses float numbers, but they can be specified in the integar format too.
Examples of numbers: 1, 5, 60, -1.0, 0.0, 0.01, 0.5, 1000.0
To convert other types to a number you can use the NUM operator like in the folowing example.




STRING

String types are used to work with text data.
They are represented by a series of characters, between quotation marks.
Examples: "Hello World!", "some text"
An empty string is equal to "" and has no visible characters inside.
Escape characters are supported ( \\, \r, \n, \t, \", meaning back slash, return carret, new line, tab and quote).
To convert other types to strings you can use the STR keyword.
Even more, you can specify an optional conversion parameter that will format the result using leading zeros or the specified number of decimals. See the flowing example.



TABLE

Tabels are used to store lists of data.
Examples: [1,2,3], ["one","two","three"], [ ]
The number of elements from a table gives the table's size.
The data elements inside a table can be addressed by indexing with a special indexing operator [ ].

  Let's see some relevant examples.

So a table works like an indexed list of variables.
Note that the indexes in a table start with 0, for the first element, and end with the size-1 (elements count - 1), for the last element. You can create an unitialized table using TAB keyword, and giving the size of the table as a parameter. You can't convert other types to a table.

  Here are two graphic examples.


3.4 Special data types

In GS9 language, there are a few other data types, used only on special occasions.

POINTER

This is a special data type, used to store some specific application data, like file handlers, or memory buffers. The only value that can be set to pointer variables is NULL, that is the equivalent of a zero value.

Ignore it for now, because you will not find use of it, until the library chapter.

FUNCTION

When you decalred the "main" function a global variable is in fact declared with this name, addressing the function's content. You can also declare other function variables, give them values of previous declared functions and use them to call these functions.

A function value that doesn't point to any valid function has the value NULFNC.

More about this in the Functions chapter.

OBJECT

This is another data type you can ignore for now.

It is used to access special objects exported from code and it supports members accessing in a C++ style, like myobj.member=1, or method calls like myobj.Method().

An object value that doesn't point to any valid object has the value NULOBJ.
For advanced details see the Embedding applications chapter.


You can use the typeof instruction to find the type of a variable and the sizeof instruction to find the size in elements of a string or table. Some numeric variables are provided for elegant type checking: TYPE_NUM, TYPE_STR, TYPE_TAB, TYPE_PTR, TYPE_OBJ, TYPE_FNC.

In the next chapter we'll have a little discution about other forms of writing numbers and representing data.