# Language syntax

## Basics

### How SQ works

When, for example, an expression in double curly braces {{}} is found in the message text, the bot evaluates the expression inside the brackets and substitutes its value in the message text.&#x20;

For example, the value of the expression 2 \* 2 is the number 4, and the value of the expression "**hello**" is the string **hello**.

### Syntax examples

{% hint style="info" %}
The symbol >>> in the examples below marks the user's input, and the line below indicates the result of evaluating this expression.&#x20;

The # symbol marks comments (explanations) to the code.
{% endhint %}

#### Arithmetic

```
>>> 2 * 2
4

>>> 2 ** (4 + 10 / 5)
64
```

#### Variables&#x20;

Variables allow you to store user data.&#x20;

Variables are divided into types: numbers, strings, arrays and dictionaries.&#x20;

The type of variable determines the set of actions that can be performed with it. For example, numbers can be compared (>, <, >=, <=, ==, !=), divided, and multiplied.&#x20;

Strings can only be added. Bot variable names (special, local, and global) begin and end with a percent sign, such as **%message%**. From the point of view of SQ, these percentages are part of the variable name, and you can create new temporary variables in SQ itself without percent signs, for example, **x = 10.**

#### Comparison operations

```
>>> x = 10
>>> x > 0
True

>>> x <= 0
False

>>> x == 10
True

>>> x += 2
>>> x
12

>>> x *= 2
>>> x
24

>>> y = 0
>>> x == 0 and y > 0 or x != 0 and y < 0
False

>>> not (x == 0 and y > 0 or x != 0 and y < 0)
True

# let's say %sex% is "female"
>>> 'yes' if %gender% == 'female' else 'no'
'Yes'

# let's say %message% is "hello world"
>>> 'priv' in %message%
True

>>> ' ' in %message%
True

>>> 'yet' not in %message%
True
```

#### Calling Functions

Functions allow you to transform data, such as getting the length of a string, generating a random number, or adding an element to an array.

In SQ, there are 3 options for calling functions: through a dot, through a pipe (|) and explicitly - they are interchangeable and equivalent.

```
'hello' | len
6

>>>len('hello')
6

>>> 'hello'.len()
6
```

#### A couple of examples:

```
# suppose %message% is "hello world"
>>> %message%.startswith('hello')
True

>>> %message%.endswith('hello')
False

>>> %message%.endswith('world')
True

>>> round(0.23)
0

>>> round(0.23, 1)
0.2

>>> 12345678 | pretty
12 345 678

# note: \ is used to escape a quote in a string
>>> 12345678 | pretty('\'')
12'345'678

>>> rand([1, 2, 3])
1

>>> rand([1, 2, 3])
1
```

#### Working with arrays&#x20;

Arrays allow you to store multiple values ​​in one variable. For example, you can store a customer's shopping list.

```
>>> cart = []
>>> cart.push('bread')
>>> cart.push('buckwheat')
>>> shopping cart
['bread', 'buckwheat']

>>> 'bread' in basket
True

>>> 'cheese' in basket
False

>>> shopping cart | pretty('\n')
bread
buckwheat
```

#### Dictionaries

A dictionary is similar to an array, but it allows you to remember the mapping from one element to another. For example, the number of items in the cart.

```
>>> cart = {}
>>> basket['bread'] = 3
>>> basket['buckwheat'] = 10
>>> shopping cart
{'bread': 3, 'buckwheat': 10}

>>> 'bread' in basket
True

>>> 'cheese' in basket
False

>>> shopping cart | pretty
bread: 3
buckwheat: 10
```

## Functional programming

{% hint style="info" %}
This is a difficult section to understand if you were not previously familiar with programming. It is possible that you can skip this section)
{% endhint %}

### Lambdas (lambda expressions)

Lambda (lambda) is an anonymous function.&#x20;

SQ does not have the ability to declare its own functions, but for some tasks, such as array transformation, filtering, and sorting, you can use lambda expressions.

#### Syntax

For example, let's consider a function that takes 3 arguments: a, b, c and returns their sum:

```
a, b, c => a + b + c
```

Thus, a lambda expression consists of 2 parts: the list of arguments (before the arrow) and the expression (after the arrow). The result of the expression will be returned when this function is called.

Example

```
>>> cart = {}
>>> basket['bread'] = 3
>>> basket['buckwheat'] = 10

# display the dictionary in the format we need
>>> shopping cart | map((k, v) => k + ' - ' + v + 'kg') | join
bread - 3 kg
buckwheat - 10 kg
```

Here we used the *map()* function, to which we passed our lambda as an argument. The *map()* function will iterate over all the key-value pairs *(k, v)* in the *cart* dictionary and return a string instead, the result of adding **k**, a hyphen, **v**, and the word "**kg**".
