Skip to main content
FW version: Stable

Expressions

YOS shell interpreter offers an expression evaluating mechanism. An expression is a special string, containing constants, variables, operators, and functions.

Variables

  • A read-accessible YOS filesystem variable entry.
  • Exists in the current working directory.
  • Specified with full path, relative or absolute.
  • A global or local variable from /vars directory.

Constants

  • Scalar or vector numbers (array members delimited with comma).
  • Fixed point with arbitrary base.
  • Floating point with standard or scientific notation.
  • Constant can be explicitly delimited with quotes.

Operators

  • * - multiplication
  • : - for division
  • % - for modulo
  • + - addition
  • - - subtraction
  • > - greater than
  • < - less than
  • >= - greater or equal
  • <= - less or equal
  • == - equal
  • != - not equal
  • && - logical AND
  • || - logical OR
  • & - bitwise AND
  • | - bitwise OR
  • ^ - bitwise XOR

Built-in functions

  • Array expansion: xp
  • Arithmetics: sum,min,max,sat,sgn,abs
  • Math: sin,cos,sqrt,atan2,pow

Examples

Basic arithmetic operations

var x y res int32
var res_float float

set x 5
set y 2

echo # Print empty line

set res x+y+10
echo Result addition: $x + $y + 10 = $res

set res x*y
echo Resul multiplication: $x * $y = $res

set res x:y
echo Result division: $x / $y = $res

set res x%y
echo Result modulo: $x % $y = $res

set res_float x:y
echo Result division in float: $x / $y = $res_float

set res x+y*10
echo Result: $x + $y * 10 = $res

set res x+(y*10)
echo Result: $x + ( $y~ * 10) = $res # "~" mean end ot stringify, otherwise it evaluate everything inside brackets

echo Compute inside stringify:
echo Result: $x + ( $y~ * 10) = $($x~ + ( $y~ * 10))

echo Result: 5 + 2 = $(5+2)

The output of this example looks like this:

Result addition: 5 + 2 + 10 = 17
Resul multiplication: 5 * 2 = 10
Result division: 5 / 2 = 2
Result modulo: 5 % 2 = 1
Result division in float: 5 / 2 = 2.5
Result: 5 + 2 * 10 = 70
Result: 5 + ( 2 * 10) = 25
Compute inside stringify:
Result: 5 + ( 2 * 10) = 25
Result: 5 + 2 = 7
tip
  • $ is used to access the value of a variable and convert it to a string - More about stringify.
  • The order of operation is from left to right.
  • The expression is not converted to a string but is always evaluated in a certain datatype. set function uses the datatype of the target.

Arrays basic

All expressions work with arrays, member-wise. set returns no of elements processed.

var v.2 float
set v 1,2+3,4
echo 1,2+3,4 yields $v and $retval elements processed
set v 1,2*-2,-3
echo 1,2*-2,-3 yields $v and $retval elements processed

Result:

1,2+3,4 yields 4.0,6.0 and 2 elements processed
1,2*-2,-3 yields -2.0,-6.0 and 2 elements processed

Evaluation size is trimmed according to the operand with smallest size.

var v.2 float
set v 1,2+3
echo 1,2+3 yields $v and $retval elements processed \ # "\" is used for line continuation (do not send newline)
echo ($v.1~ remains intact)

Result:

1,2+3 yields 4.0,-6.0 and 1 elements processed (-6.0 remains intact)

Concatenate two tokens into an array.

var v.2 float
var i int32

set i 3

set v i;(i+1)
echo i;(i+1) yields $v
i;(i+1) yields 3.0,4.0

An array variable in expression can be indexed and sub-array can be extracted.

var v.2 float

echo v.0 yields $v.0
echo v.1 yields $v.1
echo v.0.2 yields $v.0.2

Result:

v.0 yields 3.0
v.1 yields 4.0
v.0.2 yields 3.0,4.0

Boolean expressions

Boolean operators results nonzero in case of true.

echo 1&&1 yields $(1&&1)
echo 2&&3 yields $(2&&3)
echo 0||4 yields $(0||4)
echo 0&&4 yields $(0&&4)

Result:

1&&1 yields 1
2&&3 yields 1
0||4 yields 1
0&&4 yields 0

Built-in functions

var f float

echo sum(1,2) yields $sum(1,2)
echo min(1,2) yields $min(1,2)
echo max(1,2) yields $max(1,2)
echo sat(1,2,3) yields $sat(1,2,3) (1 saturated between 2 and 3)
echo sgn(-1) yields $sgn(-1)
echo abs(-1) yields $abs(-1)

echo
set f sum(1.2,3.4)
echo sum(1.2,3.4) evaluated in float yields $f

Result:

sum(1,2) yields 3
min(1,2) yields 1
max(1,2) yields 2
sat(1,2,3) yields 2 (1 saturated between 2 and 3)
sgn(-1) yields -1
abs(-1) yields 1

sum(1.2,3.4) evaluated in float yields 4.600000

Array expansion function xp is used to expand an array into a list of arguments.

set aa xp(0)
echo xp(0) evaluated in 4-dimensional int32 yields $aa
set v xp(0.3)
echo xp(0.3) evaluated in 2-dimensional float yields $v
echo
echo Array expansion can be specified e.g. as xp.N(...), \
echo where N defines no of input arguments per iteration.
set aa xp.2(1,2)
echo xp.2(1,2) yields $aa

Result:

xp(0) evaluated in 4-dimensional int32 yields 0,0,0,0
xp(0.3) evaluated in 2-dimensional float yields 0.3,0.3

Array expansion can be specified e.g. as xp.N(...), where N defines no of input arguments per iteration.
xp.2(1,2) yields 1,2,1,2

Trigonometric functions.

echo sin(1) yields $sin(1)
echo cos(1) yields $cos(1)
echo sin(3.141592654) yields $sin(3.141592654)
echo cos(3.141592654) yields $cos(3.141592654)
echo sqrt(2) yields $sqrt(2)
echo sqrt(sum(4,4)) yields $sqrt(sum(4,4))
echo atan2(1,1) yields $atan2(1,1)
echo atan2(1,1):3.141592654 yields $atan2(1,1):3.141592654

Result:

sin(1) yields 0.842168
cos(1) yields 0.540484
sin(3.141592654) yields 3.69549e-7
cos(3.141592654) yields -1.000000
sqrt(2) yields 1.41386
sqrt(sum(4,4)) yields 2.82772
atan2(1,1) yields 0.790350
atan2(1,1):3.141592654 yields 0.251576