Constants and variables

Variables

A variable is a primitive object, with a defined type, and a content that may be changed providing the type is the same.

Scriptol primitives can have these types:
number any kind of number (until the "double" of C++).
integer a 32 bits number rounded to the integer part.
int shortcut for integer.
natural unsigned 64 bits integer.
real a number with decimals.
boolean the true or false value.
text a string of characters.
dyn generic element of array or value of dict.
file a file on disk.

Additional types are also used to declare C++ object from included libraires.
char
byte
cstring is a C string of characters and is converted to "char *".
The * symbol denotes the content of a variable.

Primitives have constructors as other objects.
int(int | number | text)

Example:
int("123")

this is just the 123 number, not a value returned by the constructor, but the object itself.

Example:
text(10)
is the string "10".

When converted to Php, any variable become dynamic and is prefixed by a $. But the type is required by the Scriptol compiler for a right conversion into Php or C++.

Constant

A constant is a name given to a numerical value or a text. This is similar to a variable, but the content can't change.

Examples ofconstants:
constant int x = 1
constant boolean b = true
constant text t = "some text"

Declarations

A constant is declared as a variable, with the "constant" modifier at start. A value must be assigned at declaration.

Here is the list of simples types you can use. Simple type are named "scalars". There are also compound type built from simple types, but this will be the topic of the next chapter.

A variable is declared by the type followed by the name.
Examples of variables:
number a
integer x
int x
real z1
text mytext

In most cases, the variable must by initialized at declaring.
A literal value is assigned.
Example:
int x = 5
real pi = 3.14

A literal text is written between simples or doubles quotes.
In the first case this is a simple string of characters.
text t = 'abc$xyz'
This is just the string of these characters: a,b,c,$,x,y,z

In the second case, some symbols have a special effect at runtime.
text xyz = "def"
text t = "abc$xyz"
Inside doubles quotes, le $ symbol preceeds a name of variable included into the text and that will be replaced by the Php interpreter at runtime (not the C++ compiler).
The t variable is assigned characters a,b,c followed by the content of the xyz variable, and thus the a,b,c,d,e,f string is assigned rather than abc$xyz.
This may be written also:
text t = "abc" + xyz

In the same manner the {} symbols have a special role, but this is not useful in Scriptol.

Multiple declarations

Providing they have the same type, several variables may be declared at once.

The syntax is:
  type nom [= value] [, nom [= value] ] *

This means for:
- type of the variables,
and for each variable:
- the comma separator,
- the identifier
- optionally the equal sign to assign an expression

Example:

 int x = 1, y, z = 2

Predefined constants and variables

In PHP, variables are prefixed by a $. To access directly a Php variable, this syntax is kept in Scriptol.
For a constant, Scriptol uses also $, but the identifier is between parenthesis.

These conventions are used also for C++ generating.

Example array a = $argv
int x = $(PAD_LEFT)

 Exercises

 

1) Here is a list of values:
1000, "hello", 10.5, 50, 60n, "bye", 99
Do create the corresponding variables with names a,b,c, etc...
Display the content.

Answer