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
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
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.
A couple of examples:
Working with arrays
Arrays allow you to store multiple values in one variable. For example, you can store a customer's shopping list.
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.
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:
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
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