How to program in Grimport?

Ask us for help with programming in Grimport TRANSLATE THIS PAGE WITHOUT TOUCHING THE CODES

Variables & Syntax

 

What is a variable?


As a developer, you will need to store information that will be contained in variables. A variable consists of a name, a type and a value.

Name and value of a variable

The code below allows you to declare a variable:

car = "red"

Here car is the name of the variable and "red" is its value.

It is very easy to modify the value of a variable. To modify the value of a variable, you just have to assign a new value to the variable. Here the value "red" of the car is changed to "green".

car = "red"
car = "green"
console(car) // -> green

The different types of variables

The type of the variable is simply a way of classifying it.
We'll look at the simplest types for now:

  1. Integers and floating point:
    • Integers are: 1, 12, 20,347, ...
    • Floats are decimal numbers: 3.14, 45.764, ...

  2. Strings of characters:
  3. A string is information surrounded by quotation marks (either single or double). Here are some examples of string variables:
    politeness = "please"
    computer_language = 'the grimport'

    If you need a string that spans multiple lines, use a multiline string literal. This includes all lines between its opening and closing quotes.
    This is a sequence of characters surrounded by three double quotes:

    MyVariable = """ 
    	Two friends meet.  
    	"Hi, are you okay?" he asked.  
    	And, she says "Yes, I'm fine." """

    String values can be added (or concatenated) with the addition operator + to create a new string value:

    string1 = "hello"
    string2 = " there"
    welcome = string1 + string2 
    console(welcome) // -> hello there
    

    A string can also be modified by assigning it to a variable. We will use the $ symbol for that.
    Note that this is only possible with double quotes.

    var="port" 
    result = "grim$var" 
    console(result) // -> grimport

  4. Booleans: true or false?
  5. The Boolean has only two value options: true or false.
    Booleans are used to determine if an action is successful or not.
    For example, if you want to record the fact that your products are in stock, you would define your variable like this:
    products_in_stock = true
    On the contrary, if they are no longer in stock you will write:
    products_in_stock = false

 

Escape sequence


An escape sequence is a special character. It is noted with a backslash.
Here is a list of them:

\t inserts a tabulation
\n inserts a new line
\" inserts a double quote
\' inserts a single quote
\\ inserts a backslash

 

Examples:

\t:

console("Hello\tHow are you?") // -> Hello   How are you?


\n:

console("Hello\nHow are you?")
/* -> Hello 
How are you? */


\",  \';  \\:

Suppose we want to execute the following code:

console("Hello "people" How are you?") // -> error
Indeed, there is a compilation error in this code. Here the quotation marks for the word "people" are inside other quotation marks. Putting a backslash avoids this error:
console("Hello \"people\" How are you?") // -> Hello people How are you?

Similar errors also occur with other characters such as single quotes and backslashes.

 

Arithmetic operators


You can perform all kinds of arithmetic operations with Integers and Floats.

Operator
Purpose
+ addition
- subtraction
* multiplication
/ division
% remainder
** power

Addition and Subtraction

To add two variables, use the +:

number_of_boy = 5 
number_of_girl = 3 
total = number_of_boy + number_of_girl 
console(total) // -> 8

To substract two variables, use the -:

number_of_boy = 5
number_of_girl = 3 
total = number_of_boy - number_of_girl 
console(total) // -> 2

To add or subtract a number from a variable, you can use the += and -= operators:

number_of_boy = 5 
number_of_boy += 2 // number_of_boy = number_of_boy + 2 -> 7 
number_of_boy -= 2 // number_of_boy = number_of_boy - 2 -> 5

To increment or decrement (add or subtract 1), you can use ++ or --:

number_of_boy = 5 
number_of_boy++ // number_of_boy = number_of_boy + 1 -> 6 
number_of_boy-- // number_of_boy = number_of_boy + 1 -> 5

Multiplication and Division

You can multiply and divide variables using the * and / operators:

product_price = 10 
number_of_products = 4 
total_price = product_price * number_of_products // -> 40 
average_price_per_product = total_price / number_of_products // -> 10

Like with additions and subtractions, there are also the operators *= and /= to multiply or divide a number:

number_of_products = 2 
number_of_products *= 6 // -> 12 
number_of_products /= 3; // -> 4

Remainder and Power

The remainder operator % determines how many multiples of b will fit inside a and returns the value that remains. In Grimport, this would be written as follows:

a = 9  
b = 4 
c = a % b // -> 1

The power ** is the operator to get the power of a number:

a = 3 
a **= 2 
console(a) // -> 9

Relational operators


In order to compare variables, relational operators are used.
The following operators are available:

Operator
Purpose
== equal
(caution! the use of this operator is not recommended, for robustness problems, we recommend always using the equals function)
!= different
< less than
<= less than or equal
> greater than
>= greater than or equal

 

Examples:

1 == 1   // true because 1 is equal to 1

2 != 1   // true because 2 is not equal to 1

2 > 1   // true because 2 is greater than 1

1 < 2    // true because 1 is less than 2

1 >= 1   // true because 1 is greater than or equal to 1

2 <= 1   // false because 2 is not less than or equal to 1

 

