Types and Values
Scripting Language is dynamically typed. This means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type.
All values are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.
There are 5 basic types: null, Boolean, number, string, and table. The type null has one single value, null, whose main property is to be different from any other value; it often represents the absence of a useful value. The type Boolean has two values, false and true. false make a condition false while null will make a condition true; Any other value also makes a condition true.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because scripting language is case sensitive.
By default, all variables are global
Defining variables of any type is as simple as expressions below:
name = ‘Jon Doe’
age = 35
ownsHouse = true
name, age = 'Lucy Doe', 34
Local variables can be declared anywhere inside a block. The declaration can include an initialization:
local name = ‘Lui’
Arrays
Arrays are an ordered arrangement of objects, which may be a one-dimensional array containing a collection of rows or a multi-dimensional array containing multiple rows and columns.
In Rival Scripting language, arrays are implemented using indexing tables with integers.
Indexing starts from 1 for arrays defined with in Script.
Indexing starts from 0 for resultant arrays returned of any of the available library array functions.
The size of an array is not fixed, and it can grow based on our requirements, subject to memory constraints.
Single/One Dimensional
A one-dimensional array can be represented using a simple table structure and can be initialized and read using a simple for loop. An example is shown below:
arrayOfAnimals = {"Cow", "Goat"}
secondArray = {}
for i = 0, 2 do
secondArray[i] = arrayOfAnimals[i]
end
for i= 0, 2 do
log(secondArray[i])
end
When we run the above code, we wil get the following output.
null, Cow, Goat -- There is nothing at index 0 while we are trying to log/print it as arrayOfAnimals evetually gets translated to a table {1: "Cow", 2: "Goat"}
To avoid these nulls in a script like above, it is advised as a best practice to explicitly define the index if you are defining and initializing arrays in the script. Here is how the above script will look like with explicity indices
arrayOfAnimals = {[0]= "Cow", "Goat"}
secondArray = {}
for p = 0, 1 do
secondArray[p] = arrayOfAnimals[p]
end
for q= 0, 1 do
log(secondArray[q]) -- Prints Cow, Goat
end
log(secondArray) -- Prints {0: "Cow", 1: "Goat"}
Multi Dimensional
Multi-dimensional arrays can be implemented in two ways.
Array of arrays
Single dimensional array by manipulating indices
Example (Array of arrays):
-- Initializing the array
array = {}
for i=0,3 do
array[i] = {}
for j=0,3 do
array[i][j] = i*j
end
end
-- Accessing the array
for i=0,3 do
for j=0,3 do
log(array[i][j])
end
end
Example (Manipulating Indices):
-- Initializing the array
array = {}
maxRows = 3
maxColumns = 3
for row=1,maxRows do
for col=1,maxColumns do
log[row*maxColumns +col] = row*col
end
end
-- Accessing the array
for row=1,maxRows do
for col=1,maxColumns do
log(array[row*maxColumns +col])
end
end
As you can see in the above example, data is stored based on indices. It is possible to place the elements in a sparse way and it is the way implementation of a matrix works in Rival Scripting language.
Operators
An operator is a symbol that tells the interpreter to perform specific mathematical or logical manipulations. Rival Scripting language provides the following type of operators −
Arithmetic Operators
Relational Operators
Logical Operators
Misc Operators
This tutorial will explain the arithmetic, relational, logical, and other miscellaneous operators one by one.
Arithmetic Operators
The following table shows all the arithmetic operators supported by Rival Scripting language. Assume variable A holds 10 and variable B holds 20 then −
Relational Operators
The following table shows all the relational operators supported by Rival Scripting language. Assume variable A holds 10 and variable B holds 20 then −
Operator | Description | Example |
| Checks if the value of two operands are equal or not, if yes then condition becomes true. |
|
| Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. |
|
| Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
|
| Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
|
| Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
|
| Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
|
Logical Operators
Following table shows all the logical operators supported by Rival Scripting language. Assume variable A holds true and variable B holds false then −
Operator | Description | Example |
| Called Logical AND operator. If both the operands are non-zero then condition becomes true, but the result would be last operand. |
|
| Called Logical OR Operator. If any of the two operands is non-zero then the condition becomes true. Return would be first operand |
|
| Called Logical NOT Operator. Use to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
|
Misc Operators
Miscellaneous operators supported by Rival Scripting Language include concatenation and length.
Operator | Description | Example |
| Concatenates two strings. |
|
| An unary operator that return the length of a string or a table. |
|
Operators Precedence
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −
For example, x = 7 + 3 * 2;
Here x
is assigned 13
, not 20
because operator *
has higher precedence than +
so it first get multiplied with 3*2
and then adds into 7
.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category | Operator | Associativity |
Unary |
| Right to left ⬅️ |
Concatenation |
| ➡️ Left to right |
Multiplicative |
| ➡️ Left to right |
Additive |
| ➡️ Left to right |
Relational |
| ➡️ Left to right |
Equality |
| ➡️ Left to right |
Logical AND |
| ➡️ Left to right |
Logical OR |
| ➡️ Left to right |
Statements
If Statement
Syntax
if (boolean_expression) then
--[ statement(s) will execute if the boolean expression is true --]
end
Example
a = 10;
--[ check the boolean condition using if statement --]
if (a < 20) then
--[ if condition is true then print the following --]
log("a is less than 20");
end
log("value of a is :", a);
If…else Statement
Syntax
if (boolean_expression) then
--[ statement(s) will execute if the boolean expression is true --]
else
--[ statement(s) will execute if the boolean expression is false --]
end
Example
a = 100;
--[ check the boolean condition --]
if (a < 20) then
--[ if condition is true then print the following --]
log("a is less than 20" )
else
--[ if condition is false then print the following --]
log("a is not less than 20" )
end
log("value of a is :", a)
Iterators
While Loop
Syntax
while(condition)
do
statement(s)
end
Example
i = 21
while (i < 30)
do
i = i+1
end
For Loop
Syntax
for init,max/min value, increment
do
statement(s)
end
init
: Required, the number from which the loop will startmax/min
: Required, the number up to which the loop will keep runningincrement
: Optional (Default =1
), any positive or negative number
Example 1
for i = 10,1,-1
do
log(i)
end
Example 2
j = 0
for i=1,10, 5
do
log("i:", i) -- prints 6
j=i
end
log("i:", i) -- prints null as i is inaccessible outside loop's scope
log("j:", j) -- prints 6
Comments
Single line comments:
--
Multi line comments:
--[[
]]--
-- I am single line comment
--[[ this is a multi-line comment.
The compiler
will not run code
written in between. ]]--