Code flow operators
branch <arg>
Branches (jumps) to a desired place in the running script. If the branch
command is executed outside of a code block, the last entered main code block (if exists) is called and executed. Further determine where to branch with <arg>
, which can be one of these (or combinations):
s
orb
- branch to the beginning of the current block.s-
- branch to one line before the beginning of the current block (can be used to create a while cycle).s--
- branch to two lines before the beginning of the current block.s---
,s----
...s+
- branch to one line after the beginning of the current block.s++
,s+++
...e
- branch to the end of the current block.q
- branch to the end of the main block.p
- branch to the beginning of the parent block.pe
- branch to the end of the parent block.pp
- branch to the beginning of a parent parent block.ppe
,ppp
...n
- branch to the beginning of the next child block.ne
- branch to the end of the next child block.nn
- branch to the beginning of a next next child block.nne
,nnn
...
Code blocks
Code blocks start with {
and end with a pairing closing bracket }
. Multiple sub-blocks and sub-sub-blocks could be created inside the main block of code. A Code block is considered to be the parent of the code blocks declared inside it. If a code block A
is a parent of code block B
, then code block B
is a child of code block A
. Further, the first child is considered a next child, the second is considered a next next child, and so on.
{ # start of the main block
{ # start of block 1
{ # start of block 2
...
} # end of block 2
{ # start of block 3
{ # start of block 4
...
} # end of block 4
} # end of block 3
} # end of block 1
{ # start of block 5
...
} # start of block 5
} # end of the main block
- block 1 is a parent of blocks 2 and 3
- block 3 is a parent of block 4
- block 2 is a next child of block 1
- block 3 is a next next child of block 1
- block 3 is a parent of block 4
- block 1 is a parent parent of block 4
- block 1 is the next child of the main block
- the main block is a parent parent parent of block 4
Example
This example implements a simple while cycle. This cycle will print out numbers from 10
to 1
and at the end print this is the end
.
var a
set a 10
{
if a {
echo $a
set a a-1
branch s-
}
echo this is the end
}
call [func-name]
Calls previously defined functions. If an argument is given, the command will try to call the function with the given name. If no argument is given, the command will call the last defined function. Beware that this holds only for the first call
command after a function definition, every next call
will print func not found
.
Functions
Functions are essentially just blocks of code with a name, making writing scripts easier because we don't have to write all the commands again and again, we just call the functions and all the commands will be called for us.
In YOS, we create functions with this syntax: .<func-name> { code }
. An example of creating a function called greet
:
AM-felix#>.greet {
echo hello
echo ahoj
}
AM-felix#>
We created a function that upon calling will execute commands echo hello
and then echo ahoj
and end. We can then call it by executing call greet
. Beware that functions are always saved only in memory, meaning all functions will be gone after the device is turned off.
Return value
0
for successful execution, -1
when error occurred.
Examples
AM-felix#>.func1 { # define function func1
echo hi im func1
}
AM-felix#>.func2 { # define function func2
echo hello im func2
}
AM-felix#>call # call the last defined function, meaning func2
hello im func2
AM-felix#>call func1 # call func1
hi im func1
AM-felix#>
if <expression> <command(s)>
This command evaluates the given expression and, if it yields true, executes the following <command(s)>
part. <command(s)>
can also be a whole block of commands (beginning with {
and ending with }
). Command if
could be followed by command elif
or else
.
The expression is C-style string format which can contain braces, operators, variables, and constants. The expression is evaluated in a floating-point scalar and considered valid when the result is nonzero. If the bitwise operators are used in the expression, it is evaluated in a 32-bit fixed-point scalar. The operators can be: *
, :
(for division) , +
, -
, >
, <
, >=
, <=
, ==
, !=
, &&
, ||
, &
, |
, ^
. Expression is true
if the value is non-zero and false
if zero. The expression is evaluated from the left to the right and operators are not prioritized. If a priority is required, the user must specify that with braces. 3+5*4
needs to be entered as 3+(5*4)
.
Examples
if x>=3 echo $x
- prints the value of x
only if it is greater than 3
.
if (/driver/error != 0) echo Driver error!
- if the value of /driver/error
is non-zero, print Driver error!
to the terminal.
Example of finding a max element of an array:
var max i a.10
set i 0
set max 0
# now fill the array ...
set a.0 5
set a.1 2
set a.2 15
# ...
{
if (a.$i<max) set max a.$i
set i i+1
if (i<10) branch s
}
echo max element is $max
else <command(s)>
Works only if used right after if
or elif
. When the condition of if
(or elif
) is true, all command(s) in argument <command(s)>
are skipped. When the condition is false, executes <command(s)>
. Similar to if
, single command in the argument can be replaced with a whole block of commands, beginning with {
and ending with }
.
Examples
if ((i<15)&&(0>-5)) set i i+1
else set i i-1
If the value of i
is between ?5
to 15
, increment the value of i
by 1
. Otherwise, decrement i
by 1
.
elif <expression> <command(s)>
This command is used for chained conditional execution. Works only if used right after if
or another elif
. When the condition of if
(or elif
) is true, all command(s) in argument <command(s)>
are skipped. When the condition of the previous if
or elif
is false, executes the argument only if the given <expression>
evaluates to true. Similar to if
, single command in the argument can be replaced with a whole block of commands, beginning with {
and ending with }
.
Examples
if (i<0) echo i is a negative value
elif (i==0) echo i is is zero
else echo i is a positive value