Logical operators


There are 3 logical operators for boolean expressions:

  • &&: logical "and"
  • ||: logical "or"
  • !: logical "not"

Examples:

!false //"not" false is true

true && true //true "and" true is true

true || false //true "or" false is true

 

Conditional operators


Ternary operator

The ternary conditional operator is a three-part operator, which takes the form:
(condition) ? trueStatement : falseStatement

It is used as a shortcut for the if...else statement .
If condition is true, it evaluates trueStatement and returns its value; otherwise, it evaluates falseStatement and returns its value.

Example:
Here is an example that calculates the admission cost of a person at a museum according to thier age. The person will pay 10 euros if they are a child and 15 euros if they are an adult:

adult = true 
price = adult ? 15 : 10 // price is equal to  15

The above example is a shortcut to the code below:

adult = true 
if  (adult) 
{ 
	price = 15 
} 
else 
{ 
	price = 10 
} // price is equal to 15

Elvis operator

The elvis operator is a shortcut to the ternary operator. It is used to set the "right default value" to a variable.
It set a value if it exists and else you can define a default value.

Formally : conditionAndTrueStatment ?: falseStatment is equivalent to conditionAndTrueStatment ? conditionAndTrueStatment : falseStatment

Here is a simple example:

myPlayerName = userName ?: "Guest"

In the example above, if userName exists, myPlayerName will take the value of the variable userName. Else if userName is null or false or an empty string, myPlayerName will take the value "Guest".

With the ternary operator, we should have written:

myPlayerName = userName ? userName : "Guest"

 

Lists


A list is a structure used to store a collection of data items. We will see how to create, access and modify a list.

Create a list

To save a collection of objects that you want to access later, you need to create a list.
To create a list, we use square brackets [ ]. For example, the following code creates a list of different products and saves them in a variable named products_in_stock:
products_in_stock = ["t-shirt", "pants", "coat"]

