Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
An array is one of the types of TRIK Studio language that stores a set of values (array elements) identified by an index. The numbering of indices in the array starts at 0.
Arrays can be created explicitly using the following expressions (the next lines are equivalent):
Or with explicit indices:
You can use arrays without creating them. For example, like this:
In this case, "empty spaces" in the array (indices that did not have an explicit assignment) will be filled with default values:
0
— for real and integer;
false
— for boolean;
empty lines for arrays of strings.
Array values can also be used without curly braces if used as the return value. For example, this expression will return an array of numbers 1
and 2
:
This is implemented in order to interpret the enumerations of values (for example, the ports of motors in the block "Motors forward") as arrays. Therefore, wherever you use a comma-separated value notation, you can use an array.
Besides constants, in TRIK Studio reserved variables are available that store the values of the readings of sensors connected to the corresponding ports.
These values can be assigned to variables and used in mathematical expressions.
Variables have the form
For example, sensorA1
for TRIK or sensor1
for Lego NXT.
A complete list of sensor variables is provided in sections for specific platforms:
TRIK.
It has the following form:
variable1, variable2, …, variableN = expression1, expression2, …, expressionN
In its simplest form variable = expression
.
It is written as an expression and should be at the end of the statement block.
Written as ;
.
A statement block with two assignment operators and a return operator:
TRIK Studio has only one built-in constant. It can be used in the same way as , but its value cannot be changed.
The language uses spaces, tabs, and line breaks outside of string literals only as separators between tokens — they do not affect the interpretation of expressions.
The language has the following keywords:
and
false
nil
not
or
true
And the following :
String literals are written in either single or double-quotes. For example,
Integers are written either in decimal, octal, or hexadecimal. Hexadecimal numbers begin with the prefix 0x
, octal numbers begin with the prefix 0
. For example,
3
, 345
— decimal number
0xff
, 0xBEBADA
— hexadecimal number
07654321
— octal number
Real numbers are written either in conventional or exponential form. For example,
Comments begin with --
and continue to the end of the line. For example,
See the article about the syntax of expressions in the blocks:
In mathematical expressions, you can use the functions available for any platform.
To declare variables and set certain values for them, the “” block is used.
For mathematical expressions — "" block.
Also, mathematical expressions, including those that change the values of variables, can be used almost everywhere where you can specify a value.
All variables in the program are global, i.e. their values can be used in any block in the program.
Proportional controller for driving along the line using two light sensors for the TRIK platform:
Unlike , the TRIK Studio language is statically-typed, that is, the type of each expression and each variable must be known at compile time. Moreover, the language does not require (and does not even allow!) to explicitly write the types of variables. It uses automatic type inference for the use of variables.
For example, environment "understands" from the expression a = 1
that type of a
is integer.
See the article about the syntax of expressions in blocks:
Blocks and their properties that do not allow the use of mathematical expressions are described in the section .
The language used inside the blocks is based on and largely follows its syntax. Nevertheless, there are significant differences, so a full description of the language used in TRIK Studio is given here.
Declaration of several variables in one block "":
The same using blocks:
An example of using to set the ports of the “Motors forward” block. An array is created with the name a
, the port M3
is assigned to the zero element, the first is M1
, then the contents of the first element of the array are changed to M4, then the resulting array is transferred to the “Ports” property of the “Motors forward” block:
Title
Title
Title
+
Addition
Applicable to real and integer values. The result is an integer if both arguments are integer, otherwise real.
-
Subtraction
Applicable to real and integer values. The result is an integer if both arguments are integer, otherwise real.
*
Multiplication
Applicable to real and integer values. The result is an integer if both arguments are integer, otherwise real.
/
Division
Applicable to real and integer values. The result is real.
//
Integer division
Applicable to integer values. The result is an integer.
^
Power
Applicable to real and integer values. The result is real.
%
Remainder of the division
Applicable to integer values. The result is an integer.
&
Bitwise "and"
Applicable to integer values. The result is an integer.
|
Bitwise "or"
Applicable to integer values. The result is an integer.
>>, <<
Bitwise left and right shift
Applicable to integer values. The result is an integer.
..
Concatenation
Applicable to string values. The result is a string.
>=, >, <, <=
Comparison operations
Applicable to integer and real values. The result is boolean.
==
Equality check
Applicable to any type of value. The result is boolean.
~=, !=, '~=', '!='
Inequality check
Applicable to any type of value. The result is boolean. All notations are equal.
and, &&
Logical "and"
Applicable to integer, real and boolean types. The result is boolean. All notations are equal.
or, ||
Logical "or"
Applicable to integer, real and boolean types. The result is boolean. All notations are equal.
Title
Title
Title
-
Arithmetic unary minus
Applicable to integer and real values. The result is an integer or real (depending on the type of argument).
~
Bitwise "not"
Applicable to integer values. The result is an integer.
not
Logical "not"
Applies to boolean, integer, real, and string values, the result is boolean (0 or an empty string is considered false, everything else is true).
#
Length operator
Applicable to string values. The result is an integer.
Function | Description |
time() | Returns the time in milliseconds since the program started. |
sin(angle) | The sine of the angle, where "angle" is the angle in degrees. |
cos(angle) | The cosine of the angle, where "angle" is the angle in degrees. |
ln(a) | The logarithm, where "a" is a number. |
exp(a) | Exponent ("e" in the degree of argument "a"). |
asin(a) | Arcsine, where "a" is a number. |
acos(a) | Arccosine, where "a" is a number. |
atan(a) | Arctangent, where "a" is a number. |
atan2(a1, a2) | The arctangent of a1 / a2 with division check by 0. |
sgn(a) | Sign. Returns 1 if the argument “a” is positive, -1 if it is negative, and 0 if the argument is zero. |
sqrt(a) | The square root of the argument "a". If the argument is negative, the variable will be assigned the value “NaN” (Not A Number). |
abs(a) | The absolute value of number "a". |
ceil(a) | Rounds up the passed argument "a" to the nearest integer. |
floor(a) | Rounds down the passed argument "a" to the nearest integer. |
random(a) | A random number between 0 and the passed argument “a”. |
pi | 3.14159265358979 | Represents the ratio of the circumference of a circle to its diameter, determined by the constant π. |
Type | Description |
Boolean (logic) type | Can be one of two values: true or false. |
Real | Uses IEEE 754 64-bit binary64 representation (allows to store values up to 1.7E+308). |
Integer | Uses 32-bit signed representation (allows to store values in a range from -2 147 483 648 to 2 147 483 647). |
String | Allows storing character strings of arbitrary length in UTF-8 encoding. |
Null type | It has only a one-nil value and means the absence of any other value type. |
Allows storing an arbitrary number of values of an arbitrary (but for each value in the same array of the same) type, including other arrays, and access values by index. |