Language syntax

This section describes the syntax of the language

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.

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

The symbol >>> in the examples below marks the user's input, and the line below indicates the result of evaluating this expression.

The # symbol marks comments (explanations) to the code.

Arithmetic

>>> 2 * 2
4

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

Variables

Variables allow you to store user data.

Variables are divided into types: numbers, strings, arrays and dictionaries.

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

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

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

This is a difficult section to understand if you were not previously familiar with programming. It is possible that you can skip this section)

Lambdas (lambda expressions)

Lambda (lambda) is an anonymous function.

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".

Last updated