You can also assign values to the elements of the table with the colon (it's called a map or an associative array):
products_in_stock = ["t-shirt": 2, "pants": 1, "coat": 4]

Now that we have created a list, we need to be able to access to its elements.

Access to elements of a list

To be able to access the elements of a list, we use an index. Every element in a list has an index that corresponds to its position in the list. It is important to note that the index starts at 0, not at 1.

To access a specific element of a list, you can use the method:
get (list list, object_index )

products_in_stock = ["t-shirt": 2, "pants": 1, "coat": 4]

get(products_in_stock, 0) // -> 2 get(products_in_stock, "coat") // -> 4
You can also access the elements of your list in reverse, using negative numbers. For example, we use the index -1 to access the last element of the list:
get(products_in_stock,  -1) // -> 4 
get(products_in_stock,  -3) // -> 2

Modify a list

It is possible to add, remove and sort the elements of a list.
In Grimport, you can use the functions inherited from java but it is always preferable to use Grimport functions for the sake of the robustness of the code against the risk of null pointers for example. Indeed, these functions are very flexible and very "null-tolerant". There is one exception to this principle, the functions for adding elements (add and put) which must imperatively use pointers.

  • To add a specific element of a list, you can use the method:
    list.add (object value )
    map.put(object key, object value)

    products_in_stock = ["t-shirt": 2, "pants": 1] 
    products_in_stock.put("coat", 4) // -> ["t-shirt": 2, "pants": 1, "coat": 4] 
    
    products_in_stock = ["t-shirt", "pants", "coat"] 
    products_in_stock.add(1, "socks") // -> ["t-shirt", "socks ", "pants", "coat"]
    products_in_stock.add("shoes") // -> ["t-shirt", "socks ", "pants", "coat", "shoes"]

  • To remove a specific item from a list, you can use the method:
    remove (list list, object index )
    products_in_stock = ["t-shirt", "pants", "coat"] 
    remove(products_in_stock, 1) // -> ["t-shirt", "coat"] 
    remove(products_in_stock, "coat") // -> ["t-shirt"]
    
    products_in_stock=["t-shirt",null] 
    remove(products_in_stock,null) // -> ["t-shirt"]
    // do not use the Java method: 
    products_in_stock=["t-shirt",null] 
    products_in_stock.remove(null) // -> ["t-shirt",null] !!! 
    
    Often you will have to delete an item from a list you are browsing. This is normally illegal as you are changing the tree structure of the list while reading. But the iterators are there to solve the problem, as long as you use them like this:
    list = [1,2,3]
    
    iterat = list.iterator() //load the iterator of the list
    iterat.each //do the each on your iterator instead of the list
    {el->
    	if(equals(el, 2)) iterat.remove() //the remove must be on the iterator ! Not on the list.
    }
    
    console(list) // [1, 3]
    

  • To sort the elements of a list, you can use the method:
    sort (list list, boolean ascending , boolean byKey )
    The sorting is done by alphabetical order for string lists and by ascending order for numbers.
    products_in_stock=["t-shirt", "pants", "coat"] 
    sort(products_in_stock) // -> [coat, pants, t-shirt]
    list=[2,8,6,3]  
    sort(list, false) // -> [8, 6, 3, 2]
    

 

Handling null values


The null value represents a variable with no value and therefore of type null. This variable is considered as null if:

  • it has been assigned the constant null
  • it has not yet received a value

With Grimport, null has a special meaning. It means "do nothing", "leave by default". For example in a php function if the active variable is set to true, we activate the product, at false we deactivate it and at null we leave what was there before.
In the arguments of a function this behavior is important too. For example in the function select(".css", null, "src") here null is an argument between 2 arguments that must be indicated, so we must put the 2nd to be able to put the 3rd. Here the 2nd will have the default value.

 

Scope rules


A scope of a variable is the part of the program where the variable may directly be accessible.
There are local and global variables:

  • a local variables is accessible only in its block of in its function
  • a global variables is accessible everywhere in the script (after its declaration)

Local variables can only be used by instructions that are in the same block as them. They are not accessible outside the block. A block is delimited by { ... } (example: a function, an if structure, etc).

Global variables can be accessed at any time.

In Grimport, all variables are global by default. If you want to transform a variable into a local variable, you must add the keyword def when you declare it.

Examples:

a = true 
b = 10

if (a) { def b=5 //exists just in { ... } console(b) // -> 5 } console(b) // -> 10

In Grimport, there are also superGlobals. A superglobal is defined with the setGlobal() function and is found in all scripts (INITIAL, FORPAGE and FINAL) unlike a simple global which is only global in the script where it appears.

 

Functions


A function can be considered as a block of code with a name, which executes a service. For example, the function "calculateVAT", which will need two arguments, the price and the VAT rate.

Examples:

calculateVAT(price, rate) 
{ 
	return price * rate
} 

This function will be global. It is possible to make it local by putting a def before the function name.

A function is not an object in Grimport but you can transform your function into an object thanks to the {-> } structure, this is called a closure and it allows you to use a function as a variable in many interesting contexts.

Examples:

calculateVAT = {price, rate->
	return price * rate
} 

The arguments of the function are by default global variables like any other variable in Grimport. To make them local variables (important in multithreading), you have to put a def before them. The same principle applies to variables declared inside a for, remember to put a def in front of them so that they become local.

 

Optional arguments


An argument or parameter is an input that a function takes. You can define 0, 1 or more arguments to a function.

It is also possible to define optional arguments with default values.

Here is an example of a simple method with the following parameters:

def  printMe(name, age = 23, organization="Idia")
{ 
	console("name : $name, age : $age,  organization : $organization") 
}

Here, the number 23 is the default value for age and "Idia" is the default value for the organization argument. If you do not provide any values for age and organization, the default values are used.

Example:

def  language(name,  age = 23,  organization="Idia")
{
	console( "name : $name, age : $age,  organization : $organization")
}

language("Grimport") 
language("Grimport",  30) 
language("Grimport",  30, "Tech")
language() //compilation error because the first argument is mandatory

Output:

// -> name : Grimport, age : 23, organization  : Idia  
// -> name : Grimport, age : 30, organization  : Idia  
// -> name : Grimport, age : 30, organization  : Tech 

 

Default variables


In Grimport, there are variables that are provided by default.
There are 4 of them: global available on the 3 scripts and code, urlPage and arrivalUrl which are on the FORPAGE script. Here is their functionality:

  • global which is on the 3 scripts and is the array of available global variables.
  • code which is the code of the page.
  • urlPage and arrivalUrl which are the urls of the page before and after a possible redirection.

These four variables allow specific actions to be carried out, for example urlPage will be able to carry out an action on a specific page if you use it in an "if" block.

On Cloud servers, you also have the serverIp and serverPort variables.

 

Comments


Feel free to put comments so you don't get lost in your code.
There are two types of comments, the single-line comment and the multiline comment.

Single-line comments start with //.
You can put them at the beginning of the line but also after some code on the same line. Everything after // will be considered a comment until the end of the line.

// it is a comment on a single line

select("h1") // you can comment until the end of the line

Multiline comments start with /*. You can put them anywhere you want in your code. The caracters that follow /* are considered as part of the comment until the first */ that closes the comment. This allows you to write comments on multiple lines.

/*with this one you can write on this line
 and finish on another line*/


/*You can also write between code*/ select(cssSelector/*1*/, _code/*2*/, _selection/*3*/)

There are standard development comments:

  • //TODO: What still needs to be done
  • //EDIT: When you notify that you have changed something
  • //PATCH: When you make a bug correction
  • //SPECS: An update on the project specifications

And some special comments which interact with Grimport Script Editor:

  • //ALWAYS_SAVE_SCRIPT : When the script is launched without being saved, no dialog box asks you if you want to save the changes, it is automatically done.
  • //NEVER_SAVE_SCRIPT : When the script is run without being saved, no dialog box asks if you want to save the changes, the script is not saved and the version written in the editor is used.
  • //ALWAYS_SAVE_CRON_CLOUD_YES : When the script is backed up and a CRON is present on the Cloud, it is sometimes asked if the backup should impact the script in CRON. This comment automatically answers Yes to the request.
  • //ALWAYS_SAVE_CRON_CLOUD_NO : Same but answers No.



Next ❯ ❮ Previous